def renderer(render): """ Create a renderer for docs using a string render function. Can be used as a decorator. """ return query.maps(Doc.renderer(render))
def relative_to(tlds): """ Create a function that maps doc output path to be relative to some top-level path. """ rel_to_tlds = pathtools.relative_to(tlds) return query.maps(over_with(Doc.output_path, rel_to_tlds))
def permalink(permalink_template): """ Set output_path on docs using a python string template. For example, here's a typical blog year-based permalink: permalink("{yyyy}/{mm}/{dd}/{stem}/index.html") Available tokens: - name: the doc's file name, including extension (e.g. `name.html`) - stem: the doc's file name, sans extension (e.g. `name`) - suffix: the doc's file extension (e.g. `.html`) - parents: full directory path to the doc, sans file name. - parent: the immediate parent directory - tld: the top-level directory - 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))
def with_template(template): """ Set template, but only if doc doesn't have one already. """ return query.maps(Doc.with_template(template))
""" Tools for working with collections of docs """ from fnmatch import fnmatch from lettersmith import path as pathtools from lettersmith import doc as Doc from lettersmith import query from lettersmith.func import composable, compose from lettersmith.lens import get load = query.maps(Doc.load) def find(glob): """ Load all docs under input path that match a glob pattern. Example: docs.find("posts/*.md") """ return load(pathtools.glob_files(".", glob)) @composable def remove_id_path(docs, id_path): """ Remove docs with a given id_path. """ for doc in docs: if doc.id_path != id_path:
""" Stubs are summary details for a document. """ from collections import namedtuple from lettersmith import doc as Doc from lettersmith.lens import get from lettersmith import query Stub = namedtuple( "Stub", ("id_path", "output_path", "created", "modified", "title", "summary")) Stub.__doc__ = """ A namedtuple for representing a stub. A stub is just a container for the summary details of a document. No content, no meta, no template. Only hashable properties, so stubs can be used in sets. (Note that datetime objects are immutable and hashable.) """ def from_doc(doc): """ Read stub from doc """ return Stub(get(Doc.id_path, doc), get(Doc.output_path, doc), get(Doc.created, doc), get(Doc.modified, doc), get(Doc.title, doc), get(Doc.meta_summary, doc)) stubs = query.maps(from_doc)
the output_path field of the doc. """ output_path = permalink_template.format(**read_doc_permalink(doc)) return put(Doc.output_path, doc, output_path) def relative_to(tlds): """ Create a function that maps doc output path to be relative to some top-level path. """ rel_to_tlds = pathtools.relative_to(tlds) return query.maps(over_with(Doc.output_path, rel_to_tlds)) nice_path = query.maps(over_with(Doc.output_path, pathtools.to_nice_path)) def permalink(permalink_template): """ Set output_path on docs using a python string template. For example, here's a typical blog year-based permalink: permalink("{yyyy}/{mm}/{dd}/{stem}/index.html") Available tokens: - name: the doc's file name, including extension (e.g. `name.html`) - stem: the doc's file name, sans extension (e.g. `name`) - suffix: the doc's file extension (e.g. `.html`)
""" Tools for working with collections of files """ from lettersmith.path import glob_files from lettersmith import file as File from lettersmith import query load = query.maps(File.load) def find(glob): """ Load all docs under input path that match a glob pattern. Example: docs.find("posts/*.md") """ return load(glob_files(".", glob)) to_doc = query.maps(File.to_doc) from_doc = query.maps(File.from_doc)