site

files for beauhilton.com
git clone https://git.beauhilton.com/site.git
Log | Files | Refs

cmark-code-blocks.lua (953B)


      1 -- The CommonMark spec suggests that
      2 -- "```somelang" should produce <code class="language-somelang">
      3 --
      4 -- That's what most CommonMark implementation do.
      5 -- Pandoc, however, outputs <code class="somelang">
      6 --
      7 -- This makes using external highlighters much more difficult
      8 -- since you cannot match all code blocks with a known language
      9 -- using a single CSS selector.
     10 --
     11 -- This filter takes over the code block rendering process
     12 -- to produce CommonMark-style output.
     13 
     14 function CodeBlock(block)
     15   if FORMAT:match 'html' then
     16     local lang_attr = ""
     17     if (#block.classes > 0) then
     18       lang_attr = string.format([[class="language-%s"]], block.classes[1])
     19     else
     20       -- Ignore code blocks where language is not specified
     21     end
     22 
     23     local code = block.text:gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;")
     24 
     25     local html = string.format('<pre><code %s>%s</code></pre>', lang_attr, code)
     26     return pandoc.RawBlock('html', html)
     27   end
     28 end