forked from hybridgroup/GitHub-Wikifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·175 lines (150 loc) · 4.51 KB
/
pre-commit
File metadata and controls
executable file
·175 lines (150 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/zsh
#
# Wikify by Mario "Kuroir" Ricalde
#
function wikify() {
# gnu ls has natural number sorting, useful for sorting 1, 10, 2 into 1, 2, 10
function gls_or_ls() {
if hash gls 2>/dev/null; then
gls -v "$@"
else
ls "$@"
fi
}
# Filter some files and directories.
function continue_if_filtered() {
if [[ "$@" =~ "images$" ]]; then
continue
elif [[ "$@" =~ "(_sidebar|_footer|_header)" ]]; then
continue
elif [[ "$@" == "." ]]; then
continue
fi
}
function directory_depth() {
if [[ $initial_depth -ne 0 ]]; then
new_depth=$(echo $@ | grep -o / | wc -l)
# The minus 1, is for the parent subject directory, we don't count it.
echo $(($new_depth - $initial_depth - 1 ))
else
echo $@ | grep -o / | wc -l
fi
}
# 4 Spaces per level, this is the markdown standard for lists.
function calculate_spaces() {
_spaces=""
if [[ $@ -gt 0 ]]; then
i=1
while [[ $i -le $@ ]]; do
_spaces="$_spaces "
(( i++ ))
done
fi
echo $_spaces
}
function depth_and_space() {
depth=$(directory_depth $@)
space=$(calculate_spaces $depth)
}
function to_parent_subject() {
echo "$@" | sed -e 's/\/[^\/]*$//'
}
function subject_name() {
echo "$@" | sed -e 's/.*\/\([^\/]*\).md$/\1/'
}
function string_to_name_and_id() {
if [[ "$(file -ib $1)" =~ "directory" ]]; then
1="$1.md"
fi
identifier=$(echo "$1" | sed -e 's/.*\/\([^\/]*\).md$/\1/')
name=$(echo "$identifier" | sed -e 's/^[0-9\.]*\-//' | sed -e 's/\-/ /g')
}
function subject_to_list() {
string_to_name_and_id $1
if [[ ! -s $file ]]; then
flag=" ⚑"
fi
echo "${space}0. [[$name|$identifier]]$flag"
}
function id_to_string() {
echo "${@/-/ }"
}
function subject_to_header() {
string_to_name_and_id $1
echo "\n${space}### [[☰|$identifier]] $(id_to_string $identifier)"
}
function recursive_write() {
if [[ "$reverse_pointer_depth" -eq "0" ]]; then
pointer_file=$(to_parent_subject $@)
else
pointer_file=$@
fi
if [[ "$@" != "$subjects" ]]; then
if [[ "$reverse_pointer_depth" -ne "0" ]]; then
space=$(calculate_spaces $(($reverse_pointer_depth - 1)))
echo "$(subject_to_list $file)" >> "$pointer_file.md"
fi
(( reverse_pointer_depth++ ))
recursive_write $(to_parent_subject $@)
# if current - 1 = home. Then we're at the bottom!
# if [[ "$(to_parent_subject $@)" == "$subjects" ]]; then
# echo "$(subject_to_list $file)" >> "$@/_sidebar.md"
# fi
else
space=$(calculate_spaces $(($reverse_pointer_depth - 2)))
echo "$(subject_to_list $file)" >> "$subjects/Home.md"
reverse_pointer_depth=0
fi
}
function parse_subject() {
depth_and_space $@
echo "${space}Directory: $@"
# Directory write
if [[ "$depth" -eq "0" ]]; then
echo "$(subject_to_header $@.md)" >> "$subjects/Home.md"
# echo "$(subject_to_header $@.md)\n---" > "$@/_sidebar.md"
echo "$@.md" >> /tmp/wikify.txt
# echo "$@/_sidebar.md" >> /tmp/wikify.txt
fi
for file in $(gls_or_ls -l -v $@ | awk '{ print $9 }'); do;
continue_if_filtered $file
file="$@/$file"
if [[ "$(file -ib $file)" =~ "directory" ]]; then
echo "$file.md" >> /tmp/wikify.txt
recursive_write $file
parse_subject $file
else
depth_and_space $file
echo "${space}File: $file"
echo "$file" >> /tmp/wikify.txt
recursive_write $file
fi
done
}
LANG=C
subjects=$PWD
initial_depth=0
initial_depth=$(directory_depth $subjects)
reverse_pointer_depth=0
rm -f /tmp/wikify.txt; touch /tmp/wikify.txt
# Empty Home.md
home_file="$subjects/Home.md"
rm $home_file
# Cleanup Previous Generated Files.
for subject in $(find . -type d ! \( -path './.*' -o -path '.' \)); do;
rm "$subject.md"
done
# Begin doing the magic
for subject in $(gls_or_ls -l -d $PWD/* | egrep '^d' | awk '{ print $9}'); do;
continue_if_filtered $subject
parse_subject $subject
done
echo "$subjects/Home.md" >> /tmp/wikify.txt
while read line
do
[ -f $line ] && git add $line
done < /tmp/wikify.txt
echo "**Important**: The Tables of Content are generated. Any change will be overridden on the next update.<br>For more information: [GitHub Wikifier](https://github.com/hybridgroup/GitHub-Wikifier)" > "$subjects/_footer.md"
subjects="$PWD." # hack to restore ps1
}
wikify .