def to_html(text, breaks=False, safe=False, sourcepos=False, smart=False): r"""Convert markup to HTML. Parameters ---------- text: str Text marked up with `CommonMark <http://commonmark.org>`_. breaks: bool or LineBreaks How line breaks in text will be rendered. If ``True``, ``"soft"``, or :py:attr:`LineBreaks.soft` -- as newlines (``\n``). If ``False`` -- as spaces. If ``"hard"`` or :py:attr:`LineBreaks.hard` -- as ``<br />``\ s. safe: bool If ``True``, replace raw HTML (that was present in ``text``) with HTML comment. sourcepos: bool If ``True``, add ``data-sourcepos`` attribute to block elements (that is, use ``CMARK_OPT_SOURCEPOS``). smart: bool Use :py:data:`~paka.cmark.lowlevel.OPT_SMART`. Returns ------- str HTML """ opts = _add_sourcepos_to_opts( sourcepos, _add_breaks_to_opts(breaks, _lowlevel.OPT_DEFAULT)) opts = _add_smart_to_opts(smart, opts) if safe: opts |= _lowlevel.OPT_SAFE text_bytes = _lowlevel.text_to_c(text) return _lowlevel.text_from_c( _lowlevel.markdown_to_html(text_bytes, len(text_bytes), opts))
def to_xml(text, sourcepos=False, smart=False): """Convert markup to XML. Parameters ---------- text: str Text marked up with `CommonMark <http://commonmark.org>`_. sourcepos: bool If ``True``, add ``sourcepos`` attribute to all block elements (that is, use ``CMARK_OPT_SOURCEPOS``). smart: bool Use :py:data:`~paka.cmark.lowlevel.OPT_SMART`. Returns ------- str XML """ opts = _add_smart_to_opts( smart, _add_sourcepos_to_opts(sourcepos, _lowlevel.OPT_DEFAULT)) text_bytes = _lowlevel.text_to_c(text) parsed = _lowlevel.parse_document(text_bytes, len(text_bytes), opts) root = _ffi.gc(parsed, _lowlevel.node_free) return _lowlevel.text_from_c(_lowlevel.render_xml(root, opts))
def to_latex(text, breaks=False, width=0, smart=False): r"""Convert markup to LaTeX. Parameters ---------- text: str Text marked up with `CommonMark <http://commonmark.org>`_. breaks: bool or LineBreaks How line breaks will be rendered. If ``True``, ``"soft"``, or :py:attr:`LineBreaks.soft` -- as newlines. If ``False`` -- “soft break nodes” (single newlines) are rendered as spaces. If ``"hard"`` or :py:attr:`LineBreaks.hard` -- “soft break nodes” are rendered as ``\\\n``. width: int Wrap width of output by inserting line breaks (default is ``0``—no wrapping). Has no effect if ``breaks`` are ``False``. smart: bool Use :py:data:`~paka.cmark.lowlevel.OPT_SMART`. Returns ------- str LaTeX document. """ opts = _add_smart_to_opts( smart, _add_breaks_to_opts(breaks, _lowlevel.OPT_DEFAULT)) text_bytes = _lowlevel.text_to_c(text) parsed = _lowlevel.parse_document(text_bytes, len(text_bytes), opts) root = _ffi.gc(parsed, _lowlevel.node_free) return _lowlevel.text_from_c(_lowlevel.render_latex(root, opts, width))
def _invoke_html_block_callback(node): """Take HTML block, highlight code (if possible), set new literal.""" def _get_html(body): assert body.tag == "body" for old_el in body.iter("pre"): children = list(old_el) first_child = children[0] if not isinstance(first_child, lxml.html.HtmlComment): continue first_child_match = _match_highlighting_comment( first_child.text.strip()) if not first_child_match: continue fence_info = first_child_match.group("fence_info") code = children[1].text_content() highlighted = callback(fence_info, code) new_el = lxml.html.fragment_fromstring(highlighted) old_el.getparent().replace(old_el, new_el) for el in body: yield lxml.html.tostring(el, encoding="unicode") root = lxml.html.fragment_fromstring(_lowlevel.text_from_c( _lowlevel.node_get_literal(node)), create_parent="body") buf = io.StringIO() buf.writelines(_get_html(root)) new_literal = _lowlevel.text_to_c(buf.getvalue()) assert _lowlevel.node_set_literal(node, new_literal) == 1
def _invoke_code_block_callback(old_node): old_contents = _lowlevel.text_from_c( _lowlevel.node_get_literal(old_node)) fence_info = _lowlevel.text_from_c( _lowlevel.node_get_fence_info(old_node)) new_contents = callback(fence_info, old_contents) if new_contents is None: # can't create new contents, skip return new_node = _lowlevel.node_new(_lowlevel.NODE_HTML_BLOCK) try: assert _lowlevel.node_set_literal( new_node, _lowlevel.text_to_c(new_contents)) == 1 assert _lowlevel.node_replace(old_node, new_node) == 1 except Exception: _lowlevel.node_free(new_node) raise _lowlevel.node_free(old_node)
def get_version(): """Return version of underlying C library. Returns ------- str Version as X.Y.Z. """ return _lowlevel.text_from_c(_lowlevel.version_string())
def render_commonmark(text): """Render CommonMark as HTML. Highlights code in code blocks. """ text_bytes = _lowlevel.text_to_c(text) root = _lowlevel.parse_document(text_bytes, len(text_bytes), _OPTS) try: _substitute_code_blocks(root, _highlight) result = _lowlevel.text_from_c(_lowlevel.render_html(root, _OPTS)) finally: _lowlevel.node_free(root) return result