A shell script template engine for generating HTML markup
Go to file
2022-09-25 19:03:28 -06:00
dist Added ability to build and install cli binary so that templates can be executed directly 2022-09-04 13:53:04 -06:00
example_templates Changed block code tags 2022-09-25 16:20:16 -06:00
src Improved handling of escape sequences 2022-09-25 16:48:14 -06:00
.gitignore Added support for executing templates directly. After installing, templates with an appropriate hashbang can be marked as executable and ran directly. 2022-09-03 22:21:53 -06:00
cli.mjs Made child process exit handling more robust 2022-09-25 18:54:24 -06:00
index.mjs Added ability to build and install cli binary so that templates can be executed directly 2022-09-04 13:53:04 -06:00
package-lock.json Added ability to build and install cli binary so that templates can be executed directly 2022-09-04 13:53:04 -06:00
package.json Fixed install script conflict 2022-09-05 15:22:08 -06:00
README.md Updated readme 2022-09-25 19:03:28 -06:00
webpack.config.mjs Added ability to build and install cli binary so that templates can be executed directly 2022-09-04 13:53:04 -06:00

Template Engine

A shell script template engine for generating scripts that render HTML markup, or anything really.

Examples

$ #!/bin/sh
$ title='My Restaurant Randomizer';
<!doctype html>
<html>
  <head>
    <title><% $title %></title>
  </head>
  <body>
    <h1>Welcome to <% $title %></h1>
    <p>From the following restaurants:</p>
    <ul>
      # Loop through restaurants and display each one
      $ while read -r restaurant; do
        <li><% $restaurant %></li>
      $ done <restaurants.txt
    </ul>
    <p>
      The fates have decided that you shall eat at:
      <strong><$% shuf -n 1 restaurants.txt %></strong>
    </p>
  </body>
</html>

The above template will generate the following shell script:

#!/bin/sh
title='My Restaurant Randomizer';
printf '%s\n' '<!doctype html>';
printf '%s\n' '<html>';
printf '%s\n' '  <head>';
printf '%s' '    <title>';
printf '%s' "$title" | sed 's/\&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/"/\&quot;/g;s/'\''/\&#39;/g'
printf '%s\n' '</title>';
printf '%s\n' '  </head>';
printf '%s\n' '  <body>';
printf '%s' '    <h1>Welcome to ';
printf '%s' "$title" | sed 's/\&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/"/\&quot;/g;s/'\''/\&#39;/g'
printf '%s\n' '</h1>';
printf '%s\n' '    <p>From the following restaurants:</p>';
printf '%s\n' '    <ul>';
# Loop through restaurants and display each one
while read -r restaurant; do
printf '%s' '        <li>';
printf '%s' "$restaurant" | sed 's/\&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/"/\&quot;/g;s/'\''/\&#39;/g'
printf '%s\n' '</li>';
done <restaurants.txt
printf '%s\n' '    </ul>';
printf '%s\n' '    <p>';
printf '%s\n' '      The fates have decided that you shall eat at:';
printf '%s' '      <strong>';
printf '%s' "$(shuf -n 1 restaurants.txt)" | sed 's/\&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/"/\&quot;/g;s/'\''/\&#39;/g'
printf '%s\n' '</strong>';
printf '%s\n' '    </p>';
printf '%s\n' '  </body>';
printf '%s\n' '</html>';

Which when executed might produce the following HTML:

<!doctype html>
<html>
  <head>
    <title>My Restaurant Randomizer</title>
  </head>
  <body>
    <h1>Welcome to My Restaurant Randomizer</h1>
    <p>From the following restaurants:</p>
    <ul>
        <li>Luigi&apos;s</li>
        <li>Papa Johns</li>
        <li>SushiQ</li>
    </ul>
    <p>
      The fates have decided that you shall eat at:
      <strong>Luigi&apos;s</strong>
    </p>
  </body>
</html>

License

MIT