mako/fence.m4
2023-09-14 10:39:45 -07:00

97 lines
2.5 KiB
Plaintext

dnl ---------------------------------------------------------------------------#
dnl fence.m4
dnl
dnl Fence pre-formated text or code into Markdown and Gemtext documents
dnl
dnl Both Markdown extensions and Gemini use backticks to fence 'raw' text
dnl blocks. m4 uses backticks to quote text. This file simplifies managing
dnl text that would otherwise be subject to macro quoting rules.
dnl
dnl This file provides four macros to resolve this conflict:
dnl
dnl * DELIMITER -- insert three backticks delimiter
dnl * FENCE -- wraps its argument inside a backtick fence
dnl * CODE -- wraps its arguement with backticks for inline code
dnl * CODEBLOCK -- forms a code block where the first argument is placed
dnl after the leading backticks and the second argument is
dnl the code
dnl
dnl In some circumstances, changing m4's quote characters will make for
dnl a better solution.
dnl
dnl Note, the implementation uses "-<-<" and ">->-" as temporary m4 quote
dnl characters.
dnl
dnl Examples:
dnl
dnl FENCE(foo) =>
dnl ```
dnl foo
dnl ```
dnl
dnl CODEBLOCK(sh, `echo foo') =>
dnl ```sh
dnl echo foo
dnl ```
dnl
dnl CODE(`x = 3') =>
dnl `x = 3`
dnl ''
dnl
dnl Use in combination `undivert' to include code files:
dnl
dnl CODEBLOCK(c, `undivert(`hellow-world.c')')
dnl
dnl Use in combination with `syscmd`:
dnl
dnl FENCE(`syscmd(`ls')')
dnl
dnl
dnl Usage:
dnl
dnl Use the 'include()' macro to import this macros into your m4 file.
dnl
dnl Author: axs@sdf.org
dnl
dnl ---------------------------------------------------------------------------#
dnl
dnl Write backticks, handling the conflict with m4 quoting
dnl
dnl Following advice from Michael Breen, we:
dnl * Redefine the quote characters
dnl * Write the backticks
dnl * Surpress the apostrophies
dnl * Revert the quote characters to the orginal state
dnl
dnl see: https://mbreen.com/m4.html
dnl
define(`DELIMITER', `changequote(`-<-<',`>->-')```dnl'''
changequote`'')dnl
dnl
dnl ---------------------------------------------------------------------------#
dnl
dnl Create a pre-formatted text block
dnl
define(`FENCE', `dnl
DELIMITER()
$1`'dnl
DELIMITER()
')dnl
dnl ---------------------------------------------------------------------------#
dnl
dnl Create a code block
dnl
define(`CODEBLOCK', `dnl
DELIMITER()$1
$2
DELIMITER()
')dnl
dnl ---------------------------------------------------------------------------#
dnl
dnl Inline code
dnl
define(`CODE', `changequote(`-<-<',`>->-')`$1`dnl''
changequote`'')dnl
dnl
dnl ---------------------------------------------------------------------------#