using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using Json; using Json.LitJson; namespace SDKDocGenerator.Writers { public class TOCWriter : BaseTemplateWriter { private readonly Dictionary _namespaceTocs = new Dictionary(); private const string TocFilesFolderName = "_tocfiles"; private const string TocFilenameSuffix = ".toc.json"; private const string TocIdFieldName = "id"; private const string TocHrefFieldName = "href"; private const string TocNodesFieldName = "nodes"; public TOCWriter(GeneratorOptions options) : base(options) { } /// /// Creates or updates a per-namespace json file during generation of docs for types in that /// namespace. These will be added to the _namespaceTocs collection to be collated into /// one single master toc at the end of processing of all namespaces. /// /// /// An partial and annotated example of what one data file looks like for the /// 'Amazon' namespace: /// { /// "Amazon" : // this is used as the display name of the root for the entries /// { /// "id" : "Amazon", // the unique id assigned to the li element /// "href" : "./items/Amazon/N_.html", // the target of the link /// "nodes": { // collection of child nodes for the namespace /// "AWSConfigs" : { // display name for child node /// "id" : "Amazon_AWSConfigs", // the unique li id /// "href" : "./items/Amazon/TAWSConfigs.html" // the target of the link /// }, /// "LoggingOptions" : { /// "id" : "Amazon_LoggingOptions", /// "href" : "./items/Amazon\TLoggingOptions.html" /// }, /// ... /// } /// } /// } /// public void BuildNamespaceToc(string nameSpace, AssemblyWrapper sdkAssemblyWrapper) { var sb = new StringBuilder(); var jsonWriter = new JsonWriter(sb); jsonWriter.WriteObjectStart(); WriteNamespaceToc(jsonWriter, nameSpace, sdkAssemblyWrapper); jsonWriter.WriteObjectEnd(); var nsTocContents = sb.ToString(); if (_namespaceTocs.ContainsKey(nameSpace)) _namespaceTocs[nameSpace] = nsTocContents; else _namespaceTocs.Add(nameSpace, nsTocContents); PersistNamespaceToc(nameSpace); } protected override string GetTemplateName() { return "TOC.html"; } protected override string ReplaceTokens(string templateBody) { var tocContent = TransformNamespaceTocsToHtml(); var finalBody = templateBody.Replace("{TOC}", tocContent); return finalBody; } /// /// Loads the toc snippet file for the specified namespace, adding it to the managed /// collection for later collation into the overall toc file. /// //void LoadNamespaceToc(string nameSpace) //{ // var filePath = Path.Combine(Options.ComputedContentFolder, tocFilesFolderName); // if (!Directory.Exists(filePath)) // return; // var tocFile = Path.Combine(filePath, nameSpace + extensionPattern); // if (File.Exists(tocFile)) // { // using (var reader = File.OpenText(tocFile)) // { // var tocContent = reader.ReadToEnd(); // _namespaceTocs.Add(nameSpace, tocContent); // } // } //} void WriteNamespaceToc(JsonWriter writer, string ns, AssemblyWrapper sdkAssemblyWrapper) { var tocId = ns.Replace(".", "_"); var nsFilePath = Path.Combine("./" + Options.ContentSubFolderName, GenerationManifest.OutputSubFolderFromNamespace(ns), FilenameGenerator.GenerateNamespaceFilename(ns)).Replace('\\', '/'); writer.WritePropertyName(ns); writer.WriteObjectStart(); writer.WritePropertyName(TocIdFieldName); writer.Write(tocId); writer.WritePropertyName(TocHrefFieldName); writer.Write(nsFilePath); writer.WritePropertyName(TocNodesFieldName); writer.WriteObjectStart(); foreach (var type in sdkAssemblyWrapper.GetTypesForNamespace(ns).OrderBy(x => x.Name)) { var filePath = Path.Combine("./" + Options.ContentSubFolderName, GenerationManifest.OutputSubFolderFromNamespace(type.Namespace), FilenameGenerator.GenerateFilename(type)).Replace('\\', '/'); writer.WritePropertyName(type.GetDisplayName(false)); writer.WriteObjectStart(); writer.WritePropertyName(TocIdFieldName); writer.Write(type.GetDisplayName(true).Replace(".", "_")); writer.WritePropertyName(TocHrefFieldName); writer.Write(filePath); writer.WriteObjectEnd(); } writer.WriteObjectEnd(); writer.WriteObjectEnd(); } /// /// Persists a toc snippet file, in json format, from the managed collection. /// void PersistNamespaceToc(string nameSpace) { var tocFilesSubfolder = Path.Combine(Options.ComputedContentFolder, TocFilesFolderName); if (!Directory.Exists(tocFilesSubfolder)) Directory.CreateDirectory(tocFilesSubfolder); var filePath = Path.Combine(tocFilesSubfolder, nameSpace + TocFilenameSuffix); using (var writer = File.CreateText(filePath)) { writer.Write(_namespaceTocs[nameSpace]); } } /// /// Emit the set of namespace files encapsulated in json to a TOC based around /// unordered lists, returning the html for inclusion on the page. /// /// string TransformNamespaceTocsToHtml() { var writer = new StringWriter(); writer.Write(""); return writer.ToString(); } } }