]> git.rocketbowman.com Git - jetsam.git/commitdiff
feat: add rudimentary index maker
authorKyle Bowman <kyle+github@rocketbowman.com>
Sat, 28 Sep 2024 21:49:06 +0000 (17:49 -0400)
committerKyle Bowman <kyle+github@rocketbowman.com>
Sat, 28 Sep 2024 21:49:06 +0000 (17:49 -0400)
README.md
experiments.md
index.sh [new file with mode: 0755]

index 3fd9d2202058b66ceb9870757cc5a86d24bd9a6b..f875680c3fd06de60b0725b20fd29bf9d1b87974 100644 (file)
--- 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)"
index 3f38c108b9aeeb40eb1212dd94714ebd7f7e2970..11a3c6bf1101d851bf98169767a32df0f9aae4ce 100644 (file)
@@ -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 
+`<a href=...`, the `make_li` function creates `<li><a</li>\n<li>href=...</li>` 
+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 (executable)
index 0000000..263d8b1
--- /dev/null
+++ b/index.sh
@@ -0,0 +1,71 @@
+#!/bin/bash
+
+#############
+# CONSTANTS #
+#############
+BUILD_DIR="build"
+
+#############
+# FUNCTIONS #
+#############
+
+get_title() {
+    grep -oP '(?<=<title>).*?(?=</title>)' "$1"
+}
+    # DESC: Gets the text between the <title> 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</title>
+    <meta content=\"width=device-width, initial-scale=1\" name=\"viewport\">
+    <link href='../_static/css/normalize.css' rel='stylesheet' type='text/css'>
+    <link href='../_static/css/space-sakura.css' id=\"sakura-css\" rel='stylesheet' type='text/css'>
+  </head>
+  <body>
+    <header>
+      <h1>Pages in Build</h1>
+      %s 
+    </header>
+  </body>
+</html>" "$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")"