Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

For LaTeX, I used to rely on htlatex, latex2html, or more rarely tth. I know that we can use XML with ConTeXt, although I have no experience with this, and that Pandoc let us use Markdown syntax and provides several output formats (including ConTeXt and (X)HTML), as well as templates facilities.

However, does there exist a 'tex->html' converter for ConTeXt working out of the box, i.e. one that would accept a raw ConTeXt file (that can produce a PDF file) and transform it to HTML (single or multi-page)?

share|improve this question
4  
The page about epub can serve as a starting point ConTeXtgarden epub. The key is the \setupbackend[export=yes,…] option. –  Marco Feb 1 '12 at 11:23
    
@Marco (+1) Thanks for that. I will have a look into the epub option. –  chl Feb 1 '12 at 12:27
add comment

1 Answer

up vote 19 down vote accepted
+100

ConTeXt does not directly output XHTML, it outputs XML. However the current browsers (at least Opera, Firefox and Chromium) are able to display XML correctly. The XML can be styled using CSS.

When you want real XHTML, you have to transform the XML to XHTML using external tools. ConTeXt standalone ships with an example file: texmf-context/tex/context/base/export-example.{tex,css}. Here is a modified and shortened version of this file for demonstration.

% file example.tex

\setupexport      [width=470pt, hyphen=yes]
\setupbackend     [export=example.xml, css=example.css]
\setupbodyfont    [dejavu]
\setupinteraction [state=start]
\setuplayout      [width=middle]
\setupwhitespace  [big]

\definedescription   [description] [width=broad, alternative=hanging]
\definemathalignment [gather]      [n=1,align={middle}]

\starttext

\startchapter [title=Example]

  \startparagraph
    \input ward
  \stopparagraph

  \startparagraph
    You'll find more information on the ConTeXt garden.
      \startfootnote
        The ConTeXt garden is the ConTeXt wiki.
      \stopfootnote
    Another option is to contact the mailing list.
  \stopparagraph

  \startplacefigure [title=Some cows]
    \startcombination[3*1]
      {\externalfigure[cow.png][width=3cm]} {medium}
      {\externalfigure[cow.png][width=4cm]} {large}
      {\externalfigure[cow.png][width=2cm]} {small}
     \stopcombination
  \stopplacefigure

  \startdescription {Ward}
    \input ward
  \stopdescription

  \startformula
    x_{1,2} = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
  \stopformula

  \starttabulate [|l|r|p|]
    \NC And \NC here  \NC comes       \NC \NR
    \NC a   \NC table \NC \input ward \NC \NR
  \stoptabulate

\stopchapter

\stoptext

And here comes the corresponding CSS file, that is responsible for the styling in the browser.

/* file example.css */

document {
  font-family  : "DejaVu Serif", serif ;
  font-size    : 12pt ;
  line-height  : 18pt ;
  max-width    : 50em ;
  padding      : 1em ;
}

paragraph, p {
  display       : block ;
  margin-top    : 0.5em ;
  margin-bottom : 0.5em ;
}

section {
  display : block ;
}

sectioncontent {
  display       : block ;
  margin-top    : 1em ;
  margin-bottom : 1em ;
}

section[detail="chapter"], section[detail="title"] {
  margin-top    : 3em ;
  margin-bottom : 2em ;
}

section[detail="chapter"]>sectionnumber {
  display      : inline-block ;
  margin-right : 1em ;
  font-size    : 3em ;
  font-weight  : bold ;
}

section[detail="chapter"]>sectiontitle, section[detail="title"]>sectiontitle {
  font-size   : 3em ;
  font-weight : bold ;
}

description {
  display       : block ;
  margin-bottom : 1em ;
  margin-top    : 1em ;
}

descriptiontag {
  float        : left ;
  clear        : left ;
  margin-right : 1em ;
  text-align   : left ;
  font-weight  : bold ;
}

tabulate {
  display       : table ;
  margin-top    : 1em ;
  margin-bottom : 1em ;
  margin-left   : 2.5em ;
}

tabulaterow {
  display : table-row ;
}

tabulatecell[align="middle"] {
  display       : table-cell ;
  text-align    : center ;
  padding-right : 1em ;
}

tabulatecell[align="flushleft"] {
  display       : table-cell ;
  text-align    : left ;
  padding-right : 1em ;
}

tabulatecell[align="flushright"] {
  display       : table-cell ;
  text-align    : right ;
  padding-right : 1em ;
}

tabulatecell {
  display       : table-cell ;
  text-align    : left ;
  padding-right : 1em ;
}

combination {
  display       : table ;
  margin-top    : 0em ;
  margin-bottom : 0em ;
}

combinationpair {
  display       : table-cell ;
  padding-right : 1em ;
}

combinationcontent {
  display    : table-row ;
  text-align : center ;
}

combinationcaption {
  display     : table-row ;
  padding-top : 1ex ;
  text-align  : center ;
}

float {
  display       : block ;
  text-align    : center ;
  margin-top    : 1em ;
  margin-bottom : 1em ;
  margin-left   : 2.5em ;
}

floatcaption {
  display    : block ;
  margin-top : 0.5em ;
  color      : rgb(60%,60%,0%) ;
}

floatlabel {
  font-weight  : bold ;
  margin-right : 1em ;
}

floatnumber {
  font-weight : bold ;
}

formula {
  display       : block ;
  margin-top    : 1em ;
  margin-bottom : 1em ;
  margin-left   : 2.5em ;
}

sup {
  font-size      : xx-small ;
  line-height    : 0 ;
  vertical-align : top ;
}

Here is a screenshot from the PDF version:

result-pdf

And this is a screenshot from the browser:

result-xml

As you can see, the style is pretty close. It can be further optimized through tweaking the CSS file. Included graphics, of course, have to be present in a format that the browser can handle.

Hyphenation is also present. ConTeXt inserts soft hyphen (0x00AD) characters where hyphenation is possible, which enables the browser to justify the paragraphs.

The resulting XML can be displayed by browsers and e-book readers. However, not all e-book reader are compatible, especially when it comes to math. Browsers also have their problems with displaying math.

share|improve this answer
    
(+1) Many (many) thanks for taking a look at this again. What command did you use to compile the above document, context or mtxrun? I was able to reproduce this example, and yours by replacing the \setupbackend options with export=yes,xhtml=yes (and running context on it). Anyway, that deserves a bounty! –  chl May 15 '12 at 21:55
    
Regarding math: ConTeXt generates MathML output. If you want it to work across all browsers, you can add the Mathjax js lib (which also works with MathML) in the generated XHTML file. –  Aditya May 15 '12 at 21:58
    
You can also use \setupbackend[export=example.xml,xhtml=example.xhtml,css=example.css]. I simply compiled with context example. You're welcome, I'm glad I could help. –  Marco May 15 '12 at 22:01
    
Marco, @Aditya: Thanks for your helpful replies. –  chl May 16 '12 at 14:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.