diff --git a/include-code-files/README.md b/include-code-files/README.md index e52ef279..5b99a79a 100644 --- a/include-code-files/README.md +++ b/include-code-files/README.md @@ -28,6 +28,8 @@ If you want to include a specific range of lines, use `startLine` and `endLine`: ```{include="hello.c" startLine=35 endLine=80} ``` +`start-line` and `end-line` alternatives are also recognized. + ### Dedent Using the `dedent` attribute, you can have whitespaces removed on each line, diff --git a/include-code-files/expected.native b/include-code-files/expected.native index 18e4aca1..994f3538 100644 --- a/include-code-files/expected.native +++ b/include-code-files/expected.native @@ -1,8 +1,9 @@ [Header 1 ("inclusion",[],[]) [Str "Inclusion"] -,CodeBlock ("",["lua","numberLines"],[]) "--- include-code-files.lua \8211 filter to include code from source files\n---\n--- Copyright: \169 2020 Bruno BEAUFILS\n--- License: MIT \8211 see LICENSE file for details\n\n--- Dedent a line\nlocal function dedent (line, n)\n return line:sub(1,n):gsub(\" \",\"\") .. line:sub(n+1)\nend\n\n--- Filter function for code blocks\nlocal function transclude (cb)\n if cb.attributes.include then\n local content = \"\"\n local fh = io.open(cb.attributes.include)\n if not fh then\n io.stderr:write(\"Cannot open file \" .. cb.attributes.include .. \" | Skipping includes\\n\")\n else\n local number = 1\n local start = 1\n if cb.attributes.startLine then\n cb.attributes.startFrom = cb.attributes.startLine\n start = tonumber(cb.attributes.startLine)\n end\n for line in fh:lines (\"L\")\n do\n if cb.attributes.dedent then\n line = dedent(line, cb.attributes.dedent)\n end\n if number >= start then\n if not cb.attributes.endLine or number <= tonumber(cb.attributes.endLine) then\n content = content .. line\n end\n end\n number = number + 1\n end \n fh:close()\n end \n -- remove key-value pair for used keys\n cb.attributes.include = nil\n cb.attributes.startLine = nil\n cb.attributes.endLine = nil\n cb.attributes.dedent = nil\n -- return final code block\n return pandoc.CodeBlock(content, cb.attr)\n end\nend\n\nreturn {\n { CodeBlock = transclude }\n}\n" +,CodeBlock ("",["lua","numberLines"],[]) "--- include-code-files.lua \8211 filter to include code from source files\n---\n--- Copyright: \169 2020 Bruno BEAUFILS\n--- License: MIT \8211 see LICENSE file for details\n\n--- Dedent a line\nlocal function dedent (line, n)\n return line:sub(1,n):gsub(\" \",\"\") .. line:sub(n+1)\nend\n\n--- Filter function for code blocks\nlocal function transclude (cb)\n if cb.attributes.include then\n local content = \"\"\n local fh = io.open(cb.attributes.include)\n if not fh then\n io.stderr:write(\"Cannot open file \" .. cb.attributes.include .. \" | Skipping includes\\n\")\n else\n local number = 1\n local start = 1\n\n -- change hyphenated attributes to PascalCase\n for i,pascal in pairs({\"startLine\", \"endLine\"})\n do\n local hyphen = pascal:gsub(\"%u\", \"-%0\"):lower()\n if cb.attributes[hyphen] then\n cb.attributes[pascal] = cb.attributes[hyphen]\n cb.attributes[hyphen] = nil\n end\n end\n\n if cb.attributes.startLine then\n cb.attributes.startFrom = cb.attributes.startLine\n start = tonumber(cb.attributes.startLine)\n end\n for line in fh:lines (\"L\")\n do\n if cb.attributes.dedent then\n line = dedent(line, cb.attributes.dedent)\n end\n if number >= start then\n if not cb.attributes.endLine or number <= tonumber(cb.attributes.endLine) then\n content = content .. line\n end\n end\n number = number + 1\n end \n fh:close()\n end \n -- remove key-value pair for used keys\n cb.attributes.include = nil\n cb.attributes.startLine = nil\n cb.attributes.endLine = nil\n cb.attributes.dedent = nil\n -- return final code block\n return pandoc.CodeBlock(content, cb.attr)\n end\nend\n\nreturn {\n { CodeBlock = transclude }\n}\n" ,Header 1 ("ranges",[],[]) [Str "Ranges"] ,CodeBlock ("",["lua","numberLines"],[("startFrom","7")]) "local function dedent (line, n)\n return line:sub(1,n):gsub(\" \",\"\") .. line:sub(n+1)\nend\n" +,CodeBlock ("",["lua","numberLines"],[("startFrom","7")]) "local function dedent (line, n)\n return line:sub(1,n):gsub(\" \",\"\") .. line:sub(n+1)\nend\n" ,Header 1 ("detent",[],[]) [Str "Detent"] ,Para [Code ("",[],[]) "detent",Space,Str "removes",Space,Str "specified",Space,Str "number",Space,Str "of",Space,Str "whitespaces",Space,Str "(and",Space,Str "only",SoftBreak,Str "whitespaces)",Space,Str "from",Space,Str "beginning",Space,Str "of",Space,Str "each",Space,Str "line"] ,CodeBlock ("",["lua","bash","numberLines"],[("startFrom","8")]) "return line:sub(1,n):gsub(\" \",\"\") .. line:sub(n+1)\n" -,CodeBlock ("",["lua","numberLines"],[("startFrom","50")]) "{CodeBlock = transclude }\n"] +,CodeBlock ("",["lua","numberLines"],[("startFrom","61")]) "{CodeBlock = transclude }\n"] diff --git a/include-code-files/include-code-files.lua b/include-code-files/include-code-files.lua index df744bf7..5a5269ca 100644 --- a/include-code-files/include-code-files.lua +++ b/include-code-files/include-code-files.lua @@ -18,6 +18,17 @@ local function transclude (cb) else local number = 1 local start = 1 + + -- change hyphenated attributes to PascalCase + for i,pascal in pairs({"startLine", "endLine"}) + do + local hyphen = pascal:gsub("%u", "-%0"):lower() + if cb.attributes[hyphen] then + cb.attributes[pascal] = cb.attributes[hyphen] + cb.attributes[hyphen] = nil + end + end + if cb.attributes.startLine then cb.attributes.startFrom = cb.attributes.startLine start = tonumber(cb.attributes.startLine) diff --git a/include-code-files/sample.md b/include-code-files/sample.md index a929adde..404f987f 100644 --- a/include-code-files/sample.md +++ b/include-code-files/sample.md @@ -13,6 +13,9 @@ title: Including Hello World ``` {include="include-code-files.lua" .lua startLine=7 endLine=9 .numberLines} ``` +``` {include="include-code-files.lua" .lua start-line=7 end-line=9 .numberLines} +``` + # Detent `detent` removes specified number of whitespaces (and only @@ -21,6 +24,6 @@ whitespaces) from beginning of each line ``` {include="include-code-files.lua" .lua startLine=8 endLine=8 dedent=4 .bash .numberLines} ``` -``` {include="include-code-files.lua" .lua startLine=50 endLine=50 dedent=5 .numberLines} +``` {include="include-code-files.lua" .lua startLine=61 endLine=61 dedent=5 .numberLines} ```