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
/
mb-bd2xhtml.cs
221 lines (167 loc) · 5.02 KB
/
mb-bd2xhtml.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
//
// mb-bd2xhtml.cs -- Convert bundle XML documentation into XHTML
//
using System;
using System.Reflection;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using Mono.GetOptions;
namespace MBBd2XHtml {
public class MainClass : Options {
public MainClass () : base () {
// Be more mcs-like in option handling
//BreakSingleDashManyLettersIntoManyOptions = true;
//ParsingMode = OptionsParsingMode.Linux;
}
// Options
[Option("Specify a non-default stylesheet to use", 's', "stylesheet")]
public string extra_stylesheet = null;
public override WhatToDoNext DoAbout () {
base.DoAbout ();
return WhatToDoNext.AbandonProgram;
}
string outpath;
// Table-of-contents file processing
XmlDocument tocfile = new XmlDocument ();
Hashtable namespaces = new Hashtable ();
void SetupTocfile () {
XmlElement e = tocfile.CreateElement ("monkeyguide");
tocfile.AppendChild (e);
XmlElement intro = tocfile.CreateElement ("intro");
e.AppendChild (intro);
XmlElement summary = tocfile.CreateElement ("doc");
summary.SetAttribute ("name", "Summary");
summary.SetAttribute ("href", "bundle/bundle.xhtml");
intro.AppendChild (summary);
}
void WriteTocfile () {
string path = Path.Combine (outpath, "toc.xml");
FileStream s = new FileStream (path, FileMode.Create, FileAccess.Write);
StreamWriter output = new StreamWriter (s);
tocfile.Save (output);
output.Close ();
}
void AddNamespaceDoc (string ns, string title, string file) {
XmlElement e;
if (ns == "bundle")
return;
if (namespaces.Contains (ns))
e = (XmlElement) namespaces[ns];
else {
e = tocfile.CreateElement ("part");
e.SetAttribute ("name", ns);
tocfile.DocumentElement.AppendChild (e);
namespaces[ns] = e;
}
XmlElement doc = tocfile.CreateElement ("doc");
doc.SetAttribute ("name", title);
doc.SetAttribute ("href", file);
e.AppendChild (doc);
}
// Per-file processing
XslTransform xform = new XslTransform ();
XsltArgumentList args = new XsltArgumentList ();
XmlResolver resolver = new XmlUrlResolver ();
void ProcessFile (string file) {
string ns;
string pathbase = MakeOutputPathBase (file, out ns);
string path = Path.Combine (outpath, pathbase);
string dir = Path.GetDirectoryName (path);
if (!Directory.Exists (dir))
Directory.CreateDirectory (dir);
XPathDocument input = new XPathDocument (file);
AddNamespaceDoc (ns, GetDocumentTitle (input), pathbase);
FileStream s = new FileStream (path, FileMode.Create, FileAccess.Write);
StreamWriter output = new StreamWriter (s);
xform.Transform (input, args, output, resolver);
output.Close ();
}
string MakeOutputPathBase (string file, out string ns) {
string dir = Path.GetDirectoryName (file);
int i = dir.LastIndexOf (Path.DirectorySeparatorChar);
ns = dir.Substring (i + 1);
return Path.ChangeExtension (Path.Combine (ns, Path.GetFileName (file)), "xhtml");
}
string GetDocumentTitle (XPathDocument doc) {
XPathNavigator nav = doc.CreateNavigator ();
nav.MoveToFirstChild ();
string node = nav.LocalName;
string name = nav.GetAttribute ("name", "");
switch (node) {
case "result":
node = "Result";
break;
case "provider":
node = "Provider";
break;
case "rule":
node = "Rule";
break;
case "regex_matcher":
node = "Regex Matcher";
break;
case "matcher":
node = "Matcher";
break;
}
return String.Format ("{0} {1}", name, node);
}
// Stylesheet loading
const string ResourceName = "bd2xhtml.xsl";
public XmlTextReader LoadStylesheet () {
Stream s;
if (extra_stylesheet == null)
s = Assembly.GetExecutingAssembly ().GetManifestResourceStream (ResourceName);
else {
if (!File.Exists (extra_stylesheet)) {
Console.Error.WriteLine ("Could not open stylesheet file {0}!", extra_stylesheet);
return null;
}
s = new FileStream (extra_stylesheet, FileMode.Open, FileAccess.Read);
}
return new XmlTextReader (s);
}
// Main logic
public int Launch () {
if (RemainingArguments.Length < 2) {
DoAbout ();
return 1;
}
outpath = RemainingArguments[0];
if (!Directory.Exists (outpath)) {
Console.Error.WriteLine ("Documentation output directory {0} does not exist!",
outpath);
return 1;
}
try {
XmlTextReader r = LoadStylesheet ();
if (r == null)
return 1;
xform.Load (r);
SetupTocfile ();
for (int i = 1; i < RemainingArguments.Length; i++) {
string file = RemainingArguments[i];
if (!File.Exists (file)) {
Console.Error.WriteLine ("Input file {0} does not exist!",
file);
return 1;
}
ProcessFile (file);
}
WriteTocfile ();
} catch (Exception e) {
Console.Error.WriteLine ("Error: {0}", e);
return 1;
}
return 0;
}
public static int Main (string[] args) {
MainClass options = new MainClass ();
options.ProcessArgs (args);
return options.Launch ();
}
}
}