The Transformer class¶
- class ferenda.Transformer(transformertype, template, templatedirs, documentroot=None, config=None)¶
Transforms parsed “pure content” documents into “browser-ready” HTML5 files with site branding and navigation, using a template of some kind.
- transformertype (str) – The engine to be used for transforming. Right now only "XSLT" is supported.
- template (str) – The main template file.
- templatedirs (str) – Directories that may contain supporting templates used by the main template.
- documentroot (str) – The base directory for all generated files – used to make relative references to CSS/JS files correct.
- config – Any configuration information used by the transforming engine. Can be a path to a config file, a python data structure, or anything else compatible with the engine selected by transformertype.
An initialized Transformer object only transforms using the template file provided at initialization. If you need to use another template file, create another Transformer object.
- transform(indata, depth, parameters=None, uritransform=None)¶
Perform the transformation. This method always operates on the “native” datastructure – this might be different depending on the transformer engine. For XSLT, which is implemented through lxml, its in- and outdata are lxml trees
- indata – The document to be transformed
- depth (int) – The directory nesting level, compared to documentroot
- parameters (dict) – Any parameters that should be provided to the template
- uritransform (callable) – A function, when called with an URI, returns a transformed URI/URL (such as the relative path to a static file) – used when transforming to files used for static offline use.
The transformed document
- transform_stream(instream, depth, parameters=None, uritransform=None)¶
Accepts a file-like object, returns a file-like object.
- transform_file(infile, outfile, parameters=None, uritransform=None)¶
Accepts two filenames, reads from infile, writes to outfile.