m4 macro processor (2020)

the one correct templating language

2020-06-17

Contents

  1. Background
  2. Exposure
  3. First Steps
  4. Catalyst
  5. Adoption
  6. Conclusion

Background

m4 is a macro processor, in the sense that it copies its input to the output, rising macros as it goes

https://www.gnu.org/savannah-checkouts/gnu/m4/handbook/m4-1.4.18/m4.html#Preliminaries

m4 is a core gnu utility, and is installed by default on most *nix systems. Every particular individual that I’ve encountered, in point of fact. Including macOS.

I procure in tips myself a barely serious relate line nerd and am accustomed to sed, awk, grep, carve, variety, tr, and so forth. But I had by no methodology heard of this macro processor. It looked very mysterious and imprecise when I first encountered it.

Exposure

I’m now not 100% when and the put I first encountered m4. It totally may well well beget been in the colophon of technomancy.us the put the creator boasts that the entire dwelling is “printed through GNU M4, rsync, and a 12-line Makefile,” which used to be vast magnificent to the minimalist in me.

I then be unsleeping reading Notes on the M4 Macro Language on my phone in the airport lounge on my ability off to vacation somewhere, attempting to learn more about this mysterious little program that has been below my nostril this entire time.

Later I found apostl on github, a miniature wrapper for m4 with the purpose of with out utter at the side of partials in your HTML. This used to be an application that resonated with me: a 0 overhead, framework-free strategy of modularizing HTML for of us like me who steady beget to jot down static stammer material with no additional instruments or bloat.

Lastly I will abstract out my header, footer, and nav parts, and consist of them the put ever I want.

First Steps

What I wished from m4 used to be as a intention to tumble an consist of(nav.md) and an consist of(footer.md) in my markdown and introduce somewhat of bit of modularity into my workflow.

I did this for a hot minute, and it used to be vast. I had expandable macros embedded in my markdown. I’ll well well pipe each markdown file thru m4 to boost it, after which pipe the assembled markdown thru pandoc to assemble an HTML page.

It used to be glorious.

Besides for, I couldn’t abet the feeling that it ought to be better. I was silent doing lots of things by hand like updating the “all posts” page of my blog. And I had the sneaking suspicion that things may well well rating intention more programmatic if I’ll well well greatest fully remove the aptitude of these macros.

Tranquil, it used to be steady adequate.

Catalyst

Unless it wasn’t.

The catalyst for corpulent adoption, for a deep dive into the aptitude of m4, came from something I by no methodology idea I’d ever glimpse.

I started attempting to blog on my new pinebook. This arm64 machine now not greatest didn’t beget pandoc installed, but pandoc wasn’t even in the application repository. I idea of as compiling it myself, but the language it’s written in, Haskel, doesn’t even collect on arm64.

The component I idea I’d by no methodology glimpse is a computer with no pandoc.

So I started down the path of looking out down all markdown from my blog, resigning myself to writing HTML. And I at remaining reversed that option due to I abominate writing HTML that a lot, and went motivate to writing markdown all once more and merely the utilization of a markdown to HTML converter that isn’t pandoc.

Pandoc is definitely overkill for steady converting markdown to HTML. I don’t need a swiss military knife. I need a butter knife.

But between looking out down markdown and inserting it motivate, I spent an afternoon with Technomancy’s makefile and m4 scientific doctors, and surely started to fancy.

Adoption

The essential component I surely overlooked about losing pandoc used to be atmosphere metadata in the YAML frontmatter.

Easy fix, even when. That every particular person modified into macro definitions on the live of the doc:

clarify(__timestamp, 2020-06-17)dnl
clarify(__title, `m4')dnl
clarify(__subtitle, `the one correct templating language')dnl
clarify(__id, 9)dnl

Then the stammer material will get sandwiched between the includes that I first fell in like with:

consist of(src/header.html)
markdown markdown markdown
consist of(src/footer.html)

And the motive that works so well is–and I by no methodology idea I’d ever hear myself mumble this–thanks to the not most likely thing about global inform!

Any macro you clarify in Relate A is supplied to Relate B if you happen to consist of Relate B from Relate A.

So the title, subtitle, and so forth is supplied to header.html on this case. The put I will region </code> in the head and tumble an <code></p> <h1></code> in the body, very like pandoc did.</p> <p>Lastly, I learned to swap the default quote strings. Because when writing markdown corpulent of <code>code blocks</code>, it merely does now not attain to beget m4 attempting to escape a string each time it encounters a backtick.</p> <p>So now a blog put up looks as if this: </p> <pre><code>clarify(__timestamp, 2020-06-17)dnl clarify(__title, `m4')dnl clarify(__subtitle, `the one correct templating language')dnl clarify(__id, 9)dnl consist of(src/header.html) changequote(<!,!>)dnl Or else `code blocks` confuse m4 markdown markdown markdown markdown changequote`'dnl swap quotes `motivate to default' consist of(src/footer.html)</code></pre> <p>Now right here are two things I lifted straight from Technomancy: populating the “All posts” list, and auto-populating the rss feed.</p> <p>Here’s implement a “countdown” for loop in m4, straight from <em>Notes on the M4 Macro Language</em> with minor adjustments: </p> <pre><code>clarify(`for',`ifelse($#,0,``$0'',`ifelse(eval($2>=$3),1, `pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',decr($2),$3,`$4')')')')</code></pre> <p>Mistaken, comely? m4 is vast for macros and includes. Now not vast relaxing for frequent programming. But, like immigrants, it will get the job accomplished.</p> <p>So now “all posts” and rss.xml can each, with the introduction of one “LATEST” variable outlined in the Makefile (which tells the macros what number of recordsdata there are in entire), iterate over the entire recordsdata and cargo them, diverting their output to a throwaway register but maintaining their macro definitions. So it’s that it’s most likely you’ll well perhaps also deem of to yank the title and timestamp from each put up, as an example, and spit them out into the list of all posts. <a href="https://github.com/chrisman/chrisman.github.io/blob/a2a949f3f43613ea1a87677162e34f9138f6804c/src/list.m4">Like so</a>.</p> <p>The final bit of cleverness that I borrowed from Technomancy used to be to beget header.html and footer.html both, reckoning on the precense of a “__feed” macro passed in by the Makefile, wrap the put up up in an HTML doc for the blog or an XML doc for the feed. <a href="https://github.com/chrisman/chrisman.github.io/blob/a2a949f3f43613ea1a87677162e34f9138f6804c/src/header.html#L1">Like so</a>.</p> <h2 id="conclusion">Conclusion</h2> <p>m4 isn’t very most likely for rapid macro growth and for at the side of recordsdata.</p> <p>But it surely speedily becomes vast unwieldly if you happen to want, mumble, to implement a for loop or attain a lot of the rest programmatic in any appreciate.</p> <p>It is subtle to debug. I speedily found my default habits when encountering something surprising is to steady enlarge the escape quotes round something. That in general fixed it.</p> <p>That acknowledged, it’s an gripping language, and I found it rewarding to learn and work with, and I’ll potentially attain for all of it once more if I surely beget the chance to.</p> <p>I’m perfectly relaxed with the present inform of writing for this blog. I surely feel like I’m 90% of the ability there to my normal imaginative and prescient of “gross markdown with includes,” and the additional m4 doesn’t surely distress me in the slightest. It’s silent surely trim, with nearly the entire complexity and grotesque stuff moved faraway from the put I write into auxiliary recordsdata.</p> </div> <p><a href="https://chrisman.github.io/9.html" class="button purchase" rel="nofollow noopener noreferrer" target="_blank">Read More</a></p> </div><!-- .entry-content --> <footer class="entry-meta entry-meta-footer"> <span class="cat-links cat-links-single">Posted in <a href="https://jirnal.com/category/technology/" rel="category tag">Technology</a></span><span class="tags-links tags-links-single">Tagged <a href="https://jirnal.com/tag/macro/" rel="tag">Macro</a>, <a href="https://jirnal.com/tag/processor/" rel="tag">Processor</a></span> </footer><!-- .entry-meta --> </div><!-- .entry-data-wrapper --> </div><!-- .post-content-wrapper --> </article><!-- #post-## --> </div><!-- .post-wrapper-hentry --> <nav class="navigation post-navigation" aria-label="Posts"> <h2 class="screen-reader-text">Post navigation</h2> <div class="nav-links"><div class="nav-previous"><a href="https://jirnal.com/hktdc-twin-jewellery-shows-develop-lately/" rel="prev"><span class="meta-nav">Prev</span> <span class="post-title">HKTDC twin jewellery shows develop lately</span></a></div><div class="nav-next"><a href="https://jirnal.com/diginex-boosts-sustainability-metric-monitoring-instruments-with-6m-elevate/" rel="next"><span class="meta-nav">Next</span> <span class="post-title">Diginex boosts sustainability metric monitoring instruments with $6M elevate</span></a></div></div> </nav> </div><!-- .post-wrapper --> </main><!-- #main --> </div><!-- #primary --> <div id="site-sidebar" class="sidebar-area col-16 col-sm-16 col-md-16 col-lg-5 col-xl-5 col-xxl-5"> <div id="secondary" class="sidebar widget-area sidebar-widget-area" role="complementary"> <aside id="search-2" class="widget widget_search"> <form role="search" method="get" class="search-form" action="https://jirnal.com/"> <label> <span class="screen-reader-text">Search for:</span> <input type="search" class="search-field" placeholder="Search …" value="" name="s" title="Search for:" /> </label> <button type="submit" class="search-submit"><span class="screen-reader-text">Search</span></button> </form> </aside> <aside id="recent-posts-2" class="widget widget_recent_entries"> <h2 class="widget-title">Recent Posts</h2> <ul> <li> <a href="https://jirnal.com/ciara-celebrates-her-birthday-in-precisely-there-beaded-bra-high-and-skirt/">Ciara celebrates her birthday in precisely-there beaded bra high and skirt</a> </li> <li> <a href="https://jirnal.com/is-maxar-applied-sciences-headed-for-a-breakdown-by-stocknews/">Is Maxar Applied sciences Headed for a Breakdown? By StockNews</a> </li> <li> <a href="https://jirnal.com/dorit-kemsleys-robbery-erika-jaynes-reunion-antics-and-more-bravo-files-of-the-week/">Dorit Kemsley’s robbery, Erika Jayne’s reunion antics and more Bravo files of the week</a> </li> <li> <a href="https://jirnal.com/3-monetary-stocks-to-take-up-as-rates-continues-to-upward-push-by-stocknews/">3 Monetary Stocks to Take Up as Rates Continues to Upward push By StockNews</a> </li> <li> <a href="https://jirnal.com/the-varied-procedures-armandos-dad-isnt-attending-wedding-hollywood-life/">The Varied Procedure’s Armando’s Dad Isn’t Attending Wedding – Hollywood Life</a> </li> </ul> </aside><aside id="archives-2" class="widget widget_archive"><h2 class="widget-title">Archives</h2> <ul> <li><a href='https://jirnal.com/2021/10/'>October 2021</a></li> <li><a href='https://jirnal.com/2021/09/'>September 2021</a></li> <li><a href='https://jirnal.com/2021/08/'>August 2021</a></li> <li><a href='https://jirnal.com/2021/07/'>July 2021</a></li> <li><a href='https://jirnal.com/2021/06/'>June 2021</a></li> <li><a href='https://jirnal.com/2021/05/'>May 2021</a></li> <li><a href='https://jirnal.com/2021/04/'>April 2021</a></li> <li><a href='https://jirnal.com/2021/03/'>March 2021</a></li> <li><a href='https://jirnal.com/2021/02/'>February 2021</a></li> <li><a href='https://jirnal.com/2021/01/'>January 2021</a></li> <li><a href='https://jirnal.com/2020/12/'>December 2020</a></li> <li><a href='https://jirnal.com/2020/11/'>November 2020</a></li> <li><a href='https://jirnal.com/2020/10/'>October 2020</a></li> <li><a href='https://jirnal.com/2020/09/'>September 2020</a></li> <li><a href='https://jirnal.com/2020/08/'>August 2020</a></li> <li><a href='https://jirnal.com/2020/07/'>July 2020</a></li> <li><a href='https://jirnal.com/2020/06/'>June 2020</a></li> <li><a href='https://jirnal.com/2020/05/'>May 2020</a></li> <li><a href='https://jirnal.com/2020/04/'>April 2020</a></li> <li><a href='https://jirnal.com/2020/03/'>March 2020</a></li> <li><a href='https://jirnal.com/2020/02/'>February 2020</a></li> <li><a href='https://jirnal.com/2020/01/'>January 2020</a></li> <li><a href='https://jirnal.com/2019/12/'>December 2019</a></li> <li><a href='https://jirnal.com/2019/11/'>November 2019</a></li> <li><a href='https://jirnal.com/2019/10/'>October 2019</a></li> <li><a href='https://jirnal.com/2019/09/'>September 2019</a></li> <li><a href='https://jirnal.com/2019/08/'>August 2019</a></li> <li><a href='https://jirnal.com/2019/07/'>July 2019</a></li> <li><a href='https://jirnal.com/2019/06/'>June 2019</a></li> <li><a href='https://jirnal.com/2019/02/'>February 2019</a></li> <li><a href='https://jirnal.com/2019/01/'>January 2019</a></li> <li><a href='https://jirnal.com/2018/12/'>December 2018</a></li> <li><a href='https://jirnal.com/2018/11/'>November 2018</a></li> <li><a href='https://jirnal.com/2018/09/'>September 2018</a></li> <li><a href='https://jirnal.com/2018/07/'>July 2018</a></li> <li><a href='https://jirnal.com/2018/05/'>May 2018</a></li> <li><a href='https://jirnal.com/2018/01/'>January 2018</a></li> <li><a href='https://jirnal.com/2017/12/'>December 2017</a></li> <li><a href='https://jirnal.com/2017/10/'>October 2017</a></li> <li><a href='https://jirnal.com/2017/09/'>September 2017</a></li> <li><a href='https://jirnal.com/2017/06/'>June 2017</a></li> <li><a href='https://jirnal.com/2017/04/'>April 2017</a></li> <li><a href='https://jirnal.com/2017/03/'>March 2017</a></li> <li><a href='https://jirnal.com/2017/02/'>February 2017</a></li> <li><a href='https://jirnal.com/2017/01/'>January 2017</a></li> <li><a href='https://jirnal.com/2016/11/'>November 2016</a></li> <li><a href='https://jirnal.com/2016/09/'>September 2016</a></li> <li><a href='https://jirnal.com/2016/08/'>August 2016</a></li> <li><a href='https://jirnal.com/2016/07/'>July 2016</a></li> <li><a href='https://jirnal.com/2016/06/'>June 2016</a></li> <li><a href='https://jirnal.com/2016/05/'>May 2016</a></li> <li><a href='https://jirnal.com/2016/02/'>February 2016</a></li> <li><a href='https://jirnal.com/2015/10/'>October 2015</a></li> <li><a href='https://jirnal.com/2015/07/'>July 2015</a></li> <li><a href='https://jirnal.com/2015/04/'>April 2015</a></li> <li><a href='https://jirnal.com/2015/03/'>March 2015</a></li> <li><a href='https://jirnal.com/2014/09/'>September 2014</a></li> <li><a href='https://jirnal.com/2014/08/'>August 2014</a></li> <li><a href='https://jirnal.com/2014/06/'>June 2014</a></li> <li><a href='https://jirnal.com/2014/04/'>April 2014</a></li> <li><a href='https://jirnal.com/2014/02/'>February 2014</a></li> <li><a href='https://jirnal.com/2013/11/'>November 2013</a></li> <li><a href='https://jirnal.com/2013/10/'>October 2013</a></li> <li><a href='https://jirnal.com/2013/06/'>June 2013</a></li> <li><a href='https://jirnal.com/2013/05/'>May 2013</a></li> <li><a href='https://jirnal.com/2013/04/'>April 2013</a></li> <li><a href='https://jirnal.com/2007/03/'>March 2007</a></li> <li><a href='https://jirnal.com/1970/01/'>January 1970</a></li> </ul> </aside><aside id="categories-2" class="widget widget_categories"><h2 class="widget-title">Categories</h2> <ul> <li class="cat-item cat-item-2"><a href="https://jirnal.com/category/business/">Business</a> </li> <li class="cat-item cat-item-3"><a href="https://jirnal.com/category/entertainment/">Entertainment</a> </li> <li class="cat-item cat-item-4"><a href="https://jirnal.com/category/health-and-medical/">Health and Medical</a> </li> <li class="cat-item cat-item-8"><a href="https://jirnal.com/category/news/">News</a> </li> <li class="cat-item cat-item-5"><a href="https://jirnal.com/category/science-and-nature/">Science and Nature</a> </li> <li class="cat-item cat-item-6"><a href="https://jirnal.com/category/sport/">Sport</a> </li> <li class="cat-item cat-item-7"><a href="https://jirnal.com/category/technology/">Technology</a> </li> <li class="cat-item cat-item-1"><a href="https://jirnal.com/category/uncategorized/">Uncategorized</a> </li> </ul> </aside> </div><!-- .sidebar --> </div><!-- .col-* columns of main sidebar --> </div><!-- .row --> </div><!-- .container --> </div><!-- .site-content-inside --> </div><!-- #content --> <footer id="colophon" class="site-footer"> <div class="site-info"> <div class="site-info-inside"> <div class="container"> <div class="row"> <div class="col"> <div class="credits-wrapper"> <div class="credits credits-blog"><div style="font-size:12px;color:#555">Jirnal © 2020 – Powered by <a style="color:#555" href="https://wordpress.org" rel="nofollow" target="_blank">Wordpress </a></div> </div><div class="credits credits-designer">Amphibious Theme by <a href="https://templatepocket.com" title="TemplatePocket">TemplatePocket</a> <span>⋅</span> Powered by <a href="https://wordpress.org" title="WordPress">WordPress</a></div> </div><!-- .credits --> </div><!-- .col --> </div><!-- .row --> </div><!-- .container --> </div><!-- .site-info-inside --> </div><!-- .site-info --> </footer><!-- #colophon --> </div><!-- #page .site-wrapper --> <div class="overlay-effect"></div><!-- .overlay-effect --> <script type="text/javascript" src="https://jirnal.com/wp-content/themes/amphibious/js/enquire.js?ver=2.1.6" id="enquire-js"></script> <script type="text/javascript" src="https://jirnal.com/wp-content/themes/amphibious/js/fitvids.js?ver=1.1" id="fitvids-js"></script> <script type="text/javascript" src="https://jirnal.com/wp-content/themes/amphibious/js/hover-intent.js?ver=r7" id="hover-intent-js"></script> <script type="text/javascript" src="https://jirnal.com/wp-content/themes/amphibious/js/superfish.js?ver=1.7.10" id="superfish-js"></script> <script type="text/javascript" src="https://jirnal.com/wp-content/themes/amphibious/js/custom.js?ver=1.0" id="amphibious-custom-js"></script> </body> </html>