diff --git a/docs/README.pdf b/docs/README.pdf new file mode 100644 index 00000000..a5230406 Binary files /dev/null and b/docs/README.pdf differ diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 00000000..e579517c --- /dev/null +++ b/docs/index.html @@ -0,0 +1,380 @@ + + + + + + + + CSV Tables in Markdown — Pandoc Filter for CSV Tables + + + + + + +
+

CSV Tables in Markdown — Pandoc Filter for CSV Tables

+

July 1, 2020

+
+ + +

Python package GitHub Releases PyPI version Development Status Python version Downloads License

+

Warning: panflute and pantable only support pandoc < 2.10 and is so far incompatible with pandoc 2.10+. This is because there’s a new AST change related to table spans. See https://github.com/jgm/pandoc/issues/1024, https://github.com/sergiocorreia/panflute/issues/142. Until that is resolved, please stick with 2.9.2.1 or below when used together with pantable.

+

The pantable package comes with 2 pandoc filters, pantable.py and pantable2csv.py. pantable is the main filter, introducing a syntax to include CSV table in markdown source. pantable2csv complements pantable, is the inverse of pantable, which convert native pandoc tables into the CSV table format defined by pantable.

+

Some example uses are:

+
    +
  1. You already have tables in CSV format.

  2. +
  3. You feel that directly editing markdown table is troublesome. You want a spreadsheet interface to edit, but want to convert it to native pandoc table for higher readability. And this process might go back and forth.

  4. +
  5. You want lower-level control on the table and column widths.

  6. +
  7. You want to use all table features supported by the pandoc’s internal AST table format, which is not possible in markdown for pandoc <= 1.18.1

  8. +
+

1 pantable

+

This allows CSV tables, optionally containing markdown syntax (disabled by default), to be put in markdown as a fenced code blocks.

+

1.1 Example

+

Also see the README in GitHub Pages. There’s a LaTeX output too.

+
```table
+---
+caption: '*Awesome* **Markdown** Table'
+alignment: RC
+table-width: 2/3
+markdown: True
+---
+First row,defaulted to be header row,can be disabled
+1,cell can contain **markdown**,"It can be aribrary block element:
+
+- following standard markdown syntax
+- like this"
+2,"Any markdown syntax, e.g.",$$E = mc^2$$
+```
+

becomes

+ + +++++ + + + + + + + + + + + + + + + + + + + +
Awesome Markdown Table

First row

defaulted to be header row

can be disabled

1

cell can contain markdown

It can be aribrary block element:

+
    +
  • following standard markdown syntax
  • +
  • like this
  • +

2

Any markdown syntax, e.g.


E = mc2

+

(The equation might not work if you view this on PyPI.)

+

1.2 Install and Use

+

Install:

+
pip install -U pantable
+

Use:

+
pandoc -F pantable -o README.html README.md
+

1.3 Syntax

+

Fenced code blocks is used, with a class table. See Example.

+

Optionally, YAML metadata block can be used within the fenced code block, following standard pandoc YAML metadata block syntax. 7 metadata keys are recognized:

+
+
caption
+

the caption of the table. If omitted, no caption will be inserted. Default: disabled.

+
+
alignment
+

a string of characters among L,R,C,D, case-insensitive, corresponds to Left-aligned, Right-aligned, Center-aligned, Default-aligned respectively. e.g. LCRD for a table with 4 columns. Default: DDD...

+
+
width
+

a list of relative width corresponding to the width of each columns. e.g.

+
- width
+    - 0.1
+    - 0.2
+    - 0.3
+    - 0.4
+

Default: auto calculated from the length of each line in table cells.

+
+
table-width
+

the relative width of the table (e.g. relative to \linewidth). default: 1.0

+
+
header
+
If it has a header row or not. True/False/yes/NO are accepted, case-insensitive. default: True +
+
markdown
+
If CSV table cell contains markdown syntax or not. Same as above. Default: False +
+
include
+
the path to an CSV file, can be relative/absolute. If non-empty, override the CSV in the CodeBlock. default: None +
+
include-encoding
+
if specified, the file from include will be decoded according to this encoding, else assumed to be UTF-8. +
+
csv-kwargs
+

If specified, should be a dictionary passed to csv.reader as options. e.g.

+
---
+csv-kwargs:
+  dialect: unix
+  key: value...
+...
+
+
pipe_tables
+

If True, a pipe table will be constructed directly in markdown syntax instead of via AST. markdown is implied to be True. header will be overridden as true because pipe_tables must has header in pandoc.

+

This trades correctness for speed. It won’t be correct if any of the cell is multiline for example, resulting in an invalid pipe table. However, it is much faster comparing to previous markdown: True case because previously per cell a subprocess to execute pandoc the parse the markdown to AST is needed.

+
+
grid_tables
+

If True, a grid table will be constructed directly in markdown syntax instead of via AST. markdown is implied to be True. header can be used together with this.

+

This trades correctness for speed. This should be more robust than pipe_tables since the grid_tables syntax supports everything the pandoc AST supports. This however depends on an external dependency. Install it by either pip install terminaltables or conda install terminaltables.

+
+
raw_markdown
+

If True, force output the table as a pipe table (which is tab-delimited.) This is sometimes useful if pandoc is very stubborn to not emit a pipe table even if markdown-grid_tables... is used. Note that this should only be used if the output format is markdown.

+
+
+

When the metadata keys is invalid, the default will be used instead. Note that width and table-width accept fractions as well.

+

2 pantable2csv

+

This one is the inverse of pantable, a panflute filter to convert any native pandoc tables into the CSV table format used by pantable.

+

Effectively, pantable forms a “CSV Reader”, and pantable2csv forms a “CSV Writer”. It allows you to convert back and forth between these 2 formats.

+

For example, in the markdown source:

+
+--------+---------------------+--------------------------+
+| First  | defaulted to be     | can be disabled          |
+| row    | header row          |                          |
++========+=====================+==========================+
+| 1      | cell can contain    | It can be aribrary block |
+|        | **markdown**        | element:                 |
+|        |                     |                          |
+|        |                     | -   following standard   |
+|        |                     |     markdown syntax      |
+|        |                     | -   like this            |
++--------+---------------------+--------------------------+
+| 2      | Any markdown        | $$E = mc^2$$             |
+|        | syntax, e.g.        |                          |
++--------+---------------------+--------------------------+
+
+: *Awesome* **Markdown** Table
+

running pandoc -F pantable2csv -o output.md input.md, it becomes

+
``` {.table}
+---
+alignment: DDD
+caption: '*Awesome* **Markdown** Table'
+header: true
+markdown: true
+table-width: 0.8055555555555556
+width: [0.125, 0.3055555555555556, 0.375]
+---
+First row,defaulted to be header row,can be disabled
+1,cell can contain **markdown**,"It can be aribrary block element:
+
+-   following standard markdown syntax
+-   like this
+"
+2,"Any markdown syntax, e.g.",$$E = mc^2$$
+```
+

3 Related Filters

+

The followings are pandoc filters written in Haskell that provide similar functionality. This filter is born after testing with theirs.

+ + +++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
pandoc-csv2tablepandoc-placetablepanflute examplepantable
captioncaptioncaptiontitlecaption
alignsaligns = LRCDaligns = LRCDaligns = LRCD
widthwidths = "0.5 0.2 0.3"width: [0.5, 0.2, 0.3]
table-widthtable-width: 1.0
headerheader = yes | noheader = yes | nohas_header: True | Falseheader: True | False | yes | NO
markdowninlinemarkdownmarkdown: True | False | yes | NO
sourcesourcefilesourceinclude
otherstype = simple | multiline | grid | pipe
delimiter
quotechar
id (wrapped by div)
Noteswidth are auto-calculated when width is not specified
+
+
+
    +
  1. In pandoc 1.19, grid-tables is improved to support all features available to the AST too.↩︎

  2. +
+
+ + diff --git a/gh-pages/README.pdf b/gh-pages/README.pdf new file mode 100644 index 00000000..b9f22a3d Binary files /dev/null and b/gh-pages/README.pdf differ diff --git a/gh-pages/index.html b/gh-pages/index.html new file mode 100644 index 00000000..50d51b85 --- /dev/null +++ b/gh-pages/index.html @@ -0,0 +1,279 @@ + + + + + + + + CSV Tables in Markdown — Pandoc Filter for CSV Tables + + + + + + +
+

CSV Tables in Markdown — Pandoc Filter for CSV Tables

+

November 9, 2020

+
+ +

Python package GitHub Releases PyPI version Development Status Python version Downloads License

+

Warning: panflute and pantable only support pandoc < 2.10 and is so far incompatible with pandoc 2.10+. This is because there’s a new AST change related to table spans. See https://github.com/jgm/pandoc/issues/1024, https://github.com/sergiocorreia/panflute/issues/142. Until that is resolved, please stick with 2.9.2.1 or below when used together with pantable.

+

The pantable package comes with 2 pandoc filters, pantable.py and pantable2csv.py. pantable is the main filter, introducing a syntax to include CSV table in markdown source. pantable2csv complements pantable, is the inverse of pantable, which convert native pandoc tables into the CSV table format defined by pantable.

+

Some example uses are:

+
    +
  1. You already have tables in CSV format.

  2. +
  3. You feel that directly editing markdown table is troublesome. You want a spreadsheet interface to edit, but want to convert it to native pandoc table for higher readability. And this process might go back and forth.

  4. +
  5. You want lower-level control on the table and column widths.

  6. +
  7. You want to use all table features supported by the pandoc’s internal AST table format, which is not possible in markdown for pandoc <= 1.18.1

  8. +
+

1 pantable

+

This allows CSV tables, optionally containing markdown syntax (disabled by default), to be put in markdown as a fenced code blocks.

+

1.1 Example

+

Also see the README in GitHub Pages. There’s a LaTeX output too.

+
```table
+---
+caption: '*Awesome* **Markdown** Table'
+alignment: RC
+table-width: 2/3
+markdown: True
+---
+First row,defaulted to be header row,can be disabled
+1,cell can contain **markdown**,"It can be aribrary block element:
+
+- following standard markdown syntax
+- like this"
+2,"Any markdown syntax, e.g.",$$E = mc^2$$
+```
+

becomes

+ + +++++ + + + + + + + + + + + + + + + + + + + +
Awesome Markdown Table

First row

defaulted to be header row

can be disabled

1

cell can contain markdown

It can be aribrary block element:

+
    +
  • following standard markdown syntax
  • +
  • like this
  • +

2

Any markdown syntax, e.g.


E = mc2

+

(The equation might not work if you view this on PyPI.)

+

1.2 Install and Use

+

Install:

+
pip install -U pantable
+

Use:

+
pandoc -F pantable -o README.html README.md
+

1.3 Syntax

+

Fenced code blocks is used, with a class table. See Example.

+

Optionally, YAML metadata block can be used within the fenced code block, following standard pandoc YAML metadata block syntax. 7 metadata keys are recognized:

+
+
caption
+

the caption of the table. If omitted, no caption will be inserted. Default: disabled.

+
+
alignment
+

a string of characters among L,R,C,D, case-insensitive, corresponds to Left-aligned, Right-aligned, Center-aligned, Default-aligned respectively. e.g. LCRD for a table with 4 columns. Default: DDD...

+
+
width
+

a list of relative width corresponding to the width of each columns. e.g.

+
- width
+    - 0.1
+    - 0.2
+    - 0.3
+    - 0.4
+

Default: auto calculated from the length of each line in table cells.

+
+
table-width
+

the relative width of the table (e.g. relative to \linewidth). default: 1.0

+
+
header
+
If it has a header row or not. True/False/yes/NO are accepted, case-insensitive. default: True +
+
markdown
+
If CSV table cell contains markdown syntax or not. Same as above. Default: False +
+
include
+
the path to an CSV file, can be relative/absolute. If non-empty, override the CSV in the CodeBlock. default: None +
+
include-encoding
+
if specified, the file from include will be decoded according to this encoding, else assumed to be UTF-8. +
+
csv-kwargs
+

If specified, should be a dictionary passed to csv.reader as options. e.g.

+
---
+csv-kwargs:
+  dialect: unix
+  key: value...
+...
+
+
pipe_tables
+

If True, a pipe table will be constructed directly in markdown syntax instead of via AST. markdown is implied to be True. header will be overridden as true because pipe_tables must has header in pandoc.

+

This trades correctness for speed. It won’t be correct if any of the cell is multiline for example, resulting in an invalid pipe table. However, it is much faster comparing to previous markdown: True case because previously per cell a subprocess to execute pandoc the parse the markdown to AST is needed.

+
+
grid_tables
+

If True, a grid table will be constructed directly in markdown syntax instead of via AST. markdown is implied to be True. header can be used together with this.

+

This trades correctness for speed. This should be more robust than pipe_tables since the grid_tables syntax supports everything the pandoc AST supports. This however depends on an external dependency. Install it by either pip install terminaltables or conda install terminaltables.

+
+
raw_markdown
+

If True, force output the table as a pipe table (which is tab-delimited.) This is sometimes useful if pandoc is very stubborn to not emit a pipe table even if markdown-grid_tables... is used. Note that this should only be used if the output format is markdown.

+
+
+

When the metadata keys is invalid, the default will be used instead. Note that width and table-width accept fractions as well.

+

2 pantable2csv

+

This one is the inverse of pantable, a panflute filter to convert any native pandoc tables into the CSV table format used by pantable.

+

Effectively, pantable forms a “CSV Reader”, and pantable2csv forms a “CSV Writer”. It allows you to convert back and forth between these 2 formats.

+

For example, in the markdown source:

+
+--------+---------------------+--------------------------+
+| First  | defaulted to be     | can be disabled          |
+| row    | header row          |                          |
++========+=====================+==========================+
+| 1      | cell can contain    | It can be aribrary block |
+|        | **markdown**        | element:                 |
+|        |                     |                          |
+|        |                     | -   following standard   |
+|        |                     |     markdown syntax      |
+|        |                     | -   like this            |
++--------+---------------------+--------------------------+
+| 2      | Any markdown        | $$E = mc^2$$             |
+|        | syntax, e.g.        |                          |
++--------+---------------------+--------------------------+
+
+: *Awesome* **Markdown** Table
+

running pandoc -F pantable2csv -o output.md input.md, it becomes

+
``` {.table}
+---
+alignment: DDD
+caption: '*Awesome* **Markdown** Table'
+header: true
+markdown: true
+table-width: 0.8055555555555556
+width: [0.125, 0.3055555555555556, 0.375]
+---
+First row,defaulted to be header row,can be disabled
+1,cell can contain **markdown**,"It can be aribrary block element:
+
+-   following standard markdown syntax
+-   like this
+"
+2,"Any markdown syntax, e.g.",$$E = mc^2$$
+```
+

3 Related Filters

+

The followings are pandoc filters written in Haskell that provide similar functionality. This filter is born after testing with theirs.

+ +
---
+Caption: Comparison
+include: comparison.csv
+...
+
+
+
    +
  1. In pandoc 1.19, grid-tables is improved to support all features available to the AST too.↩︎

  2. +
+
+ + diff --git a/makefile b/makefile index 91cac00f..b0546109 100644 --- a/makefile +++ b/makefile @@ -117,3 +117,6 @@ README.html: README.rst gh-pages-init: git add gh-pages && git commit -m "initial gh-pages subtree commit" + +gh-pages-push: + git subtree push --prefix gh-pages origin gh-pages