home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / manual / header.xslt < prev    next >
Encoding:
Extensible Markup Language  |  2008-02-03  |  8.1 KB  |  247 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3.  ScummC
  4.  Copyright (C) 2008  Alban Bedel
  5.  
  6.  This program is free software; you can redistribute it and/or
  7.  modify it under the terms of the GNU General Public License
  8.  as published by the Free Software Foundation; either version 2
  9.  of the License, or (at your option) any later version.
  10.  
  11.  This program is distributed in the hope that it will be useful,
  12.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  GNU General Public License for more details.
  15.  
  16.  You should have received a copy of the GNU General Public License
  17.  along with this program; if not, write to the Free Software
  18.  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  19. -->
  20. <!-- Add entities for new-line and quote -->
  21. <!DOCTYPE xsl [
  22.   <!ENTITY nl " ">
  23.   <!ENTITY q  """>
  24. ]>
  25. <xsl:stylesheet version="1.0"
  26.                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  27.                 xmlns="http://www.w3.org/1999/xhtml">
  28.  
  29.   <xsl:output method='text' version='1.0' encoding='UTF-8' indent='yes'/>
  30.   <xsl:strip-space elements="*"/>
  31.  
  32.   <xsl:template match="/">
  33.     <xsl:text>/* This file was generated, do not edit. */&nl;</xsl:text>
  34.     <xsl:apply-templates select="man/command"/>
  35.   </xsl:template>
  36.  
  37.   <xsl:template match="command">
  38.     <!-- Write the parameters definitions -->
  39.     <xsl:apply-templates select="." mode="desc-groups"/>
  40.  
  41.     <!-- Write the help definition -->
  42.     <xsl:text>static scc_help_t </xsl:text>
  43.     <xsl:apply-templates select="@name" mode="c-sym"/>
  44.     <xsl:text>_help = {&nl;</xsl:text>
  45.  
  46.     <xsl:text>  .name = &q;</xsl:text>
  47.     <xsl:apply-templates select="@name" mode="c-string"/>
  48.     <xsl:text>&q;,&nl;</xsl:text>
  49.  
  50.     <xsl:text>  .usage = &q;</xsl:text>
  51.     <xsl:apply-templates mode="usage"/>
  52.     <xsl:text>&q;,&nl;</xsl:text>
  53.  
  54.     <xsl:text>  .param_help = </xsl:text>
  55.     <xsl:apply-templates select="@name" mode="c-sym"/>
  56.     <xsl:text>_param_help&nl;</xsl:text>
  57.  
  58.     <xsl:text>};&nl;</xsl:text>
  59.   </xsl:template>
  60.  
  61.   <xsl:template match="*"/>
  62.  
  63.   <xsl:template match="*" mode="desc"/>
  64.  
  65.   <!-- Write a parameters list -->
  66.   <xsl:template match="command|param-group" mode="desc-groups">
  67.     <!-- Write the child lists first -->
  68.     <xsl:apply-templates select="param-group" mode="desc-groups"/>
  69.     <!-- Then write this list -->
  70.     <xsl:text>static scc_param_help_t </xsl:text>
  71.     <xsl:apply-templates select="@name" mode="c-sym"/>
  72.     <xsl:text>_param_help[] = {&nl;  </xsl:text>
  73.     <xsl:apply-templates select="param-group|param" mode="desc"/>
  74.     <xsl:text>{}&nl;};&nl;&nl;</xsl:text>
  75.   </xsl:template>
  76.  
  77.   <!-- Parameter entry for a sub group -->
  78.   <xsl:template match="param-group" mode="desc">
  79.     <xsl:text>{&nl;</xsl:text>
  80.  
  81.     <xsl:text>    .name = &q;</xsl:text>
  82.     <xsl:apply-templates select="@name" mode="c-string-up"/>
  83.     <xsl:text>&q;,&nl;</xsl:text>
  84.  
  85.     <xsl:text>    .group = </xsl:text>
  86.     <xsl:apply-templates select="@name" mode="c-sym"/>
  87.     <xsl:text>_param_help&nl;  },</xsl:text>
  88.   </xsl:template>
  89.  
  90.   <!-- Parameter description -->
  91.   <xsl:template match="param" mode="desc">
  92.     <xsl:text>{&nl;</xsl:text>
  93.  
  94.     <xsl:text>    .name = &q;</xsl:text>
  95.     <xsl:apply-templates select="@name" mode="c-string"/>
  96.     <xsl:text>&q;,&nl; </xsl:text>
  97.  
  98.     <xsl:text>   .arg = </xsl:text>
  99.     <xsl:choose>
  100.       <xsl:when test="@arg">
  101.         <xsl:text>&q;</xsl:text>
  102.         <xsl:apply-templates select="@arg" mode="c-string"/>
  103.         <xsl:text>&q;,&nl;</xsl:text>
  104.       </xsl:when>
  105.       <xsl:otherwise>NULL,&nl;</xsl:otherwise>
  106.     </xsl:choose>
  107.  
  108.     <xsl:text>    .dfault = </xsl:text>
  109.     <xsl:choose>
  110.       <xsl:when test="@default">
  111.         <xsl:text>&q;</xsl:text>
  112.         <xsl:apply-templates select="@default" mode="c-string"/>
  113.         <xsl:text>&q;,&nl;</xsl:text>
  114.       </xsl:when>
  115.       <xsl:otherwise>NULL,&nl;</xsl:otherwise>
  116.     </xsl:choose>
  117.  
  118.  
  119.     <xsl:text>    .desc = </xsl:text>
  120.     <xsl:text>&q;</xsl:text>
  121.     <xsl:apply-templates select="." mode="desc-txt"/>
  122.     <xsl:text>&q;&nl;  },</xsl:text>
  123.  
  124.   </xsl:template>
  125.  
  126.   <!-- Write the description of a parameter -->
  127.   <xsl:template match="param" mode="desc-txt">
  128.     <xsl:choose>
  129.       <xsl:when test="short">
  130.         <xsl:apply-templates select="short" mode="c-string"/>
  131.       </xsl:when>
  132.       <xsl:otherwise>
  133.         <xsl:apply-templates mode="c-string"/>
  134.       </xsl:otherwise>
  135.     </xsl:choose>
  136.   </xsl:template>
  137.  
  138.   <!-- Write the usage line -->
  139.   <xsl:template match="file|param|param-group" mode="usage">
  140.     <xsl:if test="position() > 1">
  141.       <xsl:text> </xsl:text>
  142.     </xsl:if>
  143.     <xsl:if test="not(@required = 'true')">
  144.       <xsl:text>[</xsl:text>
  145.     </xsl:if>
  146.     <xsl:apply-templates select="@name" mode="usage"/>
  147.     <xsl:if test="@arg">
  148.       <xsl:text> </xsl:text>
  149.       <xsl:apply-templates select="@arg" mode="c-string"/>
  150.     </xsl:if>
  151.     <xsl:if test="@repeat = 'true'"> ...</xsl:if>
  152.     <xsl:if test="not(@required = 'true')">
  153.       <xsl:text>]</xsl:text>
  154.     </xsl:if>
  155.   </xsl:template>
  156.  
  157.   <!-- Write usage parts with c string escaping -->
  158.   <xsl:template match="*/@name" mode="usage">
  159.     <xsl:apply-templates select="." mode="c-string"/>
  160.   </xsl:template>
  161.  
  162.   <!-- Write group names in upper case -->
  163.   <xsl:template match="param-group/@name" mode="usage">
  164.     <xsl:apply-templates select="." mode="c-string-up"/>
  165.   </xsl:template>
  166.  
  167.   <!-- Add a - prefix to parameter names -->
  168.   <xsl:template match="param/@name" mode="usage">
  169.     <xsl:text>-</xsl:text>
  170.     <xsl:apply-templates select="." mode="c-string"/>
  171.   </xsl:template>
  172.  
  173.   <!-- Write something as a valid c string -->
  174.   <xsl:template match="*" mode="c-string">
  175.     <xsl:apply-templates mode="c-string"/>
  176.   </xsl:template>
  177.  
  178.   <!-- Write text and attributes values as normalized c string -->
  179.   <xsl:template match="text()|@*" mode="c-string">
  180.     <xsl:call-template name="c-string-escape">
  181.       <xsl:with-param name="str" select="normalize-space(.)"/>
  182.     </xsl:call-template>
  183.   </xsl:template>
  184.  
  185.   <!-- Write text and attributes values as uppercase normalized c string -->
  186.   <xsl:template match="@*|*" mode="c-string-up">
  187.     <xsl:call-template name="c-string-escape">
  188.       <xsl:with-param name="str" select="translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
  189.     </xsl:call-template>
  190.   </xsl:template>
  191.  
  192.   <!-- Escape " and \ -->
  193.   <xsl:template name="c-string-escape">
  194.     <xsl:param name="str"/>
  195.     <xsl:choose>
  196.       <xsl:when test="contains($str,'&q;')">
  197.         <xsl:call-template name="c-string-escape">
  198.           <xsl:with-param name="str" select="substring-before($str,'&q;')"/>
  199.         </xsl:call-template>
  200.         <xsl:text>\&q;</xsl:text>
  201.         <xsl:call-template name="c-string-escape">
  202.           <xsl:with-param name="str" select="substring-after($str,'&q;')"/>
  203.         </xsl:call-template>
  204.       </xsl:when>
  205.       <xsl:when test="contains($str,'\')">
  206.         <xsl:call-template name="c-string-escape">
  207.           <xsl:with-param name="str" select="substring-before($str,'\')"/>
  208.         </xsl:call-template>
  209.         <xsl:text>\\</xsl:text>
  210.         <xsl:call-template name="c-string-escape">
  211.           <xsl:with-param name="str" select="substring-after($str,'\')"/>
  212.         </xsl:call-template>
  213.       </xsl:when>
  214.       <xsl:otherwise>
  215.         <xsl:value-of select="$str"/>
  216.       </xsl:otherwise>
  217.     </xsl:choose>
  218.   </xsl:template>
  219.  
  220.   <!-- Create a valid symbol name out of some text -->
  221.   <xsl:template match="text()|@*|*" mode="c-sym">
  222.     <xsl:call-template name="c-sym-escape">
  223.       <xsl:with-param name="str" select="normalize-space(.)"/>
  224.     </xsl:call-template>
  225.   </xsl:template>
  226.  
  227.   <!-- Convert anything not in a-Z0-9_ to _ -->
  228.   <xsl:template name="c-sym-escape">
  229.     <xsl:param name="str"/>
  230.     <xsl:choose>
  231.       <xsl:when test="contains('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_',substring($str,0,2))">
  232.         <xsl:value-of select="substring($str,0,2)"/>
  233.       </xsl:when>
  234.       <xsl:otherwise>
  235.         <xsl:text>_</xsl:text>
  236.       </xsl:otherwise>
  237.     </xsl:choose>
  238.     <xsl:if test="substring($str,2)">
  239.       <xsl:call-template name="c-sym-escape">
  240.         <xsl:with-param name="str" select="substring($str,2)"/>
  241.       </xsl:call-template>
  242.     </xsl:if>
  243.   </xsl:template>
  244.  
  245.  
  246. </xsl:stylesheet>
  247.