Skip to content

Instantly share code, notes, and snippets.

@K4zuki
Last active June 1, 2022 12:01
Show Gist options
  • Save K4zuki/04e1e1af2fcf06db1a6ea640aa8feadd to your computer and use it in GitHub Desktop.
Save K4zuki/04e1e1af2fcf06db1a6ea640aa8feadd to your computer and use it in GitHub Desktop.
Personal note of Pandoc's **NEW** Table structure in Lua (WIP!)

Applies to Pandoc ≥ 2.10

From my notes during preparation of my filter compatibility for 2.10

See here for primary reference

-t native always gives great hints

Try:

$ pandoc -t native
| table |
|-------|
| cell  |
(Ctrl-D to send EOF)

Pandoc prints:

[Table ("",[],[]) (Caption Nothing
 [])
 [(AlignDefault,ColWidthDefault)]
 (TableHead ("",[],[])
 [Row ("",[],[])
  [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
   [Plain [Str "table"]]]])
 [(TableBody ("",[],[]) (RowHeadColumns 0)
  []
  [Row ("",[],[])
   [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
    [Plain [Str "cell"]]]])]
 (TableFoot ("",[],[])
 [])]

From 2.11.4:

[ Table
    ( "" , [] , [] )
    (Caption Nothing [])
    [ ( AlignLeft , ColWidthDefault ) ]
    (TableHead
       ( "" , [] , [] )
       [ Row
           ( "" , [] , [] )
           [ Cell
               ( "" , [] , [] )
               AlignDefault
               (RowSpan 1)
               (ColSpan 1)
               [ Plain [ Str "Header" ] ]
           ]
       ])
    [ TableBody
        ( "" , [] , [] )
        (RowHeadColumns 0)
        []
        [ Row
            ( "" , [] , [] )
            [ Cell
                ( "" , [] , [] )
                AlignDefault
                (RowSpan 1)
                (ColSpan 1)
                [ Plain [ Str "Cell" ] ]
            ]
        ]
    ]
    (TableFoot ( "" , [] , [] ) [])
]

Debug setup in lua filter

local tablex = require("pl.tablex") -- PenLight
local function debug(string)
    io.stderr:write(string .. "\n")
end

Pair

  • a list whose size is 2. sometimes items are named, mostly unnamed

Caption

  • tbl.caption
  • a Pair of (List of Blocks) (aka. long) and (List of Inlines) (aka. short)
  • Seems no short caption syntax available yet. it can be omitted
{
long={Para, ...} 
, short={Str, ...} -- can work without this line
}

ColSpec

  • a List of Pair of Alignment and Column width
    • -t native shows ColWidthDefault which is not (yet) available for Lua filters. when a pair contains Alignments only, Pandoc considers/applies default Column width.
    • float value also allowed (why not!)
    • "ColWidth" class exists but constructor for Lua seems not available (yet)
{
{pandoc.AlignDefault},
{pandoc.AlignLeft, 0.1},
...
}

local function merge_colspecs(colspecs, widths)
    for idx, _ in ipairs(colspecs) do
        table.insert(colspecs[idx], widths[idx])
    end
    return colspecs
end

Cell

  • List of attributes, alignment, number of row/colspan and (List of Blocks)
  • no spanning syntax available yet. just set them 1.
{
attr = { "", {}, {} }, -- not sure if pandoc.Attr() is allowed
alignment = pandoc.AlignDefault,
row_span = 1,
col_span = 1,
contents = pandoc.List({Para, ...}) 
}

Row

  • Pair of attributes and (List of Cells)
{
{"", {}, {}}, -- not sure if pandoc.Attr() is allowed
{Cell, ...}
}

Head, Foot

  • Pair of attributes and (List of Rows)
  • multi-row header/footer seems allowed but (markdown) syntax seems not available (yet)
{
{"", {}, {}}, -- not sure if pandoc.Attr() is allowed
{Row, ...}
}

Body

  • List of Rows

Bodies

  • List of List of attribute, head, body and row_head_columns
table.bodies = { { attr = empty_attr,
                   head = {   },
                   body = rows,
                   row_head_columns = 0 } }

Table

  • List of caption, colspecs, head, bodies and foot (applies Caption, Colspec, Head, Bodies and Foot, respectively)

Keys (2.17+)

  • tbl.{attr,bodies,caption,colspecs,foot,head,tag,clone,show,walk}

Easy construction hack

  • construct Table from scratch using structures above didn't work for me. instead use pandoc.read() to read a template table and override by your content.
local table_template = [==[
| table |
|-------|
| cell  |

: caption
]==]
local my_table = pandoc.read(table_template, "markdown").blocks[1]

local table = my_table:clone()
table.head = { empty_attr, { header } }
--pretty.dump(table.head)
table.caption = { long = { pandoc.Plain(caption) } }
--pretty.dump(table.caption)
table.colspecs = tablex.zip(alignment, widths)
--pretty.dump(table.colspecs)
table.bodies = { { attr = empty_attr,
                   head = {   },
                   body = rows,
                   row_head_columns = 0 } }
--pretty.dump(table.bodies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment