![]() |
Adding converters | ![]() |
New data formats and converters between them are declared using the macros define-format and converter, which are defined in
kernel/texmacs/tm-convert.scm
. TeXmacs maintains a graph of all declared converters and automatically combines them when no direct converter exists between two formats. Formats which can be converted from or into TeXmacs automatically appear in the File\(\rightarrow\)Import and File\(\rightarrow\)Export menus. Typical examples of declarations can be found in the files convert/*/init-*.scm, such as
convert/html/init-html.scm
. The internals of the LaTeX converters are described in the section on conversions.
Each format fm gives rise to several representations, which are distinguished by suffixes: fm-file (a URL), fm-document (a string with the complete document), fm-snippet (a string with a document fragment) and fm-stree (a parsed Scheme tree). For TeXmacs itself, the relevant formats are texmacs-file, texmacs-document, texmacs-snippet, texmacs-stree and texmacs-tree.
Declare a new format name, together with converters between name-file and name-document. The following options are supported:
The name of the format as it appears in menus.
The file suffixes of the format; the first one is the default suffix.
A predicate on strings which recognizes documents in this format (used when the suffix does not determine the format). With the variant :must-recognize, a matching suffix is not sufficient: files which are not recognized by the predicate are opened as verbatim text.
Do not show the format in menus.
Other options (such as :option, which occurs in some format declarations) are currently ignored by define-format; options should be attached to converters instead.
Declare a converter from the format from to the format to (both given as symbols or strings with the suffixes mentioned above). The following options are supported:
The conversion is done by applying fun to the input.
fun takes a second argument: an association list of conversion options.
The conversion is done by an external program prog. The special arguments from and to are replaced by the names of the input and output files. The converter is removed if prog cannot be found in the path.
The converter is only defined if cond evaluates to true. This option (possibly preceded by :penalty) must come first. It allows for alternative implementations of the same converter depending on the availability of external tools: the last valid declaration is retained. Notice that cond is evaluated when the converter declaration is expanded, that is, when the module is loaded.
The cost of the converter (by default \(1.0\)), which is used when searching for the cheapest chain of converters.
Declare an option of the converter, which is stored as a user preference name with the given default value and passed to the converter function in the option list.
For instance:
(define-format blablah
(:name "Blablah")
(:suffix "bla"))
(converter blablah-file latex-file
(:require (url-exists-in-path? "bla2tex"))
(:shell "bla2tex" from ">" to)) |
Convert what from the format from to the format to, using the cheapest chain of declared converters. Returns #f if no such chain exists. For instance, (convert "<b>x</b>" "html-snippet" "texmacs-stree") parses a piece of Html. The variant (convert-to-file what from to dest) writes the result to the file dest.
(converter-search from to)
(converters-from from …)
Return the cheapest chain of formats leading from from to to (or #f), resp. the lists of formats which can be reached from, or converted to, the given formats.
(format-from-suffix suffix)
(format-get-name fm)
Determine the format corresponding to a file suffix, the name of a format as shown in menus, resp. the default suffix of a format.
Promise that module defines formats and converters. This is how the initialization files declare the formats, as in (lazy-format (prog prog-format) scheme) in
init-texmacs.scm
: the module is loaded when the editor has been idle during two seconds, or as soon as information about formats or converters is needed. The names of the formats after module are only informative.