sc0ttj / mdsh

A simple static site generator, using Markdown and Bash
https://sc0ttj.github.io/mdsh/
10 stars 0 forks source link

Enable proper rebuilds #12

Closed sc0ttj closed 5 years ago

sc0ttj commented 5 years ago

Currently there is not way to re-generate a .md file from its .mdsh source file.

The mdshell script can only take input from the terminal, line by line. It cannot read a file line by line.

In order to update existing pages, the user must edit the .md files, and run updates_pages to re-build the HTML.

So far it all works like this:

create_post:

create_page:

..we need create_post to also take an .mdsh file as $1, and to build a valid .md file from it.

Then, we can do the following:

create_post path/to/file.mdsh > path/to/output-file.md

This will leave us with the following rebuild options:

sc0ttj commented 5 years ago

The following solution reads from a file if the script is called with a file name as the first parameter $1 otherwise from standard input.

while IFS= read -r line
do
  echo "$line"
done < "${1:-/dev/stdin}"

... Also see:

To read from the file or stdin (if argument is not present), you can extend it to:

file=${1--} # POSIX-compliant; ${1:--} can be used either.
while IFS= read -r line; do
  printf '%s\n' "$line" # Or: env POSIXLY_CORRECT=1 echo "$line"
done < <(cat -- "$file")
Notes:

- read -r - Do not treat a backslash character in any special way. Consider each backslash to be part of the input line.

- Without setting IFS, by default the sequences of Space and Tab at the beginning and end of the lines are ignored (trimmed).

- Use printf instead of echo to avoid printing empty lines when the line consists of a single -e, -n or -E. However there is a workaround by using env POSIXLY_CORRECT=1 echo "$line" which executes your external GNU echo which supports it. See: How do I echo "-e"?
sc0ttj commented 5 years ago

(https://stackoverflow.com/questions/6980090/how-to-read-from-a-file-or-stdin-in-bash)

sc0ttj commented 5 years ago

Done.. Added a new script called mdsh2md ... And create_page will now rebuild from mdsh if passed the mdsh as $1 .. Also, updated update_pages, so it now has a -ALL option, which updates from mdsh, not just from md (like `-all)..

So in other words, it now works like so:

and

Using -ALL is a bit slower, but not too bad .. Although it depends on the commands you embed on your mdsh files... Slow commands are the bottle neck.