Python markdown.Markdown() Examples

The following are 30 code examples of markdown.Markdown(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module markdown , or try the search function .
Example #1
Source File: docnado.py    From docnado with MIT License 6 votes vote down vote up
def build_nav_menu(meta_cache):
    """ Given a cache of Markdown `Meta` data, compile a structure that can be
    used to generate the NAV menu.
    This uses the `nav: Assembly>Bench>Part` variable at the top of the Markdown file.
    """
    root = NavItem('root', 0)

    # Pre-sort the nav-items alphabetically by nav-string. This will get overridden with the arange()
    # function, but this avoids-un arranged items moving round between page refreshes due to Dicts being 
    # unordered.
    sorted_meta_cache = sorted(
        meta_cache.items(),
        key = lambda items: items[1].get('nav', [''])[0].split('>')[-1] # Sort by the last part of the nav string for each page.
        )

    for path, meta in sorted_meta_cache:
        nav_str = meta.get('nav', [None])[0]
        nav_chunks = parse_nav_string(nav_str)
        node = root
        for name, weight in nav_chunks:
            n = NavItem(name, weight)
            node = node.add(n)
        node.bind(meta=meta, link=path)
    root.arrange()
    return root 
Example #2
Source File: views.py    From blog with Apache License 2.0 6 votes vote down vote up
def get_object(self):
        obj = super(DetailView, self).get_object()
        # 设置浏览量增加时间判断,同一篇文章两次浏览超过半小时才重新统计阅览量,作者浏览忽略
        u = self.request.user
        ses = self.request.session
        the_key = 'is_read_{}'.format(obj.id)
        is_read_time = ses.get(the_key)
        if u != obj.author:
            if not is_read_time:
                obj.update_views()
                ses[the_key] = time.time()
            else:
                now_time = time.time()
                t = now_time - is_read_time
                if t > 60 * 30:
                    obj.update_views()
                    ses[the_key] = time.time()
        md = markdown.Markdown(extensions=[
            'markdown.extensions.extra',
            'markdown.extensions.codehilite',
            TocExtension(slugify=slugify),
        ])
        obj.body = md.convert(obj.body)
        obj.toc = md.toc
        return obj 
Example #3
Source File: test_markdown_exts.py    From statik with MIT License 6 votes vote down vote up
def test_yaml_extension(self):
        md = Markdown(
            extensions=[MarkdownYamlMetaExtension()]
        )
        html = md.convert(TEST_VALID_CONTENT1)
        self.assertEqual("<p>This is where the <strong>Markdown</strong> content will go.</p>", html.strip())
        expected_meta = {
            'some-variable': 'Value',
            'other-variable': 'Another value',
            'yet-another': ['this', 'is', 'a', 'list']
        }
        self.assertEqual(expected_meta, md.meta)

        html = md.convert(TEST_VALID_CONTENT2)
        self.assertEqual("<p>This is just some plain old <strong>Markdown</strong> content, without metadata.</p>", html.strip())
        # no metadata
        self.assertEqual({}, md.meta) 
Example #4
Source File: __init__.py    From zulip with Apache License 2.0 6 votes vote down vote up
def handleMatch(self, match: Match[str]) -> Element:
        rendered = render_tex(match.group('body'), is_inline=True)
        if rendered is not None:
            # We need to give Python-Markdown an ElementTree object, but if we
            # give it one with correctly stored XML namespaces, it will mangle
            # everything when serializing it.  So we play this stupid game to
            # store xmlns as a normal attribute.  :-[
            assert ' zulip-xmlns="' not in rendered
            rendered = rendered.replace(' xmlns="', ' zulip-xmlns="')
            parsed = etree.iterparse(StringIO(rendered))
            for event, elem in parsed:
                if 'zulip-xmlns' in elem.attrib:
                    elem.attrib['xmlns'] = elem.attrib.pop('zulip-xmlns')
                root = elem
            return root
        else:  # Something went wrong while rendering
            span = Element('span')
            span.set('class', 'tex-error')
            span.text = '$$' + match.group('body') + '$$'
            return span 
Example #5
Source File: test_markdown_exts.py    From statik with MIT License 6 votes vote down vote up
def test_permalink_extension(self):
        md = Markdown(
            extensions=[
                MarkdownPermalinkExtension(
                    permalink_text="¶",
                    permalink_class="permalink",
                    permalink_title="Permalink to this headline"
                )
            ]
        )
        html = "<html><body>"+md.convert(TEST_PERMALINK_CONTENT)+"</body></html>"
        tree = ET.fromstring(html)

        for tag_id in range(1, 7):
            heading = tree.findall('./body/h%d' % tag_id)[0]
            self.assertEqual('heading-%d' % tag_id, heading.get('id'))
            link = tree.findall('./body/h%d/a' % tag_id)[0]
            self.assertEqual('#heading-%d' % tag_id, link.get('href'))
            self.assertEqual('permalink', link.get('class'))
            self.assertEqual('Permalink to this headline', link.get('title')) 
Example #6
Source File: test_markdown_exts.py    From statik with MIT License 6 votes vote down vote up
def test_lorem_ipsum_extension(self):
        md = Markdown(
            extensions=[
                MarkdownYamlMetaExtension(),
                MarkdownLoremIpsumExtension()
            ]
        )
        html = "<html>"+md.convert(TEST_LOREM_IPSUM_CONTENT)+"</html>"
        tree = ET.fromstring(html)
        p = tree.findall('./p')[0]
        self.assertEqual(100, lipsum.count_words(p.text))

        p = tree.findall('./p')[1]
        self.assertEqual(5, lipsum.count_sentences(p.text))
        self.assertEqual(3, len(tree.findall('./p')[2:]))

        html = "<html>"+md.convert(TEST_LOREM_IPSUM_SINGLE_INSTANCES)+"</html>"
        tree = ET.fromstring(html)
        p = tree.findall('./p')[0]
        self.assertEqual(1, lipsum.count_words(p.text))

        p = tree.findall('./p')[1]
        self.assertEqual(1, lipsum.count_sentences(p.text))
        self.assertEqual(1, len(tree.findall('./p')[2:])) 
Example #7
Source File: __init__.py    From zulip with Apache License 2.0 6 votes vote down vote up
def zulip_specific_link_changes(self, el: Element) -> Union[None, Element]:
        href = el.get('href')

        # Sanitize url or don't parse link. See linkify_tests in markdown_test_cases for banned syntax.
        href = sanitize_url(self.unescape(href.strip()))
        if href is None:
            return None  # no-op; the link is not processed.

        # Rewrite local links to be relative
        db_data = self.md.zulip_db_data
        href = rewrite_local_links_to_relative(db_data, href)

        # Make changes to <a> tag attributes
        el.set("href", href)

        # Show link href if title is empty
        if not el.text.strip():
            el.text = href

        # Prevent realm_filters from running on the content of a Markdown link, breaking up the link.
        # This is a monkey-patch, but it might be worth sending a version of this change upstream.
        el.text = markdown.util.AtomicString(el.text)

        return el 
Example #8
Source File: test_plantuml.py    From plantuml-markdown with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_arg_source(self):
        """
        Test for the correct parsing of the source argument
        """
        include_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
        self.md = markdown.Markdown(extensions=['markdown.extensions.fenced_code',
                                                'pymdownx.snippets', 'plantuml_markdown'],
                                    extension_configs={
                                        'plantuml_markdown': {
                                            'base_dir': include_path
                                        }
                                    })

        text = self.text_builder.diagram("ignored text").source("included_diag.puml").build()
        self.assertEqual(self._stripImageData(self._load_file('include_output.html')),
                         self._stripImageData(self.md.convert(text))) 
Example #9
Source File: pages.py    From mkdocs with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def path_to_url(self, url):
        scheme, netloc, path, params, query, fragment = urlparse(url)

        if (scheme or netloc or not path or url.startswith('/') or url.startswith('\\')
                or AMP_SUBSTITUTE in url or '.' not in os.path.split(path)[-1]):
            # Ignore URLs unless they are a relative link to a source file.
            # AMP_SUBSTITUTE is used internally by Markdown only for email.
            # No '.' in the last part of a path indicates path does not point to a file.
            return url

        # Determine the filepath of the target.
        target_path = os.path.join(os.path.dirname(self.file.src_path), urlunquote(path))
        target_path = os.path.normpath(target_path).lstrip(os.sep)

        # Validate that the target exists in files collection.
        if target_path not in self.files:
            log.warning(
                "Documentation file '{}' contains a link to '{}' which is not found "
                "in the documentation files.".format(self.file.src_path, target_path)
            )
            return url
        target_file = self.files.get_file_from_path(target_path)
        path = target_file.url_relative_to(self.file)
        components = (scheme, netloc, path, params, query, fragment)
        return urlunparse(components) 
Example #10
Source File: views.py    From blog with Apache License 2.0 6 votes vote down vote up
def get_object(self):
        obj = super(DetailView, self).get_object()
        # 设置浏览量增加时间判断,同一篇文章两次浏览超过半小时才重新统计阅览量,作者浏览忽略
        u = self.request.user
        ses = self.request.session
        the_key = 'is_read_{}'.format(obj.id)
        is_read_time = ses.get(the_key)
        if u != obj.author:
            if not is_read_time:
                obj.update_views()
                ses[the_key] = time.time()
            else:
                now_time = time.time()
                t = now_time - is_read_time
                if t > 60 * 30:
                    obj.update_views()
                    ses[the_key] = time.time()
        md = markdown.Markdown(extensions=[
            'markdown.extensions.extra',
            'markdown.extensions.codehilite',
            TocExtension(slugify=slugify),
        ])
        obj.body = md.convert(obj.body)
        obj.toc = md.toc
        return obj 
Example #11
Source File: __main__.py    From bioforum with MIT License 6 votes vote down vote up
def run():  # pragma: no cover
    """Run Markdown from the command line."""

    # Parse options and adjust logging level if necessary
    options, logging_level = parse_options()
    if not options:
        sys.exit(2)
    logger.setLevel(logging_level)
    console_handler = logging.StreamHandler()
    logger.addHandler(console_handler)
    if logging_level <= WARNING:
        # Ensure deprecation warnings get displayed
        warnings.filterwarnings('default')
        logging.captureWarnings(True)
        warn_logger = logging.getLogger('py.warnings')
        warn_logger.addHandler(console_handler)

    # Run
    markdown.markdownFromFile(**options) 
Example #12
Source File: html.py    From knowledge-repo with Apache License 2.0 6 votes vote down vote up
def _render_markdown(self, skip_headers=False, images_base64_encode=True, urlmappers=[]):
        """
        Returns the `Markdown` instance as well as the rendered html output
        as a tuple: (`Markdown`, str).
        """
        # Copy urlmappers locally so we can modify it without affecting global
        # state
        urlmappers = urlmappers[:]
        if images_base64_encode:
            urlmappers.insert(0, self.base64_encode_image_mapper)

        # proxy posts are assumed to be embeddable links
        if 'proxy' in self.kp.headers:
            return None, '<a href="{0}">Linked Post</a>\n<iframe width=100% height=1000 src="{0}"></iframe>'.format(self.kp.headers['proxy'].strip())

        html = ''
        if not skip_headers:
            html += self.render_headers()

        md = markdown.Markdown(extensions=MARKDOWN_EXTENSIONS)
        html += md.convert(self.kp.read())

        html = self.apply_url_remapping(html, urlmappers)

        return md, html 
Example #13
Source File: tabbed_sections.py    From zulip with Apache License 2.0 6 votes vote down vote up
def generate_content_blocks(self, tab_section: Dict[str, Any], lines: List[str]) -> str:
        tab_content_blocks = []
        for index, tab in enumerate(tab_section['tabs']):
            start_index = tab['start'] + 1
            try:
                # If there are more tabs, we can use the starting index
                # of the next tab as the ending index of the previous one
                end_index = tab_section['tabs'][index + 1]['start']
            except IndexError:
                # Otherwise, just use the end of the entire section
                end_index = tab_section['end_tabs_index']

            content = '\n'.join(lines[start_index:end_index]).strip()
            tab_content_block = DIV_TAB_CONTENT_TEMPLATE.format(
                data_language=tab['tab_name'],
                # Wrapping the content in two newlines is necessary here.
                # If we don't do this, the inner Markdown does not get
                # rendered properly.
                content=f'\n{content}\n')
            tab_content_blocks.append(tab_content_block)
        return '\n'.join(tab_content_blocks) 
Example #14
Source File: test.py    From plueprint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main():
    m = Markdown(extensions=["plueprint"])
    m.set_output_format("apiblueprint")
    tests_dir = os.path.join(os.path.dirname(__file__), "api-blueprint",
                             "examples")
    skip_number = 0
    index = 0
    for doc in sorted(os.listdir(tests_dir)):
        if os.path.splitext(doc)[1] != ".md" or doc == "README.md":
            continue
        index += 1
        if index <= skip_number:
            continue
        with codecs.open(os.path.join(tests_dir, doc), "r", "utf-8") as fin:
            txt = fin.read()
        api = m.convert(txt)
        print("-- %s --" % doc)
        print(api)
        try:
            api[">"].print_resources()
        except KeyError:
            pass
        print("Actions:")
        for action in api["/"]:
            print(action) 
Example #15
Source File: __main__.py    From plueprint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-o", "--output", help="Output pickle file path",
                        default=None)
    parser.add_argument("input", help="Input API Blueprint file")
    args = parser.parse_args()
    with codecs.open(args.input, "r", "utf-8") as fin:
        txt = fin.read()
    m = Markdown(extensions=["plueprint"])
    m.set_output_format("apiblueprint")
    api = m.convert(txt)
    if args.output is not None:
        with open(args.output, "wb") as fout:
            pickle.dump(api, fout, protocol=-1)
    else:
        print(api)
        print("Resource groups:")
        for g in api:
            print("  %s" % g)
            print("  Resources:")
            for r in g:
                print("    %s" % r)
                print("    Actions:")
                for a in r:
                    print("      %s" % a) 
Example #16
Source File: models.py    From django-mptt-comments with MIT License 6 votes vote down vote up
def pre_save(self, model_instance, add):
        instance = model_instance
        value = None

        for attr in self.source.split('.'):
            value = getattr(instance, attr)
            instance = value

        if value is None or value == '':
            return value

        extensions = [
            'markdown.extensions.extra',
            'markdown.extensions.codehilite',
        ]

        md = markdown.Markdown(extensions=extensions)
        value = md.convert(value)
        value = bleach_value(value)
        value = bleach.linkify(value)

        setattr(model_instance, self.attname, value)

        return value 
Example #17
Source File: blogrenderer.py    From blask with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, name, md, content):
        """
        Default constructor
        :param name: name of the post
        :param md: Markdown information
        :param content: String with the Content in HTML.
        """
        self.content = md.convert(content)
        self.name = name
        meta = md.Meta
        if meta:
            if "date" in meta.keys():
                self.date = datetime.strptime(meta["date"][0], "%Y-%m-%d")
            if "tags" in meta.keys():
                self.tags = meta["tags"][0].split(",")
            if "template" in meta.keys():
                self.template = meta["template"][0]
            if "category" in meta.keys():
                self.category = meta["category"][0]
            if "author" in meta.keys():
                self.author = meta["author"][0]
            if "title" in meta.keys():
                self.title = meta["title"][0] 
Example #18
Source File: plugin.py    From mknotebooks with MIT License 6 votes vote down vote up
def on_page_content(
        self, html: str, page: Page, config: MkDocsConfig, files: Files
    ):
        if str(page.file.abs_src_path).endswith("ipynb") and not (
            "markdown.extensions.md_in_html" in config["markdown_extensions"]
            or "markdown.extensions.extra" in config["markdown_extensions"]
        ):
            log.debug(f"Re-rendering page with markdown in divs: {page}")
            extensions = [
                _RelativePathExtension(page.file, files),
                "markdown.extensions.md_in_html",
            ] + config["markdown_extensions"]
            md = markdown.Markdown(
                extensions=extensions, extension_configs=config["mdx_configs"] or {}
            )
            html = md.convert(page.markdown)
            page.toc = get_toc(getattr(md, "toc_tokens", []))

        return html 
Example #19
Source File: __init__.py    From mkdocstrings with ISC License 6 votes vote down vote up
def update_env(self, md: Markdown, config: dict) -> None:
        """
        Update the Jinja environment.

        Arguments:
            md: The Markdown instance. Useful to add functions able to convert Markdown into the environment filters.
            config: Configuration options for `mkdocs` and `mkdocstrings`, read from `mkdocs.yml`. See the source code
                of [mkdocstrings.plugin.MkdocstringsPlugin.on_config][] to see what's in this dictionary.
        """
        # Re-instantiate md: see https://github.com/tomchristie/mkautodoc/issues/14
        md = Markdown(extensions=config["mdx"], extensions_configs=config["mdx_configs"])

        def convert_markdown(text):
            return do_mark_safe(md.convert(text))

        self.env.filters["convert_markdown"] = convert_markdown 
Example #20
Source File: extension.py    From mkdocstrings with ISC License 6 votes vote down vote up
def atomic_brute_cast(tree: Element) -> Element:
    """
    Cast every node's text into an atomic string to prevent further processing on it.

    Since we generate the final HTML with Jinja templates, we do not want other inline or tree processors
    to keep modifying the data, so this function is used to mark the complete tree as "do not touch".

    Reference: issue [Python-Markdown/markdown#920](https://github.com/Python-Markdown/markdown/issues/920).

    On a side note: isn't `atomic_brute_cast` such a beautiful function name?

    Arguments:
        tree: An XML node, used like the root of an XML tree.

    Returns:
        The same node, recursively modified by side-effect. You can skip re-assigning the return value.
    """
    if tree.text:
        tree.text = AtomicString(tree.text)
    for child in tree:
        atomic_brute_cast(child)
    return tree 
Example #21
Source File: html.py    From knowledge-repo with Apache License 2.0 5 votes vote down vote up
def run(self, lines):
        """ Parse Meta-Data and store in Markdown.Meta. """
        cnt = 0
        for i, line in enumerate(lines):
            if line.strip() == '---':
                cnt = cnt + 1
            if cnt == 2:
                break
        return lines[i + 1:] 
Example #22
Source File: docnado.py    From docnado with MIT License 5 votes vote down vote up
def build_meta_cache(root):
    """ Recursively search for Markdown files and build a cache of `Meta`
    from metadata in the Markdown.
    :param root: str: The path to search for files from.
    """
    doc_files = glob.iglob(root + '/**/*.md', recursive=True)

    def _meta(path):
        with open(path, 'r', encoding='utf-8') as f:
            md = markdown.Markdown(extensions=mdextensions)
            md.page_root = os.path.dirname(path)
            Markup(md.convert(f.read()))
            return md.Meta if hasattr(md, 'Meta') else None

    doc_files_meta = {os.path.relpath(path, start=root): _meta(path) for path in doc_files}
    doc_files_meta = {path: value for path, value in doc_files_meta.items() if value is not None}

    # If a nav filter is set, exclude relevant documents.
    # This takes the comma separated string supplied to `nav_limit`
    # and excludes certain documents if they are NOT in this list.
    global CMD_ARGS
    if CMD_ARGS.nav_limit:
        nav_filters = CMD_ARGS.nav_limit.split(',')
        nav_filters = [nav_filter.strip().lower() for nav_filter in nav_filters]
        nav_filters = [nav_filter for nav_filter in nav_filters if nav_filter]

        def _should_include(doc_meta):
            nav_strings = [nav.lower() for nav in doc_meta.get('nav', [])]
            return any([y.startswith(x) for x in nav_filters for y in nav_strings])
        doc_files_meta = {path: value for path, value in doc_files_meta.items() if _should_include(value)}

    return doc_files_meta 
Example #23
Source File: __init__.py    From 2buntu-blog with Apache License 2.0 5 votes vote down vote up
def cmarkdown(value):
    """
    Render Markdown with our custom processors.

    :param value: raw markdown
    :returns: rendered HTML
    """
    return mark_safe(cm.convert(value)) 
Example #24
Source File: config_options.py    From mkdocs with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def run_validation(self, value):
        if not isinstance(value, (list, tuple)):
            raise ValidationError('Invalid Markdown Extensions configuration')
        extensions = []
        for item in value:
            if isinstance(item, dict):
                if len(item) > 1:
                    raise ValidationError('Invalid Markdown Extensions configuration')
                ext, cfg = item.popitem()
                extensions.append(ext)
                if cfg is None:
                    continue
                if not isinstance(cfg, dict):
                    raise ValidationError('Invalid config options for Markdown '
                                          "Extension '{}'.".format(ext))
                self.configdata[ext] = cfg
            elif isinstance(item, str):
                extensions.append(item)
            else:
                raise ValidationError('Invalid Markdown Extensions configuration')

        extensions = utils.reduce_list(self.builtins + extensions)

        # Confirm that Markdown considers extensions to be valid
        try:
            markdown.Markdown(extensions=extensions, extension_configs=self.configdata)
        except Exception as e:
            raise ValidationError(e.args[0])

        return extensions 
Example #25
Source File: base.py    From mkdocs with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_markdown_toc(markdown_source):
    """ Return TOC generated by Markdown parser from Markdown source text. """
    md = markdown.Markdown(extensions=['toc'])
    md.convert(markdown_source)
    return md.toc_tokens 
Example #26
Source File: pages.py    From mkdocs with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def render(self, config, files):
        """
        Convert the Markdown source file to HTML as per the config.
        """

        extensions = [
            _RelativePathExtension(self.file, files)
        ] + config['markdown_extensions']

        md = markdown.Markdown(
            extensions=extensions,
            extension_configs=config['mdx_configs'] or {}
        )
        self.content = md.convert(self.markdown)
        self.toc = get_toc(getattr(md, 'toc_tokens', [])) 
Example #27
Source File: pages.py    From mkdocs with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _set_title(self):
        """
        Set the title for a Markdown document.

        Check these in order and use the first that returns a valid title:
        - value provided on init (passed in from config)
        - value of metadata 'title'
        - content of the first H1 in Markdown content
        - convert filename to title
        """
        if self.title is not None:
            return

        if 'title' in self.meta:
            self.title = self.meta['title']
            return

        title = get_markdown_title(self.markdown)

        if title is None:
            if self.is_homepage:
                title = 'Home'
            else:
                title = self.file.name.replace('-', ' ').replace('_', ' ')
                # Capitalize if the filename was all lowercase, otherwise leave it as-is.
                if title.lower() == title:
                    title = title.capitalize()

        self.title = title 
Example #28
Source File: markdown_parser.py    From cmdbac with Apache License 2.0 5 votes vote down vote up
def parse(text):
    md = Markdown(extensions=["codehilite", "tables", "smarty", "admonition", "toc"])
    md.inlinePatterns["image_link"] = ImageLookupImagePattern(IMAGE_LINK_RE, md)
    html = md.convert(text)
    return html 
Example #29
Source File: python.py    From mkdocstrings with ISC License 5 votes vote down vote up
def update_env(self, md: Markdown, config: dict) -> None:  # noqa: D102 (ignore missing docstring)
        super(PythonRenderer, self).update_env(md, config)
        self.env.trim_blocks = True
        self.env.lstrip_blocks = True
        self.env.keep_trailing_newline = False 
Example #30
Source File: extension.py    From mkdocstrings with ISC License 5 votes vote down vote up
def extendMarkdown(self, md: Markdown) -> None:
        """
        Register the extension.

        Add an instance of our [`AutoDocProcessor`][mkdocstrings.extension.AutoDocProcessor] to the Markdown parser.

        Args:
            md: A `markdown.Markdown` instance.
        """
        md.registerExtension(self)
        processor = AutoDocProcessor(md.parser, md, self._config)
        md.parser.blockprocessors.register(processor, "mkdocstrings", 110)