Commit Graph
53 Commits
Author SHA1 Message Date
Albert Krewinkel 992842d07b Lua: accept filename-contents pairs as env for pandoc.read
Key-value pairs, mapping from filename to contents, can be used to fill
the ersatz file system used in the reader sandbox.
2025-05-14 09:17:24 -07:00
Albert Krewinkel 5b896bc1a9 Lua: allow to pass files to the pandoc.read sandbox
The sandbox is now enabled if the fourth parameter is a list of files.
The files are read and then made available in the sandbox via a mock
file system.
2025-05-14 09:17:24 -07:00
Albert Krewinkel 31f74fac06 Lua: support sandboxed parsing with pandoc.read.
The function `pandoc.read` is now taking an optional fourth parameter
that specifies the environment in which the parser will be run. Passing
the string `sandbox` as the argument causes the reader to run in a
sandbox, thereby preventing all access to the network and file system.

Closes: #10831
2025-05-13 23:38:36 +02:00
Albert Krewinkel a084731070 Lua: support more elements as input to pandoc.utils.stringify.
Elements of type Caption, Cell, TableHead, and TableFoot can now be
stringified.

Fixes: #10450
2024-12-10 11:45:51 +01:00
Albert Krewinkel d399943759 Lua: Remove prefixes from Lua type names
Lua type names were inconsistent with regard to the use of prefixes; all
prefixes are removed now, and Lua types now have the same name as the
Haskell types.

The use of app-specific prefixes is suggested by the Lua manual to avoid
collisions. However, this shouldn't be a problem with pandoc, as it
cannot be used as a Lua package.

Closes: #8574
2024-10-14 07:53:12 +02:00
Albert Krewinkel 4cf6ddb027 Lua: allow returning a single filter from filter files
It is now possible to return a single filter from a filter file, e.g.

``` lua
-- Switch single- and double quotes
return {
  Quoted = function (q)
    elem.quotetype = elem.quotetype == 'SingleQuote'
      and 'DoubleQuote' or 'SingleQuote'
    return elem
  end
}

The filter must not contain numerical indexes, or it might be treated as
a list of filters.
2024-10-01 12:23:36 +02:00
Albert Krewinkel 73ce302479 Harmonize maintainer email addresses in module headers. 2024-07-08 13:27:45 +02:00
Albert KrewinkelandCarsten Gips 9af5b1ca8b Lua: add function pandoc.template.get.
The function allows to specify a template with the same argument value
that would be used with the `--template` command line parameter.

Closes: #9854

Co-authored-by: Carsten Gips <cagix@hsbi.de>
2024-06-08 08:42:47 -07:00
Albert Krewinkel 6a88abae3d Lua: keep CommonState object in the registry
The state is an internal value and should be treated as such. The
`PANDOC_STATE` global is merely a copy; unsetting the global no longer
breaks the Lua engine.
2024-06-08 14:39:58 +02:00
Albert Krewinkel d50bd30324 Lua: add tests for PANDOC_STATE global 2024-06-08 14:39:58 +02:00
Albert Krewinkel 430d525378 Lua: allow passing an environment to run_lua_filter.
The default is now to use a *copy* of the global environment when running a
filter; this ensures better separation when `run_lua_filter` is used multiple
times. A custom environment can be specified via the optional third parameter.
2024-06-07 08:36:24 +02:00
Albert Krewinkel 83de55bb35 Lua: Set pandoc.List as default metatable for JSON lists
Lists created by `pandoc.json.decode` now behave like lists generated
via `pandoc.List`. This also ensures that `pandoc.List` tables are
encoded as JSON arrays when passed to `pandoc.json.encode`.

Fixes: #9834
2024-06-01 21:10:29 +02:00
Albert Krewinkel e5c135e68e Lua: add a pandoc.log module. 2024-05-10 19:03:29 -07:00
John MacFarlane 03630a2c6e Update copyright dates to 2024. 2024-04-25 18:16:49 -07:00
Albert Krewinkel 5f937eae61 Lua: add new module pandoc.image
The module provides basic querying functions for image properties.
2024-04-16 10:23:08 -07:00
John MacFarlane 03bb426a4a Shared: makeSections behavior changes.
+ When the optional base level parameter is provided, we no
  longer ensure that the sequence of heading levels is gapless
  [behavior change]. Instead, we set the lowest heading level to
  the specified base level, and adjust the others accordingly. If
  an author wants to skip a level, e.g. from level 1 to level 3,
  they can do that. In general, the heading levels specified
  in the source document are preserved; `makeSections` only
  puts them into a hierarchical structure. Closes #9398.

+ Section numbers are now assigned so that the top level
  gets `1`, no matter what heading level is used. So, even
  if the top heading level is 2, numbers will be `1`, `2`, etc.
  rather than `0.1`, `0.2`, as in the past. Closes #5071.

+ We revert to the old behavior when the `--number-offset` option
  is used. So, for example, if a document begins with a level-3
  heading, and `--number-offset=1,2` is used, the top-level section
  numbers will be `1.2.1`, `1.2.2`, etc. This is mainly for
  backwards-compatibility.
2024-02-03 22:19:29 -08:00
John MacFarlane 32b6db15dc Update lua tests for change to toTOCTree. 2024-01-30 08:05:25 -08:00
Albert Krewinkel 0e4b397e70 Lua: fix json.encode for nested AST elements.
Ensures that objects with nested AST elements can be encoded as JSON.
2023-03-20 16:06:19 +01:00
Albert Krewinkel ed5197f525 Lua: load text module as pandoc.text.
This only affects the name in the Lua-internal documentation. It is
still possible to load the modules via `require 'text'`, although this
is deprecated.
2023-03-20 16:06:18 +01:00
Albert Krewinkel afee37abc7 Lua tests: add more tests for pandoc.mediabag. 2023-03-19 22:38:08 +01:00
Albert Krewinkel 75b5851d21 Lua: add module pandoc.json to handle JSON encoding
Closes: #8605
2023-02-11 14:53:53 -08:00
Albert Krewinkel 9eee4a2582 Lua: add field chunk_template to WriterOptions objects [API change]
The PathTemplate type exported from Text.Pandoc.Chunks is now an
instance of the ToJSON and FromJSON classes.

Closes: #8607
2023-02-09 08:15:13 +01:00
Albert Krewinkel 3a0fc17880 Lua: add function pandoc.format.extensions.
This simplifies the creation of custom readers and writers that are
based on built-in formats.
2023-01-18 08:59:53 -08:00
John MacFarlane c6551433b2 T.P.Chunks changes.
+ Re-use `toTocTree` in constructing `chunkedTOC`.
  Previously we used an entirely different function toTOCTree'.
+ Improve `tocToList` so that it avoids empty lists of
  items that are omitted because they are below the toc depth.

pandoc-lua-engine:

+ Fix structure tests in light of last change.

T.P.Writers.ChunkedHTML:

+ Reuse `tocToList` in `buildTOC`.

T.P.Writers.EPUB:

+ Adjust EPUB writer for Chunks changes.
2023-01-15 22:32:16 -08:00
Albert Krewinkel cdf8c69fb9 Lua: add module pandoc.structure.
Adds support for table of contents and chunks handling. The function
`make_sections` has been given a friendlier interface and was moved to
the new module; the old `pandoc.utils.make_sections` has been
deprecated.
2023-01-15 10:47:54 -08:00
John MacFarlane 3376286b7d Update pandoc-lua-engine test: test for Figure, not Null. 2023-01-13 15:08:57 -08:00
Albert KrewinkelandAner Lucero 909ced5153 Support complex figures. [API change]
Thanks and credit go to Aner Lucero, who laid the groundwork for this
feature in the 2021 GSoC project. He contributed many changes, including
modifications to the readers for HTML, JATS, and LaTeX, and to the HTML
and JATS writers.

Shared (Albert Krewinkel):

- The new function `figureDiv`, exported from `Text.Pandoc.Shared`,
  offers a standardized way to convert a figure into a Div element.

Readers (Aner Lucero):

- HTML reader: `<figure>` elements are parsed as figures, with the
  caption taken from the respective `<figcaption>` elements.

- JATS reader: The `<fig>` and `<caption>` elements are parsed into
  figure elements, even if the contents is more complex.

- LaTeX reader: support for figures with non-image contents and for
  subfigures.

- Markdown reader: paragraphs containing just an image are treated as
  figures if the `implicit_figures` extension is enabled. The identifier
  is used as the figure's identifier and the image description is also
  used as figure caption; all other attributes are treated as belonging
  to the image.

Writers (Aner Lucero, Albert Krewinkel):

- DokuWiki, Haddock, Jira, Man, MediaWiki, Ms, Muse, PPTX, RTF, TEI,
  ZimWiki writers: Figures are rendered like Div elements.

- Asciidoc writer: The figure contents is unwrapped; each image in the
  the figure becomes a separate figure.

- Classic custom writers: Figures are passed to the global function
  `Figure(caption, contents, attr)`, where `caption` and `contents` are
  strings and `attr` is a table of key-value pairs.

- ConTeXt writer: Figures are wrapped in a "placefigure" environment
  with `\startplacefigure`/`\endplacefigure`, adding the features
  caption and listing title as properties. Subfigures are place in a
  single row with the `\startfloatcombination` environment.

- DocBook writer: Uses `mediaobject` elements, unless the figure contains
  subfigures or tables, in which case the figure content is unwrapped.

- Docx writer: figures with multiple content blocks are rendered as
  tables with style `FigureTable`; like before, single-image figures are
  still output as paragraphs with style `Figure` or `Captioned Figure`,
  depending on whether a caption is attached.

- DokuWiki writer: Caption and "alt-text" are no longer combined. The
  alt text of a figure will now be lost in the conversion.

- FB2 writer: The figure caption is added as alt text to the images in
  the figure; pre-existing alt texts are kept.

- ICML writer: Only single-image figures are supported. The contents of
  figures with additional elements gets unwrapped.

- HTML writer: the alt text is no longer constructed from the caption,
  as was the case with implicit figures. This reduces duplication, but
  comes at the risk of images that are missing alt texts. Authors should
  take care to provide alt texts for all images.

  Some readers, most notably the Markdown reader with the
  `implicit_figures` extension, add a caption that's identical to the
  image description. The writer checks for this and adds an
  `aria-hidden` attribute to the `<figcaption>` element in that case.

- JATS writer: The `<fig>` and `<caption>` elements are used write
  figures.

- LaTeX writer: complex figures, e.g. with non-image contents and
  subfigures, are supported. The `subfigure` template variable is set if
  the document contains subfigures, triggering the conditional loading
  of the *subcaption* package. Contants of figures that contain tables
  are become unwrapped, as longtable environments are not allowed within
  figures.

- Markdown writer: figures are output as implicit figures if possible,
  via HTML if the `raw_html` extension is enabled, and as Div elements
  otherwise.

- OpenDocument writer: A separate paragraph is generated for each block
  element in a figure, each with style `FigureWithCaption`. Behavior for
  single-image figures therefore remains unchanged.

- Org writer: Only the first element in a figure is given a caption;
  additional block elements in the figure are appended without any
  caption being added.

- RST writer: Single-image figures are supported as before; the contents
  of more complex images become nested in a container of type `float`.

- Texinfo writer: Figures are rendered as float with type `figure`.

- Textile writer: Figures are rendered with the help of HTML elements.

- XWiki: Figures are placed in a group.

Co-authored-by: Aner Lucero <4rgento@gmail.com>
2023-01-13 09:13:27 -08:00
John MacFarlane d7960212c0 Replace --epub-chapter-level with --split-level.
Rationale: we need this splitting level now not just in
EPUB but in chunked HTML.

`--epub-chapter-level` will still function as a deprecated
synonynm. `epub-chapter-level` will also continue to work in
defaults files, ande `epub_chapter_level` will still work for
Lua marshalling.

[API changes]

Text.Pandoc.App.Opt: remove `optEpubChapterLevel`, add
`optSplitLevel`.

Text.Pandoc.Options: remove `writerEpubChapterLevel`, add
`writerSplitLevel`.
2023-01-12 09:04:29 -08:00
Albert Krewinkel b55cd83a60 Update copyright years, it's 2023! 2023-01-10 20:28:27 +01:00
John MacFarlane 063480accd T.P.Scripting: Refactor the scripting engine.
The new type CustomComponents is exported from T.P.Scripting, and the
ScriptEngine fields are changed. Instead of separate fields for custom
readers and writers, we now have a single function that loads any number
of "components" from a script: these may be custom readers, custom
writers, templates for writers, or extension configs. (Note: it's
possible to have a custom reader and a custom writer for a format
together in the same file.)

Pandoc now checks the folder `custom` in the user's data directory for a
matching script if it can't find one in the local directory. Previously,
the `readers` and `writers` data directories were search for custom
readers and writers, respectively. Scripts in those directories must be
moved to the `custom` folder.

Custom readers used to implement a fallback behavior that allowed to
consume just a string value as input to the `Reader` function. This has
been removed, the first argument is now always a list of sources. Use
`tostring` on that argument to get a string.

Closes #8417.

Signed-off-by: Albert Krewinkel <albert@zeitkraut.de>
2022-12-20 20:58:44 -08:00
Albert Krewinkel 1328e57f08 Shared: use LineBreak as default block sep in blocksToInlines
This change also affects the `pandoc.utils.blocks_to_inlines` Lua
function.

Closes: #8499
2022-12-20 13:39:16 -08:00
Albert Krewinkel a36f12119f Lua: allow table structure as format spec.
This allows to pass structured values as format specifiers to
`pandoc.write` and `pandoc.read`.
2022-12-16 10:43:01 -08:00
Albert Krewinkel 038df3b24c Format: use simpler structure for ExtensionsDiff. 2022-12-16 10:43:01 -08:00
Albert Krewinkel e0e6087145 Shared: change defaultBlocksSeparator to PARAGRAPH SEPARATOR
This Unicode char (U+2029) is intended as a semantic separator between
paragraphs; it is cleaner and less intrusive than the pilcrow sign that
we used before. This also changes the default `sep` value used in the
`pandoc.utils.blocks_to_inlines` Lua function.
2022-12-08 00:24:07 +01:00
Albert Krewinkel a03b77fbd3 Lua: support -D CLI option for custom writers [API change]
A new error `PandocNoTemplateError` (code 87) is thrown if a template is
required but cannot be found.
2022-12-05 08:52:37 -08:00
John MacFarlane 8b3be07ae8 hlint suggestions. 2022-10-30 11:02:00 -07:00
Albert Krewinkel 999916642c Fix test for template marshaling. 2022-10-27 22:46:34 +02:00
Albert Krewinkel 6c4d885bda Lua: allow strings in place of compiled templates.
This allows to use a string as parameter to `pandoc.template.apply` and
in the WriterOptions `template` field.

Closes: #8321
2022-10-27 22:09:39 +02:00
John MacFarlane 6e1cfa2af5 Lua: rename reader_extensions/writer_extensions globals...
...as `Extensions`.

Update documnetation.

Include a custom extension in the documentation example.

See #8390.
2022-10-25 10:17:23 -07:00
Albert Krewinkel 79d6b45566 Remove sample.lua from data files (#8397)
The file is kept around in the pandoc-lua-engine test-suite.

Closes: #8356
2022-10-24 09:46:42 -07:00
John MacFarlane e5bcfb6a7d Fix import in pandoc-lua-engine tests.
We neglected to change the import after adding T.P.Version.
2022-10-19 10:14:53 -07:00
Albert Krewinkel 8900b0f953 Lua: Support built-in default templates for custom writers
Custom writers can define a default template via a global `Template`
function; the data directory is no longer searched for a default
template.

Writer authors can restore the old lookup behavior with

``` lua
Template = function ()
  local template
  return template.compile(template.default(PANDOC_SCRIPT_FILE))
end
```
2022-10-14 10:37:37 -07:00
Albert Krewinkel 253d2e768a Lua: support extensions in custom readers.
Like custom readers, like writers, can define the set of supported
extensions by setting a global. E.g.:

``` lua
reader_extensions = {
  smart = true,
  citations = false,
}
```
2022-10-12 11:58:18 +02:00
Albert Krewinkel 78d7fc46fe Lua: add function pandoc.template.apply
The new function applies a context, containing variable assignments, to
a template.
2022-10-11 14:03:08 -07:00
Albert Krewinkel a088cbf563 Lua: support extensions in custom writers
Custom writers can define the extensions that they support via the
global `writer_extensions`. The variable's value must be a table with
all supported extensions as keys, and their default status as values.

E.g., the below specifies that the writer support the extensions `smart`
and `sourcepos`, but only the `smart` extension is enabled by default:

    writer_extensions = {
      smart = true,
      sourcepos = false,
    }
2022-10-10 09:39:18 -07:00
Albert Krewinkel a5ffaaa413 Lua: support custom bytestring readers. 2022-10-10 11:27:42 +02:00
Albert Krewinkel a4218b9719 [API Change] Add new module "Text.Pandoc.Format"
The module provides functions and types for format spec parsing and
processing.

The function `parseFormatSpec` was moved from Text.Pandoc.Extensions to
the new module and renamed to `parseFlavoredFormat`. It now operates in
a PandocMonad and is based on the updated types.
2022-10-08 16:05:48 -07:00
Albert Krewinkel 96bdd34a90 Lua: fix tests on Windows broken in previous commit. 2022-10-05 22:07:38 +02:00
Albert Krewinkel 48ab7a17cf Lua: produce more informative error messages for pandoc errors
Errors are reported in Lua in the same words in which they would be
reported in the terminal.
2022-10-05 21:53:03 +02:00
Albert Krewinkel 86e009b495 Lua: add new module pandoc.format.
The module provides functions to query the set of extensions supported
by formats, and the set of extension enabled per default.
2022-10-04 13:06:23 -07:00