Rewrote templating engine

This commit is contained in:
Jocelyn 2024-11-19 22:51:41 +01:00
parent 63dd5fd5b3
commit 577eba63a0
3 changed files with 29 additions and 7 deletions

View file

@ -9,9 +9,9 @@ mkdir dist&&cd dist
cp ../style.css . cp ../style.css .
for file in $(du -a ../src/ | cut -f2 | grep -E '.md$'); do for file in $(du -a ../src/ | cut -f2 | grep -E '.md$'); do
target="$(echo $file | source_to_target)" target="$(echo $file | source_to_target)"
content="$(pandoc --from markdown+emoji --wrap=none -i $file | sed -e 's/[]\/$*.^[]/\\&/g' | escape_newlines )" # To escape the string for sed later on export CONTENT="$(pandoc --from markdown+emoji --wrap=none -i $file)"
prefix=$(realpath --relative-to $(dirname $target) .) export PREFIX=$(realpath --relative-to $(dirname $target) .)
sidebar=$(for x in ../src/**/*.md; do echo "<li><a href=\"$(echo $prefix/$x | source_to_target)\">$(realpath --relative-to ../src/ $x | sed -e 's/.md$//')</a></li>" | sed -e 's/[]\/$*.^[]/\\&/g' | escape_newlines; done) export SIDEBAR=$(for x in ../src/**/*.md; do echo "<li><a href=\"$(echo $PREFIX/$x | source_to_target)\">$(realpath --relative-to ../src/ $x | sed -e 's/.md$//')</a></li>"; done)
mkdir -p $(dirname $target) mkdir -p $(dirname $target)
cat ../template.tpl | sed -e "s/{CONTENT}/$content/" -e "s:{PREFIX}:$prefix:" -e "s/{SIDEBAR}/$sidebar/" > $target cat ../template.tpl | awk -f ../template.awk | bash > $target
done done

22
template.awk Normal file
View file

@ -0,0 +1,22 @@
# escaped lines
/^[ \t]*%/ {
i = index($0, "%");
print substr($0, i+1, length($0)-i);
next;
}
# inline code
/%\(.*%\)/ {
gsub(/'/, "\\'", $0);
gsub(/%\(.*%\)/, "';echo -n &;echo '", $0);
gsub(/%\(/, "", $0);
gsub(/%\)/, "", $0);
printf("echo -n '%s'\n", $0);
next;
}
{
gsub(/'/, "\\'", $0);
printf("echo '%s'\n", $0);
}

View file

@ -2,7 +2,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Jocelyn Otto</title> <title>Jocelyn Otto</title>
<link rel="stylesheet" href="{PREFIX}/style.css"> <link rel="stylesheet" href="%($PREFIX%)/style.css">
</head> </head>
<body> <body>
@ -12,11 +12,11 @@
</header> </header>
<div class="sidebar"> <div class="sidebar">
<ul class="sidemenu"> <ul class="sidemenu">
{SIDEBAR} % echo $SIDEBAR
</ul> </ul>
</div> </div>
<div class="content"> <div class="content">
{CONTENT} % echo $CONTENT
</div> </div>
</body> </body>
</html> </html>