forked from tohagan/freemind-to-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreeMind2TreeStoreJSON.xsl
134 lines (109 loc) · 4.16 KB
/
FreeMind2TreeStoreJSON.xsl
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
129
130
131
132
133
134
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- FreeMind to JSON for Sencha TreeStore
Copyright (C) 2012 MIT Licence Tony O'Hagan tony@ohagan.name
-->
<xsl:output method="text" indent="no"/>
<xsl:param name="indent-increment" select="' '" />
<xsl:template match="map">
<!-- Ignores root 'node' -->
<xsl:apply-templates select="node/node"/>
</xsl:template>
<xsl:template match="node">
<xsl:param name="indent" select="'
'"/>
<xsl:variable name="indent0" select="$indent" />
<xsl:variable name="indent1" select="concat($indent, $indent-increment)" />
<xsl:if test="position() > 1">, </xsl:if>
<xsl:text>{</xsl:text>
<xsl:value-of select="$indent1"/>
<xsl:text>text: '</xsl:text>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string" select="@TEXT"/>
</xsl:call-template>
<xsl:text>'</xsl:text>
<xsl:if test="@LINK">
<xsl:text>,</xsl:text>
<xsl:value-of select="$indent1"/>
<xsl:text>href: "</xsl:text>
<xsl:value-of disable-output-escaping='yes' select="@LINK"/>
<xsl:text>"</xsl:text>
</xsl:if>
<xsl:if test="attribute">
<xsl:apply-templates select="attribute">
<xsl:with-param name="indent" select="$indent1"/>
</xsl:apply-templates>
</xsl:if>
<xsl:choose>
<!-- You can prune the tree by setting an attribute having leaf=true inside FreeMind. -->
<xsl:when test="node and not(node/attribute[@NAME = 'leaf' and @VALUE = 'true'])">
<xsl:text>,</xsl:text>
<xsl:value-of select="$indent1"/>
<xsl:text>children: [</xsl:text>
<!-- Recursion -->
<xsl:apply-templates select="node">
<xsl:with-param name="indent" select="$indent1"/>
</xsl:apply-templates>
<xsl:text>]</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>,</xsl:text>
<xsl:value-of select="$indent1"/>
<xsl:text>leaf: true</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$indent0"/>
<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="attribute">
<xsl:param name="indent"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$indent"/>
<xsl:value-of select="@NAME"/>
<xsl:text>: "</xsl:text>
<xsl:value-of select="@VALUE"/>
<xsl:text>"</xsl:text>
</xsl:template>
<!--
Javascript string escape template by Jeni Tennison
Source: http://holytshirt.blogspot.com/2008/06/xslt-javascript-escaping.html
Author page: http://www.jenitennison.com/
-->
<xsl:template name="escape-javascript">
<xsl:param name="string" />
<xsl:choose>
<xsl:when test='contains($string, "'")'>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select='substring-before($string, "'")' />
</xsl:call-template>
<xsl:text>\'</xsl:text>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select='substring-after($string, "'")' />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string, '
')">
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select="substring-before($string, '
')" />
</xsl:call-template>
<xsl:text>\n</xsl:text>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select="substring-after($string, '
')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string, '\')">
<xsl:value-of select="substring-before($string, '\')" />
<xsl:text>\\</xsl:text>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select="substring-after($string, '\')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>