comparison Tools/ExampleGenerator/Generator.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.IO;
3
4 namespace ExampleGenerator
5 {
6 class Generator
7 {
8 class Lang
9 {
10 public Lang(string name, string extension, string comment)
11 {
12 Name = name;
13 Extension = extension;
14 Comment = comment;
15 }
16
17 public string Name;
18 public string Extension;
19 public string Comment;
20 }
21
22 static void ProcessFile(StreamWriter sw, string fileName, Lang lang)
23 {
24 using (StreamReader sr = File.OpenText(fileName))
25 {
26 string ln = sr.ReadLine();
27
28 if (ln.StartsWith(lang.Comment + " example:"))
29 {
30 Console.WriteLine("{0}", fileName);
31
32 string[] path = sr.ReadLine().Split(new char[]{' '});
33
34 sw.WriteLine("<{0} name=\"{1}\">", path[1], path[2]);
35 sw.WriteLine("<example>");
36
37 ln = sr.ReadLine();
38
39 if (ln.StartsWith(lang.Comment + " comment:"))
40 {
41 for (ln = sr.ReadLine(); ln.StartsWith(lang.Comment); ln = sr.ReadLine())
42 {
43 ln = ln.Substring(3);
44
45 if (ln.Length > 0 && ln[0] == ' ')
46 ln = ln.Substring(1);
47
48 sw.WriteLine(ln);
49 }
50 }
51
52 ln += "\n" + sr.ReadToEnd();
53 ln.Trim();
54
55 while (ln.Length > 0 && (ln[ln.Length-1] == '\r' || ln[ln.Length-1] == '\n'))
56 ln = ln.Substring(0, ln.Length - 1).TrimEnd();
57
58 ln = ln.Replace("<", "&lt;").Replace(">", "&gt;");
59
60 sw.WriteLine("<code lang=\"{0}\">", lang.Name);
61 sw.WriteLine(ln);
62 sw.WriteLine("</code>");
63 sw.WriteLine("</example>");
64 sw.WriteLine("</{0}>", path[1]);
65 sw.WriteLine();
66 }
67 }
68 }
69
70 static void ScanDirectory(StreamWriter sw, string path, Lang lang)
71 {
72 string[] files = Directory.GetFiles(path, lang.Extension);
73
74 foreach (string file in files)
75 ProcessFile(sw, file, lang);
76
77 string[] dirs = Directory.GetDirectories(path);
78
79 foreach (string dir in dirs)
80 ScanDirectory(sw, dir, lang);
81 }
82
83 [STAThread]
84 static void Main(string[] args)
85 {
86 string path = null;
87 string xml = null;
88
89 foreach (string arg in args)
90 {
91 if (arg.ToLower().StartsWith("/path:"))
92 path = arg.Substring(6);
93
94 if (arg.ToLower().StartsWith("/xml:"))
95 xml = arg.Substring(5);
96 }
97
98 //Console.WriteLine("/path:{0} /xml:{1}", path, xml);
99
100 if (path == null || xml == null)
101 {
102 Console.WriteLine(@"Use: ExampleGenerator /path:..\Examples /xml:..\Source\Examples.xml");
103 }
104
105 using (StreamWriter sw = File.CreateText(xml))
106 {
107 sw.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8"" ?>");
108 sw.WriteLine(@"<examples>");
109 sw.WriteLine();
110
111 ScanDirectory(sw, path, new Lang("C#", "*.cs", "//@"));
112 ScanDirectory(sw, path, new Lang("VB", "*.vb", "''@"));
113
114 sw.WriteLine();
115 sw.WriteLine(@"</examples>");
116 }
117 }
118 }
119 }