Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0b68e2
commit 64e9ad8
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
|
||
- An *autofilters* filter that reads the metadata and then run several filters | ||
- This allows for simple executions... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Count frequency of each element | ||
Sample usage: | ||
> pandoc example.md -F tabulate-elements.py --to=markdown | ||
Element Freqency | ||
------------- ---------- | ||
MetaBool 2 | ||
SoftBreak 1 | ||
Str 46 | ||
MetaInlines 18 | ||
RawInline 7 | ||
Doc 1 | ||
MetaBlocks 1 | ||
Emph 1 | ||
MetaMap 2 | ||
Space 24 | ||
Para 2 | ||
MetaList 2 | ||
""" | ||
|
||
from collections import Counter | ||
import panflute as pf | ||
|
||
def prepare(doc): | ||
doc.counter = Counter() | ||
|
||
|
||
def action(elem, doc): | ||
doc.counter[elem.tag] += 1 | ||
|
||
|
||
def finalize(doc): | ||
c1 = pf.TableCell(pf.Plain(pf.Str("Element"))) | ||
c2 = pf.TableCell(pf.Plain(pf.Str("Freqency"))) | ||
header = pf.TableRow(c1, c2) | ||
rows = [] | ||
for tag in doc.counter: | ||
c1 = pf.TableCell(pf.Plain(pf.Str(tag))) | ||
c2 = pf.TableCell(pf.Plain(pf.Str(str(doc.counter[tag])))) | ||
rows.append(pf.TableRow(c1, c2)) | ||
table = pf.Table(*rows, header=header) | ||
doc.content = [table] # bugbug? | ||
|
||
if __name__ == '__main__': | ||
pf.toJSONFilter(action, prepare, finalize) |