Beispiel #1
0
def convert_markdown(markdown_source,
                     site_navigation=None,
                     extensions=(),
                     strict=False):
    """
    Convert the Markdown source file to HTML content, and additionally
    return the parsed table of contents, and a dictionary of any metadata
    that was specified in the Markdown file.

    `extensions` is an optional sequence of Python Markdown extensions to add
    to the default set.
    """

    # Prepend a table of contents marker for the TOC extension
    markdown_source = toc.pre_process(markdown_source)

    # Generate the HTML from the markdown source
    builtin_extensions = ['meta', 'toc', 'tables', 'fenced_code']
    mkdocs_extensions = [
        RelativePathExtension(site_navigation, strict),
    ]
    extensions = builtin_extensions + mkdocs_extensions + list(extensions)
    md = markdown.Markdown(extensions=extensions)
    html_content = md.convert(markdown_source)
    meta = md.Meta

    # Strip out the generated table of contents
    (html_content, toc_html) = toc.post_process(html_content)

    # Post process the generated table of contents into a data structure
    table_of_contents = toc.TableOfContents(toc_html)

    return (html_content, table_of_contents, meta)
Beispiel #2
0
def convert_markdown(markdown_source,
                     site_navigation=None,
                     extensions=(),
                     strict=False):
    """
    Convert the Markdown source file to HTML content, and additionally
    return the parsed table of contents, and a dictionary of any metadata
    that was specified in the Markdown file.

    `extensions` is an optional sequence of Python Markdown extensions to add
    to the default set.
    """

    # Generate the HTML from the markdown source
    builtin_extensions = ['meta', 'toc', 'tables', 'fenced_code']
    mkdocs_extensions = [
        RelativePathExtension(site_navigation, strict),
    ]
    extensions = builtin_extensions + mkdocs_extensions + list(extensions)
    md = markdown.Markdown(extensions=extensions)
    html_content = md.convert(markdown_source)

    # On completely blank markdown files, no Meta or tox properties are added
    # to the generated document.
    meta = getattr(md, 'Meta', {})
    toc_html = getattr(md, 'toc', '')

    # Post process the generated table of contents into a data structure
    table_of_contents = toc.TableOfContents(toc_html)

    return (html_content, table_of_contents, meta)
Beispiel #3
0
def convert_markdown(markdown_source,
                     site_navigation=None,
                     extensions=(),
                     strict=False):
    """
    Convert the Markdown source file to HTML content, and additionally
    return the parsed table of contents, and a dictionary of any metadata
    that was specified in the Markdown file.

    `extensions` is an optional sequence of Python Markdown extensions to add
    to the default set.
    """

    # Generate the HTML from the markdown source
    if isinstance(extensions, dict):
        user_extensions = list(extensions.keys())
        extension_configs = dict([(k, v) for k, v in extensions.items()
                                  if isinstance(v, dict)])
    else:
        user_extensions = list(extensions)
        extension_configs = {}
    builtin_extensions = ['meta', 'toc', 'tables', 'fenced_code']
    mkdocs_extensions = [
        RelativePathExtension(site_navigation, strict),
    ]
    extensions = utils.reduce_list(builtin_extensions + mkdocs_extensions +
                                   user_extensions)

    html_content, table_of_contents, meta = utils.convert_markdown(
        markdown_source, extensions, extension_configs)

    return (html_content, table_of_contents, meta)
Beispiel #4
0
def convert_markdown(markdown_source, config, site_navigation=None):
    """
    Convert the Markdown source file to HTML as per the config and
    site_navigation. Return a tuple of the HTML as a string, the parsed table
    of contents, and a dictionary of any metadata that was specified in the
    Markdown file.
    """

    extensions = [RelativePathExtension(site_navigation, config['strict'])
                  ] + config['markdown_extensions']

    return utils.convert_markdown(markdown_source=markdown_source,
                                  extensions=extensions,
                                  extension_configs=config['mdx_configs'])
Beispiel #5
0
    def render(self, config, site_navigation=None):
        """
        Convert the Markdown source file to HTML as per the config and
        site_navigation.

        """

        extensions = [
            RelativePathExtension(site_navigation, config['strict'])
        ] + config['markdown_extensions']

        md = markdown.Markdown(extensions=extensions,
                               extension_configs=config['mdx_configs'] or {})
        self.content = md.convert(self.markdown)
        self.toc = toc.TableOfContents(getattr(md, 'toc', ''))