-
Notifications
You must be signed in to change notification settings - Fork 1
/
wiki2html.sh
executable file
·93 lines (76 loc) · 2.8 KB
/
wiki2html.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
FORCE="$1"
SYNTAX="$2"
EXTENSION="$3"
OUTPUTDIR="$4"
INPUT="$5"
CSSFILE="$6" # Not used bad concatenation
FILE=$(basename "$INPUT")
FILENAME=$(basename "$INPUT" .$EXTENSION)
FILEPATH=${INPUT%$FILE}
OUTDIR=${OUTPUTDIR%$FILEPATH*}
OUTPUT="$OUTDIR"/$FILENAME
# Define $MATH
HAS_MATH=$(grep -o "\$\$.\+\$\$" "$INPUT")
if [ ! -z "$HAS_MATH" ]; then
MATH="--mathjax=https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
else
MATH=""
fi
copy_src(){
# TODO get a time stamp and do it once a day
cp -r ~/wiki/wiki/Css/* ~/wiki/wiki_html/Css/
vim -e -c'VimwikiIndex 1' -c'let a=vimwiki#base#get_globlinks_escaped()' -c'let b=join(split(a, "\n"))' -c'call writefile([b], expand("$HOME/wiki/wiki/Src/links.txt"))' -c'q' || echo "vim failed: $?"
vim -e -c'VimwikiIndex 2' -c'let a=vimwiki#base#get_globlinks_escaped()' -c'let b=join(split(a, "\n"))' -c'call writefile([b], expand("$HOME/wiki/todo/Src/links.txt"))' -c'q'
}
# Compile for unix (with pandoc & Perl)
# TODO if there is no level2 afeter level1 ?
munix(){
# Read metadata
meta=$(perl -0777 -ne '$_ =~ /^ *---(.+?)---/s ; $res .= $1; END { print $res; }' "$INPUT");
# Read `wiki_css:` in metadata
CSSFILE=$(realpath --relative-to=$OUTDIR $HOME/wiki/wiki_html/Css/include.css)
export CSS_EMBED=$(echo -e "$meta" | perl -0777 -ne 'while ($_ =~ /^wiki_css:(.+)$/mg) {$res .= " -c " . ($1 =~ s/,/ -c /gr)}; print substr $res, 4')
[ "$CSS_EMBED" ] && export CSSFILE="$CSS_EMBED"
# Read `wiki_pandoc:` in metadata
PANDOC=""
export PANDOC=$(echo -e "$meta" | perl -0777 -ne 'while ($_ =~ /^wiki_pandoc:(.+)$/mg) {print $1}')
# Convertion pipeline
cat "$INPUT" |
# Add h3-section betewwen h3/h2 headings and end
#
# Newline after title
perl -pe ' s/(\s*)(#.*)/\n\2/; ' |
# Replace vim by language-vim for prism color higlight
perl -pe ' s/```vim/```language-vim/; ' |
# Change links: add html
perl -pe ' s/(\[.+?\])\(([^#)]+?)\)/\1(\2.html)/g' |
# Double the new line before code
perl -0pe ' s/((^|\n\S)[^\n]*)\n\t([^*])/\1\n\n\t\3/g;' |
# Remove spaces in void lines
perl -lpe ' s/^\s*$//' |
# Debug
# tee test.md |
# Compile: can add --standalone --self-contained and --include-header=<file>
pandoc $MATH $PANDOC --highlight-style breezedark --section-divs -f $SYNTAX -t html -T $FILE -c $CSSFILE > "$OUTPUT.html"
echo -e "Css: $CSSFILE \nPandoc: $PANDOC \nMeta: $meta"
}
# Obsolete ?
mtermux(){
cat "$INPUT" |
perl -pe ' s/(\[.+\])\(([^#)]+)\)/\1(\2.html)/g' |
python -m markdown |
sed -r 's/<li>(.*)\[ \]/<li class="todo done0">\1/g; s/<li>(.*)\[X\]/<li class="todo done4">\1/g' >"$OUTPUT.html"
# pandoc $MATH -s -f $SYNTAX -t html -c $CSSFILENAME | \
}
# Main
mmain(){
tmp=$(uname -a)
shopt -s nocasematch
if [[ "$tmp" =~ "android" ]] ; then
mtermux
else
munix
fi
}
# Start
mmain