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.
142 lines (112 sloc)
4.69 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
""" | |
reMarkable: Convert markdown to docs using Templates | |
Copyright (C) 2020 Dan Goldsmith (djgoldsmith@googlemail.com) | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <https://www.gnu.org/licenses/>. | |
""" | |
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 | |
#We also need to do some calculations here | |
totalAwarded = 0 | |
totalPossible = 0 | |
for item in sections.values(): | |
if item.marks: | |
totalAwarded += item.marks | |
if item.maxMarks: | |
totalPossible += item.maxMarks | |
calcs = {"marksAwarded": totalAwarded, | |
"marksPossible": totalPossible} | |
out = self.template.render({"content":sections, | |
"header": header, | |
"order": theParser.sectionOrder, | |
"calcs": calcs}) | |
return out |