jgm/pandocfilters — pull request #13 by aaren (2014-11-16 18:25:50+00:00)
Title: Attribute class: parse / emit formatted attributes

This implements a powerful Attribute object that allows for easy
conversion between different attribute representations.

I've found this useful when writing more advanced filters and have it on pypi
already (`pip install pandoc-attributes`). Maybe it would be useful here? I've
included fairly comprehensive tests.

If nothing else, it provides an easy way to use pandoc attributes:

attr = Attributes(['id', ['class1', 'class2'], [['key', 'value']]])

attr.id
>>> 'id'

attr.classes
>>> ['class1', 'class2']

attr['key']
>>> 'value'

The original `pandocfilters.attributes` function has the same behaviour
as before:

attributes({'id': 'id', 'classes': ['class1', 'class2'], 'key'='value'})
>>> ['id', ['class1', 'class2'], [['key', 'value']]]

`Attributes` can read and write attributes in any of these formats:
- markdown
- html
- dictionary
- pandoc

usage:

from pandocfilters import Attributes

attrs = '#id .class1 .class2 key=value'
attributes = Attributes(attrs, format='markdown')

attributes.to_markdown()
>>> '{#id .class1 .class2 key=value}'

attributes.to_dict()
>>> {'id': 'id', 'classes': ['class1', 'class2'], 'key'='value'}

attributes.to_html()
>>> id="id" class="class1 class2" key='value'

attributes.to_pandoc()
>>> ['id', ['class1', 'class2'], [['key', 'value']]]

attributes.id
>>> 'id'

attributes.classes
>>> ['class1', 'class2']

attributes.kvs
>>> OrderedDict([('key', 'value')])

attributes['id']
>>> 'id'

attributes['classes']
>>> ['class1', 'class2']

attributes['key']
>>> 'value'

attributes['new'] = 'another value'
attributes['new']
>>> 'another value'