Major development update.

* Updated LICENSE, READMES/METADATA.md and TODO.md
* Added example blog to examples/
* Added preliminary Pygments support for embedding code in pages.
* Add preliminary Wordpress dump importer
* Expansions to template_tools and metadata to support Blog use case.
This commit is contained in:
2019-05-23 17:51:21 -07:00
parent 81532f3462
commit a0c4381c99
142 changed files with 1564 additions and 180 deletions

View File

@ -17,8 +17,8 @@ from typing import Dict, List, cast
from .processchain import ProcessorChains
from .processors.processors import PassthroughException
from .metadata import MetaTree
from .template_tools import file_list, file_name, file_content, file_metadata, time_iso8601
from .template_tools import file_list, file_name, file_content, file_metadata, time_iso8601, file_raw
from .pygments import pygments_get_css, pygments_markup_contents_html
logger = logging.getLogger()
@ -79,18 +79,22 @@ def main() -> int:
"summary": "",
"description": "",
"author": "",
"author_email": ""
"author_email": "",
}
meta_tree = MetaTree(args.root, default_metadata)
file_list_cache = cast(Dict, {})
file_cont_cache = cast(Dict, {})
file_name_cache = cast(Dict, {})
file_raw_cache = cast(Dict, {})
default_metadata["globals"] = {
"get_file_list": file_list(args.root, file_list_cache),
"get_file_name": file_name(args.root, meta_tree, process_chains, file_name_cache),
"get_file_content": file_content(args.root, meta_tree, process_chains, file_cont_cache),
"get_raw": file_raw(args.root, file_raw_cache),
"get_file_metadata": file_metadata(meta_tree),
"get_time_iso8601": time_iso8601("UTC"),
"pygments_get_css": pygments_get_css,
"pygments_markup_contents_html": pygments_markup_contents_html,
}
for root, _, files in os.walk(args.root):

View File

@ -1,5 +1,6 @@
"""Constructs a tree-like object containing the metadata for a given path, and caches said metadata."""
import fnmatch
import logging
import mimetypes
import os
@ -108,11 +109,15 @@ m load .meta (JSON formatted dictionary) for that level, and
# iterate path components from root to target path
comps = [self._root] + rel_path.split("/")
fullpath = ""
ospath = os.path.join(self._root, rel_path)
for pth in comps:
fullpath = os.path.join(fullpath, pth)
st = os.stat(fullpath)
cachekey = fullpath + ".meta"
if os.path.isdir(fullpath):
cachekey = os.path.join(fullpath, ".meta")
else:
cachekey = fullpath + ".meta"
meta = cast(Dict, {})
try:
st_meta = os.stat(cachekey)
@ -126,16 +131,20 @@ m load .meta (JSON formatted dictionary) for that level, and
meta = jstyleson.load(open(cachekey, "r"))
self._cache.put(cachekey, meta, st_meta.st_mtime)
if fullpath == ospath and "wildcard_metadata" in metablob:
for wild in metablob["wildcard_metadata"]:
if fnmatch.fnmatch(pth, wild[0]):
metablob.update(wild[1])
metablob.update(meta)
# return final dict
metablob["dir"], metablob["file_name"] = os.path.split(rel_path)
metablob["file_path"] = rel_path
metablob["uuid"] = uuid.uuid3(
uuid.NAMESPACE_OID, metablob["uuid-oid-root"] + os.path.join(self._root, rel_path)
)
metablob["relpath"] = os.path.relpath("/", "/" + metablob["dir"])
metablob["uuid"] = uuid.uuid3(uuid.NAMESPACE_OID, metablob["uuid-oid-root"] + ospath)
metablob["os-path"], _ = os.path.split(fullpath)
metablob["guessed-type"] = guess_mime(os.path.join(self._root, rel_path))
metablob["guessed-type"] = guess_mime(ospath)
if "mime-type" not in metablob:
metablob["mime-type"] = metablob["guessed-type"]
metablob["stat"] = {}

36
pixywerk2/pygments.py Normal file
View File

@ -0,0 +1,36 @@
"""Map Pygments into the Template API for inclusion in outputs."""
from typing import Optional
import pygments
import pygments.formatters
import pygments.lexers
import pygments.util
import pygments.styles
def pygments_markup_contents_html(input_text: str, file_type: str, style: Optional[str]=None) -> str:
"""Format input string with Pygments and return HTML."""
if style is None:
style = 'default'
style = pygments.styles.get_style_by_name(style)
formatter = pygments.formatters.get_formatter_by_name('html', style=style)
try:
lexer = pygments.lexers.get_lexer_for_filename(file_type)
except pygments.util.ClassNotFound:
try:
lexer = pygments.lexers.get_lexer_by_name(file_type)
except pygments.util.ClassNotFound:
lexer = pygments.lexers.get_lexer_by_mimetype(file_type)
return pygments.highlight(input_text, lexer, formatter)
def pygments_get_css(style: Optional[str]=None) -> str:
"""Return the CSS styles associated with a particular style definition."""
if style is None:
style = 'default'
style = pygments.styles.get_style_by_name(style)
formatter = pygments.formatters.get_formatter_by_name('html', style=style)
return formatter.get_style_defs()

View File

@ -51,6 +51,14 @@ def file_name(root: str, metatree: MetaTree, processor_chains: ProcessorChains,
return get_file_name
def file_raw(root: str, contcache: Dict) -> Callable:
def get_raw(file_name: str) -> str:
if file_name in contcache:
return contcache[file_name]
with open(os.path.join(root, file_name), 'r', encoding="utf-8") as f:
return f.read()
return get_raw
def file_content(root: str, metatree: MetaTree, processor_chains: ProcessorChains, contcache: Dict) -> Callable:
def get_file_content(file_name: str) -> Iterable:
@ -59,7 +67,7 @@ def file_content(root: str, metatree: MetaTree, processor_chains: ProcessorChain
metadata = metatree.get_metadata(file_name)
chain = processor_chains.get_chain_for_filename(os.path.join(root, file_name), ctx=metadata)
contcache[file_name] = chain.output
return chain.output
return unicode(chain.output)
return get_file_content