LukeSmithxyz/lb

User m4 for HTML

geowy opened this issue · 1 comments

geowy commented

I've been investigating m4 for one of my own websites and thought you might be interested. You can change lines like this:

printf "<html>\\n<head>\\n<title>%s</title>\\n<link rel='stylesheet' type='text/css' href='%s'>\\n<meta charset='utf-8'/>\\n</head>\\n<body>\\n<h1>%s</h1>\\n<small>[<a href='%s#%s'>link</a>&mdash;<a href='%s'>standalone</a>]</small>\\n%s\\n<footer>by <strong><a href='%s'>%s</a></strong></footer>\\n</body>\\n\\n</html>" "$realname" "$css" "$realname" "../$blogfile" "$base" "$basefile" "$(cat "$webdir/blog/.drafts/$basefile")" "$website" "$name" > "$webdir/blog/$basefile"

Into something like this (untested):

m4 -DREALNAME=$realname -DCSS=$css -DBLOGFILE="../$blogfile" -DBASE=$base -DBASEFILE=$basefile -DCONTENT="$(cat "$webdir/blog/.drafts/$basefile")" -DWEBSITE=$website -DNAME=$name <<HTML > "$webdir/blog/$basefile"
<html>
  <head>
    <title>REALNAME</title>
    <link rel='stylesheet' type='text/css' href='CSS'>
    <meta charset='utf-8'/>
  </head>
  <body>
    <h1>REALNAME</h1>
    <small>[<a href='BLOGFILE#BASE'>link</a>&mdash;<a href='BASEFILE'>standalone</a>]</small>
    CONTENT
    <footer>by <strong><a href='WEBSITE'>NAME</a></strong></footer>
  </body>
</html>
HTML

Much more readable IMO, and m4 comes standard pretty much everywhere.

You could also store the HTML template in a separate file to keep your code cleaner.

That's fine, but I don't like this kind of formatting in this context. Since this text isn't really supposed to be modified, I care more about the script looking readable as a script, so I prefer it as it is (with nowrap set in vim) so every one of these commands appears as a line.

I markedly do not want to add extra template files. That leads makes it more difficult to install for people who don't read directions, thus more confused and angry emails for me.