[yelp-xsl] mal2html-api: Experimental extension for API references



commit 6700c07039f36641b40f2696a31434bb107a1093
Author: Shaun McCance <shaunm gnome org>
Date:   Wed Aug 24 11:27:48 2011 -0400

    mal2html-api: Experimental extension for API references
    
    This is the third time I've needed a function to repeat a string,
    so I put one in utils.xsl.

 xslt/common/utils.xsl                |   23 ++++
 xslt/docbook/html/db2html-table.xsl  |   19 +---
 xslt/mallard/html/Makefile.am        |    1 +
 xslt/mallard/html/mal2html-api.xsl   |  198 ++++++++++++++++++++++++++++++++++
 xslt/mallard/html/mal2html-links.xsl |   11 ++-
 xslt/mallard/html/mal2html-table.xsl |   31 ++----
 xslt/mallard/html/mal2xhtml.xsl      |    1 +
 7 files changed, 244 insertions(+), 40 deletions(-)
---
diff --git a/xslt/common/utils.xsl b/xslt/common/utils.xsl
index 8e8e61f..3196f88 100644
--- a/xslt/common/utils.xsl
+++ b/xslt/common/utils.xsl
@@ -30,6 +30,28 @@ the Yelp stylesheets.
 
 
 <!--**==========================================================================
+utils.repeat_string
+Repeat a string a given number of times.
+:Revision:version="1.0" date="2011-08-24" status="final"
+$string: The string to repeat.
+$number: The number of times to repeat ${string}.
+
+This template repeats the ${string} argument ${number} times.
+-->
+<xsl:template name="utils.repeat_string">
+  <xsl:param name="string" select="''"/>
+  <xsl:param name="number" select="0"/>
+  <xsl:if test="$number &gt; 0">
+    <xsl:value-of select="$string"/>
+    <xsl:call-template name="utils.repeat_string">
+      <xsl:with-param name="string" select="$string"/>
+      <xsl:with-param name="number" select="$number - 1"/>
+    </xsl:call-template>
+  </xsl:if>
+</xsl:template>
+
+
+<!--**==========================================================================
 utils.strip_newlines
 Strip leading or trailing newlines from a string.
 :Revision:version="1.0" date="2010-05-25" status="final"
@@ -83,6 +105,7 @@ trailing newlines are ignored to make source formatting easier for authors.
   </xsl:choose>
 </xsl:template>
 
+
 <!--**==========================================================================
 utils.linenumbering
 Number each line in a verbatim environment.
diff --git a/xslt/docbook/html/db2html-table.xsl b/xslt/docbook/html/db2html-table.xsl
index 87f991b..83113ec 100644
--- a/xslt/docbook/html/db2html-table.xsl
+++ b/xslt/docbook/html/db2html-table.xsl
@@ -861,8 +861,7 @@ REMARK: This template needs to be explained in detail, but I forgot how it works
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
-      <xsl:call-template name="copy-string">
-        <xsl:with-param name="count" select="$colspan"/>
+      <xsl:call-template name="utils.repeat_string">
         <xsl:with-param name="string">
           <xsl:choose>
             <xsl:when test="$entry/@morerows">
@@ -872,6 +871,7 @@ REMARK: This template needs to be explained in detail, but I forgot how it works
           </xsl:choose>
           <xsl:text>:</xsl:text>
         </xsl:with-param>
+        <xsl:with-param name="number" select="$colspan"/>
       </xsl:call-template>
       <xsl:choose>
         <xsl:when test="$entry/following-sibling::*[1]">
@@ -945,21 +945,6 @@ REMARK: This template needs to be explained in detail, but I forgot how it works
   </xsl:if>
 </xsl:template>
 
-<!--#* copy-string -->
-<!-- FIXME: replace with str:padding? -->
-<xsl:template name="copy-string">
-  <xsl:param name="count" select="1"/>
-  <xsl:param name="string"/>
-  <xsl:if test="$count &gt; 0">
-    <xsl:value-of select="$string"/>
-    <xsl:call-template name="copy-string">
-      <xsl:with-param name="count" select="$count - 1"/>
-      <xsl:with-param name="string" select="$string"/>
-    </xsl:call-template>
-  </xsl:if>
-</xsl:template>
-
-
 <!-- = entrytbl = -->
 <xsl:template match="entrytbl | db:entrytbl">
   <xsl:variable name="colsep">
diff --git a/xslt/mallard/html/Makefile.am b/xslt/mallard/html/Makefile.am
index 4a0898c..9d19611 100644
--- a/xslt/mallard/html/Makefile.am
+++ b/xslt/mallard/html/Makefile.am
@@ -1,6 +1,7 @@
 xsldir=$(datadir)/yelp-xsl/xslt/mallard/html
 
 xsl_DATA =				\
+	mal2html-api.xsl		\
 	mal2html-block.xsl		\
 	mal2html-facets.xsl		\
 	mal2html-gloss.xsl		\
diff --git a/xslt/mallard/html/mal2html-api.xsl b/xslt/mallard/html/mal2html-api.xsl
new file mode 100644
index 0000000..3b7956f
--- /dev/null
+++ b/xslt/mallard/html/mal2html-api.xsl
@@ -0,0 +1,198 @@
+<?xml version='1.0' encoding='UTF-8'?><!-- -*- indent-tabs-mode: nil -*- -->
+<!--
+This program is free software; you can redistribute it and/or modify it under
+the terms of the GNU Lesser General Public License as published by the Free
+Software Foundation; either version 2 of the License, or (at your option) any
+later version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with this program; see the file COPYING.LGPL.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+-->
+
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
+                xmlns:mal="http://projectmallard.org/1.0/";
+                xmlns:ui="http://projectmallard.org/experimental/ui/";
+                xmlns:e="http://projectmallard.org/experimental/";
+                xmlns:api="http://projectmallard.org/experimental/api/";
+                xmlns:exsl="http://exslt.org/common";
+                xmlns:math="http://exslt.org/math";
+                xmlns:html="http://www.w3.org/1999/xhtml";
+                xmlns="http://www.w3.org/1999/xhtml";
+                exclude-result-prefixes="mal e api exsl math html"
+                version="1.0">
+
+<!--!!==========================================================================
+Mallard to HTML - UI Extension
+Support for Mallard API extension elements.
+
+This stylesheet contains templates to support features from the Mallard API
+extension.
+-->
+
+
+<!--**==========================================================================
+mal2html.api.links.function
+Output links as a synopsis of functions.
+$node: A #{links} element to link from.
+$links: A list of topic links already filtered by group.
+
+This template outputs links as a synopsis according to the programming language
+specified by the #{api:mime} attribute of ${node}. If #{api:mime} is recognized,
+one of the language-specific templates in this stylesheet is called. Otherwise,
+the links are passed to *{mal2html.links.ul}.
+
+This template does not handle titles or other wrapper information for #{links}
+elements. It should be called by an appropriate template that handles the
+#{links} element.
+-->
+<xsl:template name="mal2html.api.links.function">
+  <xsl:param name="node"/>
+  <xsl:param name="links"/>
+  <xsl:choose>
+    <xsl:when test="$node/@api:mime = 'text/x-csrc' or $node/@api:mime = 'text/x-chdr'">
+      <xsl:call-template name="mal2html.api.links.function.c">
+        <xsl:with-param name="node" select="$node"/>
+        <xsl:with-param name="links" select="$links"/>
+      </xsl:call-template>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:call-template name="mal2html.links.ul">
+        <xsl:with-param name="node" select="$node"/>
+        <xsl:with-param name="links" select="$links"/>
+      </xsl:call-template>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
+
+
+<!--**==========================================================================
+mal2html.api.links.function.c
+Output links as a synopsis of C functions.
+$node: A #{links} element to link from.
+$links: A list of topic links already filtered by group.
+
+This template outputs links as a synopsis of C functions. It is called by
+*{mal2html.api.links.function} when the #{api:mime} attribute of ${node} is
+#{text/x-csrc} or #{text/x-chdr}. The target nodes of ${links} are expected
+to have at least an #{api:name} element. Any links whose target does not
+have an #{api:name} element will be passed to *{mal2html.links.ul} after
+the synopsis.
+-->
+<xsl:template name="mal2html.api.links.function.c">
+  <xsl:param name="node"/>
+  <xsl:param name="links"/>
+  <xsl:variable name="out_">
+    <xsl:for-each select="$links">
+      <xsl:sort data-type="number" select="@groupsort"/>
+      <xsl:sort select="mal:title[ type = 'sort']"/>
+      <xsl:variable name="link" select="."/>
+      <xsl:for-each select="$mal.cache">
+        <xsl:variable name="target" select="key('mal.cache.key', $link/@xref)"/>
+        <xsl:variable name="function" select="$target/mal:info/api:function"/>
+        <xsl:choose>
+          <xsl:when test="$function/api:name">
+            <api:pre>
+              <xsl:apply-templates mode="mal2html.inline.mode" select="$function/api:returns/api:type/node()"/>
+              <xsl:variable name="tab" select="20 - string-length($function/api:returns/api:type)"/>
+              <xsl:choose>
+                <xsl:when test="$tab > 1">
+                  <xsl:call-template name="utils.repeat_string">
+                    <xsl:with-param name="string" select="' '"/>
+                    <xsl:with-param name="number" select="$tab"/>
+                  </xsl:call-template>
+                </xsl:when>
+                <xsl:otherwise>
+                  <xsl:text>&#x000A;</xsl:text>
+                  <xsl:text>                    </xsl:text>
+                </xsl:otherwise>
+              </xsl:choose>
+              <a>
+                <xsl:attribute name="href">
+                  <xsl:call-template name="mal.link.target">
+                    <xsl:with-param name="node" select="$node"/>
+                    <xsl:with-param name="xref" select="$link/@xref"/>
+                  </xsl:call-template>
+                </xsl:attribute>
+                <xsl:attribute name="title">
+                  <xsl:call-template name="mal.link.tooltip">
+                    <xsl:with-param name="node" select="$node"/>
+                    <xsl:with-param name="xref" select="$link/@xref"/>
+                  </xsl:call-template>
+                </xsl:attribute>
+                <xsl:value-of select="$function/api:name"/>
+              </a>
+              <xsl:variable name="paren" select="40 - string-length($function/api:name)"/>
+              <xsl:choose>
+                <xsl:when test="$paren > 1">
+                  <xsl:call-template name="utils.repeat_string">
+                    <xsl:with-param name="string" select="' '"/>
+                    <xsl:with-param name="number" select="$paren"/>
+                  </xsl:call-template>
+                </xsl:when>
+                <xsl:otherwise>
+                  <xsl:text>&#x000A;</xsl:text>
+                  <xsl:text>                                                            </xsl:text>
+                </xsl:otherwise>
+              </xsl:choose>
+              <xsl:text>(</xsl:text>
+              <xsl:for-each select="$function/api:arg | $function/api:varargs">
+                <xsl:if test="position() != 1">
+                  <xsl:text>                                                             </xsl:text>
+                </xsl:if>
+                <xsl:choose>
+                  <xsl:when test="self::api:varargs">
+                    <xsl:text>...</xsl:text>
+                  </xsl:when>
+                  <xsl:otherwise>
+                    <xsl:apply-templates mode="mal2html.inline.mode" select="api:type/node()"/>
+                    <xsl:if test="substring(api:type, string-length(api:type)) != '*'
+                                  or not(contains(api:type, ' '))">
+                      <xsl:text> </xsl:text>
+                    </xsl:if>
+                    <xsl:apply-templates mode="mal2html.inline.mode" select="api:name/node()"/>
+                  </xsl:otherwise>
+                </xsl:choose>
+                <xsl:choose>
+                  <xsl:when test="position() != last()">
+                    <xsl:text>,</xsl:text>
+                  </xsl:when>
+                  <xsl:otherwise>
+                    <xsl:text>);</xsl:text>
+                  </xsl:otherwise>
+                </xsl:choose>
+                <xsl:text>&#x000A;</xsl:text>
+              </xsl:for-each>
+              <xsl:if test="not($function/api:arg or $function/api:varargs)">
+                <xsl:text>void);&#x000A;</xsl:text>
+              </xsl:if>
+            </api:pre>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:copy-of select="$link"/>
+          </xsl:otherwise>
+        </xsl:choose>
+      </xsl:for-each>
+    </xsl:for-each>
+  </xsl:variable>
+  <xsl:variable name="out" select="exsl:node-set($out_)"/>
+  <div class="synopsis">
+    <pre class="contents">
+      <xsl:copy-of select="$out/api:pre/node()"/>
+    </pre>
+  </div>
+  <xsl:if test="$out/mal:link">
+    <xsl:call-template name="mal2html.links.ul">
+      <xsl:with-param name="node" select="$node"/>
+      <xsl:with-param name="links" select="$out/mal:link"/>
+    </xsl:call-template>
+  </xsl:if>
+</xsl:template>
+
+</xsl:stylesheet>
diff --git a/xslt/mallard/html/mal2html-links.xsl b/xslt/mallard/html/mal2html-links.xsl
index 0cc9a9e..a41a9d0 100644
--- a/xslt/mallard/html/mal2html-links.xsl
+++ b/xslt/mallard/html/mal2html-links.xsl
@@ -20,9 +20,12 @@ Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
                 xmlns:mal="http://projectmallard.org/1.0/";
                 xmlns:ui="http://projectmallard.org/experimental/ui/";
                 xmlns:e="http://projectmallard.org/experimental/";
+                xmlns:api="http://projectmallard.org/experimental/api/";
                 xmlns:exsl="http://exslt.org/common";
+                xmlns:math="http://exslt.org/math";
+                xmlns:html="http://www.w3.org/1999/xhtml";
                 xmlns="http://www.w3.org/1999/xhtml";
-                exclude-result-prefixes="mal e exsl"
+                exclude-result-prefixes="mal e api exsl math html"
                 version="1.0">
 
 <!--!!==========================================================================
@@ -646,6 +649,12 @@ when determining which links to output.
           </xsl:if>
           <div class="region">
             <xsl:choose>
+              <xsl:when test="$node/self::mal:links/@api:type='function'">
+                <xsl:call-template name="mal2html.api.links.function">
+                  <xsl:with-param name="node" select="$node"/>
+                  <xsl:with-param name="links" select="$_links"/>
+                </xsl:call-template>
+              </xsl:when>
               <xsl:when test="contains($style, ' mouseovers ')">
                 <xsl:call-template name="_mal2html.links.mouseovers">
                   <xsl:with-param name="node" select="$node"/>
diff --git a/xslt/mallard/html/mal2html-table.xsl b/xslt/mallard/html/mal2html-table.xsl
index 8d4aa43..032ebfa 100644
--- a/xslt/mallard/html/mal2html-table.xsl
+++ b/xslt/mallard/html/mal2html-table.xsl
@@ -267,19 +267,6 @@ REMARK: Describe this module
   </tfoot>
 </xsl:template>
 
-<!--#* repeat -->
-<xsl:template name="repeat">
-  <xsl:param name="str" select="''"/>
-  <xsl:param name="num" select="0"/>
-  <xsl:if test="$num &gt; 0">
-    <xsl:value-of select="$str"/>
-    <xsl:call-template name="repeat">
-      <xsl:with-param name="str" select="$str"/>
-      <xsl:with-param name="num" select="$num - 1"/>
-    </xsl:call-template>
-  </xsl:if>
-</xsl:template>
-
 <!-- = tr = -->
 <xsl:template match="mal:tr">
   <xsl:param name="cols"/>
@@ -291,9 +278,9 @@ REMARK: Describe this module
     <xsl:for-each select="mal:td">
       <xsl:text>0:</xsl:text>
       <xsl:if test="@colspan">
-        <xsl:call-template name="repeat">
-          <xsl:with-param name="str" select="'0:'"/>
-          <xsl:with-param name="num" select="number(@colspan) - 1"/>
+        <xsl:call-template name="utils.repeat_string">
+          <xsl:with-param name="string" select="'0:'"/>
+          <xsl:with-param name="number" select="number(@colspan) - 1"/>
         </xsl:call-template>
       </xsl:if>
     </xsl:for-each>
@@ -393,12 +380,12 @@ REMARK: Describe this module
           </xsl:otherwise>
         </xsl:choose>
       </xsl:variable>
-      <xsl:call-template name="repeat">
-        <xsl:with-param name="str">
+      <xsl:call-template name="utils.repeat_string">
+        <xsl:with-param name="string">
           <xsl:value-of select="$rowspan - 1"/>
           <xsl:text>:</xsl:text>
         </xsl:with-param>
-        <xsl:with-param name="num" select="$colspan"/>
+        <xsl:with-param name="number" select="$colspan"/>
       </xsl:call-template>
       <xsl:variable name="nextpos" select="$pos + $colspan"/>
       <xsl:if test="$nextpos &lt;= count($spans)">
@@ -430,9 +417,9 @@ REMARK: Describe this module
     <xsl:for-each select="preceding-sibling::mal:td">
       <xsl:text>.</xsl:text>
       <xsl:if test="@colspan">
-        <xsl:call-template name="repeat">
-          <xsl:with-param name="str" select="'.'"/>
-          <xsl:with-param name="num" select="number(@colspan) - 1"/>
+        <xsl:call-template name="utils.repeat_string">
+          <xsl:with-param name="string" select="'.'"/>
+          <xsl:with-param name="number" select="number(@colspan) - 1"/>
         </xsl:call-template>
       </xsl:if>
     </xsl:for-each>
diff --git a/xslt/mallard/html/mal2xhtml.xsl b/xslt/mallard/html/mal2xhtml.xsl
index 0724c96..5822022 100644
--- a/xslt/mallard/html/mal2xhtml.xsl
+++ b/xslt/mallard/html/mal2xhtml.xsl
@@ -42,6 +42,7 @@ REMARK: Describe this module
 <xsl:param name="mal.if.env" select="'html xhtml'"/>
 <xsl:param name="mal.link.extension" select="$html.extension"/>
 
+<xsl:include href="mal2html-api.xsl"/>
 <xsl:include href="mal2html-block.xsl"/>
 <xsl:include href="mal2html-facets.xsl"/>
 <xsl:include href="mal2html-gloss.xsl"/>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]