annals.combine#
Many files of many kinds as ONE document: a Markdown, html or PDF file.
The mechanism is a registry. A converter turns one file of one kind into a part of the
output format (markdown text, an html fragment, a PDF); a joiner puts the parts of a
format together (a table of contents and a section each; bookmarks and pages). Which pairs
exist is the table REGISTRY, filled by register():
@register("pdf", "md", requires=("pdfdol",))
def pdf_to_md(item, ctx): ...
so a new pair (docx to pdf, say) is one decorated function, and the web page, the API and
the CLI all learn it from supported() without a change.
What goes in:
========= =========== =======================================================
input to md to html / to pdf
========= =========== =======================================================
md as it is rendered (images of the document inside)
html converted its text and structure (scripts and styles dropped)
text a code block a preformatted block
pdf its text html: its text; pdf: its pages, joined as they are
image - a figure (html) / a page of its own (pdf)
========= =========== =======================================================
Audio, video and other files have no place in a document: they are reported as left out, each with the reason, never silently dropped. A file that cannot be read (a damaged or password-protected PDF) is left out the same way, and the others still go in.
An image or a text file that sits in a document with pages (markdown, html, a PDF) is part of that document, not a section of its own: a report and its figures combine as the report.
Heavy libraries load only when a pair needs them (WeasyPrint for PDF pages, pypdf and pdfdol
to join and read PDFs, markdownify for html to markdown); a missing one is a
annals.requirements.MissingDependency that says what to install, raised before any
work starts. Nothing is stored: the document is made per call.
>>> combined = combine([_demo_source({"b.md": "# B\ntext", "a.md": "hello"})], "md", title="T")
>>> combined.sections, combined.ext
(2, 'md')
>>> print(combined.data.decode().splitlines()[0])
# T
Module Attributes
kinds whose file is a document in itself |
|
kinds that illustrate or accompany a document with pages, so are not sections of its own |
|
skipped files listed in a result (and its headers); the rest are counted |
|
a PDF with more pages than this is not read for its text (about 0.2 s a page) |
|
files made into PDF pages by rendering (about 0.4 s each) in one output |
|
a text or html file larger than this is left out, and the others still go in |
|
html nested deeper than this is flattened (very deep trees overflow the converters) |
|
a table cell may span this many columns or rows (a bigger number is a layout attack) |
|
headings of an embedded markdown or html file go this far down, under the section's own |
|
(kind, format) -> |
|
show itself, with its own styles and embedded images |
Functions
|
Every usable file of |
|
One sentence on what a |
|
The content of an html page as a safe fragment: its structure and words, nothing live. |
|
The files to try for a |
|
The kinds of file that can be put in a |
|
Decorator: the function turns a file of |
|
The skipped files as compact JSON for a response header: few, short, ascii. |
What can be made: |
Classes
|
The result: the file's bytes, what it is, how many files went in and which did not. |
|
What a converter may use besides the item: where images go, and the output's own needs. |
|
An output format: how it is named, joined, and whether each file gets its own context. |
|
One file to put in the output: a section, or a run of pages. |
|
What a joiner needs besides the parts. |
|
One (kind, format) cell of the registry. |
|
A file left out, and why. |
|
One document, as a caller lets it be read: its files and how to get at them. |
Exceptions
One file could not be converted (damaged, protected, too large); the rest go on. |
|
No file could be put in the output (the message says which kinds could). |
- annals.combine.COMPANION_KINDS = frozenset({'image', 'text'})#
kinds that illustrate or accompany a document with pages, so are not sections of its own
- class annals.combine.Combined(data, fmt, media_type, ext, sections, skipped=())[source]#
Bases:
objectThe result: the file’s bytes, what it is, how many files went in and which did not.
- class annals.combine.Context(fmt, embedder=Embedder())[source]#
Bases:
objectWhat a converter may use besides the item: where images go, and the output’s own needs.
- exception annals.combine.ConversionError[source]#
Bases:
ValueErrorOne file could not be converted (damaged, protected, too large); the rest go on.
- class annals.combine.Format(name, label, ext, media_type, join, requires=(), context_per_item=False)[source]#
Bases:
objectAn output format: how it is named, joined, and whether each file gets its own context.
- annals.combine.HTML_CSP = "default-src 'none'; img-src data:; style-src 'unsafe-inline'"#
show itself, with its own styles and embedded images
- Type:
what a combined html page may do
- annals.combine.HTML_DEMOTE = 2#
headings of an embedded markdown or html file go this far down, under the section’s own
- class annals.combine.Item(source, rel, kind, label, title)[source]#
Bases:
objectOne file to put in the output: a section, or a run of pages.
- class annals.combine.Job(title, demote, embedder, toc=True, unwrap=True)[source]#
Bases:
objectWhat a joiner needs besides the parts.
- annals.combine.MAX_HTML_DEPTH = 100#
html nested deeper than this is flattened (very deep trees overflow the converters)
- annals.combine.MAX_RENDERED_FILES = 60#
files made into PDF pages by rendering (about 0.4 s each) in one output
- annals.combine.MAX_SKIPPED_LISTED = 4#
skipped files listed in a result (and its headers); the rest are counted
- annals.combine.MAX_SPAN = 100#
a table cell may span this many columns or rows (a bigger number is a layout attack)
- annals.combine.MAX_TEXT_FILE_BYTES = 2000000#
a text or html file larger than this is left out, and the others still go in
- annals.combine.MAX_TEXT_PAGES = 300#
a PDF with more pages than this is not read for its text (about 0.2 s a page)
- exception annals.combine.NothingToCombine[source]#
Bases:
ValueErrorNo file could be put in the output (the message says which kinds could).
- annals.combine.PAGE_KINDS = frozenset({'html', 'md', 'pdf'})#
kinds whose file is a document in itself
- class annals.combine.Pair(kind, fmt, fn, requires=())[source]#
Bases:
objectOne (kind, format) cell of the registry.
- annals.combine.REGISTRY: dict[tuple[str, str], Pair] = {('html', 'html'): Pair(kind='html', fmt='html', fn=<function _html_body_of>, requires=()), ('html', 'md'): Pair(kind='html', fmt='md', fn=<function _html_to_md>, requires=('markdownify',)), ('html', 'pdf'): Pair(kind='html', fmt='pdf', fn=<function _page_to_pdf.<locals>.convert>, requires=('weasyprint',)), ('image', 'html'): Pair(kind='image', fmt='html', fn=<function _image_body>, requires=()), ('image', 'pdf'): Pair(kind='image', fmt='pdf', fn=<function _page_to_pdf.<locals>.convert>, requires=('weasyprint',)), ('md', 'html'): Pair(kind='md', fmt='html', fn=<function _md_body>, requires=('markdown_it',)), ('md', 'md'): Pair(kind='md', fmt='md', fn=<function _md_to_md>, requires=()), ('md', 'pdf'): Pair(kind='md', fmt='pdf', fn=<function _page_to_pdf.<locals>.convert>, requires=('markdown_it', 'weasyprint')), ('pdf', 'html'): Pair(kind='pdf', fmt='html', fn=<function _pdf_body>, requires=('pdfdol', 'pypdf')), ('pdf', 'md'): Pair(kind='pdf', fmt='md', fn=<function _pdf_to_md>, requires=('pdfdol', 'pypdf')), ('pdf', 'pdf'): Pair(kind='pdf', fmt='pdf', fn=<function _pdf_to_pdf>, requires=('pypdf', 'pdfdol')), ('text', 'html'): Pair(kind='text', fmt='html', fn=<function _text_body>, requires=()), ('text', 'md'): Pair(kind='text', fmt='md', fn=<function _text_to_md>, requires=()), ('text', 'pdf'): Pair(kind='text', fmt='pdf', fn=<function _page_to_pdf.<locals>.convert>, requires=('weasyprint',))}#
(kind, format) ->
Pair; add a pair withregister()
- class annals.combine.Source(meta, files, read_text, open_path, file_url=<function Source.<lambda>>, read_bytes=None)[source]#
Bases:
objectOne document, as a caller lets it be read: its files and how to get at them.
read_text(rel)is the file’s text (a share gives the filtered text);open_path(rel)its path on disk, for bytes. Either may raiseOSErrororKeyErrorfor a file that is gone, which leaves that file out.
- annals.combine.combine(sources, fmt='md', *, title='', demote=1, toc=True, unwrap=True, order=<function numbered_first>, max_text_bytes=8000000, max_input_bytes=120000000, registry=None)[source]#
Every usable file of
sourcesas onefmt(“md”, “html” or “pdf”) file.sourcesare the documents (Source), in their order; files are arranged byorder(annals.aggregate.numbered_first(): numbered names first, numerically;Nonekeeps the documents’ order).demoteis how far an embedded markdown file’s headings go down in a markdown output, which also takestoc(a table of contents) andunwrap(join hard-wrapped lines).max_text_bytescaps all the text read (annals.aggregate.TooLarge),max_input_bytesall the files together.A file with no converter, or that cannot be read, is left out and listed in
Combined.skippedwith the reason. RaisesNothingToCombinewhen nothing is left,annals.requirements.MissingDependencywhen a library the conversions need is not installed (before any work), andValueErrorfor an unknownfmt.- Return type:
- annals.combine.explain(fmt)[source]#
One sentence on what a
fmtoutput is made of (the ‘supported’ part of a message).- Return type:
- annals.combine.html_body(text, *, rewrite_src=<function <lambda>>, rewrite_href=None, demote=0)[source]#
The content of an html page as a safe fragment: its structure and words, nothing live.
Only a fixed set of tags survives, with a few plain attributes: no script, style, frame, form, event handler,
classorid; a link keeps only anhttp(s)ormailtoaddress, whichrewrite_href(href), when given, may change or refuse (None: the link keeps its words, loses its address).rewrite_src(src)decides each image: the url to show it from, orNonefor its alt text instead (an image is never loaded from where the page says).demotepushes headings that many levels down. Unbalanced tags are closed, so a fragment can never swallow what is joined after it.>>> html_body('<body><script>x()</script><h1 onclick="x()">Hi</h1><a href="javascript:x()">y</a></body>') '<h1>Hi</h1><a>y</a>'
The Download PDF button annals put in the page (
annals.pagepdf) is not content, so it is left out too.- Return type:
- annals.combine.items_of(sources, fmt)[source]#
The files to try for a
fmtoutput, in the documents’ order.A document with pages (markdown, html, PDF) contributes those; its images and text files are its own parts. A document with none contributes whatever it holds, so what cannot be used (a video) is reported and not silently missing. A folder, or a document with several such files, names each by its title then the file (
Report/01-intro.md).
- annals.combine.kinds_for(fmt)[source]#
The kinds of file that can be put in a
fmtoutput, in a stable order.
- annals.combine.register(kind, fmt, *, requires=())[source]#
Decorator: the function turns a file of
kindinto a part of thefmtoutput.requiresare the (importable) modules it needs, checked before any work starts.
- annals.combine.skipped_header(skipped)[source]#
The skipped files as compact JSON for a response header: few, short, ascii.
At most
MAX_SKIPPED_LISTEDare listed (the count says how many there were) and names and reasons are cut, so the header stays far under what a proxy accepts.- Return type: