def test_tableRaises(self): self.assertRaises(ValueError, MarkdownTable, None) self.assertRaises(ValueError, MarkdownTable, "") self.assertRaises(ValueError, MarkdownTable, []) table = MarkdownTable([u"First Header", u"Second", u"3rd", u"Fourth"]) table.addRow(["some longer data", "small", "3"]) self.assertRaises( ValueError, table.addRow, ["some longer data", "small", "3", "Fourth", "Error maker"])
def test_tableRaises(self): self.assertRaises(ValueError, MarkdownTable, None) self.assertRaises(ValueError, MarkdownTable, "") self.assertRaises(ValueError, MarkdownTable, []) table = MarkdownTable([u"First Header", u"Second", u"3rd", u"Fourth"]) table.addRow(["some longer data", "small", "3"]) self.assertRaises( ValueError, table.addRow, ["some longer data", "small", "3", "Fourth", "Error maker"] )
def test_tableHeadersAndRows(self): table = MarkdownTable([u"First Header", u"Second", u"3rd", u"Fourth"]) table.addRow(["some data", "Longer column", "3", "Fourth"]) table.addRow(["some longer data", "small", "3", "Fourth"]) self.md.addTable(table) self.assertEquals(self.md.getStream(), """\ First Header | Second | 3rd | Fourth -----------------|---------------|-----|------- some data | Longer column | 3 | Fourth some longer data | small | 3 | Fourth """)
def test_tableHeadersOnly(self): table = MarkdownTable(["First Header", "Second", "3rd", "Last"]) self.md.addTable(table) self.assertEquals( self.md.getStream(), """\ First Header | Second | 3rd | Last -------------|--------|-----|----- """)
def test_tableHeadersAndRows(self): table = MarkdownTable([u"First Header", u"Second", u"3rd", u"Fourth"]) table.addRow(["some data", "Longer column", "3", "Fourth"]) table.addRow(["some longer data", "small", "3", "Fourth"]) self.md.addTable(table) self.assertEquals( self.md.getStream(), """\ First Header | Second | 3rd | Fourth -----------------|---------------|-----|------- some data | Longer column | 3 | Fourth some longer data | small | 3 | Fourth """)
import os import json import collections from markdownwriter import MarkdownWriter from markdownwriter import MarkdownTable markdown = MarkdownWriter() markdown.addHeader('Netflix categories', 2) markdown.addSimpleLineBreak() def sort(dictionary): return collections.OrderedDict(sorted(dictionary.items())) with open('categories.json', 'r') as f: categories = sort(json.loads(f.read())) for categoryName, categoryValue in categories.iteritems(): if type(categoryValue) is dict: markdown.addHeader(categoryName, 4) table = MarkdownTable(["Subcategory", "URL"]) for subcategoryName, subcategoryId in sort(categoryValue).iteritems(): table.addRow([subcategoryName, 'https://www.netflix.com/browse/genre/%s' % subcategoryId]) markdown.addTable(table) markdown.addSimpleLineBreak() with open('README.md', 'w') as f: f.write(markdown.getStream())
def convert(postman_file, multi_file=True): try: with open(postman_file, 'r') as f: import json postman_json = json.loads(f.read()) # make dir dir_path = "./" + postman_json['name'] if not os.path.isdir(dir_path): os.mkdir(dir_path) from markdownwriter import MarkdownWriter from markdownwriter import MarkdownTable for req in postman_json['requests']: if multi_file: mode = 'w+' md_file_name = req['name'] + '.md' else: mode = 'a+' md_file_name = postman_json['name'] + '.md' with open(os.path.join(dir_path, md_file_name), mode) as f: md = MarkdownWriter() md.addHeader(req['name'], 1) md.addSimpleLineBreak() if len(req['description']) > 0: md.addText('-' + req['description']) md.addSimpleLineBreak() md.addHeader('Resource URL', 2) md.addSimpleLineBreak() md.addSpace() md.addText('-') md.addSpace() md.addText("[" + req['method'] + "] : ") method = req['method'].upper() if method == 'GET': from urlparse import urlparse o = urlparse(req['url']) base_url = o.scheme + "://" + o.netloc md.addLink(base_url, base_url) else: md.addLink(req['url'], req['url']) md.addDoubleLineBreak() if method == 'GET': if o.query: md.addHeader('Request Parameters', 2) md.addSimpleLineBreak() mt = MarkdownTable(['name', 'type', 'example value']) from urlparse import parse_qs qs = parse_qs(o.query) for k, v in qs.items(): mt.addRow([k, 'text', v[0]]) md.addTable(mt) md.addDoubleLineBreak() else: if req['data']: md.addHeader('Request Parameters', 2) md.addSimpleLineBreak() mt = MarkdownTable(['name', 'type', 'example value']) for d in req['data']: mt.addRow([d['key'], d['type'], d['value']]) md.addTable(mt) md.addDoubleLineBreak() md.addHeader('Request example:', 3) if method == 'GET': md.addCodeBlock(req['url']) else: qs = {} for d in req['data']: qs[d['key']] = d['value'] from urllib import urlencode post_qs = urlencode(qs) md.addCodeBlock(req['url'] + "\n" + post_qs) md.addDoubleLineBreak() f.write(md.getStream()) except Exception as e: print e raise e