Пример #1
0
def process_markdown(convertibles, input_dir, output_dir, page_template,
                     article_template):
    """Process markdown files.

    This method processes the convertibles, converts them to html and
    saves them to the respective destination paths.

    If a markdown file has a `date` metadata field it will be recognized
    as article otherwise as page.

    Parameters
    ----------
    convertibles : List[Tuple[str, str]]
        relative paths to markdown- (src) html- (dest) files
    input_dir : str
    output_dir : str
    page_template, archive_template : jinja2 template
        templats for pages and articles

    Returns
    -------
    articles, pages : List[Tuple[str, Dict]]

    """
    logger.info("Converting Markdown files...")
    md = markdown_factory()

    articles = []
    pages = []
    for src, dst in convertibles:
        logger.info(f'Processing {src}')
        with open(f'{input_dir}/{src}', 'r') as fh:
            body = fh.read()

        content, meta = convert_markdown(md, body)

        context = dict(content=content)
        context.update(meta)

        # if markdown has date in meta, we treat it as a blog article,
        # everything else are just pages
        if meta and 'date' in meta:
            articles.append((dst, context))
            result = article_template.render(context)
        else:
            pages.append((dst, context))
            result = page_template.render(context)
        with open(f'{output_dir}/{dst}', 'w') as fh_dest:
            fh_dest.write(result)

    # sort articles by date, descending
    articles = sorted(articles, key=lambda x: x[1]['date'], reverse=True)
    return articles, pages
Пример #2
0
def test_smarty_code():
    md = markdown_factory()

    md1 = """
```
this --- is -- a test ...
```
    """
    html, meta = convert_markdown(md, md1)
    assert 'mdash' not in html
    assert 'ndash' not in html
    assert 'hellip' not in html
Пример #3
0
def test_smarty():
    md = markdown_factory()

    md1 = """

this --- is -- a test ...

    """
    html, meta = convert_markdown(md, md1)
    assert 'mdash' in html
    assert 'ndash' in html
    assert 'hellip' in html
Пример #4
0
def test_markdown_factory():
    md = markdown_factory()
    assert isinstance(md, markdown.Markdown)
Пример #5
0
def test_convert_metadata(input_, expected):
    md = markdown_factory()
    _, meta = convert_markdown(md, input_)
    assert expected == meta
Пример #6
0
def test_convert_markdown_links(input_, expected):
    md = markdown_factory()
    html, _ = convert_markdown(md, input_)
    assert expected in html