-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenapi.xslt
158 lines (142 loc) · 5.98 KB
/
openapi.xslt
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" media-type="text/yml" />
<xsl:strip-space elements="*"/>
<xsl:variable name="nl"><xsl:text>
</xsl:text></xsl:variable>
<!-- use technique from https://stackoverflow.com/a/11194007 -->
<xsl:template name="indent">
<xsl:param name="depth" />
<xsl:if test="$depth > 0">
<xsl:value-of select="' '"/>
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$depth - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="/">
---
openapi: 3.0.3
<xsl:apply-templates select="//api_info"/>
<xsl:call-template name="components"/>
responses:
securitySchemes:
paths:
<xsl:apply-templates select="//api_path"/>
...
</xsl:template>
<xsl:template match="api_info">
<xsl:variable name="indent" select="0"/>
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:text>info:</xsl:text><xsl:value-of select="$nl"/>
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent+1"/>
</xsl:call-template>
<xsl:text>title: </xsl:text><xsl:value-of select="concat(title,$nl)"/>
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent+1"/>
</xsl:call-template>
<xsl:text>description: </xsl:text><xsl:value-of select="concat(description,$nl)"/>
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent+1"/>
</xsl:call-template>
<xsl:text>version: </xsl:text><xsl:value-of select="concat(version,$nl)"/>
</xsl:template>
<xsl:template name="components">
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:text>components:</xsl:text><xsl:value-of select="$nl"/>
<xsl:call-template name="schemas"/>
</xsl:template>
<xsl:template match="api_path">
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:value-of select="concat(./[@path],':',$nl)"/>
<xsl:apply-templates match="method"/>
</xsl:template>
<xsl:template match="api_path/method">
</xsl:template>
<xsl:template name="schemas">
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent+1"/>
</xsl:call-template>
<xsl:text>schemas:</xsl:text><xsl:value-of select="$nl"/>
<xsl:apply-templates select="//para[contains(.,'@api_schema')]/ancestor::compounddef[@kind='class']"/>
</xsl:template>
<xsl:template match="compounddef[@kind='class']">
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:variable name="className" select="concat(replace(compoundname,'::','.'),':',$nl)"/>
<xsl:value-of select="$className"/>
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent+1"/>
</xsl:call-template>
<xsl:value-of select="concat('type: object', $nl)"/>
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent+1"/>
</xsl:call-template>
<xsl:value-of select="concat('properties:', $nl)"/>
<xsl:apply-templates select=".//memberdef[@kind='variable']"/>
<xsl:if test="position() < last()"><xsl:value-of select="$nl"/></xsl:if>
</xsl:template>
<xsl:template match="memberdef[@kind='variable']">
<xsl:if test="not(contains(briefdescription/para,'api_schema_ignore'))">
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:apply-templates select="./type/text"/>
<xsl:value-of select="concat(./name,':',$nl)"/>
<xsl:apply-templates select="./type"/>
<xsl:if test="position() < last()"><xsl:value-of select="$nl"/></xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match="memberdef/type[text()='uint64_t']" >
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:value-of select="concat('type: integer',$nl)"/>
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:value-of select="'format: uint64'"/>
</xsl:template>
<xsl:template match="memberdef/type[contains(text(),'boost::optional')]" >
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:analyze-string select="."
regex="boost::optional<\s?(.*)\s?>">
<xsl:matching-substring>
<xsl:variable name="actualType" select="replace(regex-group(1),'std::','')"/>
<xsl:text>type: </xsl:text>
<xsl:value-of select=" $actualType"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template name="std__string" match="std__string" mode="direct_call">
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:value-of select="'type: string'"/>
</xsl:template>
<xsl:template match="memberdef/type[(text()='std::string')]">
<xsl:variable name="indent" select="count(ancestor-or-self::*)" />
<xsl:call-template name="indent">
<xsl:with-param name="depth" select="$indent"/>
</xsl:call-template>
<xsl:value-of select="'type: string'"/>
</xsl:template>
</xsl:stylesheet>