~zenomat/tinywiki

tinywiki/build.sh -rwxr-xr-x 3.3 KiB
728e6a1a — Adrian Change grep regex to not replace inter-document links a month ago
                                                                                
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
#!/bin/sh -e

function parse_yml()
{
	yml="$(awk 'f{ i=0; if (/---/){printf "%s", buf; f=0; buf="";i++} else buf = buf $0 ORS}; /---/{f=1; if (i==1){exit}}' "$1")"
	yml_data[0]="$(echo "$yml" | shyaml get-value title 2>/dev/null || echo "No title provided")"
	yml_data[1]="$(echo "$yml" | shyaml get-value desc 2>/dev/null || echo "No description provided")"
	yml_data[2]="$(echo "$yml" | shyaml get-value toc 2>/dev/null || echo "false")"
}

workdir="$(readlink -f "$1")"
if ! [ -z "$2" ]; then
	basepath="$2"
else
	basepath=""
fi

outdir="$workdir/public_html"

mkdir -p "$outdir/tmp"
mkdir -p "$outdir/assets"
mkdir -p "$outdir/assets/css"
mkdir -p "$outdir/assets/js"
cp "$HOME/.local/share/tinywiki/template.html" "$outdir/assets"
cp "$HOME/.local/share/tinywiki/style.css" "$outdir/assets/css"
cp "$HOME/.local/share/tinywiki/katex.min.css" "$outdir/assets/css"
cp "$HOME/.local/share/tinywiki/katex.min.js" "$outdir/assets/js"

cd "$workdir"

find . -name "*.md" | while read -r file; do
	path=$(dirname $file)
	name=$(basename $file | cut -f 1 -d '.')
	title=$(grep title "$file" | cut -d":" -f 2- | cut -b 2-)

	if [ "$title" = "" ]; then
		title="$name"
	fi

	if ! [ "$basepath" = "" ]; then
		echo "["$title"]("$basepath/$path/$name".html)" >> "$outdir/tmp/index-ins.md"
	else 
		echo "["$title"]("$outdir/$path/$name".html)" >> "$outdir/tmp/index-ins.md"
	fi
done

find . -name "*.md" ! -name "index-ins.md" | while read -r file; do
	echo "Converting $file"
	file=$(echo $file | cut -c 3-)
	path=$(dirname $file)
	name=$(basename $file | cut -f 1 -d '.')
	if ! [ "$basepath" = "" ]; then
		path_to_root="$basepath"
	else
		path_to_root="$(realpath --relative-to="$outdir/$path" "$outdir")"
	fi
	escaped_path_to_root=$(printf '%s\n' "$path_to_root" | sed -e 's/[\/&]/\\&/g')

	declare -a yml_data
	parse_yml "$file"

	cp $workdir/$file $outdir/tmp
	awk -v S="$(cat $outdir/tmp/index-ins.md)" '/\[\[index\]\]/{$0=S}1' "$file" > "$outdir/tmp/$name.md"

	grep -Po '\[.+\]\((?!#.*)(?!https?:\/\/).*(?<!\.html|\.png|\.svg|\.jpg)\)' "$workdir/$file" | while read match; do
		clean_match=$(echo $match | awk -F '(' ' {{print $2}} ' | cut -f 1 -d ')')
		link=$clean_match.html
		escaped_match=$(printf '%s\n' "$clean_match" | sed -e 's/[\/&]/\\&/g')
		escaped_link=$(printf '%s\n' "$link" | sed -e 's/[\/&]/\\&/g')

		sed -ie "s/$escaped_match/$escaped_link/" "$outdir/tmp/$name.md"
	done

	mkdir -p $outdir/$path
	if [ "${yml_data[2]}" = "True" ]; then
		pandoc --toc -s --template="$outdir/assets/template.html" --variable home="$path_to_root/index.html" --css=$path_to_root/assets/css/style.css --highlight-style breezedark -f markdown+hard_line_breaks --katex $outdir/tmp/"$name".md -o $outdir/$path/$name.html
	else
		pandoc -s --template="$outdir/assets/template.html" --variable home="$path_to_root/index.html" --css=$path_to_root/assets/css/style.css --highlight-style breezedark -f markdown+hard_line_breaks --katex $outdir/tmp/"$name".md -o $outdir/$path/$name.html
	fi

	sed -i "s/https:\/\/cdn.jsdelivr.net\/npm\/katex@[0-9]*\.[0-9]*\.[0-9]*\/dist\/katex.min.css/$escaped_path_to_root\/assets\/css\/katex.min.css/g" "$outdir/$path/$name.html"
	sed -i "s/https:\/\/cdn.jsdelivr.net\/npm\/katex@[0-9]*\.[0-9]*\.[0-9]*\/dist\/katex.min.js/$escaped_path_to_root\/assets\/js\/katex.min.js/g" "$outdir/$path/$name.html"
done

rm -rf $outdir/tmp
echo DONE