From 5fa86c870941586f7f8a6f7da34fce632a7dbce5 Mon Sep 17 00:00:00 2001 From: Kyle Bowman Date: Sat, 28 Sep 2024 17:49:06 -0400 Subject: [PATCH] feat: add rudimentary index maker --- README.md | 5 ++++ experiments.md | 31 ++++++++++++++++++++++ index.sh | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100755 index.sh diff --git a/README.md b/README.md index 3fd9d22..f875680 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,11 @@ you can use symlinks. From `src`, use `ln -s ../../dir src/dir` to make it appear that your markdown files are in the directory, even if they are in a separate +## Building + +`make all` builds all the HTML from the Markdown. +`./index.sh` builds the index based on content from build directory. + # Common Errors ## "pandoc: build/public/note1: withFile: does not exist (No such file or directory)" diff --git a/experiments.md b/experiments.md index 3f38c10..11a3c6b 100644 --- a/experiments.md +++ b/experiments.md @@ -77,6 +77,37 @@ non-option argument command_string. If there are arguments after the command_ arguments are assigned to the positional parameters*. The assignment to $0 sets the name of the shell, which is used in warning and error messages. +# Regarding Map in Bash + +Map would be nice because then you don't have to loop inside definitions +But looping doesn't play nice with spaces. +I was pleasantly suprised it worked. Maybe there's a way around the +space issue, but today isn't the day for that. + +``` bash +map() { + for item in $2; do + # shellcheck disable=SC2005 + echo "$("$1" "$item")" + done +} +``` + +## BUG: Map breaks on spaces +Context `map "make_li" "$1"`. + +When `$1` contains a space, it breaks there. *e.g.* when it contains +`\n
  • href=...
  • ` +and so on. To be clear, this is not the fault of `make_li`. +This is due to `$1` begin a space-delimited list and `map` breaking +on spaces. Also, to be fair, that's just the way shells work by default. +There's probably a clever way to manipulate lists as strings, but I don't know +what the idiomatic shell way of doing it is. + +## Cut Fiasco +`cut ... $1` reads the file itself. +`echo $1 | cut ...` reads the string in `$1`. + # Conclusion After tidying it all up, this command builds my entire stack of HTML and the links work! diff --git a/index.sh b/index.sh new file mode 100755 index 0000000..263d8b1 --- /dev/null +++ b/index.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +############# +# CONSTANTS # +############# +BUILD_DIR="build" + +############# +# FUNCTIONS # +############# + +get_title() { + grep -oP '(?<=).*?(?=)' "$1" +} + # DESC: Gets the text between the tags of an HTML file. + # $1: Path to HTML file + +make_a() { + local path + path="$(echo "$1" | cut -d '/' -f2- )" + printf "<a href=\"%s\">%s</a>\n" "$path" "$(get_title "$1")" +} + # DESC: Writes an HTML anchor based on a built HTML file + # $1: Path to HTML file + # NOTE: Cut build/ from the path b/c the path needs to be relative + # and the the file we are creating will go into the build dir. + +make_li(){ + printf "<li>%s</li>\n" "$1" +} + +make_ul() { + echo "<ul>" + for item in $1; do + make_li "$(make_a "$item")" + done + echo "</ul>" +} + # DESC: Adds <ul> tags around a list of links. + # $1: A list of items that have <li> + +make_boilerplate(){ + printf \ +"<!DOCTYPE html> +<html> + <head> + <meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\"> + <title>Pages in Build + + + + + +
    +

    Pages in Build

    + %s +
    + +" "$1" +} + # DESC: Defines the template and injects the body. + # $1: The body of the HTML to add. + # NOTE: The CSS for index is one level higher b/c I'm dumping into + # the build directory rather than one of its subdirectories. + + +######## +# MAIN # +######## +FILES="$(find "$BUILD_DIR" -type 'f')" +make_boilerplate "$(make_ul "$FILES")" -- 2.39.5