Major code import.

This commit is contained in:
2019-04-14 17:50:56 -07:00
parent 13fb5dac1c
commit 43d40f7fce
40 changed files with 1228 additions and 0 deletions

View File

@ -0,0 +1 @@
# processors metadata here

View File

@ -0,0 +1,32 @@
"""Define a Jinja2 Processor which applies programmable templating to the input stream."""
from typing import Iterable, Optional, Dict, cast
from jinja2 import Environment, FileSystemLoader
from .passthrough import PassThrough
class Jinja2(PassThrough):
"""Pass the input stream through Jinja2 for scritable templating."""
def process(self, input_file: Iterable, ctx: Optional[Dict] = None) -> Iterable:
"""Return an iterable object of the post-processed file.
Arguments:
input_file (iterable): An input stream
ctx (dict, optional): A context object generated from the processor configuration
Returns:
iterable: The post-processed output stream
"""
ctx = cast(Dict, ctx)
template_env = Environment(loader=FileSystemLoader(ctx["templates"]))
template_env.globals.update(ctx["globals"])
template_env.filters.update(ctx["filters"])
tmpl = template_env.from_string("".join([x for x in input_file]))
return tmpl.render(metadata=ctx)
processor = Jinja2

View File

@ -0,0 +1,76 @@
"""Define a Jinja2 processor which embeds the (presumably HTML) input stream into a Page Template
as defined in the ctx metadata (the ``content`` variable is assigned to the input stream and
the target template is rendered)."""
import os
from typing import Iterable, Optional, Dict, cast
from jinja2 import Environment, FileSystemLoader
from .processors import Processor
class Jinja2PageEmbed(Processor):
"""Embed input stream as ``content`` variable in page template defined in context key ``template``."""
def filename(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the filename of the post-processed file.
Arguments:
oldname (str): the previous name for the file.
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new name for the file
"""
return os.path.splitext(oldname)[0] + ".html"
def mime_type(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the mimetype of the post-processed file.
Arguments:
oldname (str): the input filename
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new mimetype of the file after processing
"""
return "text/html"
def process(self, input_file: Iterable, ctx: Optional[Dict] = None) -> Iterable:
"""Return an iterable object of the post-processed file.
Arguments:
input_file (iterable): An input stream
ctx (dict, optional): A context object generated from the processor configuration
Returns:
iterable: The post-processed output stream
"""
ctx = cast(Dict, ctx)
template_env = Environment(loader=FileSystemLoader(ctx["templates"]))
template_env.globals.update(ctx["globals"])
template_env.filters.update(ctx["filters"])
tmpl = template_env.get_template(ctx["template"])
content = "".join([x for x in input_file])
return tmpl.render(content=content, metadata=ctx)
def extension(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the mimetype of the post-processed file.
Arguments:
oldname (str): the input filename
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new extension of the file after processing
"""
return "html"
processor = Jinja2PageEmbed

View File

@ -0,0 +1,68 @@
"""Passthrough progcessor which takes input and returns it."""
import os
from .processors import Processor
from ..utils import guess_mime
from typing import Iterable, Optional, Dict, cast
class PassThrough(Processor):
"""A simple passthrough processor that takes input and sends it to output."""
def filename(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the filename of the post-processed file.
Arguments:
oldname (str): the previous name for the file.
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new name for the file
"""
return oldname
def mime_type(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the mimetype of the post-processed file.
Arguments:
oldname (str): the input filename
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new mimetype of the file after processing
"""
result = cast(str, guess_mime(oldname))
if result == "directory":
result = "DIR"
return result
def process(self, input_file: Iterable, ctx: Optional[Dict] = None) -> Iterable:
"""Return an iterable object of the post-processed file.
Arguments:
input_file (iterable): An input stream
ctx (dict, optional): A context object generated from the processor configuration
Returns:
iterable: The post-processed output stream
"""
return input_file
def extension(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the mimetype of the post-processed file.
Arguments:
oldname (str): the input filename
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new extension of the file after processing
"""
return os.path.splitext(oldname)[-1]
processor = PassThrough

View File

@ -0,0 +1 @@
processor = None

View File

@ -0,0 +1,69 @@
"""Convert an MD stream into an HTML stream"""
import io
import os
from typing import Iterable, Optional, Dict
import markdown
from .processors import Processor
class MarkdownProcessor(Processor):
"""Convert an MD stream into an HTML stream"""
def filename(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the filename of the post-processed file.
Arguments:
oldname (str): the previous name for the file.
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new name for the file
"""
return os.path.splitext(oldname)[0] + ".html"
def mime_type(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the mimetype of the post-processed file.
Arguments:
oldname (str): the input filename
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new mimetype of the file after processing
"""
return "text/html"
def extension(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the mimetype of the post-processed file.
Arguments:
oldname (str): the input filename
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new extension of the file after processing
"""
return "html"
def process(self, input_file: Iterable, ctx: Optional[Dict] = None) -> Iterable:
"""Return an iterable object of the post-processed file.
Arguments:
input_file (iterable): An input stream
ctx (dict, optional): A context object generated from the processor configuration
Returns:
iterable: The post-processed output stream
"""
md = u"".join([x for x in input_file])
return io.StringIO(markdown.markdown(md, extensions=["extra", "admonition", "wikilinks"]))
processor = MarkdownProcessor # pylint: disable=invalid-name

View File

@ -0,0 +1 @@
processor = None

View File

@ -0,0 +1 @@
processor = None

View File

@ -0,0 +1 @@
processor = None

View File

@ -0,0 +1,63 @@
import abc
from typing import Iterable, Optional, Dict
class ProcessorException(Exception): # pragma: no cover
"""A base exception class to be used by processor objects."""
class Processor(abc.ABC): # pragma: no cover
def __init__(self, *args, **kwargs):
"""Initialize the class."""
@abc.abstractmethod
def filename(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the filename of the post-processed file.
Arguments:
oldname (str): the previous name for the file.
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new name for the file
"""
@abc.abstractmethod
def mime_type(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the mimetype of the post-processed file.
Arguments:
oldname (str): the input filename
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new mimetype of the file after processing
"""
@abc.abstractmethod
def extension(self, oldname: str, ctx: Optional[Dict] = None) -> str:
"""Return the mimetype of the post-processed file.
Arguments:
oldname (str): the input filename
ctx (dict, optional): A context object generated from the processor configuration
Returns:
str: the new extension of the file after processing
"""
@abc.abstractmethod
def process(self, input_file: Iterable, ctx: Optional[Dict] = None) -> Iterable:
"""Return an iterable object of the post-processed file.
Arguments:
input_file (iterable): An input stream
ctx (dict, optional): A context object generated from the processor configuration
Returns:
iterable: The post-processed output stream
"""