Wiki
Download
Manual
Eggs
API
Tests
Bugs
show
edit
You can edit this page using
wiki syntax
for markup.
Article contents:
[[tags: egg]] == multidoc Convert SXML documents to HTML, LaTeX, Texinfo, Markdown, plain text and other formats. [[toc:]] === Description multidoc reads documents written in an SXML form based on the CHICKEN wiki, as produced by svnwiki-sxml, into an intermediate representation (IR), and converts the IR to a document format by means of one of several writer backends. The pipeline has three stages, based on the structure of pandoc: # a '''reader''' turns SXML into the IR; # '''filters''' (procedures from documents to documents) transform the IR; # a '''writer''' turns the IR into a {{wl-pprint}} document, which is laid out at a chosen line width. The IR is a closed set of algebraic data types which can be extended in three ways: * sections represent a named ''division kind'' (part, chapter, section, ...), so a document can be placed at any depth of a larger hierarchy; * custom nodes' represent new kinds of content; each comes with a lowering into core IR, so every writer can render it, and writers may register their own handlers for it; * attributes represent a ''style'' whose properties are addressed to particular writers. Writers are defined as Yasos objects that are kept in a registry. {{multidoc}} providres functionality to register new writers, or derive a writer from an existing one and override part of its behavior. === Author [[/users/ivan-raikov|Ivan Raikov]] === Requirements [[datatype]], [[matchable]], [[yasos]], [[wl-pprint]], [[getopt-long]], [[srfi-1]], [[srfi-13]], [[srfi-14]], [[srfi-69]]; [[test]] for the tests. === Quick start <enscript highlight="scheme"> (import multidoc) ;; Convert an SXML document to LaTeX on standard output. (convert (with-input-from-file "page.sxml" read) to: 'latex width: 72) ;; Or step by step: read, filter, render to a string. (define d ((shift-divisions 1) (sxml->doc (with-input-from-file "page.sxml" read)))) (layout->string (render d to: 'markdown width: 72) 72) </enscript> From the command line: multidoc -t texinfo -o page.texi page.sxml multidoc -t markdown --auto-ext *.sxml === The command line multidoc [OPTION ...] [FILE.sxml ...] With no {{FILE}}, or with {{-}}, the document is read from standard input. Options are parsed with [[getopt-long]]: a long option takes its value after {{=}} ({{--to=latex}}), a short option as the next argument ({{-t latex}}), and short options without values may be combined ({{-h}}). Options that may be repeated take effect in order, so a later {{-V}} for the same key wins. ; {{-t}}, {{--to=FORMAT}} : output format (default {{html}}); {{--list-formats}} lists them ; {{-w}}, {{--width=N}} : line width for filling text (default 80) ; {{-o}}, {{--output=PATH}} : write to {{PATH}} instead of standard output; {{-o -}} is standard output ; {{--auto-ext}} : write each {{FILE.sxml}} to {{FILE.<extension>}} ; {{--fragment}} : omit headers and wrappers ; {{--top-division=KIND}} : division kind of wiki level 2 (default {{section}}) ; {{--shift-divisions=N}} : move all sections {{N}} levels down (up if negative) ; {{--number-sections}} : number section headings, where the format allows ; {{-V}}, {{--option=KEY=VALUE}} : set a writer option; the value is read as a number, {{#t}}, {{#f}}, a list, or else a string ; {{--citations=NAME}} : citation processor: {{native}} (the default) or {{plain-keys}}, or one registered by a library ; {{--xref-in=FILE}} : read cross-references of other documents from {{FILE}}; may be given several times ; {{--xref-out=FILE}} : write the cross-references of the input documents to {{FILE}} ; {{--doc-name=NAME}} : document name for the cross-references of standard input ; {{--check}} : report, for each custom node in the input, how the writer renders it ; {{--list-formats}} : list the registered output formats When cross-references are used, the targets of all input files are collected before any is written, so links between the pages of one run resolve without a database file: multidoc -t html --auto-ext -V xref-suffix=.html --xref-out=site.xref *.sxml The exit status is 0 on success, 1 if a document could not be converted, and 2 for bad usage. === Reading SXML <procedure>(sxml->doc tree #!key (top-division 'section))</procedure> Reads a document. {{tree}} may be a {{(wiki-page (Header ...) (body ...))}} element, a {{(body ...)}} element, a single element, or a list of elements. The fields of {{(Header ...)}} and a top-level {{(tags ...)}} become document metadata. {{top-division}} is the division kind that wiki level 2 (the top of a page) becomes. The reader knows these elements: ; sections : {{section}}, {{section*}} (unnumbered): {{(section LEVEL TITLE CONTENT ...)}} ; blocks : {{p}}, {{pre}}, {{verbatim}}, {{highlight}}, {{blockquote}}, {{div}}, {{hr}}, {{toc}}, {{bibliography}} ; lists : {{ul}}, {{ol}}, {{li}}, {{dl}}, {{dt}}, {{dd}} ; definitions : {{(def (sig (procedure "(f x)") ...) CONTENT ...)}} ; examples : {{(examples (example (init ..) (expr ..) (result ..)) ...)}} ; tables : {{table}}, {{tr}}, {{th}}, {{td}}, with the attributes {{border}}, {{align}}, {{caption}}, {{rowspan}} and {{colspan}} ; inline markup : {{tt}}, {{em}}, {{i}}, {{strong}}, {{b}}, {{small}}, {{br}}, {{n_}} ; links and images : {{(link URL [TEXT])}}, {{(int-link PAGE#SECTION [TEXT])}}, {{(image-link SRC [ALT])}}, {{img}} ; raw content : {{tex}}, {{texinfo}}, {{script}} ; references : {{(ref KEY [TEXT])}}; {{KEY}} is an identifier of this document or {{PAGE#KEY}} for another document ; citations : {{(cite KEY ...)}} with the optional attributes {{mode}} ({{normal}}, {{author-in-text}} or {{suppress-author}}), {{prefix}}, {{suffix}} and {{locator}} A {{(label "id")}} child gives its parent element an identifier, which references can use. A labelled section can still be referred to by its title, as {{int-link}} does, unless another element has the identifier its title would give. Other elements become a {{Div}} or {{Span}} whose class is the element name, so no content is lost. <procedure>(sxml-reader-add-rule! tag rule)</procedure> Adds or replaces the rule for elements named {{tag}}. {{rule}} is called as {{(rule tag attrs children recur)}} and returns a block, an inline, or a list of them. {{attrs}} is the SXML attribute list. {{(recur nodes mode)}} reads nested content; {{mode}} is one of {{items}}, {{inlines}}, {{title}} (inlines with outer spaces removed), {{blocks}} (inline runs become paragraphs) or {{plain}} (inline runs become {{Plain}} blocks). <parameter>sxml-reader-rules</parameter> The rule table, an alist from tags to rules. <enscript highlight="scheme"> (sxml-reader-add-rule! 'note (lambda (tag attrs children recur) (Div (make-attr #f '("note") '() #f) (recur children 'blocks)))) </enscript> === The intermediate representation ==== Inlines {{(Str s)}}, {{(Space)}}, {{(SoftBreak)}}, {{(LineBreak)}}, {{(NBSpace)}}, {{(Emph xs)}}, {{(Strong xs)}}, {{(Small xs)}}, {{(Code attr s)}}, {{(Link attr xs target kind)}} with {{kind}} {{external}} or {{internal}}, {{(Image attr alt src)}}, {{(Span attr xs)}}, {{(RawInline format s)}}, {{(Cite citations xs)}}, {{(Ref kind key xs)}}, {{(CustomInline tag attr payload)}}. ==== Blocks {{(Plain xs)}}, {{(Para xs)}}, {{(CodeBlock attr s)}}, {{(RawBlock format s)}}, {{(BlockQuote blocks)}}, {{(BulletList items)}}, {{(OrderedList items)}} (each item a list of blocks), {{(DefinitionList items)}} (each item {{(term . definitions)}}), {{(Section division attr numbered title body)}}, {{(Definition attr sigs body)}} (each signature {{(kind . inlines)}}), {{(Table attr caption colspecs head bodies foot)}}, {{(TableOfContents)}}, {{(Bibliography attr)}}, {{(HorizontalRule)}}, {{(Div attr blocks)}}, {{(Null)}}, {{(CustomBlock tag attr payload)}}. Use {{cases}} from [[datatype]] to take them apart. ==== Attributes and styles <procedure>(make-attr id classes kvs style)</procedure> <procedure>(make-style name props)</procedure> An attribute holds an identifier (or {{#f}}), a list of class names, a list of {{(key . value)}} strings, and a style (or {{#f}}). The properties of a style are addressed to writers: <enscript highlight="scheme"> (make-style "note" '((html (class "admonition")) (latex (env "quote")))) </enscript> Each writer reads the properties under its own name and ignores the rest. The HTML writer understands {{class}}, {{style}} and {{attributes}}; the LaTeX writer understands {{env}} and {{macro}}. <procedure>(style-property attr section key #!optional default)</procedure> Looks up a style property. {{section}} is a writer name, or {{table}} for the table vocabulary below. ==== Tables Tables have a header part, one or more body parts and a footer part. Cells may span several rows or columns. <procedure>(make-table-part attr rows)</procedure> <procedure>(make-table-row attr cells)</procedure> <procedure>(make-table-cell attr align rowspan colspan blocks)</procedure> <procedure>(make-colspec align width)</procedure> The table style vocabulary, under the key {{table}}, is: {{bold}}, {{italic}}, {{fg}} and {{bg}} (colours as {{"#rrggbb"}}), {{border-top}}, {{border-bottom}}, {{border-left}} and {{border-right}} (widths in points), {{halign}}, {{valign}}, {{padding}}, and on the table itself {{border}}. Each writer renders what its format allows. <procedure>(table-cells table #!key part row col)</procedure> The cells of a {{Table}} block selected by part ({{head}}, {{body}} or {{foot}}), row and column, as {{cell-ref}} records. A selector is a value, a list of values, or a predicate. The column is the grid column, which accounts for spans. <procedure>(table-grid part ncols)</procedure> The grid of a table part: every slot is {{(Origin cell)}}, {{(Covered cell row col)}} (with the coordinates of the covering cell) or {{(Missing)}}. See also {{grid-rows}}, {{grid-ref}}, {{table-fold-cells}}, {{cell-spans?}} and {{cell-colored?}}. ==== Divisions The standard division kinds are, from the outermost: {{part chapter section subsection subsubsection paragraph subparagraph}}. <procedure>(shift-divisions n)</procedure> Returns a filter that moves every section {{n}} levels down (up, if {{n}} is negative), so that a page can become a chapter of a book. <procedure>(define-division-scheme kinds)</procedure> <procedure>(define-division-alias! kind standard)</procedure> A scheme may contain kinds of your own; writers render them as the standard kind of the same rank, or as the kind given by an alias. ==== Custom nodes <procedure>(define-custom-node tag #!key lower (block? #t) collect)</procedure> Declares a custom node kind. {{lower}} is required: it takes an attribute and a payload and returns core IR (a list of blocks, or of inlines for an inline node). {{collect}}, if given, is called during resolution with the attribute, the payload and a procedure {{(add! key value)}} that records an entry in the resolution table. <enscript highlight="scheme"> (define-custom-node 'note lower: (lambda (attr text) (list (Para (list (Strong (list (Str "Note:"))) (Space) (Str text)))))) </enscript> ==== Filters and traversals <procedure>(walk-blocks f blocks)</procedure> <procedure>(walk-inlines f blocks)</procedure> Apply {{f}} bottom-up to every block (or inline). {{f}} returns an element or a list of elements to splice in its place. <procedure>(query-blocks f seed blocks)</procedure> <procedure>(query-inlines f seed blocks)</procedure> Fold {{f}} over every block (or inline), in document order. <procedure>(resolve-doc doc #!key external)</procedure> Gives every section a unique identifier, numbers sections and labelled tables, and returns the document together with a resolution table: reference targets, the section tree, the features the document uses, and the keys of unresolved references. Rendering runs it for you. === References and citations ==== References A {{(Ref kind key text)}} inline refers to the element whose identifier is {{key}}: a section, a table or definition with a {{(label ...)}}, or any element with an {{id}}. Writers render it as a link whose text is {{text}} or, if {{text}} is empty, the title of the target (LaTeX writes {{\autoref}} for numbered targets). With the option {{(ref-style . number)}}, targets are named by kind and number, as in "section 1.2". A reference that cannot be resolved is reported as a warning and rendered as its text, or as {{???}}. ==== Reference databases <procedure>(reference-db? x)</procedure> <procedure>(refdb-lookup-target db doc-name key)</procedure> <procedure>(refdb-add-targets db doc-name targets)</procedure> <procedure>(refdb-lookup-entry db key)</procedure> <procedure>(refdb-targets db)</procedure> <procedure>(refdb-save db path)</procedure> The protocol of reference databases. A database finds the targets of other documents and, if it holds a bibliography, the entry for a citation key. The target of a whole document has the document name as its key. When the option {{refdb}} holds a database, keys that a document does not define are looked up in it: {{"PAGE#KEY"}} names a target of another page, and {{"PAGE"}} the page itself. <procedure>(make-xref-db #!optional targets)</procedure> <procedure>(load-xref-db path ...)</procedure> The built-in database holds cross-references only. Its file has one record per line: (xref (doc "A") (key "intro") (kind section) (number (1)) (title "Intro") (anchor "intro")) Other records in the file are ignored, so bibliography databases may share it. <procedure>(export-xrefs doc doc-name)</procedure> The targets of a document, recorded under {{doc-name}}, including one for the document as a whole. <procedure>(int-links->refs doc)</procedure> Turns links read from {{int-link}} elements into references, so that they are checked. Rendering applies it whenever a database is given; without one, {{int-link}}s stay plain links. The option {{xref-suffix}} (for example {{".html"}}) is appended to the names of other documents in the links that writers produce. ==== Citations A {{(Cite citations text)}} inline holds citation records: <procedure>(make-citation key prefix suffix locator mode)</procedure> A ''citation processor'' rewrites citations and {{(Bibliography)}} blocks before a document is rendered. The option {{citations}} names it (default {{native}}). ; {{native}} : leaves citations to the writer. LaTeX writes {{\cite}}, natbib or biblatex commands (see the option {{latex-citations}}); Markdown writes pandoc citations; other writers show the keys in brackets and warn. ; {{plain-keys}} : writes each citation as its keys in brackets, and the bibliography as the list of cited keys. <procedure>(make-citation-processor name process)</procedure> <procedure>(register-citation-processor! processor)</procedure> <procedure>(lookup-citation-processor name)</procedure> {{process}} is called as {{(process doc refdb options)}} and returns the processed document. {{(cited-keys doc)}} lists the keys of a document in the order they are first cited. === Rendering <procedure>(render doc #!key (to 'html) (width 80) (options '()))</procedure> Renders a document with the writer {{to}} (a writer, or the name of a registered one) and returns a {{wl-pprint}} document. <procedure>(convert sxml #!key (to 'html) (width 80) (options '()) (filters '()) (top-division 'section) port)</procedure> Reads, filters, resolves and writes a document. {{port}} is a port, a path, or {{#f}} for the current output port. <procedure>(layout->string doc width)</procedure> Lays out a {{wl-pprint}} document as a string. ==== Writers and their options Options are given as an alist, for example {{'((standalone . #f) (number-sections . #t))}}. ; {{html}} : XHTML 1.0 Strict. {{standalone}}, {{html-top-heading}} (heading level of the top division, default 2), {{number-sections}}, {{style}}, {{print-style}}, {{canonical}}. ; {{latex}} : {{standalone}}, {{document-class}} (default {{"article"}}), {{class-options}}, {{latex-packages}}, {{latex-preamble}}, {{maketitle}}, {{ref-style}}, {{latex-citations}} ({{cite}}, {{natbib}} or {{biblatex}}), {{bibliography}} (the {{.bib}} file), {{bibliography-style}}. Packages for links, images, spans, colours and citations are added as needed. ; {{texinfo}} : {{standalone}}, {{setfilename}}, {{texinfo-headers}}. The top division is written as {{@chapter}}; every section down to {{@subsubsection}} gets a node, and nodes with children get a menu. ; {{markdown}} : pandoc Markdown. {{standalone}} (YAML metadata block), {{markdown-top-heading}} (default 1). Tables become pipe tables when they can, grid tables otherwise. ; {{plain}} : filled plain text with ASCII tables. {{standalone}}, {{number-sections}}. ; {{native}} : the IR itself, as S-expressions, with custom nodes shown unlowered. All writers understand {{width}}, {{top-division}}, {{refdb}}, {{citations}} and {{xref-suffix}}. With {{(ref-style . number)}}, references to numbered targets are written as kind and number ("section 1.2") instead of by title. === Writing writers A writer is a Yasos object that implements these operations: <procedure>(writer-name w)</procedure> <procedure>(writer-extension w)</procedure> <procedure>(writer-parent w)</procedure> <procedure>(writer-output-kind w)</procedure> <procedure>(write-doc w doc ctx)</procedure> <procedure>(write-block w block ctx)</procedure> <procedure>(write-inline w inline ctx)</procedure> <procedure>(write-text w string ctx)</procedure> <procedure>(write-output w doc ctx dest)</procedure> {{write-doc}}, {{write-block}} and {{write-inline}} return {{wl-pprint}} documents; {{write-text}} escapes a string. {{write-output}} is optional: by default it lays out {{write-doc}} at the width option and writes the text to {{dest}} (a port, a path, or {{#f}}). A writer whose output is not one text stream, such as an archive or a directory of pages, overrides it and reports {{file}} or {{directory}} from {{writer-output-kind}}; the command line then requires {{-o}}. <procedure>(make-writer #!key name extension parent output-kind doc block inline text output)</procedure> Constructs a writer object. {{doc}}, {{block}} and {{inline}} are required; each procedure takes the writer itself followed by the arguments of the operation. <procedure>(register-writer! writer)</procedure> <procedure>(lookup-writer name)</procedure> <procedure>(registered-writers)</procedure> Inside a writer, recurse with these helpers rather than with the writer's own methods. They call the outermost writer, so overrides in a derived writer apply at any depth, and they dispatch custom nodes. <procedure>(render-block ctx block)</procedure> <procedure>(render-blocks ctx blocks)</procedure> <procedure>(render-inline ctx inline)</procedure> <procedure>(render-inlines ctx inlines)</procedure> {{render-inlines}} fills text: a space becomes a line break only where the next element does not fit on the line. Other helpers are {{render-flat}}, {{render-string}}, {{render-text}}, {{vcat}}, {{vsep}}, {{verbatim-lines}}, {{make-escaper}}, {{render-text-table}}, {{ref-target}}, {{ref-label-inlines}}, {{(xref-href ctx target)}} and {{(cite-fallback-inlines ctx citations)}}. The context {{ctx}} holds the options ({{ctx-option}}, and {{ctx-option-symbol}} for options that name a choice and may be given as strings), the resolution table ({{ctx-resolved}}) and the document ({{ctx-doc}}); {{ctx-with-option}} returns a context with one more option. ==== Deriving a writer <procedure>(derive-writer base #!key name extension output-kind doc block inline text output)</procedure> Constructs a writer object that behaves like {{base}} except for the given overrides. An override is called like the base procedure with one more argument, {{next}}, which runs the base behaviour: <enscript highlight="scheme"> (import multidoc datatype (prefix wl-pprint pp:)) ;; HTML in which definitions are shown as boxes. (define boxed-html (derive-writer html-writer name: 'boxed-html block: (lambda (self b ctx next) (cases block b (Definition (a sigs body) (vcat (list (pp:text "<div class=\"box\">") (next b ctx) (pp:text "</div>")))) (else (next b ctx)))))) (register-writer! boxed-html) (convert (with-input-from-file "page.sxml" read) to: 'boxed-html) </enscript> A derived writer also inherits the custom node handlers of its base. ==== Custom node writers <procedure>(define-node-writer tag writer-name handler)</procedure> Registers {{(handler attr payload ctx)}}, which returns a {{wl-pprint}} document, for custom nodes named {{tag}} rendered by the named writer or by writers derived from it. Writers with no handler render the node's lowering. <procedure>(writer-coverage writer doc)</procedure> For each custom node used in {{doc}}, how {{writer}} will render it: {{native}}, {{(inherited . name)}}, {{lowered}} or {{undeclared}}. === Examples The directory {{examples}} of the source distribution contains programs to run with {{csi -s}} from that directory: ; {{convert.scm}} : converts {{guide.sxml}}, a sample page, with every registered writer, and shows {{render}} and {{layout->string}} ; {{build-ir.scm}} : builds a document with the IR constructors, and transforms it with filters and queries ; {{styled-table.scm}} : a table with header and footer parts, spanning cells and styles, in plain text, Markdown, HTML and LaTeX ; {{extend.scm}} : new SXML elements read as custom nodes, node writers for HTML and Texinfo, a derived writer and a new writer ; {{cli.sh}} : the {{multidoc}} program, including a site of two pages whose links are checked and resolved === Version history ; 3.0.0 : Reimplementation with new intermediate representation with division kinds, custom nodes, styles and spanning tables; writers built on wl-pprint and kept in a registry; Markdown, plain text and native writers; references, cross-document reference databases and citation processors; the {{multidoc}} program. The sxml-transforms rule sets of earlier versions are no longer provided. ; 2.3.2 : Last CHICKEN 4 release. === License BSD
Description of your changes:
I would like to authenticate
Authentication
Username:
Password:
Spam control
What do you get when you add 22 to 8?