Lua: add pandoc.WriterOptions constructor

This commit is contained in:
Albert Krewinkel 2021-12-31 20:12:23 +01:00 committed by John MacFarlane
parent 0d1d52f0a0
commit 6a5ac90bf1
6 changed files with 271 additions and 8 deletions

View file

@ -261,11 +261,13 @@ variables.
`PANDOC_READER_OPTIONS`
: Table of the options which were provided to the parser.
([ReaderOptions](#type-readeroptions))
`PANDOC_WRITER_OPTIONS`
: Table of the options that will be passed to the writer.
While the object can be modified, the changes will **not**
be picked up by pandoc.
([WriterOptions](#type-writeroptions))
This variable is also set in custom writers.
@ -2021,6 +2023,121 @@ Fields:
: track changes setting for docx; one of `accept-changes`,
`reject-changes`, and `all-changes` (string)
## WriterOptions {#type-writeroptions}
Pandoc writer options
Fields:
`cite_method`
: How to print cites -- one of 'citeproc', 'natbib', or
'biblatex' (string)
`columns`
: Characters in a line (for text wrapping) (integer)
`dpi`
: DPI for pixel to/from inch/cm conversions (integer)
`email_obfuscation`
: How to obfuscate emails -- one of 'none', 'references', or
'javascript' (string)
`epub_chapter_level`
: Header level for chapters, i.e., how the document is split
into separate files (integer)
`epub_fonts`
: Paths to fonts to embed (sequence of strings)
`epub_metadata`
: Metadata to include in EPUB (string|nil)
`epub_subdirectory`
: Subdir for epub in OCF (string)
`extensions`
: Markdown extensions that can be used (sequence of strings)
`highlight_style`
: Style to use for highlighting; see the output of `pandoc
--print-highlight-style=...` for an example structure. The
value `nil` means that no highlighting is used. (table|nil)
`html_math_method`
: How to print math in HTML; one 'plain', 'gladtex', 'webtex',
'mathml', 'mathjax', or a table with keys `method` and
`url`. (string|table)
`html_q_tags`
: Use `<q>` tags for quotes in HTML (boolean)
`identifier_prefix`
: Prefix for section & note ids in HTML and for footnote marks
in markdown (string)
`incremental`
: True if lists should be incremental (boolean)
`listings`
: Use listings package for code (boolean)
`number_offset`
: Starting number for section, subsection, ... (sequence of
integers)
`number_sections`
: Number sections in LaTeX (boolean)
`prefer_ascii`
: Prefer ASCII representations of characters when possible
(boolean)
`reference_doc`
: Path to reference document if specified (string|nil)
`reference_links`
: Use reference links in writing markdown, rst (boolean)
`reference_location`
: Location of footnotes and references for writing markdown;
one of 'end-of-block', 'end-of-section', 'end-of-document'.
The common prefix may be omitted when setting this value.
(string)
`section_divs`
: Put sections in div tags in HTML (boolean)
`setext_headers`
: Use setext headers for levels 1-2 in markdown (boolean)
`slide_level`
: Force header level of slides (integer\|nil)
`tab_stop`
: Tabstop for conversion btw spaces and tabs (integer)
`table_of_contents`
: Include table of contents (boolean)
`toc_depth`
: Number of levels to include in TOC (integer)
`top_level_division`
: Type of top-level divisions; one of 'top-level-part',
'top-level-chapter', 'top-level-section', or
'top-level-default'. The prefix `top-level` may be omitted
when setting this value. (string)
`variables`
: Variables to set in template; string-indexed table (table)
`wrap_text`
: Option for wrapping text; one of 'wrap-auto', 'wrap-none',
or 'wrap-preserve'. The `wrap-` prefix may be omitted when
setting this value. (string)
## CommonState {#type-commonstate}
The state used by pandoc to collect information and make it
@ -3170,6 +3287,30 @@ format, and functions to filter and modify a subtree.
-- default reader options, but columns set to 66.
local short_colums_opts = pandoc.ReaderOptions {columns = 66}
[`WriterOptions (opts)`]{#pandoc.writeroptions}
: Creates a new [WriterOptions][] value.
Parameters
`opts`:
: Either a table with a subset of the properties of a
[WriterOptions] object, or another WriterOptions object.
Uses the defaults specified in the manual for all
properties that are not explicitly specified. Throws an
error if a table contains properties which are not present
in a WriterOptions object. ([WriterOptions]|table)
Returns: new [WriterOptions] object
Usage:
-- copy of the writer options that were defined on the command line.
local cli_opts = pandoc.WriterOptions(PANDOC_WRITER_OPTIONS)
-- default writer options, but DPI set to 300.
local short_colums_opts = pandoc.WriterOptions {dpi = 300}
## Helper functions
### pipe {#pandoc.pipe}

View file

@ -93,7 +93,8 @@ typeWriterOptions = deftype "WriterOptions"
(pushText, writerEpubSubdirectory)
(peekText, \opts x -> opts{ writerEpubSubdirectory = x })
, property "extensions" "Markdown extensions that can be used"
, property "extensions"
"Markdown extensions that can be used"
(pushViaJSON, writerExtensions)
(peekViaJSON, \opts x -> opts{ writerExtensions = x })
@ -147,16 +148,16 @@ typeWriterOptions = deftype "WriterOptions"
(maybe pushnil pushString, writerReferenceDoc)
(optional . peekString, \opts x -> opts{ writerReferenceDoc = x })
, property "reference_location"
"Location of footnotes and references for writing markdown"
(pushViaJSON, writerReferenceLocation)
(peekViaJSON, \opts x -> opts{ writerReferenceLocation = x })
, property "reference_links"
"Use reference links in writing markdown, rst"
(pushBool, writerReferenceLinks)
(peekBool, \opts x -> opts{ writerReferenceLinks = x })
, property "reference_location"
"Location of footnotes and references for writing markdown"
(pushViaJSON, writerReferenceLocation)
(peekViaJSON, \opts x -> opts{ writerReferenceLocation = x })
, property "section_divs"
"Put sections in div tags in HTML"
(pushBool, writerSectionDivs)

View file

@ -34,6 +34,8 @@ import Text.Pandoc.Lua.Marshal.AST
import Text.Pandoc.Lua.Marshal.Filter (peekFilter)
import Text.Pandoc.Lua.Marshal.ReaderOptions ( peekReaderOptions
, pushReaderOptions)
import Text.Pandoc.Lua.Marshal.WriterOptions ( peekWriterOptions
, pushWriterOptions)
import Text.Pandoc.Lua.Module.Utils (sha1)
import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua), liftPandocLua)
import Text.Pandoc.Options ( ReaderOptions (readerExtensions)
@ -129,6 +131,13 @@ otherConstructors =
<#> parameter peekReaderOptions "ReaderOptions|table" "opts" "reader options"
=#> functionResult pushReaderOptions "ReaderOptions" "new object"
#? "Creates a new ReaderOptions value."
, defun "WriterOptions"
### liftPure id
<#> parameter peekWriterOptions "WriterOptions|table" "opts"
"writer options"
=#> functionResult pushWriterOptions "WriterOptions" "new object"
#? "Creates a new WriterOptions value."
]
stringConstants :: [Field e]

View file

@ -29,7 +29,7 @@ import Text.Pandoc.Definition (Attr, Block (BlockQuote, Div, Para), Pandoc,
Inline (Emph, Str), pandocTypesVersion)
import Text.Pandoc.Error (PandocError (PandocLuaError))
import Text.Pandoc.Filter (Filter (LuaFilter), applyFilters)
import Text.Pandoc.Lua (runLua)
import Text.Pandoc.Lua (Global (..), runLua, setGlobals)
import Text.Pandoc.Options (def)
import Text.Pandoc.Shared (pandocVersion)
@ -239,7 +239,9 @@ assertFilterConversion msg filterPath docIn expectedDoc = do
runLuaTest :: HasCallStack => Lua.LuaE PandocError a -> IO a
runLuaTest op = runIOorExplode $ do
res <- runLua op
res <- runLua $ do
setGlobals [ PANDOC_WRITER_OPTIONS def ]
op
case res of
Left e -> error (show e)
Right x -> return x

View file

@ -31,6 +31,8 @@ tests =
("lua" </> "module" </> "pandoc-types.lua")
, testPandocLua "pandoc.utils"
("lua" </> "module" </> "pandoc-utils.lua")
, testPandocLua "globals"
("lua" </> "module" </> "globals.lua")
]
testPandocLua :: TestName -> FilePath -> TestTree

108
test/lua/module/globals.lua Normal file
View file

@ -0,0 +1,108 @@
local tasty = require 'tasty'
local test = tasty.test_case
local group = tasty.test_group
local assert = tasty.assert
-- These tests exist mainly to catch changes to the JSON representation of
-- WriterOptions and its components. UPDATE THE DOCS if anything changes.
return {
group 'PANDOC_WRITER_OPTIONS' {
test('cite_method', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.cite_method), 'string')
end),
test('columns', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.columns), 'number')
end),
test('dpi', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.dpi), 'number')
end),
test('email_obfuscation', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.email_obfuscation), 'string')
end),
test('epub_chapter_level', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.epub_chapter_level), 'number')
end),
test('epub_fonts', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.epub_fonts), 'table')
end),
test('epub_metadata', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.epub_metadata), 'nil')
end),
test('epub_subdirectory', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.epub_subdirectory), 'string')
end),
test('extensions', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.extensions), 'table')
for _, v in ipairs(PANDOC_WRITER_OPTIONS.extensions) do
assert.are_equal(type(v), 'string')
end
end),
test('highlight_style', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.highlight_style), 'table')
end),
test('html_math_method', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.html_math_method), 'string')
end),
test('html_q_tags', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.html_q_tags), 'boolean')
end),
test('identifier_prefix', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.identifier_prefix), 'string')
end),
test('incremental', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.incremental), 'boolean')
end),
test('listings', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.listings), 'boolean')
end),
test('number_offset', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.number_offset), 'table')
for _, v in ipairs(PANDOC_WRITER_OPTIONS.number_offset) do
assert.are_equal(type(v), 'number')
end
end),
test('number_sections', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.number_sections), 'boolean')
end),
test('prefer_ascii', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.prefer_ascii), 'boolean')
end),
test('reference_doc', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.reference_doc), 'nil')
end),
test('reference_links', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.reference_links), 'boolean')
end),
test('reference_location', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.reference_location), 'string')
end),
test('section_divs', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.section_divs), 'boolean')
end),
test('setext_headers', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.setext_headers), 'boolean')
end),
test('slide_level', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.slide_level), 'nil')
end),
test('tab_stop', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.tab_stop), 'number')
end),
test('table_of_contents', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.table_of_contents), 'boolean')
end),
test('toc_depth', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.toc_depth), 'number')
end),
test('top_level_division', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.top_level_division), 'string')
end),
test('variables', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.variables), 'table')
end),
test('wrap_text', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.wrap_text), 'string')
end),
}
}