forked from chainer/pg-dl-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contents-util
executable file
·203 lines (180 loc) · 4.17 KB
/
contents-util
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
CONFIG_FILE=book.yml
with_dummy=false
exit_with_usage() {
cat <<EOS
usage:
$0 [commands]
commands:
generate [--with-dummy-text|-t] <# of sections> <# of contents per section>:
generate a specified number of contents without section
clean: (DANGER) remove all numeric contents directories
add-metadata: generate metadata.yml into the same directory where contents.md exists
insert-page <section-path> <position>: insert new page into specified position page
EOS
exit 1
}
generate_sections() {
num_sections=$1
num_contents=$2
for i in `seq 1 $num_sections`; do
generate_section $i $num_contents
done
}
generate_section() {
i=$1
word=`dummy_name`
num_contents=$2
section_name=`printf %03d-%s $i $word`
echo "Generating $section_name section..."
mkdir $section_name
cd $section_name
generate_contents $num_contents
cd ../
}
generate_contents() {
num=$1
for i in `seq 1 $num`; do
generate_content $i
done
}
generate_content() {
i=$1
id=`printf %03d $i`
echo " Generating $id ..."
if [ -d $id ]; then
echo " $id is already exists. skipped"
return
fi
mkdir -p $id
cd $id
generate_skeletone
[ with_dummy ] && append_dummy_texts
cd ..
}
append_dummy_texts() {
if which shuf > /dev/null; then
shuf -n40 /usr/share/dict/words | sed "s|'||" | xargs -n10 >> contents.md
elif which gshuf > /dev/null; then
gshuf -n40 /usr/share/dict/words | sed "s|'||" | xargs -n10 >> contents.md
else
echo " shuf or gshuf isn't found here. skipped generating dummy contents.."
fi
}
dummy_title() {
if which shuf > /dev/null; then
shuf -n5 /usr/share/dict/words | sed "s|'||" | xargs
elif which gshuf > /dev/null; then
gshuf -n5 /usr/share/dict/words | sed "s|'||" | xargs
fi
}
dummy_name() {
if which shuf > /dev/null; then
shuf -n1 /usr/share/dict/words
elif which gshuf > /dev/null; then
gshuf -n1 /usr/share/dict/words
fi
}
generate_skeletone() {
generate_metadata `pwd`
title="<Content Title>"
[ with_dummy ] && title=`dummy_title`
cat<<EOS > contents.md
# $title
EOS
}
generate_metadata() {
path=$1
uuid=`gen_uuid`
cat<<EOS > $path/metadata.yml
uuid: $uuid
EOS
}
generate_config() {
if [ -f $CONFIG_FILE ]; then
echo "Config is already exists. Skipped to generate config"
return
fi
echo "Generate config ..."
uuid=`gen_uuid`
title="<Content Title>"
[ with_dummy ] && title=`dummy_title`
cat<<EOS > $CONFIG_FILE
uuid: $uuid
title: $title
EOS
}
gen_uuid() {
if [ -f /proc/sys/kernel/random/uuid ]; then
cat /proc/sys/kernel/random/uuid
elif which uuidgen > /dev/null; then
uuidgen
else
# Puts a placeholder since this platform doesn't support to generate uuid
echo "<PUT UUID here>"
fi
}
check_dummy_capability() {
if which shuf > /dev/null; then
return
elif which gshuf > /dev/null; then
return
else
echo "shuf and gshuf are not found. Install either, or don't use '--with-dummy-text' flag"
exit 1
fi
}
add_metadata() {
for dir in `find . -name 'contents.md' -exec dirname {} \;`; do
if [ ! -f $dir/metadata.yml ]; then
echo "Generating $dir/metadata.yml..."
generate_metadata $dir
fi
done
}
insert_page() {
path=$1
position=$2
last_position=`ls -1 $path | tail -1 | bc -l`
for i in `seq $last_position $position`; do
src=$path/`printf "%03d" $i`
dst=$path/`printf "%03d" $(($i+1))`
echo "Moving files from $src to $dst ..."
git mv $src $dst
done
cd $path
generate_content $position
cd ../
}
case "$1" in
generate)
shift
if [ "$1" == "-t" -o "$1" == "--with-dummy-text" ]; then
check_dummy_capability
with_dummy=true
shift
fi
[ $# -eq 1 ] && exit_with_usage
generate_config
generate_sections $1 $2
;;
clean)
ret=n
echo -n "Do you delete all content files? y[n]: "
read -r ret
if [ "$ret" == "y" ]; then
echo "Deleting all content files..."
ls -1 | egrep '[0-9]+' | xargs -n1 rm -rf
fi
;;
add-metadata)
add_metadata
;;
insert-page)
shift
[ $# -lt 2 ] && exit_with_usage
insert_page $@
;;
*)
exit_with_usage
esac