Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pandoc Include Code Files
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
b3
Pandoc Include Code Files
Commits
909c8be4
Commit
909c8be4
authored
2 years ago
by
Bruno BEAUFILS
Browse files
Options
Downloads
Patches
Plain Diff
Import filter's lua code
parent
ec6024bd
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
greetings.lua
+0
-21
0 additions, 21 deletions
greetings.lua
include-code-files.lua
+62
-0
62 additions, 0 deletions
include-code-files.lua
with
62 additions
and
21 deletions
greetings.lua
deleted
100644 → 0
+
0
−
21
View file @
ec6024bd
--- greetings.lua – turns any document into a friendly greeting
---
--- Copyright: © 2021–2022 Contributors
--- License: MIT – see LICENSE for details
-- Makes sure users know if their pandoc version is too old for this
-- filter.
PANDOC_VERSION
:
must_be_at_least
'2.17'
--- Amends the contents of a document with a simple greeting.
local
function
say_hello
(
doc
)
doc
.
meta
.
subtitle
=
doc
.
meta
.
title
-- demote title to subtitle
doc
.
meta
.
title
=
pandoc
.
Inlines
'Greetings!'
-- set new title
doc
.
blocks
:
insert
(
1
,
pandoc
.
Para
'Hello from the Lua filter!'
)
return
doc
end
return
{
-- Apply the `say_hello` function to the main Pandoc document.
{
Pandoc
=
say_hello
}
}
This diff is collapsed.
Click to expand it.
include-code-files.lua
0 → 100644
+
62
−
0
View file @
909c8be4
--- include-code-files.lua – filter to include code from source files
---
--- Copyright: © 2020 Bruno BEAUFILS
--- License: MIT – see LICENSE file for details
--- Dedent a line
local
function
dedent
(
line
,
n
)
return
line
:
sub
(
1
,
n
):
gsub
(
" "
,
""
)
..
line
:
sub
(
n
+
1
)
end
--- Filter function for code blocks
local
function
transclude
(
cb
)
if
cb
.
attributes
.
include
then
local
content
=
""
local
fh
=
io.open
(
cb
.
attributes
.
include
)
if
not
fh
then
io.stderr
:
write
(
"Cannot open file "
..
cb
.
attributes
.
include
..
" | Skipping includes\n"
)
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
)
end
for
line
in
fh
:
lines
(
"L"
)
do
if
cb
.
attributes
.
dedent
then
line
=
dedent
(
line
,
cb
.
attributes
.
dedent
)
end
if
number
>=
start
then
if
not
cb
.
attributes
.
endLine
or
number
<=
tonumber
(
cb
.
attributes
.
endLine
)
then
content
=
content
..
line
end
end
number
=
number
+
1
end
fh
:
close
()
end
-- remove key-value pair for used keys
cb
.
attributes
.
include
=
nil
cb
.
attributes
.
startLine
=
nil
cb
.
attributes
.
endLine
=
nil
cb
.
attributes
.
dedent
=
nil
-- return final code block
return
pandoc
.
CodeBlock
(
content
,
cb
.
attr
)
end
end
return
{
{
CodeBlock
=
transclude
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment