![]() |
Importing HTML and MathML | ![]() |
An HTML document is imported along the path html-document \(\rightarrow\) html-stree \(\rightarrow\) texmacs-stree of the converter graph:
parse-html-document (
convert/html/htmltm.scm
) calls htmltm-parse (
convert/tools/xmltm.scm
), which parses the string with the C++ parser parse_html and normalizes the namespaces of the resulting sxml. The result is wrapped into (!file …); parse-html-snippet does the same without the wrapper.
html->texmacs converts the sxml into a TeXmacs stree with the dispatch table htmltm-methods%, delegating MathML to
convert/mathml/mathtm.scm
, and post-processes the result in C++ with clean-html (clean_html).
For a complete document, the body is wrapped into a TeXmacs file with the style browser.
parse_xml and parse_plain_html in
Data/Convert/Xml/parsexml.cpp
are two modes of the same parser, xml_html_parser, which aims to accept a superset of the valid documents and never reports errors. It proceeds in three passes:
A flat tokenization (xml_html_parser::parse ()) into opening and closing tags, text, comments, processing instructions, CDATA sections and the DOCTYPE. Entities are expanded on the fly (expand_entities), using the entities declared in the doctype and the tables
HTMLlat1.scm
,
HTMLspecial.scm
,
HTMLsymbol.scm
and
XML.scm
in $TEXMACS_PATH/langs/encoding. For HTML, the input is first transcoded to UTF-8 (transcode), using the encoding of the XML prolog if present.
The construction of the tree (build), which closes elements implicitly according to the rules of HTML: empty elements (br, img, …), elements with optional closing tags (p, li, td, …), and badly nested elements (build_valid_child, build_must_close, build_can_close).
The conversion into sxml (finalize_sxml), which produces the element names, the attribute lists (@ …) and the special nodes *PI* and *DOCTYPE*.
The result is a tree of the form (*TOP* (html (@ …) …)), which is converted into a Scheme expression by the glue. Unit tests for this parser are in
src/tests/Data/Convert/Xml
.
parse_html (
Data/Convert/Xml/parsehtml.cpp
) is a wrapper around parse_plain_html. If the page loads MathJax.js in its head (contains_mathjax), the formulas written in TeX syntax ($…$, \(…\), equation environments, …) are extracted before parsing and replaced by elements <mathjax>n</mathjax>; the formula is stored under the number n and can be retrieved with retrieve-mathjax. The handler htmltm-mathjax later imports it with the LaTeX importer (parse-latex and latex->texmacs).
The parser is not aware of namespaces. xmltm-parse therefore rewrites all element and attribute names with normalized prefixes: h: for XHTML (and any HTML without namespace), m: for MathML, x: for the reserved xml namespace, and g: and c: for the Coq formats which use the same machinery. The xmlns attributes are consumed in the process. A <math> element of HTML5 without namespace thus becomes h:math; the handler htmltm-math renames the prefixes of its content to m: before calling the MathML importer.
The generic dispatcher sxml-dispatch (
xmltm.scm
) splits the name of an element into prefix and local name and looks up the local name in the table of the namespace: htmltm-methods% for h, mathtm-methods% for m. Elements without entry are handled by a pass method, which converts the content and keeps it (htmltm-pass); strings are converted by xmltm-text. Every method has the signature (env attributes content) and returns a list which is either empty or contains one TeXmacs stree.
Most entries of htmltm-methods% are produced by
Defined in
xmltm.scm
; within
htmltm.scm
it is abbreviated as handler. model describes the treatment of white space in the content: :empty (empty element), :element (text nodes are ignored), :mixed (leading and trailing white space dropped, internal white space collapsed), :collapse (collapsed but kept at the ends) or :pre (preserved). kind is :block or :inline; block handlers always return a document. method is either a procedure, the name of a unary TeXmacs macro (a string, e.g. "strong"), a list such as (with "font-series" "bold") to which the converted content is appended, or a literal node list. The handler also turns an id attribute into a label.
For instance
(h2 (handler :mixed :block "section*"))
(em (handler :collapse :inline "em"))
(b (handler :collapse :inline '(with "font-series" "bold")))
(ul (handler :element :block "itemize"))
(img (handler :empty :inline htmltm-wikipedia-image)) |
Elements which have no meaning in TeXmacs (head, script, style, forms, frames, …) are dropped with htmltm-drop. The specific handlers deal with tables (htmltm-table, which computes borders, widths, alignments and cell spans with the helpers of
convert/tools/old-tmtable.scm
), list items, anchors and links (htmltm-anchor), images (htmltm-image), the deprecated font element, line breaks, and a few special cases: TeX formulas given as images with class tex or as span elements in Wikipedia pages (htmltm-wikipedia-image, htmltm-wikipedia-span), and pre elements of the Scilab documentation (htmltm-scilab-pre).
The converted children are assembled into serials with htmltm-serial and the constructors of
convert/tools/stm.scm
(stm-serial, stm-concat): inline material becomes concat nodes, and block material is collected into a document whose paragraphs are the blocks and lines. An invariant of the implementation is that block structures are always wrapped into a unary document, which is why block elements must be declared with the kind :block. After the conversion, html-postproc replaces non-breaking spaces and straight double quotes, the tree is simplified, and clean_html (
Data/Convert/Xml/cleanhtml.cpp
) removes superfluous documents and white space, compresses list items and converts above/below constructs produced by MathML into big operators with scripts.
MathML elements are converted by
convert/mathml/mathtm.scm
with the dispatch table mathtm-methods% (entries made by mathtm-handler): mi, mn, mo and mtext become strings or TeXmacs symbols, mfrac, msqrt, mroot, the script elements and munder/mover become the corresponding primitives, mtable becomes a tabular, and so on. Operators, symbols and accents are translated with the tables of
convert/mathml/mathml-drd.scm
(mathml-operator->tm%, mathml-symbol->tm%, mathml-above->tm%, etc.). The function mathml->tree imports a standalone MathML string.
If the preference "mathml->texmacs:latex-annotations" is on, a semantics element with a TeX annotation (application/x-tex) is imported from the annotation with the LaTeX importer instead of from the presentation markup (mathtm-semantics). This is often more faithful for pages generated from LaTeX, such as Wikipedia.
The export to MathML is described in exporting to HTML.
Add an entry to htmltm-methods% in
htmltm.scm
, using handler with the appropriate white space model and kind. If the element maps to a unary macro, a string is enough; otherwise write a method (define (htmltm-my-element env a c) …) which uses htmltm-args-serial to convert the content and shtml-attr-non-null to read attributes.
For MathML, add an entry to mathtm-methods% in
mathtm.scm
, or a symbol to the tables of
mathml-drd.scm
.
Add a regression test to
convert/html/htmltm-test.scm
(or
convert/mathml/mathtm-test.scm
).
(regtest-htmltm), (regtest-xmltm) and (regtest-mathtm) run the regression tests of the importer (
convert/html/htmltm-test.scm
,
convert/tools/xmltm-test.scm
,
convert/mathml/mathtm-test.scm
); they are part of (run-all-tests). In a Scheme session, (parse-html-snippet s) shows the normalized sxml and (html->texmacs (parse-html-snippet s)) the resulting stree.
CSS is ignored: style sheets and most style attributes are dropped, so that the visual formatting of modern pages is largely lost. The import is mainly useful for the textual and structural content.
JavaScript is not executed, so that content generated by scripts is missing.
The MathJax pre-processing only recognizes pages which load a file named MathJax.js; pages loading MathJax 3 by other file names (such as the pages produced by the TeXmacs exporter itself) are imported without it.
The heuristics for badly formed HTML follow the rules of HTML 4 and may produce unexpected structures for HTML5 pages.