Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
reMarkAble/remarkable/renderer.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
116 lines (90 sloc)
3.55 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""And a Class for Rendering Files | |
I cant see this going much beyond HTML, but lets keep it here for | |
completeness. | |
""" | |
import logging | |
import jinja2 | |
#from jinja2 import Environment, PackageLoader, select_autoescape | |
import markdown | |
from markdown.treeprocessors import Treeprocessor | |
from markdown.extensions import Extension | |
#Extra to make sure tables have the bootstrap "table" class | |
class TableProcessor(Treeprocessor): | |
def run(self, root): | |
for element in root.iter("table"): | |
element.set("class", "table") | |
class TableExtension(Extension): | |
def extendMarkdown(self, md): | |
md.treeprocessors.register(TableProcessor(md), | |
'table processor', | |
15) | |
markdowner = markdown.Markdown(extensions=["tables", | |
'codehilite', | |
'fenced_code', | |
TableExtension(), | |
'mdx_math']) | |
class Renderer(): | |
"""Parent class for all rendering objects | |
""" | |
def __init__(self): | |
"""Create a renderer""" | |
self.log = logging.getLogger("Render") | |
def render(self, theParser): | |
"""Do the Magic required to render | |
Placeholder for inherited function | |
@param theParser: Parser object | |
@return: String representation of the output | |
""" | |
raise NotImplementedError | |
def toFile(self, theParser, outFile): | |
""" | |
Render the template to a file | |
@param parser: Parser Object | |
@param theFile: Output File | |
""" | |
self.log.info(" Render to file {0}".format(outFile)) | |
theText = self.render(theParser) | |
fd = open(outFile, "w") | |
fd.write(theText) | |
fd.close() | |
class HTMLRenderer(Renderer): | |
""" | |
Render our document to HTML using Jinja templates | |
""" | |
def __init__(self, theTemplate, templateDir=None): | |
""" | |
Create a new rendering object. | |
This will render to HTML (actually anything via Jinja) | |
based on files in the templates directory. | |
@param theTemplate: Filename of template to use | |
@param templateDir: Optional (local) directory containing templates to use | |
""" | |
super().__init__() | |
if templateDir: | |
self.log.debug("--> Template Dir Specified") | |
#Default to specified dir, fall back to system | |
theLoader = jinja2.ChoiceLoader([jinja2.FileSystemLoader(templateDir), | |
jinja2.PackageLoader("remarkable")]) | |
else: | |
self.log.debug("--> No custom Template dir Specified") | |
theLoader = jinja2.PackageLoader("remarkable") | |
self.log.info("Create Renderer") | |
#We are going to load all templates from scratch first | |
self.env = jinja2.Environment( | |
loader=theLoader, | |
autoescape=jinja2.select_autoescape(['html', 'xml']) | |
) | |
self.log.info("--> Available Templates: ") | |
self.log.info("--> {0}".format(self.env.list_templates())) | |
self.log.info("--> Template file {0}".format(theTemplate)) | |
self.template = self.env.get_template(theTemplate) | |
def render(self, theParser): | |
""" | |
Render the template to a string | |
@param theParser: Parser object to render | |
""" | |
sections = theParser.sections | |
header = theParser.header | |
out = self.template.render({"content":sections, | |
"header": header}) | |
return out |