git @ Cat's Eye Technologies Feedmark / 93582d7
Properties in OrderedDict; --ordered-json serializes them as list. Chris Pressey 6 years ago
3 changed file(s) with 47 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
2626 )
2727 argparser.add_argument('--htmlized-json', action='store_true',
2828 help='When outputting JSON, convert Markdown fields (preamble, section bodies, etc) to HTML5'
29 )
30 argparser.add_argument('--ordered-json', action='store_true',
31 help='When outputting JSON, generate properties as lists that preserve the order '
32 'from the source Feedmark document, instead of as unordered objects'
2933 )
3034
3135 argparser.add_argument('--output-links', action='store_true',
139143 if options.output_json:
140144 json_options = {
141145 'htmlize': options.htmlized_json,
146 'ordered': options.ordered_json,
142147 }
143148 output_json = {
144149 'documents': [d.to_json_data(**json_options) for d in documents]
11 # encoding: UTF-8
22
33 from datetime import datetime
4 from collections import OrderedDict
45 import re
56
67 from feedmark.formats.markdown import markdown_to_html5, markdown_to_html5_deep
3132 class Document(object):
3233 def __init__(self, title):
3334 self.title = title
34 self.properties = {}
35 self.properties = OrderedDict()
3536
3637 self.preamble = None
3738 self.sections = []
5354
5455 def to_json_data(self, **kwargs):
5556
56 htmlize = kwargs.get('htmlize', False)
57 if htmlize:
57 if kwargs.get('htmlize', False):
5858 if 'reference_links' not in kwargs:
5959 kwargs['reference_links'] = self.global_reference_links()
6060 preamble = markdown_to_html5(self.preamble, reference_links=kwargs['reference_links'])
6262 else:
6363 preamble = self.preamble
6464 properties = self.properties
65
66 if kwargs.get('ordered', False):
67 properties_list = []
68 for key, value in properties.items():
69 properties_list.append([key, value])
70 properties = properties_list
71 else:
72 properties = dict(properties)
6573
6674 return {
6775 'filename': self.filename,
7684 def __init__(self, title):
7785 self.document = None
7886 self.title = title
79 self.properties = {}
87 self.properties = OrderedDict()
8088
8189 self.lines = []
8290
8593 if self.document:
8694 s += " of " + str(self.document)
8795 return s
88
89 def add_line(self, line):
90 self.lines.append(line.rstrip())
91
92 def set(self, key, value):
93 self.properties[key] = value
9496
9597 @property
9698 def body(self):
119121
120122 def to_json_data(self, **kwargs):
121123
122 htmlize = kwargs.get('htmlize', False)
123 if htmlize:
124 if kwargs.get('htmlize', False):
124125 body = markdown_to_html5(self.body, reference_links=kwargs['reference_links'])
125126 properties = markdown_to_html5_deep(self.properties, reference_links=kwargs['reference_links'])
126127 else:
127128 body = self.body
128129 properties = self.properties
130
131 if kwargs.get('ordered', False):
132 properties_list = []
133 for key, value in properties.items():
134 properties_list.append([key, value])
135 properties = properties_list
136 else:
137 properties = dict(properties)
129138
130139 return {
131140 'title': self.title,
213222 raise ValueError('Expected property')
214223
215224 def parse_properties(self):
216 properties = {}
225 properties = OrderedDict()
217226 while self.is_blank_line() or self.is_property_line():
218227 if self.is_property_line():
219228 kind, key, val = self.parse_property()
144144 [u'<a href="mall.html">the mall</a>', u'<a href="lumberyard.html">the lumberyard</a>']
145145 )
146146
147 def test_output_unordered_json(self):
148 main(['eg/Ancient Llama Sightings.md', '--output-json'])
149 data = json.loads(sys.stdout.getvalue())
150 properties = data['documents'][0]['properties']
151 self.assertDictEqual(properties, {
152 'author': 'Alfred J. Prufrock',
153 'link-target-url': 'https://github.com/catseye/Feedmark/blob/master/eg/Ancient%20Llama%20Sightings.md',
154 'url': 'http://example.com/old_llama.xml'
155 })
156
157 def test_output_ordered_json(self):
158 main(['eg/Ancient Llama Sightings.md', '--output-json', '--ordered-json'])
159 data = json.loads(sys.stdout.getvalue())
160 properties = data['documents'][0]['properties']
161 self.assertEqual(properties, [
162 [u'author', u'Alfred J. Prufrock'],
163 [u'url', u'http://example.com/old_llama.xml'],
164 [u'link-target-url', u'https://github.com/catseye/Feedmark/blob/master/eg/Ancient%20Llama%20Sightings.md'],
165 ])
166
147167 def test_output_refdex(self):
148168 main(['eg/Recent Llama Sightings.md', 'eg/Ancient Llama Sightings.md', '--output-refdex'])
149169 data = json.loads(sys.stdout.getvalue())