using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using System.Reflection; using AWSPowerShellGenerator.Generators; namespace AWSPowerShellGenerator.Writers.Help { /// /// Base class for all web help page output writers /// internal abstract class BasePageWriter { protected GeneratorOptions Options { get; private set; } protected string OutputFolder { get; private set; } protected string RelativePathToRoot { get; private set; } private const string FeedbackSection = "{0}"; protected BasePageWriter(GeneratorOptions options, string outputFolder) { Options = options; OutputFolder = outputFolder; } protected abstract string GetTitle(); protected abstract string GetService(); protected abstract string GetName(); protected abstract string GetMetaDescription(); public abstract string GenerateFilename(); public abstract string GetTOCID(); protected abstract void WriteContent(TextWriter writer); public void Write() { var filename = Path.Combine(OutputFolder, "items", GenerateFilename()); RelativePathToRoot = ComputeRelativeRootPath(OutputFolder, filename); var directory = new FileInfo(filename).Directory.FullName; if (!Directory.Exists(directory)) { Console.WriteLine("Creating Directory: {0}", directory); Directory.CreateDirectory(directory); } using (var writer = new StringWriter()) { writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine("", RelativePathToRoot); writer.WriteLine("", RelativePathToRoot); writer.WriteLine("", RelativePathToRoot); writer.WriteLine("", RelativePathToRoot); // every page needs a title, meta description and canonical url to satisfy indexing writer.WriteLine("", GetMetaDescription()); writer.WriteLine("{0} | AWS Tools for PowerShell", GetTitle()); writer.WriteLine(""); writer.WriteLine("", this.GenerateFilename(), this.GetTOCID()); writer.WriteLine(""); writer.WriteLine(""); // every page needs two hidden divs giving the search indexer the product title and guide name writer.WriteLine("
AWS Tools for Windows PowerShell
"); writer.WriteLine("
Command Reference
"); this.WriteRegionDisclaimer(writer); this.WriteHeader(writer); this.WriteToolbar(writer); writer.WriteLine("
"); this.WriteContent(writer); writer.WriteLine("
"); this.WriteFooter(writer); writer.WriteLine(""); writer.WriteLine(""); var content = writer.ToString(); using (var fileWriter = new StreamWriter(filename)) { fileWriter.Write(content); } } } protected virtual void WriteRegionDisclaimer(TextWriter writer) { // comment disclaimer is used by DCA pipeline only at present writer.WriteLine(""); // The BJS disclaimer is handled using js/css, and is not (yet) // injected via a pipeline. It must not be present in the docs we // deploy to dca. writer.WriteLine(""); writer.WriteLine("
"); writer.WriteLine("

{0}

", string.Format(WebHelpGenerator.CNNorth1RegionDisclaimerTemplate, Options.CNNorth1RegionDocsDomain)); writer.WriteLine("
"); writer.WriteLine(""); } protected virtual void WriteHeader(TextWriter writer) { writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine("

{0}

", this.GetName()); if (this.GetService() != null) writer.WriteLine("

{0}

", this.GetService()); writer.WriteLine("
"); writer.WriteLine("
"); } protected virtual void WriteToolbar(TextWriter writer) { writer.WriteLine("
"); writer.WriteLine(""); writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine("", RelativePathToRoot); writer.WriteLine("
"); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine(""); writer.WriteLine("
"); } protected virtual void WriteFooter(TextWriter writer) { writer.WriteLine("
"); writer.WriteLine("Link to this page", this.GenerateFilename(), this.GetTOCID()); writer.WriteLine(" "); writer.WriteLine(FeedbackSection, GenerateFeedbackHTML()); writer.WriteLine(""); writer.WriteLine("
"); WriteScriptFiles(writer); } private string GenerateFeedbackHTML() { string filename = Path.GetFileNameWithoutExtension(GenerateFilename()); string baseUrl = "https://docs.aws.amazon.com/forms/aws-doc-feedback"; string queryString = string.Format("?service_name={0}&topic_url=https://docs.aws.amazon.com/powershell/latest/reference/items/{1}.html", "PowerShell-Ref", // service_name filename // guide_name ); string fullUrl = baseUrl + queryString; string feedbackContentFormat = "" + "" + "Did this page help you?  " + "Yes  " + "No   " + "Tell us about it..." + "" + ""; string feedbackContent = string.Format(feedbackContentFormat, filename, fullUrl); return feedbackContent; } protected virtual void WriteScriptFiles(TextWriter writer) { writer.WriteLine("", RelativePathToRoot); writer.WriteLine(""); writer.WriteLine("", RelativePathToRoot); writer.WriteLine("", RelativePathToRoot); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine("", RelativePathToRoot); writer.WriteLine("", RelativePathToRoot); writer.WriteLine("", RelativePathToRoot); writer.WriteLine("", RelativePathToRoot); writer.WriteLine(""); } protected void AddMemberTableSectionHeader(TextWriter writer, string name, bool showIconColumn = true) { AddMemberTableSectionHeader(writer, name, showIconColumn, "Name", "Description"); } protected void AddMemberTableSectionHeader(TextWriter writer, string name, bool showIconColumn, string nameColumnName, string descriptionColumnName) { writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine("

{0}

", name, name.ToLower()); writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine(""); if(showIconColumn) writer.WriteLine(""); writer.WriteLine("", nameColumnName); writer.WriteLine("", descriptionColumnName); writer.WriteLine(""); } protected void AddMemberTableSectionClosing(TextWriter writer) { writer.WriteLine(""); writer.WriteLine("
 {0}{0}
"); writer.WriteLine("
"); writer.WriteLine("
"); } protected void AddSectionHeader(TextWriter writer, string name) { writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine("

{0}

", name, name.ToLower()); writer.WriteLine("
"); writer.WriteLine("
"); writer.WriteLine("
"); } protected void AddSectionClosing(TextWriter writer) { writer.WriteLine("
"); writer.WriteLine("
"); } protected string GetModuleAvailability(string moduleName) { var inMonolithic = moduleName != TOCWriter.InstallerModuleName; var inModular = !WebHelpGenerator.NonModularizedServices.Contains(moduleName); var text = new StringBuilder("Available in "); if (inModular) { text.Append($"AWS.Tools.{moduleName}"); } if (inMonolithic) { text.Append(inModular ? ", " : " and "); text.Append($"AWSPowerShell.NetCore"); if (inModular) { text.Append(" and "); } text.Append($"AWSPowerShell"); } return text.ToString(); } // computes the relative distance from root of a filename and returns a ../ sequence // that can step back far enough to get to root private static string ComputeRelativeRootPath(string rootFolder, string filename) { var relativePath = new StringBuilder(); var filepath = Path.GetDirectoryName(filename); while (!filepath.Equals(rootFolder, StringComparison.OrdinalIgnoreCase)) { if (relativePath.Length > 0) relativePath.Append("/"); relativePath.Append(".."); filepath = Path.GetDirectoryName(filepath); } return relativePath.ToString(); } } }