summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/generic/hyph-utf8/conversion-to-xml/org/tug/texhyphen/ConvertTeXPattern.xsl
blob: 803962b7d5a7184cbe7c0c94eb46ca3afce44ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:tex="urn:org:tug:texhyphen"
  xmlns:lang="urn:org:tug:texhyphen:languagedata"
  exclude-result-prefixes="tex lang">

  <xsl:output doctype-system="hyphenation.dtd" indent="yes"/>
  
  <xsl:param name="comment-length" select="72"/>
  <xsl:param name="tex-code"/>
  <xsl:param name="hyphen-min-before-default" select="2"/>
  <xsl:param name="hyphen-min-after-default" select="3"/>

  <xsl:template match="/tex:tex">
    <hyphenation-info>
	  <xsl:choose>
		<xsl:when test="tex:hyphenation">
		  <!-- (comment*), (patterns), (comment*, hyphenation, comment*) => (comment*), hyphen-min, (comment*, exceptions, comment*), (patterns) -->
		  <xsl:variable name="set1" select="node()[following-sibling::tex:patterns]"/>
		  <xsl:variable name="set2" select="tex:patterns"/>
		  <xsl:variable name="set3" select="node()[preceding-sibling::tex:patterns]"/>
		  <xsl:apply-templates select="$set1"/>
		  <xsl:call-template name="hyphen-min"/>
		  <xsl:apply-templates select="$set3"/>
		  <xsl:apply-templates select="$set2"/>
		</xsl:when>
		<xsl:otherwise>
		  <!-- (comment*), (patterns, comment*) => (comment*), hyphen-min, (patterns, comment*) -->
		  <xsl:variable name="set1" select="node()[following-sibling::tex:patterns]"/>
		  <xsl:variable name="set2" select="node()[preceding-sibling::tex:patterns or self::tex:patterns]"/>
		  <xsl:apply-templates select="$set1"/>
		  <xsl:call-template name="hyphen-min"/>
		  <xsl:apply-templates select="$set2"/>
		</xsl:otherwise>
	  </xsl:choose>
    </hyphenation-info>
  </xsl:template>

  <xsl:template match="tex:patterns">
    <patterns>
      <xsl:apply-templates />
    </patterns>
  </xsl:template>

  <xsl:template match="tex:patterns" mode="call-hyphen-min">
	<xsl:call-template name="hyphen-min"/>
    <patterns>
      <xsl:apply-templates />
    </patterns>
  </xsl:template>

  <xsl:template name="hyphen-min">
    <xsl:variable name="hyphen-min"
      select="document('languages.xml')/lang:languages/lang:language[@code=$tex-code]/lang:hyphen-min" />
    <xsl:if test="count($hyphen-min)">
      <hyphen-min before="{$hyphen-min/@before}" after="{$hyphen-min/@after}" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="tex:message"/>

  <xsl:template match="tex:hyphenation">
    <exceptions>
      <xsl:apply-templates />
    </exceptions>
  </xsl:template>

  <!-- Comments in TeX contain the trailing new line.                     -->
  <!-- Here we keep the trailing new line if the comment is immediately   -->
  <!-- preceded or followed by a text node.                               -->
  <!-- Otherwise we strip the new line and pad the comment                -->
  <!-- to the parameter comment-length.                                   -->
  <!-- The XSLT engine uses the same criteria to decide if the comment    -->
  <!-- should start on a new line or not.                                 -->
  <!-- This is not quite correct, because we risk adding spaces to        -->
  <!-- elements with mixed content.                                       -->
  <!-- The following test would be more appropriate:                      -->
  <!-- test="preceding-sibling::text() or following-sibling::text()".     -->
  <xsl:template match="comment()">
    <xsl:choose>
      <xsl:when
        test="preceding-sibling::node()[1][self::text()]
            or following-sibling::node()[1][self::text()]">
        <xsl:comment>
          <xsl:value-of select="." />
        </xsl:comment>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="length" select="string-length(.)" />
        <xsl:comment>
          <xsl:value-of select="substring(.,1,$length - 1)" />
          <xsl:text> </xsl:text>
          <xsl:call-template name="make-spaces">
            <xsl:with-param name="length"
              select="$comment-length - ($length - 1)" />
          </xsl:call-template>
        </xsl:comment>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="make-spaces">
    <xsl:param name="length" select="0" />
    <xsl:choose>
      <xsl:when test="$length >= 10">
        <xsl:text>          </xsl:text>
        <xsl:call-template name="make-spaces">
          <xsl:with-param name="length" select="$length - 10" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$length >= 5">
        <xsl:text>     </xsl:text>
        <xsl:call-template name="make-spaces">
          <xsl:with-param name="length" select="$length - 5" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$length >= 1">
        <xsl:text> </xsl:text>
        <xsl:call-template name="make-spaces">
          <xsl:with-param name="length" select="$length - 1" />
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
    <xsl:text></xsl:text>
  </xsl:template>

</xsl:stylesheet>