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/controller.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
87 lines (65 sloc)
2.99 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 pathlib | |
import remarkable.parser as parser | |
import remarkable.renderer as renderer | |
class RenderError(Exception): | |
"""Exception raised for errors in the input. | |
@message: explanation of the error | |
""" | |
def __init__(self, message): | |
super().__init__() | |
self.message = message | |
#env.list_templates() | |
def convertFile(inputFile, templateFile=None, templateDir=None, outputFile=None): | |
""" | |
Given a set of options convert a file from markdown | |
to a nicely rendered output. | |
The template file can be specified as part of the function, or in the header file. | |
NOTE: options specified to the function will overwrite anything in the template. | |
@param inputFile: The Markdown File we want to render | |
@param templateFile: Template file to use to render. | |
@param templateDir: Optional Local Template directory | |
@param outputFile: Output file, defaults to <input>.hmtl | |
""" | |
log = logging.getLogger("CONTROLLER") | |
#Create a parser for this input file | |
theParser = parser.MarkdownParser(inputFile) | |
theParser.parseFile() | |
#Check for Header | |
if theParser.header: | |
#Check for either template dir or template file in the header | |
if templateFile is None: | |
templateFile = theParser.header.get("template") | |
log.debug("Using template from header: {0}".format(templateFile)) | |
if templateDir is None: | |
templateDir = theParser.header.get("templatedir") | |
if templateFile is None: | |
log.critical("No Template Specified") | |
raise RenderError("No Template has been specified") | |
#Feed this to the Renderer | |
if templateDir: | |
log.info("Render with {0} from {1}".format(templateFile, templateDir)) | |
else: | |
log.info("Render with default template: {0}".format(templateFile)) | |
theRenderer = renderer.HTMLRenderer(templateFile, templateDir) | |
#We also need to know where to output the thing. | |
if outputFile is None: | |
log.debug("No Output file specified") | |
outputFile = "{0}.html".format(pathlib.Path(inputFile).stem) | |
log.debug("Output File is now {0}".format(outputFile)) | |
log.info("Generating output {0}".format(outputFile)) | |
theRenderer.toFile(theParser, outputFile) |