def body_of(path): info = parse(path) dirname = os.path.dirname(path) body = info['body'] if isinstance(body, SimpleTemplate): body = body.render(**globals()) body = utils.pygmentize_pre_blocks(body) body = body.replace('\n</pre>', '</pre>') def format_title_reference(match): filename = match.group(1) title = title_of(os.path.join(dirname, filename)) return '<i>{}</i>'.format(title) body = re.sub(r'title_of\(([^)]+)\)', format_title_reference, body) return body
def parse(path): source = read_text_file(path) result = {} if path.endswith('.html'): if utils.detect_blogofile(source): heading, info, other_html = utils.convert_blogofile(source) parts = utils.parse_rst(heading) body_html = parts['docinfo'] + other_html body_html = utils.pygmentize_pre_blocks(body_html) body_html = body_html.replace('\n</pre>', '</pre>') result['title'] = utils.html_parser.unescape(parts['title']) result['needs_disqus'] = True result['date'] = info['date'] result['tags'] = info['tags'] else: result['title'] = utils.find_title_in_html(source) body_html = SimpleTemplate(source) result['needs_disqus'] = False result['date'] = None result['tags'] = () result['body'] = body_html result['next_link'] = None result['previous_link'] = None result['tags'] = [tag for tag in result['tags'] if tag] elif path.endswith('.md'): if utils.detect_blogofile(source): heading, info, body = utils.convert_blogofile(source) source = body result['date'] = info['date'] result['title'] = info['title'] result['needs_disqus'] = True else: result['needs_disqus'] = False result['body'] = commonmark(source) elif path.endswith('.rst'): if utils.detect_blogofile(source): heading, info, body = utils.convert_blogofile(source) source = heading + body result['title'] = info['title'] del heading, info, body result['needs_disqus'] = True else: result['needs_disqus'] = False doctree = publish_doctree(source) docinfos = doctree.traverse(nodes.docinfo) docinfo = {c.tagname: str(c.children[0]) for i in docinfos for c in i.children} parts = utils.parse_rst(source) # parts = publish_from_doctree(source, writer_name='html', # settings_overrides={'initial_header_level': 2}) body = parts['docinfo'] + utils.pygmentize_pre_blocks(parts['fragment']) result['body'] = body result['date'] = datetime.strptime( docinfo.get('date'), '%d %B %Y').date() if 'title' not in result: result['title'] = parts['title'] elif path.endswith('.ipynb'): notebook = nbformat.reads(source) docinfo = utils.build_docinfo_block_for_notebook(notebook) exporter = HTMLExporter(config=None, extra_loaders=[dl], filters=filters) exporter.template_file = 'brandon.tpl' #notebook = nbformat.convert(notebook, nbformat.current_nbformat) body, resources = exporter.from_notebook_node(notebook) body = body.replace('\n</pre>', '</pre>') body = body.replace('</h1>', '</h1>\n' + docinfo.rstrip()) date = notebook['metadata'].get('date') if date is not None: date = datetime.strptime(date, '%d %B %Y').date() result['body'] = body result['date'] = date result['needs_disqus'] = notebook['metadata'].get('needs_disqus') result['title'] = (notebook['metadata'].get('name', None) or utils.find_title_in_html(body)) else: raise ValueError('unrecognized path: {}'.format(path)) return result
def parse(path): source = read_text_file(path) result = {} if path.endswith('.html'): if utils.detect_blogofile(source): heading, info, other_html = utils.convert_blogofile(source) parts = utils.parse_rst(heading) body_html = parts['docinfo'] + other_html body_html = utils.pygmentize_pre_blocks(body_html) body_html = body_html.replace('\n</pre>', '</pre>') result['title'] = utils.html_parser.unescape(parts['title']) result['needs_disqus'] = True result['date'] = info['date'] result['tags'] = info['tags'] else: result['title'] = utils.find_title_in_html(source) body_html = SimpleTemplate(source) result['needs_disqus'] = False result['date'] = None result['tags'] = () result['body'] = body_html result['next_link'] = None result['previous_link'] = None result['tags'] = [tag for tag in result['tags'] if tag] elif path.endswith('.md'): if utils.detect_blogofile(source): heading, info, body = utils.convert_blogofile(source) source = body result['date'] = info['date'] result['title'] = info['title'] result['needs_disqus'] = True else: result['needs_disqus'] = False result['body'] = commonmark(source) elif path.endswith('.rst'): if utils.detect_blogofile(source): heading, info, body = utils.convert_blogofile(source) source = heading + body result['title'] = info['title'] del heading, info, body result['needs_disqus'] = True else: result['needs_disqus'] = False doctree = publish_doctree(source) docinfos = doctree.traverse(nodes.docinfo) docinfo = { c.tagname: str(c.children[0]) for i in docinfos for c in i.children } parts = utils.parse_rst(source) # parts = publish_from_doctree(source, writer_name='html', # settings_overrides={'initial_header_level': 2}) body = parts['docinfo'] + utils.pygmentize_pre_blocks( parts['fragment']) result['body'] = body result['date'] = datetime.strptime(docinfo.get('date'), '%d %B %Y').date() if 'title' not in result: result['title'] = parts['title'] elif path.endswith('.ipynb'): notebook = nbformat.reads(source) docinfo = utils.build_docinfo_block_for_notebook(notebook) exporter = HTMLExporter(config=None, extra_loaders=[dl], filters=filters) exporter.template_file = 'brandon.tpl' #notebook = nbformat.convert(notebook, nbformat.current_nbformat) body, resources = exporter.from_notebook_node(notebook) body = body.replace('\n</pre>', '</pre>') body = body.replace('</h1>', '</h1>\n' + docinfo.rstrip()) date = notebook['metadata'].get('date') if date is not None: date = datetime.strptime(date, '%d %B %Y').date() result['body'] = body result['date'] = date result['needs_disqus'] = notebook['metadata'].get('needs_disqus') result['title'] = (notebook['metadata'].get('name', None) or utils.find_title_in_html(body)) else: raise ValueError('unrecognized path: {}'.format(path)) return result