Skip to content

Commit

Permalink
add tabulate-elements
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiocorreia committed Oct 13, 2016
1 parent b0b68e2 commit 64e9ad8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions IDEAS.md
@@ -0,0 +1,4 @@


- An *autofilters* filter that reads the metadata and then run several filters
- This allows for simple executions...
47 changes: 47 additions & 0 deletions filters/tabulate-elements.py
@@ -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)

0 comments on commit 64e9ad8

Please sign in to comment.