|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using QuickHelp; |
| 5 | +using QuickHelp.Converters; |
| 6 | +using QuickHelp.Serialization; |
| 7 | + |
| 8 | +namespace HelpConvert |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + Convert(args, false); |
| 15 | + |
| 16 | +#if DEBUG |
| 17 | + Console.WriteLine("Press any key to continue..."); |
| 18 | + Console.ReadKey(); |
| 19 | +#endif |
| 20 | + } |
| 21 | + |
| 22 | + static void PrintUsage() |
| 23 | + { |
| 24 | + Console.WriteLine("HelpConvert input-file [input-file ...]"); |
| 25 | + } |
| 26 | + |
| 27 | + static void Convert(string[] fileNames, bool isDryRun) |
| 28 | + { |
| 29 | + HelpSystem system = new HelpSystem(); |
| 30 | + BatchHtmlConverter converter = new BatchHtmlConverter(system); |
| 31 | + |
| 32 | + foreach (string fileName in fileNames) |
| 33 | + { |
| 34 | + var decoder = new DatabaseDecoder(); |
| 35 | + foreach (HelpDatabase database in decoder.LoadDatabases(fileName)) |
| 36 | + system.Databases.Add(database); |
| 37 | + } |
| 38 | + |
| 39 | + // export HTML |
| 40 | + foreach (HelpDatabase database in system.Databases) |
| 41 | + { |
| 42 | + // TODO: check invalid chars in database name |
| 43 | + string htmlPath = database.Name.Replace('.', '_'); |
| 44 | + Directory.CreateDirectory(htmlPath); |
| 45 | + |
| 46 | + int topicIndex = 0; |
| 47 | + foreach (HelpTopic topic in database.Topics) |
| 48 | + { |
| 49 | + string html = converter.ConvertTopic(topic); |
| 50 | + string htmlFileName = Path.Combine(htmlPath, string.Format("T{0:X4}.html", topicIndex)); |
| 51 | + if (!isDryRun) |
| 52 | + { |
| 53 | + using (StreamWriter writer = new StreamWriter(htmlFileName, false, Encoding.UTF8)) |
| 54 | + { |
| 55 | + writer.Write(html); |
| 56 | + } |
| 57 | + } |
| 58 | + topicIndex++; |
| 59 | + } |
| 60 | + |
| 61 | + // Create contents.html. |
| 62 | + HelpTopic topic1 = system.ResolveUri(database, new HelpUri("h.contents")); |
| 63 | + if (topic1 != null && topic1.Database == database) |
| 64 | + { |
| 65 | + if (!isDryRun) |
| 66 | + { |
| 67 | + using (StreamWriter writer = new StreamWriter(Path.Combine(htmlPath, "Contents.html"))) |
| 68 | + { |
| 69 | + writer.WriteLine("<meta http-equiv=\"refresh\" content=\"0; url=T{0:X4}.html\">", |
| 70 | + topic1.TopicIndex); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + static string GetDatabasePath(HelpDatabase database) |
| 78 | + { |
| 79 | + string path = database.Name.Replace('.', '_'); |
| 80 | + Directory.CreateDirectory(path); |
| 81 | + return path; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + class BatchHtmlConverter : HtmlConverter |
| 86 | + { |
| 87 | + readonly HelpSystem system; |
| 88 | + |
| 89 | + public BatchHtmlConverter(HelpSystem system) |
| 90 | + { |
| 91 | + base.AutoFixHyperlinks = true; |
| 92 | + this.system = system; |
| 93 | + } |
| 94 | + |
| 95 | + protected override string ConvertUri(HelpTopic source, HelpUri uri) |
| 96 | + { |
| 97 | + switch (uri.Type) |
| 98 | + { |
| 99 | + case HelpUriType.Context: |
| 100 | + case HelpUriType.GlobalContext: |
| 101 | + case HelpUriType.LocalContext: |
| 102 | + { |
| 103 | + HelpTopic target = system.ResolveUri(source.Database, uri); |
| 104 | + if (target != null) |
| 105 | + { |
| 106 | + if (target.Database == source.Database) |
| 107 | + { |
| 108 | + return string.Format("T{0:X4}.html", target.TopicIndex); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + return string.Format("../{0}/T{1:X4}.html", |
| 113 | + GetDatabasePath(target.Database), |
| 114 | + target.TopicIndex); |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + Console.WriteLine("Warning: cannot resolve context string '{0}'", uri); |
| 120 | + } |
| 121 | + } |
| 122 | + break; |
| 123 | + |
| 124 | + case HelpUriType.TopicIndex: |
| 125 | + return string.Format("T{0:X4}.html", uri.TopicIndex); |
| 126 | + |
| 127 | + case HelpUriType.Command: |
| 128 | + case HelpUriType.File: |
| 129 | + default: |
| 130 | + // TODO: would be better if we have the source location. |
| 131 | + Console.WriteLine("Warning: cannot convert link: {0}", uri); |
| 132 | + break; |
| 133 | + } |
| 134 | + return "?" + uri.ToString(); |
| 135 | + } |
| 136 | + |
| 137 | + static string GetDatabasePath(HelpDatabase database) |
| 138 | + { |
| 139 | + string path = database.Name.Replace('.', '_'); |
| 140 | + Directory.CreateDirectory(path); |
| 141 | + return path; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments