timApp.printing package#

Submodules#

timApp.printing.documentprinter module#

Functions for calling pandoc and constructing the calls

class timApp.printing.documentprinter.DocumentPrinter(doc_entry: timApp.document.docinfo.DocInfo, template_to_use: timApp.document.docinfo.DocInfo | None, urlroot: str)[source]#

Bases: object

static get_all_templates(doc_entry: timApp.document.docentry.DocEntry, current_user: timApp.user.user.User) list[timApp.document.docentry.DocEntry][source]#
get_autocounters(user_ctx: timApp.document.usercontext.UserContext) timApp.markdown.autocounters.AutoCounters[source]#

Gets the content of the DocEntry assigned for this DocumentPrinter object. Builds autonumber counters from %%”name”|c_????%% filters.

Returns

counters for autonumbering

get_content(user_ctx: timApp.document.usercontext.UserContext, plugins_user_print: bool = False, target_format: timApp.printing.printsettings.PrintFormat = PrintFormat.PLAIN) str[source]#

Gets the content of the DocEntry assigned for this DocumentPrinter object. Fetches the markdown for the documents paragraphs, checks whether the paragraph should be printed determined by a boolean ‘print’-attribute, and returns the markdown for all the paragraphs that should be printed.

Returns the (markdown) contents of the file as a single string, as that’s the format pypandoc likes to handle.

Returns

The TIM documents contents in markdown format. Excludes the paragraphs that have attribute print=”false”

get_document_version_as_float() float[source]#
get_print_path(file_type: timApp.printing.printsettings.PrintFormat, plugins_user_print: bool = False) pathlib.Path[source]#

Formulates the printing path for the given document

Parameters
  • file_type – File format for the output

  • plugins_user_print – should print user answers

Returns

get_printed_document_path_from_db(file_type: timApp.printing.printsettings.PrintFormat, plugins_user_print: bool = False) str | None[source]#
get_template_id() int | None[source]#
get_template_version_as_float() float | None[source]#
static get_templates_as_dict(doc_entry: timApp.document.docentry.DocEntry, current_user: timApp.user.user.User)[source]#
static get_user_templates(doc_entry: timApp.document.docentry.DocEntry, current_user: timApp.user.user.User) list[timApp.document.docentry.DocEntry][source]#
hash_doc_print(plugins_user_print: bool = False) str[source]#
static parse_template_content(template_doc: timApp.document.docinfo.DocInfo, doc_to_print: timApp.document.docentry.DocEntry) str[source]#
write_to_format(user_ctx: timApp.document.usercontext.UserContext, target_format: timApp.printing.printsettings.PrintFormat, path: pathlib.Path, plugins_user_print: bool = False, eol_type: str = 'native')[source]#

Converts the document to latex and returns the converted document as a bytearray

Parameters
  • user_ctx – The user context.

  • target_format – The target file format

  • plugins_user_print – Whether or not to print user input from plugins (instead of default values)

  • path – filepath to write

  • eol_type – EOL type. Allows same option as Pandoc (crlf, lf, native)

Returns

Converted document as bytearray

exception timApp.printing.documentprinter.LaTeXError(value)[source]#

Bases: Exception

exception timApp.printing.documentprinter.PrintingError[source]#

Bases: Exception

timApp.printing.documentprinter.add_nonumber(md: str) str[source]#
Adds {.unnumbered} after every heading line that starts with #
Special cases:
  • many # lines in same md
    • before #-line there must be at least two cr

    • split between two cr

  • line starting with # may continue by ordinary line
    • the unnumbered must be added before first cr

  • line starting with # may continue next line and have at the end
    • undefined

Parameters

md – markdown to be converted

Returns

markdown with headings marked as unnumbered

timApp.printing.documentprinter.get_file(latex_file, fileurl, new_env)[source]#
timApp.printing.documentprinter.get_tex_macros(d: timApp.document.document.Document)[source]#
timApp.printing.documentprinter.get_tex_settings_and_macros(d: timApp.document.document.Document, user_ctx: timApp.document.usercontext.UserContext, template_doc: Optional[timApp.document.docentry.DocEntry] = None, tformat: timApp.printing.printsettings.PrintFormat = PrintFormat.PLAIN)[source]#
timApp.printing.documentprinter.number_lines(s: str, start: int = 1)[source]#
timApp.printing.documentprinter.run_latex(outputfile, latex_file, new_env, string_input)[source]#
timApp.printing.documentprinter.tim_convert_input(source, from_format, input_type, to, extra_args=(), outputfile=None, filters=None, removethis=None, texfiles=None, eol_type='native')[source]#
timApp.printing.documentprinter.tim_convert_text(source, to, from_format, extra_args=(), encoding='utf-8', outputfile=None, filters=None, removethis=None, texfiles=None, eol_type='native')[source]#

Converts given source from format to to.

Parameters
  • source (str) – Unicode string or bytes (see encoding)

  • to (str) – format into which the input should be converted; can be one of pypandoc.get_pandoc_formats()[1]

  • from_format (str) – the format of the inputs; can be one of pypandoc.get_pandoc_formats()[1]

  • extra_args (list) – extra arguments (list of strings) to be passed to pandoc (Default value = ())

  • encoding (str) – the encoding of the input bytes (Default value = ‘utf-8’)

  • outputfile (str) – output will be written to outfilename or the converted content returned if None (Default value = None)

  • filters (list) – pandoc filters e.g. filters=[‘pandoc-citeproc’]

  • removethis – lines that contains this text are removed from genereted LaTeX file

  • texfiles – what files need to copy

  • eol_type – EOL type to use. Allowed values are same as Pandoc (crlf, lf, native)

Returns

converted string (unicode) or an empty string if an outputfile was given

Return type

unicode

Raises
  • RuntimeError – if any of the inputs are not valid of if pandoc fails with an error

  • OSError – if pandoc is not found; make sure it has been installed and is available at path.

timApp.printing.pandoc_headernumberingfilter module#

Pandoc filter to remove numbering from header elements that have the .nonumber class.

timApp.printing.pandoc_headernumberingfilter.remove_header_numbering(key, value, fmt, meta)[source]#

timApp.printing.pandoc_imagefilepathsfilter module#

Pandoc filter to convert image sources to latex graphics source paths considering the images location according to the set of following rules:

  • If an image has an absolute path that points to the TIM machine, e.g. “http://<TIM-domain>/imagepath” or “<tim-domain>/imagepath”, then….

  • If an image has a relative path, e.g. “/images/1239854102”, then….

  • If an image points to a resource that resides at another host, simply convert the image to a simple link at the output. This is due to possible copyright infringements, as the images would othewrise be unrightly copied to the output document.

TODO: BETTER DOCUMENTATION

timApp.printing.pandoc_imagefilepathsfilter.convert_svg_to_pdf(image_path)[source]#
timApp.printing.pandoc_imagefilepathsfilter.handle_images(key, value, fmt, meta)[source]#
timApp.printing.pandoc_imagefilepathsfilter.init_whitelist()[source]#

Init whitelist for trusted image source domains.

timApp.printing.pandoc_inlinestylesfilter module#

Pandoc filter to convert class values to commands of same name in latex. Leaves (should leave…) ids, other, predefined classes and key-values intact.

timApp.printing.pandoc_inlinestylesfilter.classes_to_latex_cmds(key, value, fmt, meta)[source]#
timApp.printing.pandoc_inlinestylesfilter.latex(content)[source]#
timApp.printing.pandoc_inlinestylesfilter.wrap_with_latex_cmds(content, classes_to_wrap)[source]#

timApp.printing.print module#

Routes for printing a document

timApp.printing.print.add_counters_par(doc_info: timApp.document.docinfo.DocInfo, settings_par: timApp.document.docparagraph.DocParagraph, counters_par: timApp.document.docparagraph.DocParagraph | None, values: str) timApp.document.docparagraph.DocParagraph[source]#
timApp.printing.print.check_print_cache(doc_entry: timApp.document.docinfo.DocInfo, template: timApp.document.docinfo.DocInfo | None, file_type: timApp.printing.printsettings.PrintFormat, plugins_user_print: bool = False) str | None[source]#

Fetches the given document from the database.

Parameters
  • doc_entry

  • template

  • file_type

  • plugins_user_print

Returns

timApp.printing.print.create_printed_doc(doc_entry: timApp.document.docinfo.DocInfo, template_doc: timApp.document.docinfo.DocInfo | None, file_type: timApp.printing.printsettings.PrintFormat, temp: bool, user_ctx: timApp.document.usercontext.UserContext, plugins_user_print: bool = False, urlroot: str = '', eol_type: str = 'native') str[source]#

Adds a marking for a printed document to the db

Parameters
  • user_ctx – The user context.

  • doc_entry – Document that is being printed

  • template_doc – printing template used

  • file_type – File type for the document

  • temp – Is the document stored only temporarily (gets deleted after some time)

  • plugins_user_print – use users answers for plugins or not

  • urlroot – url root for this route

  • eol_type – EOL type. Same option as Pandoc (crlf, lf, native)

Return str

path to the created file

timApp.printing.print.do_before_requests() None[source]#
timApp.printing.print.get_doc_template_name(doc: timApp.document.docinfo.DocInfo) str | None[source]#
timApp.printing.print.get_mimetype_for_format(file_type: timApp.printing.printsettings.PrintFormat) str[source]#
timApp.printing.print.get_numbering(doc_path: str, recurse: bool = False) flask.wrappers.Response[source]#

renumber autocounters

Parameters
  • doc_path – from what document

  • recurse – Should the referenced documents be renumbered as well?

Returns

ok-response

timApp.printing.print.get_printed_document(doc_path: str, file_type: str | None = None, plugins_user_code: bool = False, template_doc_id: int = - 1, force: bool = False, showerror: bool = False) flask.wrappers.Response[source]#
timApp.printing.print.get_setting_and_counters_par(doc_info: timApp.document.docinfo.DocInfo) tuple[timApp.document.docparagraph.DocParagraph | None, timApp.document.docparagraph.DocParagraph | None][source]#
timApp.printing.print.get_template_doc(doc: timApp.document.docinfo.DocInfo, template_doc_id: int) tuple[timApp.document.docinfo.DocInfo | None, int, str | None, bool][source]#
timApp.printing.print.get_templates(doc_path: str) flask.wrappers.Response[source]#
timApp.printing.print.handle_doc_numbering(doc_info: timApp.document.docinfo.DocInfo, used_names: list[str] | None) str[source]#

Create automatic counters for document and all referenced documents.

Parameters
  • doc_info – document to handle

  • used_names – list of already used names to avoid endless recursion

Returns

Possible error string

timApp.printing.print.print_document(doc_path: str, file_type: str = Field(name='file_type', type=<class 'str'>, default=<dataclasses._MISSING_TYPE object>, default_factory=<dataclasses._MISSING_TYPE object>, init=True, repr=True, hash=None, compare=True, metadata=mappingproxy({'data_key': 'fileType'}), kw_only=False, _field_type=_FIELD), template_doc_id: int = Field(name='template_doc_id', type=<class 'int'>, default=<dataclasses._MISSING_TYPE object>, default_factory=<dataclasses._MISSING_TYPE object>, init=True, repr=True, hash=None, compare=True, metadata=mappingproxy({'data_key': 'templateDocId'}), kw_only=False, _field_type=_FIELD), plugins_user_print: bool = Field(name='plugins_user_print', type=<class 'bool'>, default=<dataclasses._MISSING_TYPE object>, default_factory=<dataclasses._MISSING_TYPE object>, init=True, repr=True, hash=None, compare=True, metadata=mappingproxy({'data_key': 'printPluginsUserCode'}), kw_only=False, _field_type=_FIELD), remove_old_images: bool = Field(name='remove_old_images', type=<class 'bool'>, default=False, default_factory=<dataclasses._MISSING_TYPE object>, init=True, repr=True, hash=None, compare=True, metadata=mappingproxy({'data_key': 'removeOldImages'}), kw_only=False, _field_type=_FIELD), force: bool = False) flask.wrappers.Response[source]#
timApp.printing.print.pull_doc_path(endpoint: str | None, values: dict[str, str] | None) None[source]#
timApp.printing.print.remove_images(doc_id: int) None[source]#
timApp.printing.print.template_by_name(template_name: str, isdef: bool = False) tuple[timApp.document.docinfo.DocInfo | None, int, str | None, bool][source]#

timApp.printing.printeddoc module#

class timApp.printing.printeddoc.PrintedDoc(**kwargs)[source]#

Bases: sqlalchemy.ext.declarative.api.Model

A printed document. A PrintedDoc is created each time a document is printed (CSS printing does not count because it happens entirely in browser).

created#

Timestamp of printing.

doc_id#

Id of the printed document.

file_type#

The filetype of the print.

id#
path_to_file#

Path to the printed document in the filesystem.

temp#

Whether the printed document is stored only temporarily (gets deleted after some time).

template_doc_id#

Id of the template document.

version#

Version (in practice, a hash) for identifying whether a document has already been printed and can be fetched from cache.

timApp.printing.printsettings module#

class timApp.printing.printsettings.PrintFormat(value)[source]#

Bases: enum.Enum

An enumeration.

HTML = 'html'#
ICS = 'ics'#
JSON = 'json'#
LATEX = 'latex'#
MD = 'md'#
PDF = 'pdf'#
PLAIN = 'plain'#
RST = 'rst'#

Module contents#