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("
");
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("
");
}
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();
}
}
}