Skip to content

Read the CSV from a file named by an include attribute on the code block - #78

Open
ickc wants to merge 3 commits into
mainfrom
include-attribute
Open

ickc wants to merge 3 commits into
mainfrom
include-attribute

Conversation

@ickc

@ickc ickc commented Nov 19, 2026

Copy link
Copy Markdown
Owner

A table's CSV can now come from a file named by the code block's include attribute:

```{.table include="fruits.csv"}
---
caption: Fruits
---
```

The include key in the YAML block and include-encoding are unchanged, so nothing breaks.

The attribute and panclude

This is the include attribute of panclude, a Lua filter that appends a file's content to the element naming it. pantable does the same to a code block of class table, then reads the block as before:

  • the file is read as UTF-8, a byte order mark and its final newline dropped, CRLF and CR read as \n;
  • an empty block's text becomes the file's text; a block with text of its own keeps it, and the file's text follows after one \n (a YAML header, then the file's rows; the block's own rows, then the file's); an empty file adds nothing;
  • the attribute is then removed, and the table is built from the block as if the text had been written in it. A file beginning with a YAML block, in a block without one, so sets the options, as it would if written in place.

So pandoc -F pantable and pandoc -L panclude.lua -F pantable build the same table: either may do the including, and after panclude, pantable sees an ordinary code block. Two differences when pantable does it alone:

  • a relative path starts at the directory pandoc runs in, as the include key's does; panclude starts at the document's directory. The two agree when pandoc runs from the document's directory;
  • panclude's other code-block attributes, startLine, endLine (start-line, end-line), dedent and raw, are not read: the whole file is included, and a warning names them.

A file that cannot be read (not found, a directory, not UTF-8) leaves the code block as it is, attribute and all, with an error naming the path and the reason, never the file's content: what the include key does for a missing file, and what panclude does.

Given both the attribute and the key, the attribute's file is appended first, then the key reads the CSV from its own file, as it always has.

Tests

  • The golden include cases use the attribute: a YAML header then the file (include_external_csv), a missing file (include_external_csv_invalid_path); and new ones: an empty block, the block's rows then the file's, an empty file, a byte order mark and CRLF, a file beginning with a YAML block, a file that is not UTF-8, a directory, the attribute and the key together.
  • The key form keeps its cases: include_key, include_key_invalid_path (the former attribute-less cases) and encoding (include-encoding).
  • pandoc now runs each golden case (and the round trip on it) from the case's own directory, where its include paths start, as panclude takes them; the include paths in the cases are relative to it.
  • tests/test_include.py checks the messages, and that, for each golden case with the attribute, pandoc -L panclude.lua -F pantable gives the same native AST as pandoc -F pantable. It downloads panclude.lua from GitHub at a fixed commit into a temporary directory, and skips when it cannot.

Choices that could go the other way

  • panclude's other attributes: ignored with a warning, the whole file included. Alternatives: ignore them silently (documented only), or treat them as a failure and leave the block as it is.
  • Failures: logged at the error level and the block shown as is, as for the include key's missing file. With the include key, a directory or a file that does not decode still stops the filter with Python's exception; that is left as it was.
  • Encoding: the attribute's file is UTF-8 only, as panclude reads it; include-encoding applies to the key's file.
  • The equality test needs the network once per test run; it could instead read a path from an environment variable, or panclude.lua could be vendored.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant