Commit Graph
42 Commits
Author SHA1 Message Date
John MacFarlane 5db482fefb T.P.Scripting: add CustomComponents, and change...
...the ScriptingEngine fields.  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.)

This is primarily motivated by the need for a smoother check
for custom templates and extensions for `-D` and `--list-extensions`,
but it also seems more elegant.

This is a partial implementation of #8417.
2022-12-05 22:50:38 +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
Albert Krewinkel b34ec00607 doc/lua-filters.md: add documentation for pandoc.format 2022-12-03 16:22:19 +01:00
John MacFarlane f038897187 Use doctemplates 0.11.
This avoids a transitive dependency on HsYAML, which we no longer
use in pandoc.
2022-11-21 10:44:59 -08:00
Alexander Batischev 1bea5e8a54 Allow mtl 2.3.1 2022-11-15 11:35:53 -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
Albert Krewinkel e243cd696f Lua: add pandoc.scaffolding.Writer (#8377)
This can be used to reduce boilerplate in custom writers.
2022-10-21 10:11:45 -07:00
John MacFarlane 296788ec2b T.P.Lua.Module.Pandoc: Remove pushModule.
This is not used and is not an exported function; it's dead code.

@tarleb if there is some reason to keep this here, please feel
free to revert this.
2022-10-20 21:29:29 -07:00
John MacFarlane d95befac55 Add custom extensions.
T.P.Extensions [API change]:

+ Add CustomExtension constructor to Extension.
+ Remove Bounded, Enum instances for Extension.
+ Add `extensionsToList` function.
+ Revise `readExtension` so it can handle CustomExtension, and so
  that it returns a Text rather than Maybe Text.
+ Add `showExtension`.

T.P.Format:

+ Revise error checking to handle CustomExtension.
2022-10-20 10:15:30 -07:00
Albert Krewinkel f711c7f2aa Lua: add new module pandoc.zip
Allows to handle docx and epub files.
2022-10-20 09:11:57 -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
John MacFarlane fde8635a18 Split Text.Pandoc.Version from Text.Pandoc.Shared.
This new module exports `pandocVersion` and `pandocVersionText`,
which are no longer exported from Text.Pandoc.Shared. [API change]

Also, we now set the `pandoc-version` variable centrally rather
than in the writers.  One effect is the man writer now emits
a comment with the pandoc version (this was intended before,
judging from the template, but it didn't happen because the
vairable wasn't set).
2022-10-19 09:16:13 -07:00
Albert Krewinkel af2b4c47bb Lua: fix rendering of Lua errors in Lua
Otherwise the `Error running Lua` message can be prepended multiple
times.
2022-10-16 21:38:25 +02:00
Albert Krewinkel 2463477229 Lua: fix peeker for PandocError.
String error messages were incorrectly popped of the stack when
retrieving a PandocError.
2022-10-16 17:10:00 +02: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 14c7b5db20 Lua: add function pandoc.template.meta_to_context.
The functions converts Meta values to template contexts; the intended
use is in combination with `pandoc.template.apply`.
2022-10-12 10:16:01 -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 543cb5d45d Lua: allow Doc values in WriterOptions.variables.
The specialized peeker and pusher function for `Context Text` values
does not go via JSON, and thus keeps Doc values unchanged during
round-tripping.
2022-10-12 11:35:44 +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 3e1d3b5e5e Lua: use Reader type for custom readers. 2022-10-10 09:13:09 +02:00
Albert Krewinkel 5589b1f8f9 [API Change] Change argument type of getReader, getWriter.
The functions now take a `FlavoredFormat` instead of a text argument.
2022-10-08 16:05:48 -07: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 afbf732a83 Lua: cleanup module dependencies
Ensures a cleaner module dependency graph.
2022-10-06 13:45:55 +02: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
Albert Krewinkel 6ad05b7451 Lua: ensure that extensions marshaling is consistent. 2022-10-04 16:55:51 +02:00
John MacFarlane ad0532244a Separate out T.P.Data, T.P.Translations from T.P.Class. (#8348)
This makes T.P.Class more self-contained, and suitable for extraction
into a separate package if desired.

[API changes]

- T.P.Data is now an exported module, providing `readDataFile`,
  `readDefaultDataFile` (both formerly provided by T.P.Class),
  and also `getDataFileNames` (formerly unexported in
  T.P.App.CommandLineOptions).

- T.P.Translations is now an exported module (along with
  T.P.Translations.Types), providing `readTranslations`,
  `getTranslations`, `setTranslations`, `translateTerm`,
  `lookupTerm`, `readTranslations`, `Term(..)`, and `Translations`.

- T.P.Class: `readDataFile`, `readDefaultDataFile`, `setTranslations`,
  and `translateTerm` are no longer exported.
  `checkUserDataDir` is now exported.

- Text.Pandoc now exports Text.Pandoc.Data and `setTranslations`
  and `translateTerm`.
2022-10-03 13:05:38 -07:00
Albert Krewinkel 8986c03f20 Add test for custom bytestring writers 2022-10-03 19:46:57 +02:00
John MacFarlane f315248865 Remove unused lua53 flag. 2022-10-03 08:50:20 -07:00
Albert Krewinkel 3324664aab [API Change] Support bytestring custom writers.
New-style custom Lua writers can now define an alternative entry function
`BinaryWriter`. If a function with that name is defined, then pandoc
will treat the returned string as binary output. This allows to generate
formats like docx and odt with custom writers.
2022-10-03 08:47:32 -07:00
Albert Krewinkel 309163f5bc [API Change] Base custom writers on Writer type.
The `T.P.Lua.writeCustom` function changed to allow either a TextWriter
or ByteStringWriter to be returned. The global variables
`PANDOC_DOCUMENT` and `PANDOC_WRITER_OPTIONS` are no longer set when the
writer script is loaded. Both variables are still set in classic writers
before the conversion is started, so they can be used when they are
wrapped in functions.
2022-10-03 08:47:32 -07:00
Albert Krewinkel 3b0e700720 Lua: add function pandoc.write_classic.
The function can be used to convert a classic writer into a new-style
writer by setting it as the value of `Writer`:

``` lua
Writer = pandoc.write_classic
```

or to fully restore the old behavior:

``` lua
function Writer (doc, opts)
  PANDOC_DOCUMENT = doc
  PANDOC_WRITER_OPTIONS = opts
  load(PANDOC_SCRIPT_FILE)()
  return pandoc.write_classic(doc, opts)
end
```
2022-10-03 08:47:32 -07:00
Albert Krewinkel 132931a491 Lua: Support running Lua with a GC-collected Lua state. 2022-10-03 08:47:32 -07:00
Albert Krewinkel c8d08b945c Remove support for Lua 5.3. 2022-10-03 08:47:32 -07:00
Albert Krewinkel 5be9052f5f [API Change] Extract Lua code into new package pandoc-lua-engine
The flag 'lua53` must now be used with that package if pandoc is to be
compiled against Lua 5.3.
2022-09-30 08:33:40 -07:00