5 // Jonathan Pryor <jpryor@novell.com>
7 // Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using System.Diagnostics;
38 using Mono.Documentation;
43 using ICSharpCode.SharpZipLib.Zip;
45 namespace Mono.Documentation
47 public class MDocExportWebdocHtml : MDocCommand
50 public Dictionary<string, List<string>> Formats = new Dictionary<string, List<string>>();
51 public List<string> Sources = new List<string>();
52 public bool UseSystemSources = true;
53 public bool ForceUpdate = false;
54 public string OutputDirectory = null;
57 public override void Run (IEnumerable<string> args)
59 var opts = new Options ();
60 var formatOptions = MDocAssembler.CreateFormatOptions (this, opts.Formats);
61 var options = new OptionSet () {
63 "Always generate new files. If not specified, will only generate " +
64 "files if the write time of the output directory is older than the " +
65 "write time of the source .tree/.zip files.",
66 v => opts.ForceUpdate = v != null },
70 "The {PREFIX} to place the generated files and directories. " +
71 "Default: \"`dirname FILE`/cache/\".\n" +
72 "Underneath {PREFIX}, `basename FILE .tree` directories will be " +
73 "created which will contain the pre-generated HTML content.",
74 v => opts.OutputDirectory = v },
76 "A {SOURCE} file to use for reference purposes.\n" +
77 "Extension methods are searched for among all {SOURCE}s which are referenced.\n" +
78 "This option may be specified multiple times.",
79 v => opts.Sources.Add (v) },
80 { "use-system-sources",
81 "Use the system-wide .source files for reference purposes. " +
82 "Default is " + (opts.UseSystemSources ? "enabled" : "disabled") + ".",
83 v => opts.UseSystemSources = v != null },
85 Parse (options, args, "export-html-webdoc",
87 "Export mdoc documentation within FILES to HTML for use by ASP.NET webdoc.\n\n" +
88 "FILES are .tree or .zip files as produced by 'mdoc assemble'.");
89 if (opts.Formats.Values.All (files => files.Count == 0))
90 Error ("No files specified.");
91 HelpSource.use_css = true;
92 HelpSource.FullHtml = false;
93 SettingsHandler.Settings.EnableEditing = false;
94 foreach (var p in opts.Formats)
95 ProcessFiles (opts, p.Key, p.Value);
98 void ProcessFiles (Options opts, string format, List<string> files)
100 foreach (var basePath in
102 Path.Combine (Path.GetDirectoryName (f), Path.GetFileNameWithoutExtension (f)))
104 string treeFile = basePath + ".tree";
105 string zipFile = basePath + ".zip";
106 if (!Exists (treeFile) || !Exists (zipFile))
108 string outDir = opts.OutputDirectory != null
109 ? Path.Combine (opts.OutputDirectory, Path.GetFileName (basePath))
110 : XmlDocUtils.GetCacheDirectory (basePath);
111 if (!opts.ForceUpdate && Directory.Exists (outDir) &&
112 MaxWriteTime (treeFile, zipFile) < Directory.GetLastWriteTime (outDir))
114 Message (TraceLevel.Warning, "Processing files: {0}, {1}", treeFile, zipFile);
115 Directory.CreateDirectory (outDir);
116 ExtractZipFile (zipFile, outDir);
117 GenerateCache (opts, basePath, format, outDir);
121 bool Exists (string file)
123 if (!File.Exists (file)) {
124 Message (TraceLevel.Error,
125 "mdoc: Could not find file: {0}", file);
131 DateTime MaxWriteTime (params string[] files)
133 return files.Select (f => File.GetLastWriteTime (f)).Max ();
136 void ExtractZipFile (string zipFile, string outDir)
138 ZipInputStream zip = new ZipInputStream (File.OpenRead (zipFile));
141 while ((entry = zip.GetNextEntry ()) != null) {
142 string file = Path.Combine (outDir, entry.Name);
143 Directory.CreateDirectory (Path.GetDirectoryName (file));
144 using (var output = File.OpenWrite (file))
145 zip.WriteTo (output);
149 void GenerateCache (Options opts, string basePath, string format, string outDir)
151 var hs = RootTree.GetHelpSource (format, basePath);
153 Error ("Unable to find a HelpSource for provider '{0}' and file '{1}.tree'.", format, basePath);
156 RootTree docRoot = null;
157 if (!opts.UseSystemSources)
158 docRoot = RootTree.LoadTree (null, null, opts.Sources);
160 docRoot = RootTree.LoadTree ();
161 foreach (var source in opts.Sources)
162 docRoot.AddSourceFile (source);
164 hs.RootTree = docRoot;
165 string helpSourceName = Path.GetFileName (basePath);
166 foreach (Node node in tree.TraverseDepthFirst<Node, Node> (t => t, t => t.Nodes.Cast<Node> ())) {
168 Message (TraceLevel.Info, "\tProcessing URL: {0}", url);
169 if (string.IsNullOrEmpty (url))
171 var file = XmlDocUtils.GetCachedFileName (outDir, url);
172 using (var o = File.AppendText (file)) {
174 string contents = hs.GetText (url, out _) ?? hs.RenderNamespaceLookup (url, out _);