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

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tools/ExampleGenerator/Generator.cs	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,119 @@
+using System;
+using System.IO;
+
+namespace ExampleGenerator
+{
+	class Generator
+	{
+		class Lang
+		{
+			public Lang(string name, string extension, string comment)
+			{
+				Name      = name;
+				Extension = extension;
+				Comment   = comment;
+			}
+
+			public string Name;
+			public string Extension;
+			public string Comment;
+		}
+
+		static void ProcessFile(StreamWriter sw, string fileName, Lang lang)
+		{
+			using (StreamReader sr = File.OpenText(fileName))
+			{
+				string ln = sr.ReadLine();
+
+				if (ln.StartsWith(lang.Comment + " example:"))
+				{
+					Console.WriteLine("{0}", fileName);
+
+					string[] path = sr.ReadLine().Split(new char[]{' '});
+
+					sw.WriteLine("<{0} name=\"{1}\">", path[1], path[2]);
+					sw.WriteLine("<example>");
+
+					ln = sr.ReadLine();
+
+					if (ln.StartsWith(lang.Comment + " comment:"))
+					{
+						for (ln = sr.ReadLine(); ln.StartsWith(lang.Comment); ln = sr.ReadLine())
+						{
+							ln = ln.Substring(3);
+
+							if (ln.Length > 0 && ln[0] == ' ')
+								ln = ln.Substring(1);
+
+							sw.WriteLine(ln);
+						}
+					}
+
+					ln += "\n" + sr.ReadToEnd();
+					ln.Trim();
+
+					while (ln.Length > 0 && (ln[ln.Length-1] == '\r' || ln[ln.Length-1] == '\n'))
+						ln = ln.Substring(0, ln.Length - 1).TrimEnd();
+
+					ln = ln.Replace("<", "&lt;").Replace(">", "&gt;");
+
+					sw.WriteLine("<code lang=\"{0}\">", lang.Name);
+					sw.WriteLine(ln);
+					sw.WriteLine("</code>");
+					sw.WriteLine("</example>");
+					sw.WriteLine("</{0}>", path[1]);
+					sw.WriteLine();
+				}
+			}
+		}
+
+		static void ScanDirectory(StreamWriter sw, string path, Lang lang)
+		{
+			string[] files = Directory.GetFiles(path, lang.Extension);
+
+			foreach (string file in files)
+				ProcessFile(sw, file, lang);
+
+			string[] dirs = Directory.GetDirectories(path);
+
+			foreach (string dir in dirs)
+				ScanDirectory(sw, dir, lang);
+		}
+
+		[STAThread]
+		static void Main(string[] args)
+		{
+			string path = null;
+			string xml  = null;
+
+			foreach (string arg in args)
+			{
+				if (arg.ToLower().StartsWith("/path:"))
+					path = arg.Substring(6);
+
+				if (arg.ToLower().StartsWith("/xml:"))
+					xml = arg.Substring(5);
+			}
+
+			//Console.WriteLine("/path:{0} /xml:{1}", path, xml);
+
+			if (path == null || xml == null)
+			{
+				Console.WriteLine(@"Use: ExampleGenerator /path:..\Examples /xml:..\Source\Examples.xml");
+			}
+
+			using (StreamWriter sw = File.CreateText(xml))
+			{
+				sw.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8"" ?>");
+				sw.WriteLine(@"<examples>");
+				sw.WriteLine();
+
+				ScanDirectory(sw, path, new Lang("C#", "*.cs", "//@"));
+				ScanDirectory(sw, path, new Lang("VB", "*.vb", "''@"));
+
+				sw.WriteLine();
+				sw.WriteLine(@"</examples>");
+			}
+		}
+	}
+}