Exemplo n.º 1
0
def content_markdown(base_url,
                     link_template=_LINK_TEMPLATE,
                     nolink_template=_NOLINK_TEMPLATE,
                     transclude_template=_TRANSCLUDE_TEMPLATE):
    """
    Render markdown and wikilinks.

    Also annotates doc meta with:

    - A summary
    - A list of links and backlinks.

    Example:

        Write _markdown_ like normal.

        - List item
        - List item
        - List item

        [[Wikilinks]] also work. They will be rendered as <a class="wikilink">Wikilinks</a>.

        [[Transclusion wikilink]]

        If you put a wikilink on it's own line, as above, it will be rendered as a rich snippet (transclude).
    """
    return compose(
        markdowntools.content,
        content_wikilinks(base_url, link_template, nolink_template,
                          transclude_template), annotate_links,
        summary_markdown)
Exemplo n.º 2
0
def rel_page_permalink(tlds):
    """
    Sets nice path that keeps original directory structure, relative to
    some top-level path.

        path/to/some/file.md

    Where `tlds` is "path/to", becomes:

        some/file/index.html
    """
    return compose(with_ext_html, nice_path, relative_to(tlds))
Exemplo n.º 3
0
    def test_1(self):
        def a(s):
            return s + "a"

        def b(s):
            return s + "b"

        def c(s):
            return s + "c"

        abc = compose(c, b, a)
        s = abc("_")
        self.assertEqual(s, "_abc")
Exemplo n.º 4
0
def content_html(base_url,
                 link_template=_LINK_TEMPLATE,
                 nolink_template=_NOLINK_TEMPLATE,
                 transclude_template=_TRANSCLUDE_TEMPLATE):
    """
    Render html (wrap bare lines with paragraphs) and wikilinks.

    Also annotates doc meta with:

    - A summary
    - A list of links and backlinks.

    Example:

        Bare lines are wrapped in paragraphs.

        Blank spaces are ignored.

        You can use HTML like <b>bold</b>, or <i>italic</i> text.

        Lines with <div>block els</div> will not get wrapped.

            <div>
                If you indent a line, it will not get wrapped
                in a paragraph
            </div>

        [[Wikilinks]] also work. They will be rendered as <a class="wikilink">Wikilinks</a>.

        [[Transclusion wikilink]]

        If you put a wikilink on it's own line, as above, it will be rendered as a rich snippet (transclude).

    """
    return compose(
        html.content,
        content_wikilinks(base_url, link_template, nolink_template,
                          transclude_template), annotate_links, summary_html)
Exemplo n.º 5
0
    """Remove funky characters that don't belong in a URL."""
    return re.sub(_STRANGE_CHAR_PATTERN, "", text)


def _lower(s):
    return s.lower()


def _strip(s):
    return s.strip()


to_slug = compose(
    _space_to_dash,
    _remove_strange_chars,
    _lower,
    _strip,
    str
)


def to_title(pathlike):
    """
    Read a pathlike as a title. This takes the stem and removes any
    leading "_".
    """
    stem = PurePath(pathlike).stem
    return stem[1:] if stem.startswith("_") else stem


def is_file_like(pathlike):
Exemplo n.º 6
0
from collections import namedtuple
from lettersmith import doc as Doc
from lettersmith import docs as Docs
from lettersmith import stub as Stub
from lettersmith import edge as Edge
from lettersmith import html
from lettersmith import wikimarkup
from lettersmith import markdowntools
from lettersmith.path import to_slug, to_url
from lettersmith.util import index_sets, expand
from lettersmith.lens import lens_compose, key, get, put, over
from lettersmith.func import compose, composable
from lettersmith.stringtools import first_sentence

# Read a summary from an HTML text blob
read_summary_html = compose(first_sentence, wikimarkup.strip_wikilinks,
                            html.strip_html)

# Read a summary from a markdown text blob
read_summary_markdown = compose(first_sentence, wikimarkup.strip_wikilinks,
                                markdowntools.strip_markdown)


def _summary(read_summary):
    """
    Render a summary from content using `read_summary` and set it on
    `doc.meta["summary"]`.

    If doc already has a `doc.meta["summary"]` it will leave it alone.
    """
    def summary(docs):
        for doc in docs:
Exemplo n.º 7
0
from markdown import markdown as md
from mdx_gfm import GithubFlavoredMarkdownExtension
from lettersmith.html import strip_html
from lettersmith import docs as Docs
from lettersmith.func import compose


def markdown(s):
    """
    Render markdown on content field.
    """
    return md(s, extensions=(GithubFlavoredMarkdownExtension(), ))


strip_markdown = compose(strip_html, markdown)
content = Docs.renderer(markdown)
Exemplo n.º 8
0
def most_recent(n):
    """
    Get most recent `n` docs, ordered by created.
    """
    return compose(query.takes(n), sort_by_created)
Exemplo n.º 9
0
            yield doc


@composable
def filter_siblings(docs, id_path):
    """
    Filter a list of dicts with `id_path`, returning a generator
    yielding only those dicts who's id_path is a sibling to
    `id_path`.
    """
    for doc in docs:
        if pathtools.is_sibling(id_path, doc.id_path):
            yield doc


remove_drafts = query.rejects(compose(pathtools.is_draft, Doc.id_path.get))
remove_index = query.rejects(compose(pathtools.is_index, Doc.id_path.get))
dedupe = query.dedupes(Doc.id_path.get)
uplift_frontmatter = query.maps(Doc.uplift_frontmatter)
sort_by_created = query.sorts(Doc.created.get, reverse=True)
sort_by_modified = query.sorts(Doc.modified.get, reverse=True)
sort_by_title = query.sorts(Doc.title.get)
autotemplate = query.maps(Doc.autotemplate)
with_ext_html = query.maps(Doc.with_ext_html)


def most_recent(n):
    """
    Get most recent `n` docs, ordered by created.
    """
    return compose(query.takes(n), sort_by_created)
Exemplo n.º 10
0
    - yy: the 2-digit year
    - yyyy: the 4-digit year
    - mm: the 2-digit month
    - dd: the 2-digit day
    """
    return query.maps(doc_permalink(permalink_template))


post_permalink = permalink("{yyyy}/{mm}/{dd}/{stem}/index.html")
post_permalink.__doc__ = """
Sets typical blog date-based output_path on docs:

    2019/12/01/my-post/index.html
"""

page_permalink = compose(with_ext_html, nice_path)
page_permalink.__doc__ = """
Sets nice path on doc, retaining original directory path, but
giving it a nice URL, and an .html extension.

    path/to/some/file.md

Becomes:

    path/to/some/file/index.html

"""


def rel_page_permalink(tlds):
    """
Exemplo n.º 11
0
def update_meta(doc, patch):
    """
    Mix keys from `patch` into `doc.meta`.
    """
    return update(meta, mix, doc, patch)


def with_ext_html(doc):
    """
    Set doc extension to ".html"
    """
    return put(ext, doc, ".html")


output_tld = compose(pathtools.tld, output_path.get)
id_tld = compose(pathtools.tld, id_path.get)


_infer_template = compose(
    pathtools.ext_html,
    pathtools.to_slug,
    id_tld
)


def autotemplate(doc):
    """
    Set template based on top-level directory in doc's id_path.

    E.g. if top-level-directory is "posts", template gets set to "posts.html".