ehehhehehe

This commit is contained in:
yosh 2024-03-16 04:33:36 -04:00
parent abcd3584f9
commit 6b31634f78
2 changed files with 20 additions and 25 deletions

View File

@ -29,14 +29,14 @@ __process_shp() {
case "$__LINE'$__SHLVL'" in
*"?>"*)
eval "${__LINE_EXEC_'$__SHLVL'%%\?>*}"
printf "%s" "${__LINE'$__SHLVL'##*\?>}"
printf "%s" "${__LINE'$__SHLVL'#*\?>}"
__SH_FLAG'$__SHLVL'=""
__LINE_EXEC_'$__SHLVL'=""
;;
*) ;;
esac
fi
done < "${1:-/dev/stdin}"
done '"${1:+"< \"\$1\""}"'
[ $__SH_FLAG'$__SHLVL' ] && eval "$__LINE_EXEC_'$__SHLVL'"'
__SHLVL=$((__SHLVL - 1))
}
@ -47,10 +47,10 @@ __lowdown() { lowdown --html-no-owasp --html-no-skiphtml --html-no-escapehtml "$
# process a pure markdown file
# uses a special file in _INCDIR, `markdown-template.sh` to create the html
__process_md() {
{
eval ' {
set_md_metadata
__process_shp "$_INCDIR"/_markdown-template.shp
} < "${1:-/dev/stdin}"
}'"${1:+"< \"\$1\""}"
}
# standard library
@ -70,6 +70,9 @@ include() {
# using /dev/stdin makes the input behave Very Weirdly, I can't really explain
# what is going on but it just doesn't work how one would expect
# this eval prevents a lengthy if-else statement
# I adapted this method to all other stuff that either accepts stdin or an argument
# for maximum portability, I guess. even though every relevant unix has /dev/stdin
# I guess it's paranoia for the accidentally swallowing stdin thing as well. whatever
set_md_metadata() {
eval ' {
# if 1st line isnt all -, skip setting metadata
@ -118,28 +121,17 @@ escapepipe() {
sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' -e 's/"/\&quot;/g' -e "s/'/\\&#39/g"
}
__cleanup() {
trap 'exit' INT HUP QUIT TERM EXIT
[ -f "${__FILE_TMP:-}" ] && rm "$__FILE_TMP"
}
trap '__cleanup' INT HUP QUIT TERM EXIT
__FILE_TMP="$(mktemp -u)"
# build folder structure
find "$_SRCDIR" -type d -exec sh -c 'for d; do mkdir -p "$_BUILDDIR"/"${d#"$_SRCDIR"}"; done' sh {} +
while read -r _FILE; do
while IFS=i read -r _FILE; do
cd "${_FILE%/*}"
__OUTFILE="$_BUILDDIR/${_FILE#"$_SRCDIR"}"
case "${_FILE##*/}" in
*.[hH][tT][mM][lL]) ( __process_shp "$_FILE" ) > "$__FILE_TMP" ;;
*.[mM][dD]) ( __process_md "$_FILE" ) > "$__FILE_TMP"; __OUTFILE="${__OUTFILE%.*}.html" ;;
mdmeta) continue ;;
*) cp -p "$_FILE" "$__FILE_TMP" ;;
*.[hH][tT][mM][lL]|*.[Ss][Hh][Pp]) ( __process_shp "$_FILE" ) > "$__OUTFILE" ;;
*.[mM][dD]) ( __process_md "$_FILE" ) > "${__OUTFILE%.*}.html" ;;
*) cp -p "$_FILE" "$__OUTFILE" ;;
esac
if ! cmp -s "$__FILE_TMP" "$__OUTFILE"; then
mv "$__FILE_TMP" "$__OUTFILE"
fi
done <<-EOF
$(find "$_SRCDIR" -type f)
EOF

View File

@ -2,7 +2,7 @@
title = shell: hypertext processor
time = 2023-11-20T23:40:00Z
uuid = 14f1934b-76d1-4dd4-ae1c-65bf0e305eea
TB_TITLE = shp
TB_TITLE = xterm - sh build.sh
---
that name might ring a few alarm bells to anyone with a sense of morality. alas, necessity is the mother of invention, and I have willed this into existence
@ -15,16 +15,16 @@ I chose to make my own. I've already had a bunch of experience writing shell, so
[the complete end result](https://git.unix.dog/yosh/website/src/commit/68a17eb2afce3eeeca336a7d30d80b75d586fd5c/build.sh) fucking sucked in retrospect. how it'd work was that you'd have to make a single line comment following the form of `<!--RES f=file;VAR=val;VAR2=val2 -->` etc. to bring in a template in the `BUILD_RESOURCES` folder, replacing any strings matching `var-VAR` with `val`, `var-VAR2` with `val2`, etc.
certainly, it was slow as shit as well. I can't remember the exact specifics, but it'd take almost 2 seconds to go through my (very small, mind you) site on my hardware, let alone how long it took on unix.dog's hardware (I want to say around 5-6 seconds?). clearly, I needed something better
certainly, it was slow as shit as well. I can't remember the exact specifics, but it'd take almost 2 seconds to go through my (very small, mind you) site on my hardware, let alone how long it took on unix.dog's hardware (I want to say around 5-6 seconds?). all those grep calls weren't really helping. clearly, I needed something better
again, I still thought no templating software was available, so I set out to make a *better* way to update my site with a shell script
again, I still thought no templating software was available, so I set out to make a *different* way to update my site with a shell script
and that's how I landed on shp
## the process
from the get-go, I had the idea of making a php-like templating method but with shell. the initial draft had the bright idea of using the html comments `<!--sh` and `-->` to represent the blocks of shell that would be run. when it came to detecting the boundaries and substituting the output of shell commands, I [used `awk` and temporary files](https://git.unix.dog/yosh/website/src/commit/b20f843c47a7adb4f5cdd4e31ad7b498b227ee24/build.sh) to perform the actual replacement, as well as grep to see if I still needed to replace stuff
of course, this was slow as shit, as well as unreliable. It'd take over a second to build the site on my machine--which keep in mind, still wasn't using markdown for anything. this [was optimized](https://git.unix.dog/yosh/website/src/commit/ab5c46d14f0a46dd39be0b4432a1896d826bffa4/build.sh) to just about a second by doing a single run of `grep` for each file, storing the counted lines, then iterating over that. even still, the unreliability persisted, because in the even that the shell block outputted `<!--sh <stuff> -->`, the awk would fuck up and flag it. of course, any reasonable person *wouldn't* do this, but reasonable people *would* put `-->` for arrows in their webpage, and managing that proved to be a much harder task than one would think in awk. another refactor had to be done
of course, this was slow as shit, as well as unreliable. It'd take over a second to build the site on my machine--which keep in mind, *still wasn't using markdown for anything*. this [was optimized](https://git.unix.dog/yosh/website/src/commit/ab5c46d14f0a46dd39be0b4432a1896d826bffa4/build.sh) to just about a second by doing a single run of `grep` for each file, storing the counted lines, then iterating over that. even still, the unreliability persisted, because in the event that the shell block outputted `<!--sh <stuff> -->`, the awk would fuck up and flag it. of course, any reasonable person *wouldn't* do this, but reasonable people *would* put `-->` for arrows in their webpage, and managing that proved to be a much harder task than one would think in awk. another refactor had to be done
[by this point](https://git.unix.dog/yosh/website/src/commit/b20f843c47a7adb4f5cdd4e31ad7b498b227ee24/build.sh), I dropped the idea of using normal html comments and went full-on using `<?shp <shell> ?>` syntax (well, `<?sh` here, but that'll change in the next commit). this didn't exactly simplify parsing relatively--you'll see why later--but it *did* play in to the idea of making this more "php-like", which I find funny. instead of using the super slow method of `grep` + `awk` for every single file, I made a `while read -r` loop for the processing function, which fed into a `case` glob parser for each line, setting a flag depending on if the line contained `<?shp` or not. if the flag was null, then echo the line. if it was just set, echo everything before the `<?shp` and set it. if it is active (including just set), `eval` the line. if the line contains `?>`, then print everything after it and unset the shell flag. this significantly sped up the process, given how it wasn't iterating over the entire file multiple times--only once now
@ -41,4 +41,7 @@ as it stands, I feel as if I am completely done with this. the "library" gives y
the only caveat that I have seen that remains in the final script is the fact that you can't start a `shp` block on the same line as one ends. for the sake of keeping the parsing code simple, I am not changing this. any reasonable person wouldn't do this.
the script is [bundled with my website repo](https://git.unix.dog/yosh/website/src/branch/master/build.sh). it's a shell script. I don't care enough to give it the whole 9 yards with a separate repo and license and etc. be reasonable
## the updated result
this part is being written from the future (2024-03-16). I went through a few iterations of the script for a bit, having an `mdmeta` at first from the end of my initial result, but now metadata is within each markdown file. the site, as it stands, builds in ~0.4 seconds on my machine. takes about 0.8 seconds on unix.dog. no external program calls are inherently part of processing any given file, except for `lowdown` for markdown processing. it's all pure shell. I consider that a win for a shell script of this caliber
the script is [bundled with my website repo](https://git.unix.dog/yosh/website/src/branch/master/build.sh). it's a shell script. I don't care enough to give it the whole 9 yards with a separate repo and license and etc. I don't think anyone other than me is even crazy enough to use it. be reasonable