~stormdragon2976/bashtml

a86ef1d164fd7e30e4d5c74ea7cde0e49a01f306 — Storm Dragon 1 year, 7 months ago e9b89a2
Updated the documentation. Added the rebuild option, still needs more testing, but works for now.
2 files changed, 58 insertions(+), 5 deletions(-)

M README.md
M bashtml.sh
M README.md => README.md +2 -1
@@ 25,13 25,14 @@ The following options are available.

- -h, --help: A brief help message.
- -n, -p, --post: Create a new post.
- -r, --rebuild: Rebuild the site. Requires the markdown files.
- -s, --sync: Sync your local copy of the site to the server, requires the rsync section of the configuration file be set.


## Plugins


Plugins extend the functionality of bashtml. A few plugins are included by default, but are not active by default. In your site directory, create a  directory called bashtml-plugins. Place the plugin you want to use in the bashtml-plugins directory. The last part of the file name determines the type of plugin. For example, the name myplugin.prefooter means that the plugin is type prefooter, and will appear just before the footer on the page.
Plugins extend the functionality of bashtml. Some plugins are available from the [bashtml-plugins project](https://gitlab.com/stormdragon2976/bashtml-plugins). In your site directory, create a  directory called bashtml-plugins. Place the plugin you want to use in the bashtml-plugins directory. The last part of the file name determines the type of plugin. For example, the name myplugin.prefooter means that the plugin is type prefooter, and will appear just before the footer on the page. You can, of course simlink plugin.sh to both plugin.prefooter and plugin.postfooter if you want so that it appears twice.


Plugins will have access to the settings variable in the .bashtmlrc. This means that your plugin will be able to use things like author, etc. Plugins are also sent the current pagename where applicable. The pre and post sync plugin types do not receive the page name.

M bashtml.sh => bashtml.sh +56 -4
@@ 5,13 5,14 @@ command=(
  h # help
  n # New post
  p # New post
  r # Rebuild site
  s # sync to server using rsync
)
htmlFile="$(mktemp -t html.XXXXXXXXXXXX)"
ifs="$IFS"
mdFile="$(mktemp -t markdown.XXXXXXXXXXXX)"
scriptName="BasHTML"
# We are in pre-alpha, so version will be 0.00#
# We are moving toward version 1.0
scriptVersion="0.90"
  
cleanup() {


@@ 426,8 427,8 @@ EOF
}

build_pages() {
  # Set the filename to index.html so that plugins that depend on it will work.
  fileName="index.html"
  # Set the filename to index.ext so that plugins that depend on it will work.
  fileName="index.${settings[pageExtension]}"
  # Start the index file
  add_header "index"
  # Remove the postnumber from the index.


@@ 547,6 548,55 @@ new_post() {
  fi
}

rebuild_site() {
    echo "Rebuilding \"${settings[blogName]}\", please wait..."
    for mdFile in *.md ; do
        htmlFile="${mdFile%.md}.${settings[pageExtension]}"
        # Extract the title, the first line, to pass to plugins
        local title="$(head -1 "$mdFile" | sed 's/^[# ]*//')"
        # Page name is the title, with dashes instead of spaces, spaces compressed to only 1 at a time, and no punctuation.
        local pageName="$(echo "$title" | sed -e 's/^[[:space:]]\+//' -e 's/[[:space:]]\+$//' -e 's/[[:punct:]]//g' -e 's/[[:space:]]\+/-/g')"
        # Page names should also be completely lowercase.
        pageName="${pageName,,}"
        # Get the final file name to pass to plugins.
  fileName="${pageName}.${settings[pageExtension]}"
        # make sure the first line (title) is a heading level 2.
        sed -i -e '1s/^\([[:space:][:alnum:]]\)/## \1/' -e '1s/^#\+/##/' "$mdFile"
        # Add the header:
        add_header "$htmlFile"
        markdown "$mdFile" >> "$htmlFile"
        # Add the date. Note the html file is edited because it is created in the step above.
        # Slashes have to be escaped in html tags to not clash with sed. Of course I could have used something else like ; but I didn't.
        # Also, converted the title into a link to the page itself.
        sed -i "0,/<h2 id=\".*\">/{s/<h2 id=\".*\">\(.*\)<\/h2>$/<h2><a href=\"${pageName}.${settings[pageExtension]}\">\1<\/a><\/h2>\n<p>Posted $(date "+${settings[dateFormat]}") by ${settings[author]}<\/p>\n/}" "$htmlFile"
        # Create an array from the posts tags in the markdown file.
        # We need 2 sed statements, because the -e thing works on the whole input, not the previous result.
        local tags=($(sed -n 's/^Tags: \+\(.*\)/\1/p' "$mdFile" | sed 's/, */ /g'))
        if [[ ${#tags[@]} -ge 1 && -n "${settings[gnuSocialURL]}" ]]; then
            # Remove the tags line from the html file.
            # This needs to be the last occurrence, and I haven't found an easy solution to do this with sed.
            # So, I am thinking to use grep to get the last line number, then delete that line
            local i=$(grep -n "^<p>Tags: .*</p>$" "$htmlFile" | tail -1 | cut -d: -f1)
            sed -i "${i}d"  "$htmlFile"
            # Add tags back as links to the tags directory
            echo "<hr>" >> "$htmlFile"
            echo "<h4>Tags</h4>" >> "$htmlFile"
            echo "<p>" >> "$htmlFile"
            for i in ${tags[@]} ; do
                echo "<a href=\"${settings[gnuSocialURL]}/search/notice?q=$i\">$i</a>," >> "$htmlFile"
            done
    echo "</p>" >> "$htmlFile"
        fi
        # Add the footer:
        add_footer
    done
    # make a temperary file for index.ext and markdown file.
    # This prevents removal of markdown file.
    htmlFile="$(mktemp -t html.XXXXXXXXXXXX)"
    mdFile="$(mktemp -t html.XXXXXXXXXXXX)"
build_pages
}

#When the script closes, make sure everything is cleaned up.
trap cleanup EXIT
# Load user settings.


@@ 558,8 608,9 @@ for i in "$@"; do
  shift
  case "$i" in
    "--help") set -- "$@" "-h";;
    "--sync")   set -- "$@" "-s";;
    "--"new|post)   set -- "$@" "-p";;
    "--rebuild")   set -- "$@" "-r";;
    "--sync")   set -- "$@" "-s";;
    *)        set -- "$@" "$i"
  esac
done


@@ 571,6 622,7 @@ while getopts $(echo "${command[@]}" | tr -d '[:space:]') i ; do
    h) show_help;;
    n|p) new_post;;
    s) sync_to_server;;
    r) rebuild_site;;
  esac
done
exit 0