-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc-docs.sh
executable file
·108 lines (88 loc) · 2.92 KB
/
doc-docs.sh
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
#!/usr/bin/env bash
create_html_file() {
tmpfile=`mktemp`
tmpcontent=`mktemp`
tmpfile_md=`mktemp`
html_file="${1%.md}.html"
dirname=`dirname $1`
if [ "$1" = "$tmpindex" ]; then
relative_path='.'
cat $1 > $tmpfile_md
else
relative_path=`realpath --relative-to="$dirname" "${PWD}"`
cat ${PAGE_HEAD} > $tmpfile_md
cat $1 >> $tmpfile_md
cat ${PAGE_TAIL} >> $tmpfile_md
fi
pandoc -f gfm $tmpfile_md -o $tmpcontent
rm $tmpfile_md
cat <<EOF > $tmpfile
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<link rel="stylesheet" href="$relative_path/style.css">
<article class="markdown-body">
EOF
cat $tmpcontent >> $tmpfile && rm $tmpcontent
cat <<EOF >> $tmpfile
</article>
EOF
mkdir -p "$2/$dirname"
mv $tmpfile "$2/$html_file"
echo "Created '$2/$html_file'"
}
mkdir -p ${OUT_DIR}
cp ${STYLESHEET} "${OUT_DIR}/style.css"
echo "Created '${OUT_DIR}' and copied '${STYLESHEET}'"
tmpdir=`mktemp -d`
tmpindex="$tmpdir/index.md"
tmplinks=`mktemp`
tmptree=`mktemp`
tmptree_dirs=`mktemp`
echo "Creating tree..."
cat <<EOF > $tmptree
<pre><code>
EOF
time tree . -f --prune -P '*.md' >> $tmptree
time find . -type f -iname "*.md" -print0 | while IFS= read -r -d $'\0' file; do
source_file=`echo "$file" | cut -d'/' -f2-`
html_filename="${source_file%.md}.html"
basename=`basename $html_filename`
modified_date=`stat -c %y $source_file | awk -F ' ' '{print $1}'`
create_html_file $source_file ${OUT_DIR}
sed -i "s~$source_file~<a href='$html_filename'>$basename</a>~g" $tmptree
cat <<EOF >> $tmplinks
$modified_date<DEL>$modified_date - [$html_filename]($html_filename)
EOF
done
sed -i "s~\./~~g" $tmptree
# Don't process links and the following 'cat' code later. Could also delete each entry indivdually while processing above.
sed '\~\.html</a>$~d' $tmptree > $tmptree_dirs
cat <<EOF >> $tmptree
</code></pre>
<hr/>
EOF
echo "Prettifying tree..."
# Start reading at line 3. Each $dirpath occurs once in our tree command output.
time sed -n '3,$p' $tmptree_dirs | while IFS="" read -r line || [ -n "$line" ]; do
dirpath=`echo "$line" | awk -F '── ' '{print $2}'`
basename=`basename $dirpath`
sed -i "s~── $dirpath$~── $basename~1" $tmptree
done
rm $tmptree_dirs
echo "Creating index..."
cat ${INDEX_HEAD} > $tmpindex
cat $tmptree >> $tmpindex && rm $tmptree
cat $tmplinks |
sort -r -b -k 1.1,1.4 -k 1.6,1.7 -k 1.9,1.10 |
awk -F '<DEL>' '{print $2}' >> $tmpindex && rm $tmplinks
cat ${INDEX_TAIL} >> $tmpindex
create_html_file $tmpindex ${OUT_DIR}
rm $tmpindex
mv "${OUT_DIR}$tmpdir/index.html" "${OUT_DIR}/index.html"
echo "Moved '${OUT_DIR}$tmpdir/index.html' to '${OUT_DIR}/index.html'"
rm -rf "${OUT_DIR}/tmp"
echo "Removed '${OUT_DIR}/tmp'"
echo "Fixing permissions: ${UID}:${GID} 0755 on ${OUT_DIR}"
chown -R ${UID}:${GID} ${OUT_DIR}
chmod -R 0755 ${OUT_DIR}
echo "Done"