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 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))
示例#3
0
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