view html.xsl @ 60:588cb9ba7c9c

display of reffed info via colour minimally working
author Henry S. Thompson <ht@markup.co.uk>
date Fri, 02 Jun 2017 15:50:43 +0100
parents f90bbd38b8f6
children 2da454c30b47
line wrap: on
line source

<?xml version='1.0'?>
<!DOCTYPE doc SYSTEM "../../../lib/xml/xsl.dtd" >
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" xmlns:s="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:e="http://markup.co.uk/excel" exclude-result-prefixes="xs s xpf e" xmlns="http://www.w3.org/1999/xhtml" xmlns:xpf="http://www.w3.org/2005/xpath-functions">
 <xsl:strip-space elements="e:a"/>
 <xsl:output omit-xml-declaration="yes"/>
 
 <xsl:include href="n2a.xsl"/>
 
 <xsl:variable name="colours" select="(
    (: Thanks to http://stackoverflow.com/questions/470690/how-to-automatically-generate-n-distinct-colors :)
      '#FFB300', (: Vivid Yellow :)
      '#803E75', (: Strong Purple :)
      '#FF6800', (: Vivid Orange :)
      '#A6BDD7', (: Very Light Blue :)
      '#C10020', (: Vivid Red :)
      '#CEA262', (: Grayish Yellow :)
      '#817066', (: Medium Gray :)
      (: The following don't work well for people with defective color vision :)
      '#007D34', (: Vivid Green :)
      '#F6768E', (: Strong Purplish Pink :)
      '#00538A', (: Strong Blue :)
      '#FF7A5C', (: Strong Yellowish Pink :)
      '#53377A', (: Strong Violet :)
      '#FF8E00', (: Vivid Orange Yellow :)
      '#B32851', (: Strong Purplish Red :)
      '#F4C800', (: Vivid Greenish Yellow :)
      '#7F180D', (: Strong Reddish Brown :)
      '#93AA00', (: Vivid Yellowish Green :)
      '#593315', (: Deep Yellowish Brown :)
      '#F13A13', (: Vivid Reddish Orange :)
      '#232C16'  (: Dark Olive Green :))"/>
 <xsl:variable name="nColours" select="count($colours)"/>
 <xsl:variable name="colourClassStyles" select="for $i in (1 to $nColours) return concat('.c',$i,' {background-color: ',$colours[$i],'}')"/>
 
 <xsl:variable name="backfi" select="distinct-values(/e:a/e:r/e:c/e:i/@ri)"/>
 <xsl:variable name="borderClassStyles" select="for $fis in $backfi return e:bcs($fis)"></xsl:variable>

 <xsl:template match="/">
  <html>
   <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <style>
       body {font-family: 'DejaVu Sans Mono' , monospace}
       td {padding: 0}
       .e {color: red}
       .formula {font-style: italic}
       .reffed {border: solid 1px black}
       table {table-layout: fixed}
       th {width: 1en}
    </style>
    <style><xsl:value-of select="string-join($colourClassStyles,'&#10;       ')"/></style>
    <style><xsl:value-of select="string-join($borderClassStyles,'&#10;       ')"/></style>
   </head>
   <body>
    <xsl:apply-templates/>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="e:a">
  <xsl:variable name="n" select="count(e:r[1]/e:c)"/>
  <table>
   <thead>
   <tr>
    <xsl:for-each select="1 to $n">
     <th/>
    </xsl:for-each>
   </tr>
   </thead>
   <tbody><xsl:apply-templates/></tbody>
  </table>
 </xsl:template>
 
 <xsl:template match="e:r">
  <tr><xsl:apply-templates/></tr>
  <xsl:text>&#10;</xsl:text>
 </xsl:template>
 
 <xsl:template match="e:c">
  <td>
   <xsl:choose>
    <xsl:when test="@t|text()">
     <xsl:attribute name="class">
      <xsl:value-of select="(if (@t) then @t else text(),
                             if (e:f) then 'formula' else (),
                             if (e:i) then e:rf(e:i/@ri) else (),
                             if (@fi) then concat('c',@fi) else ())"/>
     </xsl:attribute>
     <xsl:attribute name="title">
      <xsl:choose>
       <xsl:when test="e:i">
        <xsl:value-of select="e:i/@r"/>: <xsl:value-of select="e:i/e:r"/>
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="e:n2a(@c)"/><xsl:value-of select="../@r"/>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:attribute>
     <xsl:value-of select="if (@t) then @t else text()"/></xsl:when>
    <xsl:otherwise></xsl:otherwise>
   </xsl:choose>
   </td>
 </xsl:template>
 
 <xsl:variable name="borders" select="('left','right','top','bottom')"/>
 
 <xsl:function name="e:bcs" as="xs:string">
  <xsl:param name="fiss" as="xs:string"/>
  <xsl:value-of select="let $fis := tokenize($fiss,'_') return
                         concat('.b',$fiss,' {',
                                string-join(for $i in (1 to 4) return
                                                    e:bfs($fis[$i],
                                                          $borders[$i]),
                                             '; '),
                                '}')"/>
 </xsl:function>
 
 <xsl:function name="e:bfs" as="xs:string">
  <xsl:param name="fi" as="xs:string?"/>
  <xsl:param name="pos" as="xs:string"/>
  <xsl:value-of select="if (empty($fi)) then
                         concat('border-',$pos,': solid 1px black') else
                         concat('border-',$pos,': solid 2px ',
                                $colours[xs:integer($fi)])"/>
 </xsl:function>
 
 <xsl:function name="e:rf" as="xs:string">
  <xsl:param name="ris" as="attribute(ri)*"/>
  <xsl:value-of select="let $ff := (for $ri in tokenize(string($ris),'_')
                                return if (xs:integer($ri) le $nColours) then $ri else ())
                          return if (empty($ff))
                           then 'reffed'
                           else concat('b',
                                       string-join(subsequence($ff,1,4),'_'))"/>
 </xsl:function>
</xsl:stylesheet>