Skip to content

Commit 8b61648

Browse files
b3tarleb
authored andcommitted
include-code-files: add hyphenated attributes alternatives
1 parent f42b50f commit 8b61648

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

include-code-files/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ If you want to include a specific range of lines, use `startLine` and `endLine`:
2828
```{include="hello.c" startLine=35 endLine=80}
2929
```
3030

31+
`start-line` and `end-line` alternatives are also recognized.
32+
3133
### Dedent
3234

3335
Using the `dedent` attribute, you can have whitespaces removed on each line,

include-code-files/expected.native

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[Header 1 ("inclusion",[],[]) [Str "Inclusion"]
2-
,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"
2+
,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"
33
,Header 1 ("ranges",[],[]) [Str "Ranges"]
44
,CodeBlock ("",["lua","numberLines"],[("startFrom","7")]) "local function dedent (line, n)\n return line:sub(1,n):gsub(\" \",\"\") .. line:sub(n+1)\nend\n"
5+
,CodeBlock ("",["lua","numberLines"],[("startFrom","7")]) "local function dedent (line, n)\n return line:sub(1,n):gsub(\" \",\"\") .. line:sub(n+1)\nend\n"
56
,Header 1 ("detent",[],[]) [Str "Detent"]
67
,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"]
78
,CodeBlock ("",["lua","bash","numberLines"],[("startFrom","8")]) "return line:sub(1,n):gsub(\" \",\"\") .. line:sub(n+1)\n"
8-
,CodeBlock ("",["lua","numberLines"],[("startFrom","50")]) "{CodeBlock = transclude }\n"]
9+
,CodeBlock ("",["lua","numberLines"],[("startFrom","61")]) "{CodeBlock = transclude }\n"]

include-code-files/include-code-files.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ local function transclude (cb)
1818
else
1919
local number = 1
2020
local start = 1
21+
22+
-- change hyphenated attributes to PascalCase
23+
for i,pascal in pairs({"startLine", "endLine"})
24+
do
25+
local hyphen = pascal:gsub("%u", "-%0"):lower()
26+
if cb.attributes[hyphen] then
27+
cb.attributes[pascal] = cb.attributes[hyphen]
28+
cb.attributes[hyphen] = nil
29+
end
30+
end
31+
2132
if cb.attributes.startLine then
2233
cb.attributes.startFrom = cb.attributes.startLine
2334
start = tonumber(cb.attributes.startLine)

include-code-files/sample.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ title: Including Hello World
1313
``` {include="include-code-files.lua" .lua startLine=7 endLine=9 .numberLines}
1414
```
1515

16+
``` {include="include-code-files.lua" .lua start-line=7 end-line=9 .numberLines}
17+
```
18+
1619
# Detent
1720

1821
`detent` removes specified number of whitespaces (and only
@@ -21,6 +24,6 @@ whitespaces) from beginning of each line
2124
``` {include="include-code-files.lua" .lua startLine=8 endLine=8 dedent=4 .bash .numberLines}
2225
```
2326

24-
``` {include="include-code-files.lua" .lua startLine=50 endLine=50 dedent=5 .numberLines}
27+
``` {include="include-code-files.lua" .lua startLine=61 endLine=61 dedent=5 .numberLines}
2528
```
2629

0 commit comments

Comments
 (0)