Exemple #1
0
def convert_markdown(markdown_source, extensions=()):
    """
    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
    md = markdown.Markdown(
        extensions=['meta', 'toc', 'tables', 'fenced_code'] + list(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)
Exemple #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', 'footnotes']
    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
    toc_html = 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)
Exemple #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)
    md = markdown.Markdown(
        extensions=extensions,
        extension_configs=extension_configs
    )
    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)
Exemple #4
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', ''))
Exemple #5
0
def convert_markdown(markdown_source, extensions=None, extension_configs=None):
    """
    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.
    """
    md = markdown.Markdown(extensions=extensions or [],
                           extension_configs=extension_configs or {})
    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)
Exemple #6
0
 def markdown_to_toc(self, markdown_source):
     markdown_source = toc.pre_process(markdown_source)
     md = markdown.Markdown(extensions=['toc'])
     html_output = md.convert(markdown_source)
     html_output, toc_output = toc.post_process(html_output)
     return toc.TableOfContents(toc_output)
Exemple #7
0
 def markdown_to_toc(self, markdown_source):
     md = markdown.Markdown(extensions=['toc'])
     md.convert(markdown_source)
     toc_output = md.toc
     return toc.TableOfContents(toc_output)