From 7cfba0fd9d17305a7dc0719db601d11696153851 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Mon, 18 Apr 2022 18:25:42 +0300 Subject: [PATCH 1/2] commonmark-code-blocks: initial import --- commonmark-code-blocks/Makefile | 11 ++++ commonmark-code-blocks/README.md | 23 ++++++++ .../commonmark-code-blocks.lua | 59 +++++++++++++++++++ commonmark-code-blocks/expected.html | 2 + commonmark-code-blocks/sample.md | 3 + 5 files changed, 98 insertions(+) create mode 100644 commonmark-code-blocks/Makefile create mode 100644 commonmark-code-blocks/README.md create mode 100644 commonmark-code-blocks/commonmark-code-blocks.lua create mode 100644 commonmark-code-blocks/expected.html create mode 100644 commonmark-code-blocks/sample.md diff --git a/commonmark-code-blocks/Makefile b/commonmark-code-blocks/Makefile new file mode 100644 index 00000000..1b28b5d1 --- /dev/null +++ b/commonmark-code-blocks/Makefile @@ -0,0 +1,11 @@ +DIFF ?= diff --ignore-all-space --ignore-blank-lines -u +PANDOC ?= pandoc + +.PHONY: test +test: + @$(PANDOC) --lua-filter=commonmark-code-blocks.lua -f commonmark -t html sample.md | \ + $(DIFF) expected.html - + +.PHONY: all +all: test + diff --git a/commonmark-code-blocks/README.md b/commonmark-code-blocks/README.md new file mode 100644 index 00000000..995a2add --- /dev/null +++ b/commonmark-code-blocks/README.md @@ -0,0 +1,23 @@ +# commonmark-code-blocks + +This filter produces code blocks with language CSS class format +suggested by the CommonMark specification ("language-$lang"). + +The CommonMark spec suggests that converting ````somelang` to HTML +should produce ``, +as described in [example 141](https://spec.commonmark.org/0.30/#example-141). + +That's what most CommonMark implementations do. +Pandoc, however, outputs `` instead. + +That can be a problem for syntax highlighters that expect +`language-*` classes and use them to determine the language. + +This filter takes over the code block rendering process +to produce CommonMark-style output. + +## Usage + +This filter does not require any setup or external tools. +Just run pandoc with `--lua-filter=commonmark-code-blocks.lua`. + diff --git a/commonmark-code-blocks/commonmark-code-blocks.lua b/commonmark-code-blocks/commonmark-code-blocks.lua new file mode 100644 index 00000000..3e960645 --- /dev/null +++ b/commonmark-code-blocks/commonmark-code-blocks.lua @@ -0,0 +1,59 @@ +--[[ + +commonmark-code-blocks -- produces code blocks with language CSS class format +suggested by the CommonMark specification ("language-$lang"). + +MIT License + +Copyright (c) 2021 Daniil Baturin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +]] + +--[[ + +The CommonMark spec suggests that +"```somelang" should produce + +That's what most CommonMark implementations do. + +Pandoc, however, outputs + +That can be a problem for syntax highlighters that expect +"language-*" classes and use them to determine the language. + +This filter takes over the code block rendering process +to produce CommonMark-style output. + +]] + +function CodeBlock(block) + if FORMAT:match 'html' then + local lang_attr = "" + if (#block.classes > 0) then + lang_attr = string.format("class=\"language-%s\"", block.classes[1]) + else + -- Ignore code blocks where language is not specified + end + + local code = block.text:gsub("&", "&"):gsub("<", "<"):gsub(">", ">") + + local html = string.format('
%s
', lang_attr, code) + return pandoc.RawBlock('html', html) + end +end + diff --git a/commonmark-code-blocks/expected.html b/commonmark-code-blocks/expected.html new file mode 100644 index 00000000..30bec8c7 --- /dev/null +++ b/commonmark-code-blocks/expected.html @@ -0,0 +1,2 @@ +
10 PRINT "HELLO WORLD"
+ diff --git a/commonmark-code-blocks/sample.md b/commonmark-code-blocks/sample.md new file mode 100644 index 00000000..0f172fb4 --- /dev/null +++ b/commonmark-code-blocks/sample.md @@ -0,0 +1,3 @@ +```basic +10 PRINT "HELLO WORLD" +``` From e04cb206a9bd8beaa115b0945ebafdbfd2206b32 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Tue, 19 Apr 2022 12:39:08 +0300 Subject: [PATCH 2/2] commonmark-code-blocks: define the diff command unconditionally because DIFF is set to diff -u in the CI container image environment --- commonmark-code-blocks/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commonmark-code-blocks/Makefile b/commonmark-code-blocks/Makefile index 1b28b5d1..c3e8ae7c 100644 --- a/commonmark-code-blocks/Makefile +++ b/commonmark-code-blocks/Makefile @@ -1,4 +1,4 @@ -DIFF ?= diff --ignore-all-space --ignore-blank-lines -u +DIFF := diff --ignore-all-space --ignore-blank-lines -u PANDOC ?= pandoc .PHONY: test