/// <summary> /// Replaces html <IsThisPage> and <IsNotThisPage> with html content (within <ForAllPages> tag). /// </summary> /// <param name="forAllPagesContent">Html content to replace <ForAllPages> tag.</param> /// <param name="currentFileInLoop">Current file we are copying an instance of <ForAllPages> for.</param> /// <param name="currentPageFile">Current file being built.</param> /// <returns>Returns Html content with <IsThisPage> and <IsNotThisPage> converted into pure Html.</returns> private static string HandleThisPageSpecialTags(string forAllPagesContent, SidenavFile currentFileInLoop, SidenavFile currentPageFile) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(forAllPagesContent); IEnumerable <HtmlNode> show, hide; if (currentFileInLoop == currentPageFile) { show = Utils.CloneListNotValues(doc.DocumentNode.Descendants("IsThisPage".ToLower(Configuration.Culture))); hide = Utils.CloneListNotValues(doc.DocumentNode.Descendants("IsNotThisPage".ToLower(Configuration.Culture))); } else { hide = Utils.CloneListNotValues(doc.DocumentNode.Descendants("IsThisPage".ToLower(Configuration.Culture))); show = Utils.CloneListNotValues(doc.DocumentNode.Descendants("IsNotThisPage".ToLower(Configuration.Culture))); } foreach (HtmlNode showNode in show) { foreach (HtmlNode childNode in showNode.ChildNodes) { showNode.ParentNode.InsertBefore(childNode, showNode); } showNode.ParentNode.RemoveChild(showNode); } foreach (HtmlNode hideNode in hide) { hideNode.ParentNode.RemoveChild(hideNode); } return(doc.DocumentNode.OuterHtml); }
/// <summary> /// Builds and writes a single documentation page file. /// </summary> /// <param name="file">File to build.</param> private void BuildFile(SidenavFile file) { string outputFilePath = Path.Combine(outputFolderPath, file.GetAbsoluteFilePath()); string innerfileContent = file.StringContent; Template pageTemplate = TemplateManager.GetTemplateByElementName("Page", templates); string pagefileContent = pageTemplate.TemplateContent; pagefileContent = pagefileContent.Replace("@Page.Id;", file.Id); foreach (PProperty property in file.Properties) { pagefileContent = pagefileContent.Replace($"@Page.{property.Name};", property.Value); } if (pagefileContent == null) { throw new MissingFieldException("Could not find Page template"); } string combinedFileContent = pagefileContent.Replace("@ChildContent;", innerfileContent); combinedFileContent = TemplateManager.InjectTemplates(combinedFileContent, templates); combinedFileContent = SpecialTagManager.InjectSpecialTags(combinedFileContent, rootFolder, file); using (StreamWriter writer = new StreamWriter(outputFilePath, false)) { writer.Write(Utils.MinifyHtml(combinedFileContent)); } }
private static void LoadHtmlPageProperties(SidenavFile file) { List <PProperty> properties = new List <PProperty>(); string fullContent = Utils.GetFullFileConent(file.InputFilePath); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(fullContent); doc.RemoveComments(); HtmlNode[] propertyNodes = doc.DocumentNode.Descendants("PProperty").ToArray(); foreach (HtmlNode node in propertyNodes) { string propertyName = HttpUtility.UrlEncode(node.GetAttributeValue("name", string.Empty)); string propertyValue = node.GetAttributeValue("value", string.Empty); if (!string.IsNullOrEmpty(propertyName)) { PProperty property = new PProperty(propertyName, propertyValue); properties.Add(property); } node.Remove(); } file.SetContent(properties, doc.DocumentNode.OuterHtml); }
/// <summary> /// Returns absolute file path of the file inside Input directory. /// </summary> /// <param name="file">File to get the path of.</param> /// <returns>Absolute file path.</returns> public static string GetAbsoluteFilePath(this SidenavFile file) { if (file.Parent.FolderName == "root") { return(file.RelativeHtmlLink); } else { return(Path.Combine(file.Parent.GetAbsoluteFolderPath(), file.RelativeHtmlLink)); } }
/// <summary> /// Deserializes the Properties and html content which appear at the start of a file. /// </summary> /// <param name="file">The file to get the properties of.</param> public static void LoadProperties(SidenavFile file) { if (file.FileType == FileType.Markdown) { LoadMarkdownPageProperties(file); } else { LoadHtmlPageProperties(file); } }
/// <summary> /// Gets the absolute link to a page in sidenav. /// </summary> /// <param name="file">File to get link from.</param> /// <returns>Absolute link starting with '/'.</returns> public static string GetAbsoluteLink(this SidenavFile file) { StringBuilder builder = new StringBuilder(file.RelativeHtmlLink); SidenavFolder parentFolder = file.Parent; builder.Insert(0, $"{parentFolder.RelativeHtmlLink}/"); while (parentFolder.Parent != null) { parentFolder = parentFolder.Parent; builder.Insert(0, $"{parentFolder.RelativeHtmlLink}/"); } return(builder.ToString()); }
/// <summary> /// Generates html content which should replace the <ForAllFolders> tag. /// </summary> /// <param name="rootFolder">Sidenav folder which represents as the Input folder.</param> /// <param name="currentFile">The file currently being built for.</param> /// <param name="allSubFolderContent">Html content of <ForAllSubFolders> tag to be repeated.</param> /// <param name="allPagesContent">Html content of <ForAllSubPages> tag to be repeated.</param> /// <param name="layer">Integer representing the depth of the current folder from the Input folder.</param> /// <returns>Returns html content to replace the <ForAllFolders> tag.</returns> private static string GetHtmlToReplaceForAllFoldersTag(SidenavFolder rootFolder, SidenavFile currentFile, string allSubFolderContent, string allPagesContent, int layer) { StringBuilder forAllContent = new StringBuilder(); List <SidenavElement> elmts = new List <SidenavElement>(); foreach (SidenavFolder folder in rootFolder.Folders) { elmts.Add(folder); } foreach (SidenavFile file in rootFolder.Files) { elmts.Add(file); } IOrderedEnumerable <SidenavElement> orderedElmts = elmts.OrderBy(o => o.Order); foreach (SidenavElement elmt in orderedElmts) { if (elmt is SidenavFolder) { SidenavFolder folder = (SidenavFolder)elmt; string folderContent = GetHtmlToReplaceForAllFoldersTag(folder, currentFile, allSubFolderContent, allPagesContent, layer + 1); string allSubFolderFilledContent = allSubFolderContent.Replace("@SubFolder.Name;", folder.FolderName); allSubFolderFilledContent = allSubFolderFilledContent.Replace("@SubFolder.Layer;", layer.ToString()); allSubFolderFilledContent = HandleThisFolderSpecialTags(allSubFolderFilledContent, folder, currentFile); allSubFolderFilledContent = allSubFolderFilledContent.Replace("@SubFolder.Recursive;", folderContent); forAllContent.Append(allSubFolderFilledContent); } else { SidenavFile file = (SidenavFile)elmt; string pageContent = allPagesContent; pageContent = HandleThisPageSpecialTags(pageContent, file, currentFile); foreach (PProperty property in file.Properties) { pageContent = pageContent.Replace($"@SubPage.{property.Name};", property.Value); } pageContent = pageContent.Replace("@SubPage.Layer;", layer.ToString()); pageContent = pageContent.Replace("@SubPage.Link;", file.GetAbsoluteLink()); pageContent = pageContent.Replace("@SubPage.Id;", file.Id); forAllContent.Append(pageContent); } } return(forAllContent.ToString()); }
private static void LoadMarkdownPageProperties(SidenavFile file) { List <PProperty> properties = new List <PProperty>(); string fullContent = Utils.GetFullFileConent(file.InputFilePath); string[] lines = fullContent.Split(Configuration.NewlineChars, StringSplitOptions.None); if (lines.Length == 0) { file.SetContent(properties, string.Empty); return; } int lineNum = 0; Regex regex = new Regex("Property:*,*"); while (regex.IsMatch(lines[lineNum]) && lineNum < lines.Length) { string data = lines[lineNum].Substring(9); string[] datas = data.Split(','); string propertyName = datas[0].Trim(); string propertyValue = datas[1].Trim(); if (!string.IsNullOrEmpty(propertyName) && !string.IsNullOrEmpty(propertyValue)) { PProperty property = new PProperty(propertyName, propertyValue); properties.Add(property); } lineNum++; } StringBuilder builder = new StringBuilder(); bool first = true; for (int i = lineNum; i < lines.Length; i++) { if (!first) { builder.Append(Configuration.DefaultNewlineChar); } builder.Append(lines[i]); first = false; } var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); var markdown = Markdown.ToHtml(builder.ToString(), pipeline); file.SetContent(properties, markdown); }
private static SidenavFolder LoadSidenavFolder(string sidenavFolderName, string directory, SidenavFolder parent) { string actualFolderName = Path.GetFileName(directory); if (sidenavFolderName == "root") { actualFolderName = string.Empty; } SidenavFolder parentFolder = new SidenavFolder(sidenavFolderName, directory, HttpUtility.UrlEncode(actualFolderName), parent); DirectoryInfo directoryInfo = new DirectoryInfo(directory); foreach (FileInfo fileInfo in directoryInfo.GetFiles()) { if (fileInfo.Extension == ".html" || fileInfo.Extension == ".md") { string fileLink = HttpUtility.UrlEncode(fileInfo.Name); FileType fileType = FileType.Html; if (fileInfo.Extension == ".md") { fileLink = fileLink.Substring(0, fileLink.Length - 2) + "html"; fileType = FileType.Markdown; } SidenavFile file = new SidenavFile(fileInfo.Name, fileType, fileInfo.FullName, fileLink, parentFolder); parentFolder.Files.Add(file); } } foreach (DirectoryInfo subDirInfo in directoryInfo.GetDirectories()) { SidenavFolder folder = LoadSidenavFolder(subDirInfo.Name, subDirInfo.FullName, parentFolder); if (folder.Files.Count != 0 || folder.Folders.Count != 0) { parentFolder.Folders.Add(folder); } } return(parentFolder); }
/// <summary> /// Replaces special tags in html. /// </summary> /// <param name="htmlContent">Html to replace tags in.</param> /// <param name="rootFolder">Root SidenavFolder.</param> /// <param name="currentFile">The <see cref="SidenavFile"/> whose page is currently being built.</param> /// <returns>Html with special tags corrected.</returns> public static string InjectSpecialTags(string htmlContent, SidenavFolder rootFolder, SidenavFile currentFile) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlContent); List <HtmlNode> allFoldersNodes = doc.GetAllNodesOfType("ForAllFolders"); foreach (HtmlNode node in allFoldersNodes) { HtmlNode allSubFoldersNode = node.FindFirstChild("ForeachSubFolder"); HtmlNode allPagesNode = node.FindFirstChild("ForeachPage"); StringBuilder allSubFolderContent = new StringBuilder(); if (allSubFoldersNode != null) { foreach (HtmlNode subNode in allSubFoldersNode.ChildNodes) { allSubFolderContent.Append(subNode.OuterHtml); } } StringBuilder allPagesContent = new StringBuilder(); if (allPagesNode != null) { foreach (HtmlNode pagesNode in allPagesNode.ChildNodes) { allPagesContent.Append(pagesNode.OuterHtml); } } string rootAllFoldersContent = GetHtmlToReplaceForAllFoldersTag(rootFolder, currentFile, allSubFolderContent.ToString(), allPagesContent.ToString(), 1); HtmlDocument newDoc = new HtmlDocument(); newDoc.LoadHtml(rootAllFoldersContent); foreach (HtmlNode allFoldersNode in newDoc.DocumentNode.ChildNodes) { node.ParentNode.InsertBefore(allFoldersNode, node); } node.ParentNode.RemoveChild(node); } doc.RemoveComments(); return(doc.DocumentNode.OuterHtml); }
/// <summary> /// Replaces html <IsThisFolder> and <IsNotThisFolder> with html content (within <ForAllSubFolders> tag). /// </summary> /// <param name="forAllSubFoldersContent">Html content to replace <ForAllSubFolders> tag.</param> /// <param name="currentFolderInLoop">Current folder we are copying an instance of <ForAllPages> for.</param> /// <param name="currentPageFile">Current file being built.</param> /// <returns>Returns Html content with <IsThisFolder> and <IsNotThisFolder> converted into pure Html.</returns> private static string HandleThisFolderSpecialTags(string forAllSubFoldersContent, SidenavFolder currentFolderInLoop, SidenavFile currentPageFile) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(forAllSubFoldersContent); IEnumerable <HtmlNode> show, hide; bool isThisFolder = false; SidenavFolder loopFolder = currentPageFile.Parent; if (loopFolder == currentFolderInLoop) { isThisFolder = true; } while (loopFolder.Parent != null && !isThisFolder) { if (loopFolder.Parent == currentFolderInLoop) { isThisFolder = true; } loopFolder = loopFolder.Parent; } if (isThisFolder) { show = Utils.CloneListNotValues(doc.DocumentNode.Descendants("IsThisFolder".ToLower(Configuration.Culture))); hide = Utils.CloneListNotValues(doc.DocumentNode.Descendants("IsNotThisFolder".ToLower(Configuration.Culture))); } else { hide = Utils.CloneListNotValues(doc.DocumentNode.Descendants("IsThisFolder".ToLower(Configuration.Culture))); show = Utils.CloneListNotValues(doc.DocumentNode.Descendants("IsNotThisFolder".ToLower(Configuration.Culture))); } foreach (HtmlNode showNode in show) { foreach (HtmlNode childNode in showNode.ChildNodes) { showNode.ParentNode.InsertBefore(childNode, showNode); } showNode.ParentNode.RemoveChild(showNode); } foreach (HtmlNode hideNode in hide) { hideNode.ParentNode.RemoveChild(hideNode); } return(doc.DocumentNode.OuterHtml); }