Wiki source code of Renderer API
Last modified by Vincent Massol on 2026/08/30 19:44
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | Two components turn an XDOM into LaTeX, and both are an ##org.xwiki.rendering.renderer.BlockRenderer##: you pick one by its role hint, hand it a Block and a printer, and read the LaTeX back off the printer. | ||
| 2 | |||
| 3 | |=Role hint|=Produces | ||
| 4 | |##latex/1.0##|A complete document: the preamble, ##\begin{document}##, the content, ##\end{document}##. | ||
| 5 | |##latex+fragment/1.0##|The content alone, with no document wrapper and no leading blank lines, to embed in a document produced elsewhere. | ||
| 6 | |||
| 7 | They ship with the ##latex-syntax## module, and the fragment renderer delegates to ##latex/1.0##, so the two produce the same LaTeX for the same content. | ||
| 8 | |||
| 9 | == From Java == | ||
| 10 | |||
| 11 | {{code language="java"}} | ||
| 12 | @Inject | ||
| 13 | @Named("xwiki/2.1") | ||
| 14 | private Parser parser; | ||
| 15 | |||
| 16 | @Inject | ||
| 17 | @Named("latex/1.0") | ||
| 18 | private BlockRenderer latexRenderer; | ||
| 19 | |||
| 20 | public String toLaTeX(String content) throws ParseException | ||
| 21 | { | ||
| 22 | XDOM xdom = this.parser.parse(new StringReader(content)); | ||
| 23 | WikiPrinter printer = new DefaultWikiPrinter(); | ||
| 24 | this.latexRenderer.render(xdom, printer); | ||
| 25 | return printer.toString(); | ||
| 26 | } | ||
| 27 | {{/code}} | ||
| 28 | |||
| 29 | Render the ##XDOM## itself, not its children. The document wrapper is the XDOM Block's own [[template>>doc:documentation.extensions.dev.latex.template-mechanism.WebHome]], so ##render(xdom.getChildren(), printer)## returns the content with no preamble around it and two blank lines in front of it. Injecting ##latex+fragment/1.0## is the supported way to get a fragment, and the only change the code above needs. | ||
| 30 | |||
| 31 | == From a Wiki Page == | ||
| 32 | |||
| 33 | ##$services.rendering## does the same two steps and takes the same two role hints: | ||
| 34 | |||
| 35 | {{code language="velocity"}} | ||
| 36 | {{velocity}} | ||
| 37 | #set ($xdom = $services.rendering.parse('hello **world**', 'xwiki/2.1')) | ||
| 38 | {{{$services.rendering.render($xdom, 'latex+fragment/1.0')}}} | ||
| 39 | {{/velocity}} | ||
| 40 | {{/code}} | ||
| 41 | |||
| 42 | That prints ##hello \textbf{world}##. The verbatim block around the call keeps the produced LaTeX away from the wiki parser, which would otherwise read part of it as syntax of its own. | ||
| 43 | |||
| 44 | == What the Renderer Is Not == | ||
| 45 | |||
| 46 | It is a ##BlockRenderer## and nothing else. The extension registers no LaTeX parser and no ##PrintRendererFactory##, so LaTeX is not among the syntaxes ##$services.rendering.getAvailableRendererSyntaxes()## returns, it cannot be chosen as the syntax a page is written in, and the two role hints above are the only way to reach it. |