paritybit.ca

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 0dec40e9da3f2574c8b9d53967e1cd24388619fc
parent 91614b93ba0d880bf81a943a6bef0d5d49a50541
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Thu, 12 Sep 2019 10:27:47 -0400

Add table of contents parsing to createpages.sh

This functionality allows for a table of contents to be generated from
the content of the seventh line of every page. It will add a table
of contents section to the side-bar and will add links throughout the
page and to the sections defined in the table of contents.

Entries are to be separated by a semicolon character. Subsections and
sub-subsections are defined by prepending the section name with "sub:"
or "subsub:" respectively without any spaces after the colon. A name of
a section in the table of contents line  _must_ match the name of the
heading in the page otherwise the script does not know where to put the
destination marker.

Special character support may be limited due to the way the section
names are parsed by sed. This will be improved in the future if the need
arises but it's not a priority at the moment.

Diffstat:
Mbuild/base.css | 9+++++++++
Mbuild/header.html | 1+
Mcreatepages.sh | 55+++++++++++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/build/base.css b/build/base.css @@ -206,6 +206,15 @@ kbd { font-size: 1em; } +#toc-title { + padding-left: 10px; + margin-bottom: -10px; +} + +#toc-hr { + border: 3px #585858 solid; +} + /* Ensure proper scaling with differently-sized displays */ @media all and (max-width: 900px) { #side-bar { diff --git a/build/header.html b/build/header.html @@ -23,6 +23,7 @@ <li><a href="/toolset">My Toolset</a></li> </ul> </ul> + <!--TOC--> </div> <div id="content"> diff --git a/createpages.sh b/createpages.sh @@ -85,7 +85,7 @@ for page in $@; do printf " Compiling page content..." markdown "$pagedir"/"$pagename".md > build/"$savepath"/"$pagename"-content.html tput setaf 2; tput bold - echo " [DONE]" + echo " [DONE]" tput sgr0 # Combines output from above with pre-compiled header/footer html @@ -95,7 +95,7 @@ for page in $@; do cat build/"$savepath"/"$pagename"-content.html >> "$htmldir"/"$savepath"/"$pagename".html cat build/footer.html >> "$htmldir"/"$savepath"/"$pagename".html tput setaf 2; tput bold - echo " [DONE]" + echo " [DONE]" tput sgr0 # Assumes first line of every markdown file is the header in the format: @@ -106,7 +106,7 @@ for page in $@; do sed -i "s/<title>.*<\/title>/<title>paritybit.ca – ${pagetitle//&/\\&}<\/title>/" \ "$htmldir"/"$savepath"/"$pagename".html tput setaf 2; tput bold - echo " [DONE]" + echo " [DONE]" tput sgr0 # Assumes the third line of every markdown file is the <meta> tag @@ -120,7 +120,7 @@ for page in $@; do sed -i "s/content=\"\"/content=${metaTextClean//&/\\&}/" \ "$htmldir"/"$savepath"/"$pagename".html tput setaf 2; tput bold - echo " [DONE]" + echo " [DONE]" tput sgr0 # Assumes the fifth line of every markdown file defines the requisite @@ -138,6 +138,53 @@ for page in $@; do $htmldir/$savepath/$pagename.html done tput setaf 2; tput bold + echo " [DONE]" + tput sgr0 + + # Assumes the seventh line of every markdown file defines the sections + # to add to a table of contents. + toc=$(sed '7!d' "$pagedir"/"$pagename".md | cut -d# -f 2) + # Remove leading whitespace and quote marks + tocClean=$(echo -e "${toc}" | sed -e 's/^[[:space:]]*//') + tocClean=$(sed -e 's/^"//' -e 's/"$//' <<< "$tocClean") + printf " Parsing table of contents..." + # Split the line into separate entries, separated by a semicolon + IFS=';' read -r -a sections <<< "$tocClean" + # Check to see if the size of the array is zero, if so, do nothing + if [ ! "${#sections[@]}" -eq "0" ]; then + # Add a horizontal rule and heading + sed -i "/TOC/a <h3 id=\"toc-title\">Table of Contents</h3>" \ + $htmldir/$savepath/$pagename.html + sed -i "/TOC/a <hr id=\"toc-hr\">" \ + $htmldir/$savepath/$pagename.html + # Add each section to list after the h3 tag (done in reverse order + # because lines are appended immediately after the h3 tag) and add + # a link destination to the header of that section + sed -i "/toc-title/a </ul>" $htmldir/$savepath/$pagename.html + for ((i=${#sections[@]}-1; i>=0; i--)); do + # Remove leading whitespaces + sectionName=$(echo -e "${sections[$i]}" | sed -e 's/^[[:space:]]*//') + # Check if this is supposed to be a sub-sub-section & add 2x<ul> + if echo "$sectionName" | grep -qi "^subsub:"; then + sectionName=${sectionName:7} + sed -i "/toc-title/a <ul><ul><li><a href=\"#$sectionName\">$sectionName</a></li></ul></ul>" \ + $htmldir/$savepath/$pagename.html + # Or a sub-section + elif echo "$sectionName" | grep -qi "^sub:"; then + sectionName=${sectionName:4} + sed -i "/toc-title/a <ul><li><a href=\"#$sectionName\">$sectionName</a></li></ul>" \ + $htmldir/$savepath/$pagename.html + # Or just a regular section + else + sed -i "/toc-title/a <li><a href=\"#$sectionName\">$sectionName</a></li>" \ + $htmldir/$savepath/$pagename.html + sed -i "/<h3>$sectionName<\/h3>/i <a name=\"$sectionName\"></a>" \ + $htmldir/$savepath/$pagename.html + fi + done + sed -i "/toc-title/a <ul>" $htmldir/$savepath/$pagename.html + fi + tput setaf 2; tput bold echo " [DONE]" tput sgr0