summaryrefslogtreecommitdiff
path: root/tangle-bootstrap.xsl
blob: baa9669bbe2f2c5e245c8b968fd859d17bc50d8a (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
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mped="https://labo.planete-kraus.eu/mped.git" xmlns:docbook="http://docbook.org/ns/docbook" version="1.0">
  <xsl:output method="text" indent="no"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="docbook:programlisting[@mped:tangle-to]">
    <xsl:text>mkdir -p $(dirname "</xsl:text>
    <xsl:value-of select="@mped:tangle-to"/>
    <xsl:text>")
</xsl:text>
    <xsl:apply-templates select="." mode="source-code-formatter"/>
    <xsl:text> &gt; </xsl:text>
    <xsl:value-of select="@mped:tangle-to"/>
    <xsl:text> &lt;&lt; "_MPED_EOF"
</xsl:text>
    <xsl:apply-templates mode="copy-source-code"/>
    <xsl:text>
_MPED_EOF
</xsl:text>
  </xsl:template>
  <xsl:template match="docbook:programlisting[@language = 'xml']" mode="source-code-formatter">
    <xsl:text>xmllint --format -</xsl:text>
  </xsl:template>
  <xsl:template match="docbook:programlisting" mode="source-code-formatter">
    <xsl:text>cat</xsl:text>
  </xsl:template>
  <xsl:template match="text()" mode="copy-source-code">
    <xsl:value-of select="."/>
  </xsl:template>
  <xsl:template match="text()"/>
  <xsl:template match="mped:copy" mode="copy-source-code">
    <xsl:variable name="ref" select="@linkend"/>
    <xsl:variable name="candidates" select="count(//docbook:programlisting[@xml:id = $ref])"/>
    <xsl:if test="$candidates = 0">
      <xsl:message terminate="yes">
        <xsl:text>There are no listing with ID '</xsl:text>
        <xsl:value-of select="$ref"/>
        <xsl:text>'.
</xsl:text>
      </xsl:message>
    </xsl:if>
    <xsl:if test="$candidates &gt; 1">
      <xsl:message terminate="yes">
        <xsl:text>There are multiple listings with ID '</xsl:text>
        <xsl:value-of select="$ref"/>
        <xsl:text>'.
</xsl:text>
      </xsl:message>
    </xsl:if>
    <xsl:apply-templates select="//docbook:programlisting[@xml:id = $ref]" mode="copy-source"/>
  </xsl:template>
</xsl:stylesheet>