From 91ad649aaddf7d508ec711d8d48b634bfc9efab0 Mon Sep 17 00:00:00 2001 From: fancidev Date: Sat, 3 May 2014 18:35:51 +0900 Subject: [PATCH] Add existing code for HelpConvert project. --- DosHelp.sln | 6 ++ HelpConvert/App.config | 6 ++ HelpConvert/HelpConvert.csproj | 59 ++++++++++ HelpConvert/Program.cs | 144 +++++++++++++++++++++++++ HelpConvert/Properties/AssemblyInfo.cs | 36 +++++++ QuickHelp/Converters/HtmlConverter.cs | 6 +- 6 files changed, 254 insertions(+), 3 deletions(-) create mode 100644 HelpConvert/App.config create mode 100644 HelpConvert/HelpConvert.csproj create mode 100644 HelpConvert/Program.cs create mode 100644 HelpConvert/Properties/AssemblyInfo.cs diff --git a/DosHelp.sln b/DosHelp.sln index d0eff04..6718265 100644 --- a/DosHelp.sln +++ b/DosHelp.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickHelp", "QuickHelp\Quic EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelpBrowser", "HelpBrowser\HelpBrowser.csproj", "{00E978CD-7420-40EA-91A2-18E0F3CFF5C1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelpConvert", "HelpConvert\HelpConvert.csproj", "{469806C9-9B3B-4AA2-94D9-35A2143FC585}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {00E978CD-7420-40EA-91A2-18E0F3CFF5C1}.Debug|Any CPU.Build.0 = Debug|Any CPU {00E978CD-7420-40EA-91A2-18E0F3CFF5C1}.Release|Any CPU.ActiveCfg = Release|Any CPU {00E978CD-7420-40EA-91A2-18E0F3CFF5C1}.Release|Any CPU.Build.0 = Release|Any CPU + {469806C9-9B3B-4AA2-94D9-35A2143FC585}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {469806C9-9B3B-4AA2-94D9-35A2143FC585}.Debug|Any CPU.Build.0 = Debug|Any CPU + {469806C9-9B3B-4AA2-94D9-35A2143FC585}.Release|Any CPU.ActiveCfg = Release|Any CPU + {469806C9-9B3B-4AA2-94D9-35A2143FC585}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/HelpConvert/App.config b/HelpConvert/App.config new file mode 100644 index 0000000..343984d --- /dev/null +++ b/HelpConvert/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/HelpConvert/HelpConvert.csproj b/HelpConvert/HelpConvert.csproj new file mode 100644 index 0000000..f2b4c63 --- /dev/null +++ b/HelpConvert/HelpConvert.csproj @@ -0,0 +1,59 @@ + + + + + Debug + AnyCPU + {469806C9-9B3B-4AA2-94D9-35A2143FC585} + Exe + Properties + HelpConvert + HelpConvert + v2.0 + 512 + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + {c18c4ed1-e4b6-4dc3-b7cf-55053fec165b} + QuickHelp + + + + + \ No newline at end of file diff --git a/HelpConvert/Program.cs b/HelpConvert/Program.cs new file mode 100644 index 0000000..e43b9b6 --- /dev/null +++ b/HelpConvert/Program.cs @@ -0,0 +1,144 @@ +using System; +using System.IO; +using System.Text; +using QuickHelp; +using QuickHelp.Converters; +using QuickHelp.Serialization; + +namespace HelpConvert +{ + class Program + { + static void Main(string[] args) + { + Convert(args, false); + +#if DEBUG + Console.WriteLine("Press any key to continue..."); + Console.ReadKey(); +#endif + } + + static void PrintUsage() + { + Console.WriteLine("HelpConvert input-file [input-file ...]"); + } + + static void Convert(string[] fileNames, bool isDryRun) + { + HelpSystem system = new HelpSystem(); + BatchHtmlConverter converter = new BatchHtmlConverter(system); + + foreach (string fileName in fileNames) + { + var decoder = new DatabaseDecoder(); + foreach (HelpDatabase database in decoder.LoadDatabases(fileName)) + system.Databases.Add(database); + } + + // export HTML + foreach (HelpDatabase database in system.Databases) + { + // TODO: check invalid chars in database name + string htmlPath = database.Name.Replace('.', '_'); + Directory.CreateDirectory(htmlPath); + + int topicIndex = 0; + foreach (HelpTopic topic in database.Topics) + { + string html = converter.ConvertTopic(topic); + string htmlFileName = Path.Combine(htmlPath, string.Format("T{0:X4}.html", topicIndex)); + if (!isDryRun) + { + using (StreamWriter writer = new StreamWriter(htmlFileName, false, Encoding.UTF8)) + { + writer.Write(html); + } + } + topicIndex++; + } + + // Create contents.html. + HelpTopic topic1 = system.ResolveUri(database, new HelpUri("h.contents")); + if (topic1 != null && topic1.Database == database) + { + if (!isDryRun) + { + using (StreamWriter writer = new StreamWriter(Path.Combine(htmlPath, "Contents.html"))) + { + writer.WriteLine("", + topic1.TopicIndex); + } + } + } + } + } + + static string GetDatabasePath(HelpDatabase database) + { + string path = database.Name.Replace('.', '_'); + Directory.CreateDirectory(path); + return path; + } + } + + class BatchHtmlConverter : HtmlConverter + { + readonly HelpSystem system; + + public BatchHtmlConverter(HelpSystem system) + { + base.AutoFixHyperlinks = true; + this.system = system; + } + + protected override string ConvertUri(HelpTopic source, HelpUri uri) + { + switch (uri.Type) + { + case HelpUriType.Context: + case HelpUriType.GlobalContext: + case HelpUriType.LocalContext: + { + HelpTopic target = system.ResolveUri(source.Database, uri); + if (target != null) + { + if (target.Database == source.Database) + { + return string.Format("T{0:X4}.html", target.TopicIndex); + } + else + { + return string.Format("../{0}/T{1:X4}.html", + GetDatabasePath(target.Database), + target.TopicIndex); + } + } + else + { + Console.WriteLine("Warning: cannot resolve context string '{0}'", uri); + } + } + break; + + case HelpUriType.TopicIndex: + return string.Format("T{0:X4}.html", uri.TopicIndex); + + case HelpUriType.Command: + case HelpUriType.File: + default: + // TODO: would be better if we have the source location. + Console.WriteLine("Warning: cannot convert link: {0}", uri); + break; + } + return "?" + uri.ToString(); + } + + static string GetDatabasePath(HelpDatabase database) + { + string path = database.Name.Replace('.', '_'); + Directory.CreateDirectory(path); + return path; + } + } +} diff --git a/HelpConvert/Properties/AssemblyInfo.cs b/HelpConvert/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3a09d0c --- /dev/null +++ b/HelpConvert/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("HelpConvert")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("HelpConvert")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("a2d32d66-6dd2-4bbf-a238-e9e719544a6b")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/QuickHelp/Converters/HtmlConverter.cs b/QuickHelp/Converters/HtmlConverter.cs index dbbab16..3968f27 100644 --- a/QuickHelp/Converters/HtmlConverter.cs +++ b/QuickHelp/Converters/HtmlConverter.cs @@ -97,7 +97,7 @@ private void FormatLine(StringBuilder html, HelpTopic topic, HelpLine line) if (newAttrs.Link != null) { html.AppendFormat("", - ConvertLink(topic, newAttrs.Link)); + ConvertUri(topic, newAttrs.Link)); } } html.Append(Escape("" + line.Text[i])); @@ -113,9 +113,9 @@ private void FormatLine(StringBuilder html, HelpTopic topic, HelpLine line) html.AppendLine(); } - protected virtual string ConvertLink(HelpTopic topic, HelpUri link) + protected virtual string ConvertUri(HelpTopic topic, HelpUri uri) { - return "?" + Escape(link.ToString()); + return "?" + Escape(uri.ToString()); } private static void FormatAddedStyles(