Commit graph

90 commits

Author SHA1 Message Date
Albert Krewinkel
1f8638fb54 Lua: add pandoc.template module
The module provides a `compile` function to use strings as templates.
2022-01-04 11:55:59 -08:00
Albert Krewinkel
6a5ac90bf1 Lua: add pandoc.WriterOptions constructor 2022-01-04 11:55:59 -08:00
Albert Krewinkel
2dd1cde715 Lua: allow binary (byte string) readers to be used with pandoc.read 2021-12-30 22:41:15 +01:00
Albert Krewinkel
fbd2c8e376
Lua: improve handling of empty caption, body by from_simple_table
Create truly empty table caption and body when these are empty in the
simple table.

Fixes: #7776
2021-12-25 21:20:18 +01:00
Albert Krewinkel
0bdf373157
Lua: simplify code of pandoc.utils.stringify
Minor behavior change: plain strings nested in tables are now included
in the result string.
2021-12-21 21:50:13 +01:00
Albert Krewinkel
edb04a78db
Lua tests: add more tests for pandoc.utils.stringify. 2021-12-21 21:25:16 +01:00
Albert Krewinkel
1c389bf6b6
Lua: add tests for pandoc.utils.equals 2021-12-21 19:00:21 +01:00
Albert Krewinkel
d7cab51982 Lua: add new library function pandoc.utils.type.
The function behaves like the default `type` function from Lua's
standard library, but is aware of pandoc userdata types. A typical
use-case would be to determine the type of a metadata value.
2021-12-21 09:24:21 -08:00
Albert Krewinkel
cd2bffee1e
Lua: use more natural representation for Reference values
Omit `false` boolean values, push integers as numbers.
2021-12-20 09:41:03 +01:00
Albert Krewinkel
dc3dcc2ccd
Lua: fixup, should have been part of previous commit 2021-12-19 14:31:52 +01:00
Albert Krewinkel
fa643ba6d7 Lua: update to latest pandoc-lua-marshal (0.1.1)
- `walk` methods are added to `Block` and `Inline` values; the methods
  are similar to `pandoc.utils.walk_block` and
  `pandoc.utils.walk_inline`, but apply to filter also to the element
  itself, and therefore return a list of element instead of a single
  element.

- Functions of name `Doc` are no longer accepted as alternatives for
  `Pandoc` filter functions. This functionality was undocumented.
2021-12-09 09:22:29 -08:00
Albert Krewinkel
fa838deefc
Lua: remove pandoc.utils.text (#7720)
The new `pandoc.Inlines` function behaves identical on string input, but
allows other Inlines-like arguments as well.

The `pandoc.utils.text` function could be written as

    function pandoc.utils.text (x)
      assert(type(x) == 'string')
      return pandoc.Inlines(x)
    end
2021-11-29 09:12:30 -08:00
Albert Krewinkel
b9222e5cb1
Lua: add constructors pandoc.Blocks and pandoc.Inlines
The functions convert their argument into a list of Block and Inline
values, respectively.
2021-11-28 16:02:42 +01:00
Albert Krewinkel
3692a1d1e8
Lua: use package pandoc-lua-marshal (#7719)
The marshaling functions for pandoc's AST are extracted into a separate
package. The package comes with a number of changes:

  - Pandoc's List module was rewritten in C, thereby improving error
    messages.

  - Lists of `Block` and `Inline` elements are marshaled using the new
    list types `Blocks` and `Inlines`, respectively. These types
    currently behave identical to the generic List type, but give better
    error messages. This also opens up the possibility of adding
    element-specific methods to these lists in the future.

  - Elements of type `MetaValue` are no longer pushed as values which
    have `.t` and `.tag` properties. This was already true for
    `MetaString` and `MetaBool` values, which are still marshaled as Lua
    strings and booleans, respectively. Affected values:

      + `MetaBlocks` values are marshaled as a `Blocks` list;

      + `MetaInlines` values are marshaled as a `Inlines` list;

      + `MetaList` values are marshaled as a generic pandoc `List`s.

      + `MetaMap` values are marshaled as plain tables and no longer
        given any metatable.

  - The test suite for marshaled objects and their constructors has
    been extended and improved.

  - A bug in Citation objects, where setting a citation's suffix
    modified it's prefix, has been fixed.
2021-11-27 17:08:01 -08:00
Albert Krewinkel
a8638894ab
Lua: allow single elements as singleton MetaBlocks/MetaInlines
Single elements should always be treated as singleton lists in the Lua
subsystem.
2021-11-24 16:54:12 +01:00
Albert Krewinkel
bffd74323c
Lua: add function pandoc.utils.text (#7710)
The function converts a string to `Inlines`, treating interword spaces
as `Space`s or `SoftBreak`s. If you want a `Str` with literal spaces,
use `pandoc.Str`.

Closes: #7709
2021-11-23 09:32:53 -08:00
Albert Krewinkel
0c0945b93c
Lua: split strings into words when treating them as Inline list (#7712)
Using a Lua string where a list of inlines is expected will cause the
string to be split into words, replacing spaces and tabs into
`pandoc.Space()` elements and newlines into `pandoc.SoftBreak()`.

The previous behavior was to treat the string `s` as `{pandoc.Str(s)}`.
The old behavior can be recovered by wrapping the string into a table
`{s}`.
2021-11-23 09:30:48 -08:00
Albert Krewinkel
d4c73d5e65
Lua: fix argument order in constructor pandoc.Cite.
This restores the old behavior; argument order had been switched
accidentally in pandoc 2.15.
2021-11-09 14:45:36 +01:00
Albert Krewinkel
6b462e5933 Lua: allow to pass custom reader options to pandoc.read
Reader options can now be passed as an optional third argument to
`pandoc.read`. The object can either be a table or a ReaderOptions value
like `PANDOC_READER_OPTIONS`. Creating new ReaderOptions objects is
possible through the new constructor `pandoc.ReaderOptions`.

Closes: #7656
2021-11-06 09:04:29 -07:00
Albert Krewinkel
45bcd7d3f1
Lua: fix typo in SoftBreak constructor 2021-11-02 21:53:08 +01:00
Albert Krewinkel
dbc654e4a7
Lua tests: ensure Inline elements have all expected properties 2021-11-02 21:40:37 +01:00
Albert Krewinkel
421fd736d4
Lua: re-add content property to Strikeout elements
Fixes a regression introduced in 2.15.
2021-11-02 21:22:59 +01:00
Albert Krewinkel
cce49c5d4b
Lua: be more forgiving when retrieving the Image caption property
Fixes a regression introduced in 2.15.
2021-11-02 17:40:07 +01:00
Albert Krewinkel
b26f950cca
Lua: display Attr values using their native Haskell representation 2021-11-02 17:25:47 +01:00
Albert Krewinkel
c467f0fed1
Lua: allow omitting the 2nd parameter in pandoc.Code constructor
Fixes a regression introduced in 2.15 which required users to always
specify an Attr value when constructing a Code element.
2021-11-02 17:23:24 +01:00
Albert Krewinkel
210e4c98b0
Lua: allow to compare, show Citation values
Comparisons of Citation values are performed in Haskell; values are
equal if they represent the same Haskell value. Converting a Citation
value to a string now yields its native Haskell string representation.
2021-11-02 16:49:50 +01:00
Albert Krewinkel
3a0ac52f7b
Lua tests: ensure Block elements have expected properties 2021-11-02 10:23:14 +01:00
Albert Krewinkel
759aa50951
Lua: restore content property on Header elements 2021-11-01 15:43:51 +01:00
Albert Krewinkel
96e76d4cd4
Lua: restore List behavior of MetaList
Fixes a regression introduced in 2.16 which had MetaList elements loose
the `pandoc.List` properties.

Fixes #7650
2021-11-01 08:27:14 +01:00
Albert Krewinkel
3de8f4fdc5
Lua: re-add content property to Link elements
This was a regression introduced in version 2.15.

Fixes: #7647
2021-10-31 11:15:50 +01:00
Albert Krewinkel
f4d9b443d8
Lua: use hslua module abstraction where possible
This will make it easier to generate module documentation in the future.
2021-10-29 17:08:30 +02:00
Albert Krewinkel
e1cf0ad1be
Lua: fix placement of tests for Block elements in pandoc module tests 2021-10-28 14:10:54 +02:00
Albert Krewinkel
7fcf1d6184
Lua: re-add t and tag property to Attr values
Removal of these properties from Attr values was a regression.
2021-10-27 22:32:19 +02:00
Albert Krewinkel
b95e864ecf
Lua: marshal SimpleTable values as userdata objects 2021-10-26 21:45:16 +02:00
Albert Krewinkel
a493c7029c
Lua: marshal Block values as userdata objects
Properties of Block values are marshalled lazily, which generally
improves performance considerably. Script users may also notice the
following differences:

  - Block element properties can no longer be accessed by numerical
    indexing of the `.c` field. The `.c` property now serves as an alias
    for `.content`, so some filter that used this undocumented method
    for property access may continue to work, while others will need to
    be updated and use proper property names.

  - The marshalled Block elements now have a `show` method, and a
    `__tostring` metamethod. Both return the Haskell string
    representation of the element.

  - Block values now have the Lua type `userdata` instead of `table`.
2021-10-26 14:40:10 +02:00
Albert Krewinkel
230b133db5
Lua: marshal Citation values as userdata objects 2021-10-25 09:08:58 +02:00
Albert Krewinkel
8523bb01b2 Lua: marshal Attr values as userdata
- Adds a new `pandoc.AttributeList()` constructor, which creates the
  associative attribute list that is used as the third component of
  `Attr` values. Values of this type can often be passed to constructors
  instead of `Attr` values.

- `AttributeList` values can no longer be indexed numerically.
2021-10-22 11:16:51 -07:00
Albert Krewinkel
9e74826ba9 Switch to hslua-2.0
The new HsLua version takes a somewhat different approach to marshalling
and unmarshalling, relying less on typeclasses and more on specialized
types. This allows for better performance and improved error messages.

Furthermore, new abstractions allow to document the code and exposed
functions.
2021-10-22 11:16:51 -07:00
Albert Krewinkel
6f79042502 Add tests for search_path_separator 2021-02-02 21:04:30 -08:00
Albert Krewinkel
e0bf4bfe82 Check that all documented functions are present.
Rely on tests in the module package to check the correctness of each
function.
2021-02-02 21:04:30 -08:00
Albert Krewinkel
61b108d527 Lua: add module "pandoc.path"
The module allows to work with file paths in a convenient and
platform-independent manner.

Closes: #6001
Closes: #6565
2021-02-02 21:04:30 -08:00
Albert Krewinkel
acbea6b8c6
Lua filters: add SimpleTable for backwards compatibility (#6575)
A new type `SimpleTable` is made available to Lua filters. It is
similar to the `Table` type in pandoc versions before 2.10;
conversion functions from and to the new Table type are provided.

Old filters using tables now require minimal changes and can use,
e.g.,

    if PANDOC_VERSION > {2,10,1} then
      pandoc.Table = pandoc.SimpleTable
    end

and

    function Table (tbl)
      tbl = pandoc.utils.to_simple_table(tbl)
      …
      return pandoc.utils.from_simple_table(tbl)
    end

to work with the current pandoc version.
2020-09-20 15:48:31 -07:00
Albert Krewinkel
9c76c52e9b
Lua: fix regression in package searcher
This caused `require 'module'` to fail for third party packages.

Fixes: #6361
2020-05-12 17:10:30 +02:00
Albert Krewinkel
fb54f3d679
API change: use PandocError for exceptions in Lua subsystem
The PandocError type is used throughout the Lua subsystem, all Lua
functions throw an exception of this type if an error occurs. The
`LuaException` type is removed and no longer exported from
`Text.Pandoc.Lua`. In its place, a new constructor `PandocLuaError` is
added to PandocError.
2020-04-17 21:52:48 +02:00
Albert Krewinkel
672a4bdd1d Lua filters: allow filtering of element lists (#6040)
Lists of Inline and Block elements can now be filtered via `Inlines` and
`Blocks` functions, respectively. This is helpful if a filter conversion
depends on the order of elements rather than a single element.

For example, the following filter can be used to remove all spaces
before a citation:

    function isSpaceBeforeCite (spc, cite)
      return spc and spc.t == 'Space'
       and cite and cite.t == 'Cite'
    end

    function Inlines (inlines)
      for i = #inlines-1,1,-1 do
        if isSpaceBeforeCite(inlines[i], inlines[i+1]) then
          inlines:remove(i)
        end
      end
      return inlines
    end

Closes: #6038
2020-01-15 14:26:00 -08:00
Albert Krewinkel
4ede11a75f
Lua: add methods insert, remove, and sort to pandoc.List
The functions `table.insert`, `table.remove`, and `table.sort` are added
to pandoc.List elements.  They can be used as methods, e.g.

    local numbers = pandoc.List {2, 3, 1}
    numbers:sort()     -- numbers is now {1, 2, 3}
2020-01-11 21:31:20 +01:00
Albert Krewinkel
e98921ae57
pandoc.List.lua: make pandoc.List a callable constructor
It is now possible to construct a new List via `pandoc.List()` instead of
`pandoc.List:new()`.
2020-01-11 19:38:27 +01:00
Albert Krewinkel
57637f8aae
Add tests for pandoc.List module 2020-01-11 17:42:32 +01:00
Albert Krewinkel
d0261d7387 Lua filters: allow passing of HTML-like tables instead of Attr (#5750)
Attr values can now be given as normal Lua tables; this can be used as a
convenient alternative to define Attr values, instead of constructing
values with `pandoc.Attr`. Identifiers are taken from the *id* field,
classes must be given as space separated words in the *class* field. All
remaining fields are included as misc attributes.

With this change, the following lines now create equal elements:

    pandoc.Span('test', {id = 'test', class = 'a b', check = 1})
    pandoc.Span('test', pandoc.Attr('test', {'a','b'}, {check = 1}))

This also works when using the *attr* setter:

    local span = pandoc.Span 'text'
    span.attr = {id = 'test', class = 'a b', check = 1}

Furthermore, the *attributes* field of AST elements can now be a plain
key-value table even when using the `attributes` accessor:

    local span = pandoc.Span 'test'
    span.attributes = {check = 1}   -- works as expected now

Closes: #5744
2019-09-15 12:11:58 -07:00
John MacFarlane
9f984ff26a Replace Element and makeHierarchical with makeSections.
Text.Pandoc.Shared:

+ Remove `Element` type [API change]
+ Remove `makeHierarchicalize` [API change]
+ Add `makeSections` [API change]
+ Export `deLink` [API change]

Now that we have Divs, we can use them to represent the structure
of sections, and we don't need a special Element type.
`makeSections` reorganizes a block list, adding Divs with
class `section` around sections, and adding numbering
if needed.

This change also fixes some longstanding issues recognizing
section structure when the document contains Divs.
Closes #3057, see also #997.

All writers have been changed to use `makeSections`.
Note that in the process we have reverted the change
c1d058aeb1
made in response to #5168, which I'm not completely
sure was a good idea.

Lua modules have also been adjusted accordingly.
Existing lua filters that use `hierarchicalize` will
need to be rewritten to use `make_sections`.
2019-09-08 22:20:19 -07:00