Re: Sphinx integration

Steven Watanabe <[email protected]>
Newsgroups gmane.comp.lib.boost.documentation
Organization Providere Consulting, Inc.
Message-ID <[email protected]>
AMDG

On 10/04/2011 04:13 PM, Dave Abrahams wrote:
> 
>>>>> Also some things like the Math docs have some complex quickbook
>>>>> templates for generating differing content for PDF and HTML output
>>>>> in order to support equations etc.
> 
> See http://www.mathjax.org/
> I think that's probably the best technology for HTML math at this point.
> 

Hey, that looks pretty cool.  The attached
patch changes our DocBook customization layer
to use MathJax for inlineequation.
It only supports LaTeX, right now, but
it should be pretty easy to add MathML
support.

Features:

* Use of MathJax is conditioned on the XSL parameter
  boost.mathjax, which defaults to 0.
* MathJax is only added to the pages that need it.
* If JavaScript is disabled, falls back to a PNG image.

I've tested this with on Boost.Random, it should
also work on Boost.Accumulators which uses LaTeX
in the same way.

In Christ,
Steven Watanabe

_______________________________________________
Boost-docs mailing list
[email protected]
http://lists.boost.org/mailman/listinfo.cgi/boost-docs
boostbook-mathjax.patch (text/x-patch, 3.7 KB)
Index: tools/boostbook/xsl/html-base.xsl
===================================================================
--- tools/boostbook/xsl/html-base.xsl	(revision 74966)
+++ tools/boostbook/xsl/html-base.xsl	(working copy)
@@ -32,6 +32,10 @@
     </xsl:choose>
   </xsl:param>
 
+  <xsl:param name="boost.mathjax" select="0"/>
+  <xsl:param name="boost.mathjax.script"
+             select="'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'"/>
+
   <xsl:param name="admon.style"/>
   <xsl:param name="admon.graphics">1</xsl:param>
   <xsl:param name="boostbook.verbose" select="0"/>
@@ -276,7 +280,90 @@
   </xsl:template>
 
   <xsl:template name="generate.html.title"/>
+
+  <xsl:template match="*" mode="detect-tex">
+    <xsl:variable name="is-chunk">
+      <xsl:call-template name="chunk"/>
+    </xsl:variable>
+    <xsl:if test="$is-chunk = '0'">
+      <xsl:apply-templates mode="detect-tex"/>
+    </xsl:if>
+  </xsl:template>
   
+  <xsl:template match="text()" mode="detect-tex"/>
+  
+  <xsl:template match="textobject[@role='tex']" mode="detect-tex">
+    <xsl:text>1</xsl:text>
+  </xsl:template>
+  
+  <xsl:template name="user.head.content">
+    <xsl:if test="$boost.mathjax">
+      <xsl:variable name="has-tex">
+        <xsl:apply-templates mode="detect-tex" select="*"/>
+      </xsl:variable>
+      <xsl:if test="string($has-tex) != ''">
+        <script type="text/javascript" src="{$boost.mathjax.script}"/>
+      </xsl:if>
+    </xsl:if>
+  </xsl:template>
+
+  <xsl:template name="escape-js-string">
+    <xsl:param name="text"/>
+    <xsl:param name="index" select="1"/>
+    <xsl:if test="$index &lt;= string-length($text)">
+      <xsl:choose>
+        <xsl:when test="substring($text, $index, 1) = '\'">
+          <xsl:text>\\</xsl:text>
+        </xsl:when>
+        <xsl:when test="substring($text, $index, 1) = '&quot;'">
+          <xsl:text>\"</xsl:text>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:value-of select="substring($text, $index, 1)"/>
+        </xsl:otherwise>
+      </xsl:choose>
+      <xsl:call-template name="escape-js-string">
+        <xsl:with-param name="text" select="$text"/>
+        <xsl:with-param name="index" select="$index + 1"/>
+      </xsl:call-template>
+    </xsl:if>
+  </xsl:template>
+  
+  <xsl:template match="inlineequation">
+    <xsl:choose>
+      <xsl:when test="$boost.mathjax = 1 and inlinemediaobject/textobject[@role='tex']">
+        <xsl:variable name="content" select="string(inlinemediaobject/textobject[@role='tex'])"/>
+        <xsl:variable name="plain-content">
+          <xsl:choose>
+            <xsl:when test="substring($content, 1, 1) = '$' and
+                            substring($content, string-length($content), 1) = '$'">
+              <xsl:value-of select="substring($content, 2, string-length($content) - 2)"/>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:value-of select="$content"/>
+            </xsl:otherwise>
+          </xsl:choose>
+        </xsl:variable>
+        <xsl:variable name="escaped-content">
+          <xsl:call-template name="escape-js-string">
+            <xsl:with-param name="text" select="$plain-content"/>
+          </xsl:call-template>
+        </xsl:variable>
+        <script type="text/javascript">
+          <xsl:text>document.write("\\(</xsl:text>
+          <xsl:value-of select="$escaped-content"/>
+          <xsl:text>\\)")</xsl:text>
+        </script>
+        <noscript>
+          <xsl:apply-imports/>
+        </noscript>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:apply-imports/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+  
 <!-- ============================================================ -->
 
 <xsl:template name="output.html.stylesheets">
signature.asc (application/pgp-signature, 490 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQEcBAEBAgAGBQJOnOaKAAoJEDTBQuhymLHSnwQH/31UiduZIv2wfuaoEZTmvr3H
XsoVx7mBcUdv3zylqr1JGK3DuiYZwSu+fGgvnu7xZaYJIIlSluinVpHUPlN19yHn
ZmSPeDo9yxTBdcwZuUDW++y3Jvfcau8TyDDZPCUIy9fBJjX3f2IrBzsmlPTxpJ0q
7xeGvR2vwtsPpKoHVFh9oi+LXe+8s6CNE2b22o0dvW53gO1nwnSqg3+xf0v5/jU5
0Wu/VQv4+zg5+vlNTVlpzAmX9UR1jUMW8yvQmMR3dnGK+0GBHgtWmVkW5uhBiMMO
2t9uT6y9TVTKW3pMit1tMy6Ro1nnIEi1hE8Tfa7rorrXiupv1paAgmoUK5R35ps=
=A0L7
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.