![]() |
Exporting to HTML | ![]() |
buffer_export (
Texmacs/Data/new_buffer.cpp
) replaces the body of the document by the result of edit_typeset_rep::exec_html, which expands all macros which the HTML converter does not handle. The document is then passed to export_tree and tree_to_generic with the format html-document.
The converter graph goes from texmacs-tree to texmacs-stree, then uses texmacs->html (
convert/html/tmhtml.scm
) to produce an sxml expression (html-stree), and finally serialize-html (
convert/html/htmlout.scm
) to produce the string (html-document).
When a selection is copied as HTML (
Edit/Replace/edit_select.cpp
), exec_html is applied to the selection and the snippet variants of the converters are used.
Implemented in
Edit/Editor/edit_typeset.cpp
(accessible from Scheme as html-expand). It computes the environment at the start of the document, joins it with the patch returned by (tmhtml-env-patch), replaces each variable name by tmhtml-name when the latter exists (prefix_specific), and evaluates the body with exec. References are expanded during this evaluation, so that the converter sees their text. Finally, the title of the document and the values of the environment variables html-title, html-css, html-head-javascript, html-head-javascript-src, html-head-favicon, html-extra-css, html-extra-javascript-src, html-extra-javascript and html-site-version are passed to the converter by wrapping the body into a with (with the additional variable html-doc-title for the title).
The patch of tmhtml-env-patch (
convert/html/tmhtml-expand.scm
) defines identity macros (xmacro "x" (eval-args "x")) for the tags which the converter handles itself: sectioning titles (section-title, …), lists, the logical markup (strong, em, code*, …), verbatim, equation*, hlink, the balloons and the tags for customized HTML generation (see below), as well as a few math macros. All other macros are expanded. Contrary to the LaTeX export, the list is fixed (the comment in the code notes that the DRD should be used instead). The list must be kept consistent with the dispatch tables of
tmhtml.scm
.
Style files use the tmhtml- prefix to adapt their macros to the HTML export. For instance
packages/environment/env-math.ts
defines tmhtml-eqnarray* as an extern call of the Scheme function ext-tmhtml-eqnarray*, and
packages/standard/std-automatic.ts
defines tmhtml-render-bibitem. The package html-font-size (in
packages/html
) is another example.
For a complete file, the body, the style and the language are assembled into (!file body style lan path) and the function recurses. Otherwise the options are decoded (tmhtml-initialize), the tree is converted by tmhtml-root, and the result is finalized by tmhtml-finalize-document (for a !file) or tmhtml-finalize-selection (for a snippet).
The converter produces sxml in which HTML elements have the prefix h: and MathML elements the prefix m:, e.g. (h:p "text" (h:em "x")); attributes are written as (@ (name value) …). The finalization strips these prefixes (sxml-strip-ns-prefix), adds the XML processing instruction, an XHTML 1.1 doctype (with MathML 2.0 when MathML is enabled) and the namespace declarations. Helper functions for sxml are in
convert/tools/sxml.scm
and
sxhtml.scm
.
All handlers return a node list (a list of sxml nodes and strings), which allows a TeXmacs construct to produce zero or several HTML nodes; the main function is
(define (tmhtml x)
(cond ((and tmhtml-mathjax? (ahash-ref tmhtml-env :math))
(tmhtml-mathjax-formula x))
((and tmhtml-mathml? (ahash-ref tmhtml-env :math))
‘((m:math (@ (xmlns "http://www.w3.org/1998/Math/MathML"))
,(texmacs->mathml x tmhtml-env))))
((and tmhtml-images? (ahash-ref tmhtml-env :math)
(!= tmhtml-image-root-string "image"))
(tmhtml-png ‘(with "mode" "math" ,x)))
((string? x)
(if (string-null? x) '() (tmhtml-text x)))
(else (or (tmhtml-dispatch 'tmhtml-primitives% x)
(tmhtml-implicit-compound x))))) |
The converter keeps its state in the hash table tmhtml-env (keys :math, :math-display, :preformatted, :mag, :left-margin, …), which handlers modify locally with ahash-with.
A logic-dispatcher for the TeXmacs primitives (document, concat, with, frac, table, image, hlink, label, specific, …). Handlers receive the list of arguments.
A logic-table for the tags of the standard styles. An entry is either a procedure (applied to the arguments), or a list such as (h:strong): in the latter case, the converted arguments are appended to the list, so that (strong "x") becomes (h:strong "x").
Tags of the tmdoc style, only used when the environment variable html-site-version is not "2" (the tmweb2 style renders these tags itself).
The translation of with: an entry for a pair ("font-series" "bold") gives a wrapper (h:b), an entry for a variable gives either a handler (e.g. "color" or "par-left") or a key of tmhtml-env together with CSS properties (for the ornament parameters).
Tags which are found in none of the tables are dropped (with their content); this is why all macros which should survive must be expanded by exec_html. The explicit form (compound "name" …) is handled like the tag name.
tmhtml-document and tmhtml-paragraph map the paragraphs of a document to p elements (or to blocks such as div or h2 when the paragraph starts with a block element, see tmhtml-p and as-blocks), and vertical spaces to CSS margins. Sectioning titles (section-title, …) become h1-h6, lists become ul, ol and dl (the items are regrouped by tmhtml-post-item), label becomes an anchor <a id="…">, and hlink an anchor with href; the suffix .tm of local links is replaced by .html (or .xhtml) by tmhtml-suffix, so that links between the pages of a converted web site keep working. Tables are converted by tmhtml-table and tmhtml-tformat with the table parser of
convert/tools/tmtable.scm
; cell formatting becomes attributes and CSS styles.
tmhtml-file builds the html element. The title is taken from the variable html-title, the first title tag of the document (doc-title, tmdoc-title, …, tmhtml-find-title) or html-doc-title. Unless html-css gives an external style sheet, a default style sheet produced by tmhtml-css-header is embedded. The preference "texmacs->html:css-stylesheet" may add a link to a style sheet (the dialog offers the style sheets of https://www.texmacs.org/css/), and the variables html-extra-css, html-head-javascript, html-head-javascript-src, html-extra-javascript, html-extra-javascript-src and html-head-favicon add further elements to the head. With MathJax, a script element loading MathJax 3 from a CDN is added. For the documentation and web styles (tmdoc, tmweb, tmweb2, …), tmhtml-tmdoc-post wraps the body in a div of class tmdoc-body.
Formulas are exported in one of four ways, according to the options (at most one of the last three is on; the preferences dialog enforces this, and by default formulas are exported as images):
Used when none of the options below applies (in particular when images are requested but the export does not go to a file): tmhtml-frac, tmhtml-sub, tmhtml-sqrt etc. approximate the formula with HTML elements, tables and the CSS classes of the default style sheet; symbols are converted by tmhtml-math-token.
Each formula is converted to LaTeX by texmacs->latex with the option "texmacs->latex:mathjax" and written as \(…\) in the page (tmhtml-mathjax-formula); the page loads MathJax.
Each formula is converted by texmacs->mathml (
convert/mathml/tmmath.scm
, dispatch table tmmath-primitives%); symbols and operators are translated with the tables of
convert/mathml/mathml-drd.scm
. The output file then gets the suffix .xhtml when a whole site is converted.
Each formula is typeset by TeXmacs and saved as a PNG picture by tmhtml-png. This only happens when the export goes to a file (otherwise the image root is the default "image" and the plain conversion is used).
Equation arrays and numbered equations have dedicated handlers (tmhtml-equation*, tmhtml-equation-lab, and ext-tmhtml-eqnarray*, called from the style through tmhtml-eqnarray*).
tmhtml-image treats the image primitive:
a linked image in a web format (tmhtml-web-formats: gif, jpg, jpeg, png, bmp, svg) becomes an img element; if the file is not found relative to the target, it is looked up relative to the buffer and copied next to the target (tmhtml-web-image-name);
an embedded image in a web format is saved to a file;
other images are rendered by tmhtml-png.
tmhtml-png renders arbitrary markup (graphics, draw-over, draw-under, (specific "image" …), formulas) with print-snippet into base-n.png next to the target file (tmhtml-image-names) and computes CSS margins from the extents so that the picture is aligned with the surrounding text. Images are cached by content (tmhtml-image-cache) during one export. Labels inside the rendered markup are kept as id attributes of the image.
The standard styles (
packages/standard/std-markup.ts
) define tags which only have an effect on the HTML export: html-tag, html-attr, html-style, html-class, html-div-style, html-div-class, html-javascript, html-javascript-src and html-video. Their handlers in
tmhtml.scm
wrap the converted content into an arbitrary element or add attributes to it (tmhtml-append-attribute). Similarly, (specific "html" s) inserts raw HTML code and (specific "html*" x) converts x only for the HTML export.
The menu Tools\(\rightarrow\)Create web site opens a dialog (open-website-builder in
doc/tmweb.scm
) which calls tmweb-convert-dir or tmweb-update-dir. These functions walk through the source directory, convert every .tm file with export-buffer-main (the images go next to each page, since buffer_export sets current-save-target during the conversion) and copy all other files; the update variant only converts files which are newer than their target (needs-update?). Pages get the suffix .xhtml instead of .html when MathML export is enabled. The variants tmweb-convert-dir-keep-texmacs and tmweb-update-dir-keep-texmacs also copy the .tm sources.
|
||||||||||||||||||
The first four options are read from the option list by tmhtml-initialize; "texmacs->html:css-stylesheet" is read as a preference (also by magic-png-number, which adapts the size of formula images to the web-* style sheets).
If the tag can be expressed with existing markup, nothing needs to be done: exec_html expands it. A tmhtml-name variant may be defined in the style file if the HTML rendering should differ from the typeset rendering.
To map the tag to an HTML element, add an entry to tmhtml-stdmarkup%, e.g. (my-tag (h:span (@ (class "my-tag")))), or a procedure (my-tag ,tmhtml-my-tag) which receives the list of arguments and returns a node list (use tmhtml to convert the arguments).
Add the tag to the list in tmhtml-env-patch (
tmhtml-expand.scm
), otherwise it is expanded before the converter sees it.
For the reverse direction, add a rule to htmltm-methods%, see importing HTML.
(regtest-tmhtml) (
convert/html/tmhtml-test.scm
) runs regression tests of tmhtml-root on small strees; it is part of (run-all-tests) (
check/check-master.scm
), which can be run with texmacs -x "(run-all-tests)" -q (see
src/tests/README.md
). The tests are written with regression-test-group (
kernel/boot/debug.scm
).
In a Scheme session, (texmacs->html (tree->stree t) '()) shows the sxml for a tree, and serialize-html its serialization.
The generated markup is XHTML 1.1 with some deprecated elements (tt, font, center, …) and tables for the layout of plain HTML formulas.
Tags which are neither handled nor expanded are silently dropped; references are replaced by their text and do not become links.
Page layout (headers, footers, page breaks, columns) is mostly ignored, and graphics are only exported as bitmaps.
The conversion of a whole site relies on the conventions of the tmdoc and tmweb styles.