Install pandocfilters
masterYou can install pandocfilters from PyPI using pip or by installing from the source directory.
pip install pandocfiltersOr from source:
python setup.py installrepository·master·Indexed 20 days ago
https://github.com/jgm/pandocfiltersA Python module for writing Pandoc filters that transform the Pandoc Abstract Syntax Tree (AST) via JSON serialization. It provides utilities like toJSONFilter for creating JSON-to-JSON filters and supports the integration of various diagram types—including blockdiag, actdiag, nwdiag, rackdiag, packetdiag, and seqdiag—directly within Markdown using fenced code blocks.
You can install pandocfilters from PyPI using pip or by installing from the source directory.
pip install pandocfiltersOr from source:
python setup.py installPandoc filters are pipes that read a JSON serialization of the Pandoc AST from stdin, transform it, and write it to stdout. You can use them with Pandoc in two ways:
Using pipes:
pandoc -t json -s | ./your_filter.py | pandoc -f json
Using the --filter (or -F) flag:
pandoc --filter ./your_filter.py -s
pandoc --filter ./caps.py -sBy default, filters using get_filename4code create a directory named ...-images in the current directory to cache temporary files. This directory is not automatically removed.
To force the creation of a temporary directory that is removed at the end of execution, set the environment variable PANDOCFILTER_CLEANUP to any non-empty value (e.g., 1).
export PANDOCFILTER_CLEANUP=1You can provide configuration options for diagrams (like caption and width) using braced syntax immediately following the code block identifier. This is useful for controlling how the diagram is rendered and captioned in the output.
Example with options:
blockdiag {
A -> B -> C -> D;
A -> E -> F -> G;
}DRY Tip: If you are using the braced syntax, you do not need to include the diagram type (e.g., blockdiag { ... }) inside the code block itself; you can just provide the diagram definition.
A -> B -> C -> D;
A -> E -> F -> G;Use seqdiag for sequence diagrams.
Controlling Order: By default, seqdiag sorts elements by the order they appear. To explicitly define the order of participants (e.g., browser; database; webserver;), list them at the beginning of the seqdiag { ... } block.
seqdiag {
# define order of elements
# seqdiag sorts elements by order they appear
browser; database; webserver;
browser -> webserver [label = "GET /index.html"];
browser <-- webserver;
browser -> webserver [label = "POST /blog/comment"];
webserver -> database [label = "INSERT comment"];
webserver <-- database;
browser <-- webserver;
}You can embed blockdiag diagrams directly in Markdown using fenced code blocks. The filter supports standard ASCII characters as well as Unicode characters (e.g., Ä, ü, ö).
To use the diagram type, specify it in the language identifier of the code block:
blockdiag {
A -> B -> C -> D;
A -> E -> F -> G;
}Use nwdiag for network diagrams. It allows you to define networks and assign IP addresses to specific nodes within those networks.
nwdiag {
network dmz {
address = "210.x.x.x/24"
web01 [address = "210.x.x.1"];
web02 [address = "210.x.x.2"];
}
network internal {
address = "172.x.x.x/24";
web01 [address = "172.x.x.1"];
web02 [address = "172.x.x.2"];
db01;
db02;
}
}Use rackdiag to represent server racks. You can define multiple rack blocks, specify their size (e.g., 16U), and list items with their unit height (e.g., [2U]).
rack {
16U;
1: UPS [2U];
3: DB Server
4: Web Server
}
rack {
12U;
1: UPS [2U];
3: DB Server
}Use actdiag for activity diagrams. You can define lanes (e.g., lane user) to organize actions within the diagram.
write -> convert -> image
lane user {
label = "User"
write [label = "Writing reST"];
image [label = "Get diagram IMAGE"];
}
lane actdiag {
convert [label = "Convert reST to Image"];
}Use packetdiag to visualize packet structures, such as TCP headers. You can define bit ranges (e.g., 0-15) and set global properties like colwidth and node_height.
colwidth = 32
node_height = 72
0-15: Source Port
16-31: Destination Port
32-63: Sequence Number
64-95: Acknowledgment Number
96-99: Data Offset
100-105: Reserved
106: URG [rotate = 270]
107: ACK [rotate = 270]
108: PSH [rotate = 270]
109: RST [rotate = 270]
110: SYN [rotate = 270]
111: FIN [rotate = 270]
112-127: Window
128-143: Checksum
144-159: Urgent Pointer
160-191: (Options and Padding)
192-223: data [colheight = 3]Most users only need toJSONFilter(action). This function generates a JSON-to-JSON filter that reads from stdin and writes to stdout.
An action is a function with the signature action(key, value, format, meta):
key: The type of the pandoc object (e.g., 'Str', 'Para').value: The contents of the object (e.g., a string for 'Str', a list of inline elements for 'Para').format: The target output format.meta: The document's metadata.Action Return Values:
None: The object remains unchanged.pandoc object: Replaces the original object.list of pandoc objects: Replaces the original object; the list is spliced into the parent. An empty list deletes the object.#!/usr/bin/env python
from pandocfilters import toJSONFilter, Str
def caps(key, value, format, meta):
if key == 'Str':
return Str(value.upper())
if __name__ == "__main__":
toJSONFilter(caps)Due to changes in how Pandoc handles attributes, ensure you use the correct version of pandocfilters for your Pandoc version:
pandocfilters <= 1.2.4.pandocfilters >= 1.3.0.pandocfilters 1.4.0 supports both old and new JSON formats.