This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
wiki2ecma.cs
346 lines (315 loc) · 8.71 KB
/
wiki2ecma.cs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace Monodoc
{
public class WikiStyleDocConverter
{
public static void Main (string [] args)
{
if (args.Length < 1) {
Console.Error.WriteLine ("usage: wiki2ecma sourcefile [--full]");
return;
}
bool full = args.Length > 1 && args [1] == "--full";
bool isXml = false;
using (Stream s = File.OpenRead (args [0])) {
isXml = (s.ReadByte () == '<');
}
string text;
if (isXml) {
XmlDocument doc = new XmlDocument ();
doc.Load (args [0]);
XmlNode node = doc.SelectSingleNode ("//text");
text = node.InnerText;
} else {
StreamReader sr = new StreamReader (args [0],
Encoding.UTF8);
text = sr.ReadToEnd ();
}
// Pass the input Wiki-like content as the .ctor()
// parameter.
WikiStyleDocConverter p = new WikiStyleDocConverter (text);
XmlNode result;
if (full)
result = p.ParseEntireDoc ();
else
result = p.ParseContent ();
XmlTextWriter xw = new XmlTextWriter (Console.Out);
xw.Formatting = Formatting.Indented;
result.WriteTo (xw);
xw.Close ();
}
string [] lines;
int lineno = 0;
XmlDocument doc;
string current_member;
public WikiStyleDocConverter (string source)
{
source = TransformElements (source);
lines = source.Split ('\n');
doc = new XmlDocument ();
doc.AppendChild (doc.CreateElement ("root"));
}
private string TransformElements (string source)
{
XPathDocument doc = new XPathDocument (
new StringReader ("<root>" + source + "</root>"));
XslTransform tr = new XslTransform ();
tr.Load ("wiki2ecmahelper.xsl");
XmlReader reader = tr.Transform (doc, null, (XmlResolver) null);
reader.Read (); // should consume <root> start tag
return reader.ReadInnerXml ();
}
public XmlNode ParseContent ()
{
ProcessContent (doc.DocumentElement);
return doc.DocumentElement;
}
public XmlNode ParseEntireDoc ()
{
XmlElement el = doc.DocumentElement;
while (lineno < lines.Length) {
string line = lines [lineno].Trim ();
if (line.Length == 0) {
lineno++;
continue;
}
XmlNode node = null;
switch (line) {
case "=== Summary ===":
node = ProcessTaggedContent (EditTarget.Summary);
el.AppendChild (node);
break;
case "=== Remarks ===":
node = ProcessTaggedContent (EditTarget.Remarks);
el.AppendChild (node);
break;
case "=== Parameters ===":
ProcessList (el, "param", "name");
break;
case "=== Exceptions ===":
ProcessList (el, "exception", "type");
break;
default:
if (StrUtil.StartsWith (line, "==")) {
current_member = line.Substring (
3, line.Length - 6).Trim ();
el = doc.CreateElement ("Member");
el.SetAttribute ("MemberName", current_member);
doc.DocumentElement.AppendChild (el);
lineno++;
break;
}
throw MarkupError ("Unexpected line format: " + line);
}
}
return doc.DocumentElement;
}
void ProcessList (XmlNode parent, string elemName, string defAttr)
{
lineno++;
for (; lineno < lines.Length; lineno++) {
string line = lines [lineno];
if (line.Length == 0)
continue;
if (line [0] != ';')
break;
int idx = line.IndexOf (':');
XmlElement el = doc.CreateElement (elemName);
parent.AppendChild (el);
el.SetAttribute (defAttr, line.Substring (1, idx - 1));
ProcessSimpleLine (el, line, idx + 1);
}
}
XmlNode ProcessTaggedContent (string target)
{
XmlElement el = doc.CreateElement (target);
lineno++;
ProcessContent (el);
return el;
}
void ProcessContent (XmlNode container)
{
while (lineno < lines.Length) {
string line = lines [lineno];
if (line.Length == 0) {
lineno++;
continue;
}
switch (line [0]) {
case '=':
return;
case '{':
ProcessTable (container);
break;
case ':':
XmlElement el = doc.CreateElement ("block");
el.SetAttribute ("subset", "none");
el.SetAttribute ("type", "note");
container.AppendChild (el);
ProcessSimple (el, true);
break;
default:
el = doc.CreateElement ("para");
container.AppendChild (el);
ProcessSimple (el, false);
break;
}
}
}
void ProcessTable (XmlNode container)
{
lineno++;
XmlElement list = doc.CreateElement ("list");
container.AppendChild (list);
list.SetAttribute ("type", "table");
XmlElement tline = null;
for (; lineno < lines.Length; lineno++) {
string line = lines [lineno];
if (line == "|}") {
lineno++;
return;
}
if (line.Length == 0)
continue;
if (line == "|-") {
tline = doc.CreateElement ("item");
continue;
}
switch (line [0]) {
case '!':
tline = doc.CreateElement ("listheader");
int endTerm = line.IndexOf ('!', 1);
int beginDesc = endTerm < 0 ? -1 : line.IndexOf ('!', endTerm + 1);
if (beginDesc < 0)
throw MarkupError ("list table header has incorrect markup : " + line);
XmlElement term = doc.CreateElement ("term");
term.InnerText = line.Substring (1, endTerm - 1);
tline.AppendChild (term);
XmlElement desc = doc.CreateElement ("description");
desc.InnerText = line.Substring (beginDesc + 1);
tline.AppendChild (desc);
list.AppendChild (tline);
break;
case '|':
if (tline == null)
throw MarkupError ("Specify '|-' to begin new table line");
endTerm = line.IndexOf ('|', 1);
if (endTerm < 0)
throw MarkupError ("Missing list table separator '|'");
beginDesc = endTerm < 0 ? -1 : line.IndexOf ('|', endTerm + 1);
term = doc.CreateElement ("term");
term.InnerText = line.Substring (1, endTerm - 1);
tline.AppendChild (term);
desc = doc.CreateElement ("description");
ProcessSimpleLine (desc, line, beginDesc + 1);
tline.AppendChild (desc);
list.AppendChild (tline);
break;
}
tline = null;
}
// there is already "return" statement above.
throw MarkupError ("End of list table is missing");
}
void ProcessSimple (XmlNode container, bool allowColon)
{
for (;lineno < lines.Length; lineno++) {
string line = lines [lineno];
if (line.Length == 0) {
if (lineno + 1 < lines.Length &&
lines [lineno + 1] == String.Empty) {
lineno++;
return;
}
continue;
}
switch (line [0]) {
case '=':
case '{':
return;
case ':':
if (!allowColon)
return;
ProcessSimpleLine (container, line, 1);
break;
default:
ProcessSimpleLine (container, line, 0);
break;
}
}
}
void ProcessSimpleLine (XmlNode container, string line, int from)
{
int idx;
while ((idx = line.IndexOf ('[', from)) >= 0) {
if (idx + 1 < line.Length && line [idx + 1] == '[')
from = ProcessSee (
container, line, idx, from);
else
from = ProcessParamRef (
container, line, idx, from);
}
if (from != line.Length)
container.AppendChild (doc.CreateTextNode (line.Substring (from) + '\n'));
}
int ProcessSee (XmlNode container, string line, int idx, int from)
{
int end = line.IndexOf ("]]", idx);
int sep = end < idx ? - 1: line.IndexOf ('|', idx, end - idx);
if (sep < 0)
throw MarkupError (String.Format ("There is no matching '|' and ']' to close link at position {1} : {0}", line, idx));
if (idx > from) {
XmlText text = doc.CreateTextNode (
line.Substring (from, idx - from));
container.AppendChild (text);
}
XmlElement el = doc.CreateElement ("see");
el.SetAttribute ("cref", line.Substring (
idx + 2, sep - idx - 2).Trim ());
container.AppendChild (el);
end += 2;
return end;
}
int ProcessParamRef (XmlNode container, string line, int idx, int from)
{
int end = line.IndexOf (']', idx);
if (end < idx)
throw MarkupError (String.Format ("There is no matching ']' to close link at position {1} : {0}", line, idx));
if (idx > from) {
XmlText text = doc.CreateTextNode (
line.Substring (from, idx - from));
container.AppendChild (text);
}
XmlElement el = doc.CreateElement ("paramref");
el.SetAttribute ("name", line.Substring (
idx + 1, end - idx - 1));
container.AppendChild (el);
end += 1;
return end;
}
Exception MarkupError (string message)
{
throw new Exception (String.Format (
"At line {1} : {0}", message, lineno));
}
}
class EditTarget
{
public const string Summary = "summary";
public const string Remarks = "remarks";
}
class StrUtil
{
static CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;
public static bool StartsWith (string s, string target)
{
return ci.IsPrefix (s, target, CompareOptions.Ordinal);
}
}
}