-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve pandoc.scaffolding
section in 'Pandoc Lua Filters' docs (RE: Custom Writers)
#10531
Comments
The following is a bit of a mind dump of my process/thoughts/random ideas of what would make this easier as a new user wanting to make a custom writer: Have been trying to play with
I may have missed it when skimming around, but I have been struggling to figure out how to see what the default functions this creates actually are:
I think it would be helpful to expand the documentation to:
The next thing I noticed was that if we just create a writer with But since that's not currently a feature (to my knowledge), I next attempted to find a list of AST nodes that I needed to implement, and from a quick attempt to skim around the lua docs, I couldn't find a neat canonical list (#3262) I'm sure I could iterate through all the headings to build one, but really I wanted to be able to copy/paste a paragraph or so and reformat it into lua functions; or even better would be if there was a default set of functions I could copy from the lua filter docs/etc that would cover all of the boilerplate of setting up a 'delegating custom writer'; or perhaps this could also be 'baked in' to Luckily I remember that I was reading through some of the Haskell source for AST nodes the other day, and copied a list of them into my gist, so I could use that as a starting reference; but I don't remember where I actually found that originally. #3288 seems to allude to it, and links to this code that seems to define relevant things, though its not immediately clear to me if thats everything I would need to implement or not (and as a new user, I wouldn't want to have to dig through the source to find these sorts of answers): I was originally looking at the old Blocks = {}
Blocks.mt = {}
Blocks.mt.__index = function(tbl,key)
return function() io.stderr:write("Unimplemented " .. key .. "\n") end
end
setmetatable(Blocks, Blocks.mt)
Inlines = {}
Inlines.mt = {}
Inlines.mt.__index = function(tbl,key)
return function() io.stderr:write("Unimplemented " .. key .. "\n") end
end
setmetatable(Inlines, Inlines.mt)
From reading deeper about mettables and metmethods like I thought I tried this before.. but apparently not, as it seems to work somewhat: Writer = pandoc.scaffolding.Writer
print("Writer:")
for n in pairs(Writer) do
elem = Writer[n]
print(n, type(n), type(elem))
if type(elem) == "table" then
print("--Foo--")
for m in pairs(elem) do
print(" ", m, type(m))
end
print("--Bar--")
end
end Outputting:
I can also see that directly calling something like Writer = pandoc.scaffolding.Writer
Writer.Inline.Plain()
Next I would just need to figure out if/how I can get access to the existing writer's handlers from lua.. or if not, a method for doing similar (eg. calling write within each handler) I think that's the end of my mind dump thoughts for now. |
We should have that. We used to have a sample old-style custom writer in the source tree, but it seems to have been deleted in 79d6b45 . This contained all of the things you'd need to implement. The manual now points you to djot-writer.lua as a canonical example.
I've talked to @tarleb about this idea. We agree it would be great to have this, but it's not possible without some architectural changes. |
I have been looking at that, and it's been helpful, but a few points:
nods makes sense, and I figured that was likely the case. Just wanted to make sure to raise it all the same. |
Actually, looking at the commit history, seems there is a
Looking at that timeline and versions of the code:
Comparing those 2 versions shows the diff is quite minimal: --- -- Last version before moved to jgm/djot.lua: http…
+++ -- Version from https://github.com/jgm/djot/pull/2…
@@ -1,5 +1,9 @@
--- Last version before moved to jgm/djot.lua: https://github.com/jgm/djot/blob/239969fd84b6406b1f29e3514186667a5ac6c1cc/djot-writer.lua
+-- Version from https://github.com/jgm/djot/pull/233 merged in https://github.com/jgm/djot/blob/2336a695176d9ef15661faa5a425d9a372187db9/djot-writer.lua
-- custom writer for pandoc
+-- example of upgrading of old style writer (pandoc <3.0)
+-- into new style (pandoc >= 3.0)
+-- see end of file for modified Writer function incorporating code snippet
+-- @@jarnosz at github 2023-06-01
local unpack = unpack or table.unpack
local format = string.format
@@ -439,6 +443,13 @@
end
function Writer (doc, opts)
+-- begin patch
+-- function Writer (doc, opts)
+ PANDOC_DOCUMENT = doc
+ PANDOC_WRITER_OPTIONS = opts
+ loadfile(PANDOC_SCRIPT_FILE)()
+-- return pandoc.write_classic(doc, opts)
+-- end patch
local d = blocks(doc.blocks, blankline)
local notes = {}
for i=1,#footnotes do And diffing the last non-reverted version of --- -- Last version before moved to jgm/djot.lua: http…
+++ -- Latest version on jgm/djot.lua (from 2023-11-03…
@@ -1,4 +1,4 @@
--- Last version before moved to jgm/djot.lua: https://github.com/jgm/djot/blob/239969fd84b6406b1f29e3514186667a5ac6c1cc/djot-writer.lua
+-- Latest version on jgm/djot.lua (from 2023-11-03) https://github.com/jgm/djot.lua/blob/7d1fbbb347c0f35b92590ed799501e8ba7115156/djot-writer.lua
-- custom writer for pandoc
local unpack = unpack or table.unpack
@@ -439,12 +439,18 @@
end
function Writer (doc, opts)
+ PANDOC_WRITER_OPTIONS = opts
local d = blocks(doc.blocks, blankline)
local notes = {}
for i=1,#footnotes do
local note = hang(blocks(footnotes[i], blankline), 4, concat{format("[^%d]:",i),space})
table.insert(notes, note)
end
- return layout.render(concat{d, blankline, concat(notes, blankline)}, opts.columns)
+ local formatted = concat{d, blankline, concat(notes, blankline)}
+ if PANDOC_WRITER_OPTIONS.wrap_text == "wrap-none" then
+ return layout.render(formatted)
+ else
+ return layout.render(formatted, opts.columns)
+ end
end Since there are minimal differences across all 3 versions, I will just look at/comment on the latest version in
Based on this discovery, a few things that would probably be worth doing:
|
RE: AST Node Reference
This issue has a few mentions about it:
|
In the spirit of:
Describe your proposed improvement and the problem it solves.
In reading through the Lua Filters documentation recently, I found this section on
pandoc.scaffolding
forWriters
:But the
Fields
section is empty (unsure if that is how it is meant to be, but perhaps adding some text to say that explicitly if so to remove ambiguity):The
Writer
section is also fairly light on details:In doing some googling, I also came across these pages:
It would be good to fill out this section of the docs more, even if that is just adding some cross-links to the existing pages on Custom Writers/etc.
Describe alternatives you've considered.
I'm aware of this issue/PR, but the nuance is different:
This docs issue is also unrelated, except for improving the docs UX for new users in general:
The text was updated successfully, but these errors were encountered: