3

Every time a question about parsing tex syntaxes come up, the first answer is: it's not possible and here is an (convuluted) example why.

So I'm trying to narrow the scope of the question to what I call classic syntax:

\command[optional]{mandatory}{mandatory}

Or in the case of ConTeXt:

\command[non-print][key=value]{arguemnt}...
4
  • 1
    \command[optional][optional]{mandatory}{mandatory} is arguably not classical LaTeX if things aren't allowed to get convoluted.
    – jon
    Sep 14, 2015 at 4:57
  • @jon You're absolutely right, I'll edit the question accordingly.
    – Zii8roZi
    Sep 14, 2015 at 5:18
  • Depends on why you want to parse the input. For ConTeXt, the "easiest" solution is to export your output to XML and then use an XML parsing lib to parse the output. That is signifanctly easier than parsing ConTeXt input.
    – Aditya
    Sep 14, 2015 at 16:34
  • @Aditya I want to generate html tags with the name of the command <span class=command-name> and do the work in css or javascript. I'll take a look into the xml export of ConTeXt. Thanks.
    – Zii8roZi
    Sep 16, 2015 at 5:04

1 Answer 1

4

If the objective is to generate an HTML file, then the ConTeXt export mechanism is the best option. Here is a test file:

\setupbackend[export=yes]

\starttext
\startsection[title={Test of export}]
  This is to test how \CONTEXT\ export works. First, we input a test file
  below.

  \startnarrower
    \input ward
  \stopnarrower

 Let's see some other environments, such as\footnote{Note that we define a
 float in \in{Figure}[fig:mill].}

  \startitemize[n]
    \item A table:

      \startTABLE
        \NC 1 \NC 2 \NC 3 \NC \NR
        \NC 1 \NC 2 \NC 3 \NC \NR
        \NC 1 \NC 2 \NC 3 \NC \NR
      \stopTABLE

    \item A formula

      \startformula
        α^2 + β^2 = 2
      \stopformula

    \item A figure

      \externalfigure[mill.png][width=5cm]

  \stopitemize

  \startplacefigure
      [title={This is a Mill}, reference={fig:mill}]
    \externalfigure[mill.png][width=5cm]
  \stopplacefigure

\stopsection
\stoptext

When you compile it using context filename, you get a director filename-export that contains the following:

  • images directory
  • styles directory (containing css files corresponding to the different settings for the environments)
  • filename-div.xhtml (each environment is exported as <div class=...>)
  • filename-tag.xhtml (each environment is exported as <environment>)
  • filename-raw.xml (an xml document, similar to tag-test.xhtml)
  • filename-pub.lua (a helper program to help generate ePub file).

For example, for the above file, the filename-div.xhtml file is:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<!--

    input filename   : test
    processing date  : Wed Sep 16 12:28:10 2015
    context version  : 2015.09.13 13:31
    exporter version : 0.34

-->



<html xmlns="http://www.w3.org/1999/xhtml" xmlns:math="http://www.w3.org/1998/Math/MathML">

    <head>

        <meta charset="utf-8"/>

        <title>no title</title>

<link type="text/css" rel="stylesheet" href="styles/test-defaults.css" />
<link type="text/css" rel="stylesheet" href="styles/test-images.css" />
<link type="text/css" rel="stylesheet" href="styles/test-styles.css" />


    </head>
    <body>
        <div xmlns="http://www.pragma-ade.com/context/export">

<div class="warning">Rendering can be suboptimal because there is no default/fallback css loaded.</div>

<div>
 <div class="metadata">
 </div>
 <div id="aut-1" class="section level-3">
  <div class="sectionnumber">1</div>
   <div class="sectiontitle">Test of export</div>
  <div class="sectioncontent">
This is to test how ConTEXt export works. First, we input a test file below.
   <div class="break"><!--empty--></div>
The Earth, as a habitat for animal life, is in old age and has a fatal illness. Several, in fact. It would be happening whether humans had ever evolved or not. But our presence is like the effect of an old-age patient who smokes many packs of cigarettes per day <div class="subsentencesymbol symbol-middle">—</div> and we humans are the cigarettes.
   <div class="break"><!--empty--></div>
Let’s see some other environments, such as<div class="descriptionsymbol footnote insert-1"><div class="sup">1</div></div> 
   <div class="itemgroup itemize level-1 symbol-n">
    <div class="item">
     <div class="itemtag">1.</div>
     <div class="itemcontent">A table:<div class="table"><div class="tablerow"><div class="tablecell align-flushleft"> 1 </div><div class="tablecell align-flushleft"> 2 </div><div class="tablecell align-flushleft"> 3 </div></div><div class="tablerow"><div class="tablecell align-flushleft"> 1 </div><div class="tablecell align-flushleft"> 2 </div><div class="tablecell align-flushleft"> 3 </div></div><div class="tablerow"><div class="tablecell align-flushleft"> 1 </div><div class="tablecell align-flushleft"> 2 </div><div class="tablecell align-flushleft"> 3 </div></div></div></div>
    </div>
    <div class="item">
     <div class="itemtag">2.</div>
     <div class="itemcontent">A formula<div class="formula"> <div class="formulacontent"><m:math display="block" xmlns:m="http://www.w3.org/1998/Math/MathML"><m:mrow><m:msup><m:mi>𝛼</m:mi><m:mn>2</m:mn></m:msup><m:mo>+</m:mo><m:msup><m:mi>𝛽</m:mi><m:mn>2</m:mn></m:msup><m:mo>=</m:mo><m:mn>2</m:mn></m:mrow></m:math></div> </div></div>
    </div>
    <div class="item">
     <div class="itemtag">3.</div>
     <div class="itemcontent">A figure<div id="image-1" class="image label-3.750cm"><!--empty--></div></div>
    </div>
   </div>
   <div id="fig-mill" class="float figure">
    <div class="floatcontent"><div id="image-2" class="image label-3.750cm"><!--empty--></div></div>
    <div class="floatcaption"><div class="floatlabel figure">Figure </div><div class="floatnumber figure">1</div> <div class="floattext">This is a Mill</div></div>
    </div>
   <div class="description footnote insert-1">
    <div class="descriptiontag"><div class="sup">1</div> </div>
    <div class="descriptioncontent">Note that we define a float in Figure 1.</div>
   </div>
  </div>
 </div>
</div>

        </div>
    </body>
</html>

and the filename-tag.xhtml file is:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<!--

    input filename   : test
    processing date  : Wed Sep 16 12:28:10 2015
    context version  : 2015.09.13 13:31
    exporter version : 0.34

-->

<?xml-stylesheet type="text/css" href="styles/test-defaults.css" ?>
<?xml-stylesheet type="text/css" href="styles/test-images.css" ?>
<?xml-stylesheet type="text/css" href="styles/test-styles.css" ?>

<document xmlns:m="http://www.w3.org/1998/Math/MathML" file="test" context="2015.09.13 13:31" version="0.34" href="test" language="en" date="Wed Sep 16 12:28:10 2015">
 <metadata>
 </metadata>
 <section level="3" detail="section" id="aut:1" chain="section" implicit="1">
  <sectionnumber>1</sectionnumber>
   <sectiontitle>Test of export</sectiontitle>
  <sectioncontent>
This is to test how ConTEXt export works. First, we input a test file below.
   <break/>
The Earth, as a habitat for animal life, is in old age and has a fatal illness. Several, in fact. It would be happening whether humans had ever evolved or not. But our presence is like the effect of an old-age patient who smokes many packs of cigarettes per day <subsentencesymbol symbol="middle">—</subsentencesymbol> and we humans are the cigarettes.
   <break/>
Let’s see some other environments, such as<descriptionsymbol detail="footnote" insert="1"><sup>1</sup></descriptionsymbol> 
   <itemgroup level="1" chain="itemize" detail="itemize" symbol="n">
    <item>
     <itemtag>1.</itemtag>
     <itemcontent>A table:<table><tablerow><tablecell align="flushleft"> 1 </tablecell><tablecell align="flushleft"> 2 </tablecell><tablecell align="flushleft"> 3 </tablecell></tablerow><tablerow><tablecell align="flushleft"> 1 </tablecell><tablecell align="flushleft"> 2 </tablecell><tablecell align="flushleft"> 3 </tablecell></tablerow><tablerow><tablecell align="flushleft"> 1 </tablecell><tablecell align="flushleft"> 2 </tablecell><tablecell align="flushleft"> 3 </tablecell></tablerow></table></itemcontent>
    </item>
    <item>
     <itemtag>2.</itemtag>
     <itemcontent>A formula<formula> <formulacontent><m:math display="block" xmlns:m="http://www.w3.org/1998/Math/MathML"><m:mrow><m:msup><m:mi>𝛼</m:mi><m:mn>2</m:mn></m:msup><m:mo>+</m:mo><m:msup><m:mi>𝛽</m:mi><m:mn>2</m:mn></m:msup><m:mo>=</m:mo><m:mn>2</m:mn></m:mrow></m:math></formulacontent> </formula></itemcontent>
    </item>
    <item>
     <itemtag>3.</itemtag>
     <itemcontent>A figure<image label="3.750cm" name="mill.png" width="5.000cm" height="3.750cm" id="image-1"/></itemcontent>
    </item>
   </itemgroup>
   <float detail="figure" id="fig:mill" explicit="fig:mill" chain="figure" reference="fig:mill" implicit="3">
    <floatcontent><image label="3.750cm" name="mill.png" width="5.000cm" height="3.750cm" id="image-2"/></floatcontent>
    <floatcaption><floatlabel detail="figure">Figure </floatlabel><floatnumber detail="figure">1</floatnumber> <floattext>This is a Mill</floattext></floatcaption>
    </float>
   <description chain="footnote" detail="footnote" insert="1">
    <descriptiontag><sup>1</sup> </descriptiontag>
    <descriptioncontent>Note that we define a float in Figure 1.</descriptioncontent>
   </description>
  </sectioncontent>
 </section>
</document>

The auxiliary css style files ensure that both of these display correctly in the browser. If you want to manipulate the environments, it is much to do so at the XML level rather than the tex level.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .