~zenomat/tinywiki

c0f36feae4029925f1fc59933cbc64b46ee5787b — zeno 3 months ago e52304e
Only build.sh required now

Moved template.html, style.css to .local/share
3 files changed, 41 insertions(+), 71 deletions(-)

M build.sh
D run
M style.css
M build.sh => build.sh +40 -59
@@ 1,65 1,46 @@
#!/bin/sh -e

if [[ "$1" == 'pdf' ]]; then
	outdir="$HOME/code/tinywiki/deploy_notes/public_pdf"
elif [[ "$1" == 'html' ]]; then
	outdir="$HOME/code/tinywiki/deploy_notes/public_html"
fi
workdir="$(readlink -f "$1")"

if [[ "$1" == 'pdf' ]]; then
	mkdir -p $outdir/tmp
	outfiles=()
	for file in "${@:2}"; do
		echo "Converting $file"
		pandoc  -f markdown+hard_line_breaks "$file" -o $outdir/tmp/$(basename $file | cut -f 1 -d '.').pdf
		outfiles+=($(basename $file | cut -f 1 -d '.').pdf)
	done
	cd $outdir/tmp
	echo "Uniting pdf"
	pdfunite ${outfiles[*]} $outdir/notes.pdf
	cd $outdir
	rm -rf $outdir/tmp
	echo DONE
fi
outdir="$workdir/public_html"

if [[ "$1" == 'html' ]]; then
	mkdir -p "$outdir/tmp"
	mkdir -p "$outdir/assets"
	cp ./deploy_notes/template.html ./deploy_notes/style.css "$outdir/assets"
mkdir -p "$outdir/tmp"
mkdir -p "$outdir/assets"
cp "$HOME/.local/share/tinywiki/template.html" "$HOME/.local/share/tinywiki/style.css" "$outdir/assets"

	for file in "${@:2}"; 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
		echo "["$title"]("$path/$name".html)" >> "$outdir/tmp/toc.md"
	done
cd "$workdir"

	for file in "${@:2}"; do
		echo "Converting $file"
		file=$(echo $file | cut -c 3-)
		path=$(dirname $file)
		name=$(basename $file | cut -f 1 -d '.')
		path_to_root=$(realpath --relative-to="$outdir/$path" "$outdir")
		escaped_path_to_root=$(printf '%s\n' "$path_to_root" | sed -e 's/[\/&]/\\&/g')
		cp $outdir/../../$file $outdir/tmp
		awk -v S="$(cat $outdir/tmp/toc.md)" '/\[\[toc\]\]/{$0=S}1' "$file" > "$outdir/tmp/$name.md"
		grep -Po '\[.+\]\((?!https?:\/\/).*(?<!\.html|\.png|\.svg|\.jpg)\)' "$outdir/../../$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
		pandoc -s --template="$outdir/assets/template.html" -V home="$path_to_root/index.html" --css=$path_to_root/assets/style.css --highlight-style breezedark -f markdown+hard_line_breaks --katex $outdir/tmp/"$name".md -o $outdir/$path/$name.html
		sed -i "s/https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/KaTeX\/0.11.1\/katex.min.css/$escaped_path_to_root\/assets\/css\/katex.min.css/g" "$outdir/$path/$name.html"
		sed -i "s/https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/KaTeX\/0.11.1\/katex.min.js/$escaped_path_to_root\/assets\/js\/katex.min.js/g" "$outdir/$path/$name.html"
	done
	cd ..
	cat $outdir/tmp/toc.md
	rm -rf $outdir/tmp
	echo DONE
fi
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
	echo "["$title"]("$path/$name".html)" >> "$outdir/tmp/toc.md"
done

find . -name "*.md" ! -name "toc.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 '.')
	path_to_root=$(realpath --relative-to="$outdir/$path" "$outdir")
	escaped_path_to_root=$(printf '%s\n' "$path_to_root" | sed -e 's/[\/&]/\\&/g')
	cp $workdir/$file $outdir/tmp
	awk -v S="$(cat $outdir/tmp/toc.md)" '/\[\[toc\]\]/{$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
pandoc -s --template="$outdir/assets/template.html" -V home="$path_to_root/index.html" --css=$path_to_root/assets/style.css --highlight-style breezedark -f markdown+hard_line_breaks --katex $outdir/tmp/"$name".md -o $outdir/$path/$name.html
sed -i "s/https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/KaTeX\/0.11.1\/katex.min.css/$escaped_path_to_root\/assets\/css\/katex.min.css/g" "$outdir/$path/$name.html"
sed -i "s/https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/KaTeX\/0.11.1\/katex.min.js/$escaped_path_to_root\/assets\/js\/katex.min.js/g" "$outdir/$path/$name.html"
done
cd $workdir
rm -rf $outdir/tmp
echo DONE

D run => run +0 -9
@@ 1,9 0,0 @@
#!/bin/sh

cd $HOME/code/tinywiki
if [ "$1" == "html" ]; then
	find . -name "*.md" -exec deploy_notes/build.sh html '{}' +;
elif [ "$1" == "pdf" ]; then
	find . -name "*.md" -exec deploy_notes/build.sh pdf '{}' +;
fi
rm -rf ~/docs/notes_html

M style.css => style.css +1 -3
@@ 112,9 112,7 @@ tbody tr:nth-child(odd) th {
}

.container {
  max-width: 38rem;
  padding-left:  1rem;
  padding-right: 1rem;
  max-width: 42rem;
  margin-left:  auto;
  margin-right: auto;
}