~zenomat/tinywiki

427a81d5b39a35d4c16842907c9616fde716429b — zeno a month ago 0dac829
Add basepath
2 files changed, 47 insertions(+), 21 deletions(-)

M README.md
M build.sh
M README.md => README.md +12 -3
@@ 1,20 1,28 @@
Tinywiki is a simple bash script, which converts a folder into a static html wiki.

- No external JS is downloaded or called
- Only JS used is Katex, only if Katex math is included, and only in that file
- Katex lives locally

- this is in a super rough state at the moment
- improvments will come till it is in a state where:
	- you git clone the repo
	- issue one command
	- have a static html wiki
- **Requires bash**

# Features
- Table of contents insertion with [[toc]]
- File index insertion with [[index]]
- katex math (katex libs locally, so no external cdns)
- pandoc frontmatter
- subfolders
- maybe some others I am forgetting
- (beautiful) styling

# Requirements
- bash (not posix yet)
- pandoc
- coreutils

# Installation
``` bash
git clone https://git.sr.ht/~zenomat/tinywiki


@@ 26,6 34,7 @@ cp style.css template.html $HOME/.local/share/tinywiki

# Usage
- one `index.html` at the root
- insert `[[toc]]` to insert a toc at the place
- insert `[[index]]` to insert an index of files
- pass path of markdown folder to build.sh
- optionally pass basedir as second arg, if you want to deploy to a webserver
- open `<folder>/public_html/index.html`

M build.sh => build.sh +35 -18
@@ 1,6 1,11 @@
#!/bin/sh -e

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

outdir="$workdir/public_html"



@@ 13,45 18,57 @@ 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-)
	path_to_root=$(realpath --relative-to="$outdir/$path" "$outdir")
	escaped_path_to_root=$(printf '%s\n' "$path_to_root" | sed -e 's/[\/&]/\\&/g')

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

	# TODO(zeno): Make this webserver ready
	# only works on local machine, because absolute path,
	# on webserver with domain, we shoudl support basepath
	echo "["$title"]("$outdir/$path/$name".html)" >> "$outdir/tmp/toc.md"
	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 "toc.md" | while read -r file; do
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 '.')
	path_to_root=$(realpath --relative-to="$outdir/$path" "$outdir")
	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')

	cp $workdir/$file $outdir/tmp
	awk -v S="$(cat $outdir/tmp/toc.md)" '/\[\[toc\]\]/{$0=S}1' "$file" > "$outdir/tmp/$name.md"
	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
pandoc -s --template="$outdir/assets/template.html" -V 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
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"
		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/css/style.css --highlight-style breezedark -f markdown+hard_line_breaks --katex $outdir/tmp/"$name".md -o $outdir/$path/$name.html

	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
cd $workdir

rm -rf $outdir/tmp
echo DONE