GNU TeXmacsUser manualReferenceAboutEnglish
Importing HTML and MathML

1.The stages of the import

An HTML document is imported along the path html-document \(\rightarrow\) html-stree \(\rightarrow\) texmacs-stree of the converter graph:

  1. 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.

  2. 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.

2.Parsing

2.1.The XML/HTML parser

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:

  1. 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.

  2. 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).

  3. 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

.

2.2.MathJax

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).

2.3.Namespace normalization

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.

3.The converter

3.1.Dispatch

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.

3.2.Handlers

Most entries of htmltm-methods% are produced by

(htmltm-handler model kind method args->serial)
(make an entry for htmltm-methods%)

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).

3.3.Building the TeXmacs tree

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.

4.MathML

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.

5.How to add support for a new element

  1. 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.

  2. For MathML, add an entry to mathtm-methods% in

    mathtm.scm

    , or a symbol to the tables of

    mathml-drd.scm

    .

  3. Add a regression test to

    convert/html/htmltm-test.scm

    (or

    convert/mathml/mathtm-test.scm

    ).

6.Testing

(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.

7.Known limitations

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".