Skip to content
Permalink
9749c1a4e7
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
100 lines (68 sloc) 2.78 KB
"""
Use Markdown to generate various uni documents
Controller code to get the workflows working.
Two use cases at the moment:
1. Called with all parts specifid
- Template
- Input File
1. Called with just an input file. Which contains the template.
I would then plumb this to a command line script.
```
markdownmark <input> -t [template] [output]
```
Exceptions to check for
- jinja2.exceptions.TemplateNotFound
Things to think about:
1. Batch conversion, point him at a directory
1. PDF conversion, use something (QT?) to render the HTML to PDF.
"""
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):
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)