From 747853cb6b22a23a8e730f726fe7c31f0f8918a3 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Tue, 4 Jul 2017 12:12:12 +0200 Subject: [PATCH] Add 'StripIndexHtm' feature (default = false) --- src/DocNet/Config.cs | 8 ++++++++ src/DocNet/Engine.cs | 7 +++++-- src/DocNet/INavigationElement.cs | 6 +++--- src/DocNet/INavigationElementExtensions.cs | 26 ++++++++++++-------------- src/DocNet/NavigatedPath.cs | 6 +++--- src/DocNet/NavigationContext.cs | 7 +++++-- src/DocNet/NavigationElement.cs | 6 +++--- src/DocNet/NavigationLevel.cs | 24 ++++++++++++------------ src/DocNet/SimpleNavigationElement.cs | 17 ++++++++--------- 9 files changed, 59 insertions(+), 48 deletions(-) diff --git a/src/DocNet/Config.cs b/src/DocNet/Config.cs index 0b639b9..e9eb6f8 100644 --- a/src/DocNet/Config.cs +++ b/src/DocNet/Config.cs @@ -225,6 +225,14 @@ namespace Docnet } } + public bool StripIndexHtm + { + get + { + return _configData.StripIndexHtm ?? false; + } + } + public string ThemeName { get diff --git a/src/DocNet/Engine.cs b/src/DocNet/Engine.cs index 0857ab7..6630a98 100644 --- a/src/DocNet/Engine.cs +++ b/src/DocNet/Engine.cs @@ -48,7 +48,8 @@ namespace Docnet var navigationContext = new NavigationContext { MaxLevel = _loadedConfig.MaxLevelInToC, - PathSpecification = _loadedConfig.PathSpecification + PathSpecification = _loadedConfig.PathSpecification, + StripIndexHtm = _loadedConfig.StripIndexHtm }; GeneratePages(navigationContext); @@ -78,7 +79,9 @@ namespace Docnet return null; } - var indexElement = config.Pages.GetIndexElement(config.PathSpecification); + var navigationContext = new NavigationContext(config.PathSpecification, config.MaxLevelInToC, config.StripIndexHtm); + + var indexElement = config.Pages.GetIndexElement(navigationContext); if(indexElement == null) { Console.WriteLine("[ERROR] Root __index not found. The root navigationlevel is required to have an __index element"); diff --git a/src/DocNet/INavigationElement.cs b/src/DocNet/INavigationElement.cs index c36c64b..1cdf2c6 100644 --- a/src/DocNet/INavigationElement.cs +++ b/src/DocNet/INavigationElement.cs @@ -55,11 +55,11 @@ namespace Docnet void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath, NavigationContext navigationContext); /// - /// Gets the target URL with respect to the . + /// Gets the target URL with respect to the . /// - /// The path specification. + /// The navigation context. /// - string GetTargetURL(PathSpecification pathSpecification); + string GetTargetURL(NavigationContext navigationContext); /// /// Gets a value indicating whether this element is the __index element diff --git a/src/DocNet/INavigationElementExtensions.cs b/src/DocNet/INavigationElementExtensions.cs index b49a3f5..09579aa 100644 --- a/src/DocNet/INavigationElementExtensions.cs +++ b/src/DocNet/INavigationElementExtensions.cs @@ -11,25 +11,23 @@ namespace Docnet /// Gets the final URL by encoding the path and by removing the filename if it equals index.htm. /// /// The navigation element. - /// The path specification. + /// The navigation context. /// - public static string GetFinalTargetUrl(this INavigationElement navigationElement, PathSpecification pathSpecification) + public static string GetFinalTargetUrl(this INavigationElement navigationElement, NavigationContext navigationContext) { - var targetUrl = navigationElement.GetTargetURL(pathSpecification); + var targetUrl = navigationElement.GetTargetURL(navigationContext); var link = HttpUtility.UrlPathEncode(targetUrl); - // Disabled for now as discussed in #65 (https://github.com/FransBouma/DocNet/pull/65), but - // is required for #44 - //if (pathSpecification == PathSpecification.RelativeAsFolder) - //{ - // if (link.Length > IndexHtmFileName.Length && - // link.EndsWith(IndexHtmFileName, StringComparison.InvariantCultureIgnoreCase)) - // { - // link = link.Substring(0, link.Length - IndexHtmFileName.Length); - // } - //} + if (navigationContext.StripIndexHtm) + { + if (link.Length > IndexHtmFileName.Length && + link.EndsWith(IndexHtmFileName, StringComparison.InvariantCultureIgnoreCase)) + { + link = link.Substring(0, link.Length - IndexHtmFileName.Length); + } + } - return link; + return link; } } } \ No newline at end of file diff --git a/src/DocNet/NavigatedPath.cs b/src/DocNet/NavigatedPath.cs index 920ac65..3eda976 100644 --- a/src/DocNet/NavigatedPath.cs +++ b/src/DocNet/NavigatedPath.cs @@ -38,15 +38,15 @@ namespace Docnet /// Creates the bread crumbs HTML of the elements in this path, delimited by '/' characters. /// /// The relative path back to the URL root, e.g. ../.., so it can be used for links to elements in this path. - /// The path specification. + /// The navigation context. /// - public string CreateBreadCrumbsHTML(string relativePathToRoot, PathSpecification pathSpecification) + public string CreateBreadCrumbsHTML(string relativePathToRoot, NavigationContext navigationContext) { var fragments = new List(); // we enumerate a stack, which enumerates from top to bottom, so we have to reverse things first. foreach(var element in this.Reverse()) { - var targetURL = element.GetTargetURL(pathSpecification); + var targetURL = element.GetTargetURL(navigationContext); if(string.IsNullOrWhiteSpace(targetURL)) { fragments.Add(string.Format("
  • {0}
  • ", element.Name)); diff --git a/src/DocNet/NavigationContext.cs b/src/DocNet/NavigationContext.cs index 271c081..b2e4dfb 100644 --- a/src/DocNet/NavigationContext.cs +++ b/src/DocNet/NavigationContext.cs @@ -7,15 +7,18 @@ MaxLevel = 2; } - public NavigationContext(PathSpecification pathSpecification, int maxLevel) + public NavigationContext(PathSpecification pathSpecification, int maxLevel, bool stripIndexHtm) : this() { PathSpecification = pathSpecification; MaxLevel = maxLevel; + StripIndexHtm = stripIndexHtm; } + public PathSpecification PathSpecification { get; set; } + public int MaxLevel { get; set; } - public PathSpecification PathSpecification { get; set; } + public bool StripIndexHtm { get; set; } } } \ No newline at end of file diff --git a/src/DocNet/NavigationElement.cs b/src/DocNet/NavigationElement.cs index 8638772..8ad8bf3 100644 --- a/src/DocNet/NavigationElement.cs +++ b/src/DocNet/NavigationElement.cs @@ -55,11 +55,11 @@ namespace Docnet public abstract void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath, NavigationContext navigationContext); /// - /// Gets the target URL with respect to the . + /// Gets the target URL with respect to the . /// - /// The path specification. + /// The navigation context. /// - public abstract string GetTargetURL(PathSpecification pathSpecification); + public abstract string GetTargetURL(NavigationContext navigationContext); #region Properties /// diff --git a/src/DocNet/NavigationLevel.cs b/src/DocNet/NavigationLevel.cs index b81b4b8..9b30cff 100644 --- a/src/DocNet/NavigationLevel.cs +++ b/src/DocNet/NavigationLevel.cs @@ -167,7 +167,7 @@ namespace Docnet // first render the level header, which is the index element, if present or a label. The root always has an __index element otherwise we'd have stopped at load. var elementStartTag = "
  • "; - var indexElement = this.GetIndexElement(navigationContext.PathSpecification); + var indexElement = this.GetIndexElement(navigationContext); if (indexElement == null) { fragments.Add(string.Format("{0}{1}
  • ", elementStartTag, this.Name)); @@ -181,7 +181,7 @@ namespace Docnet else { fragments.Add(string.Format("{0}{3}", - elementStartTag, relativePathToRoot, indexElement.GetFinalTargetUrl(navigationContext.PathSpecification), this.Name)); + elementStartTag, relativePathToRoot, indexElement.GetFinalTargetUrl(navigationContext), this.Name)); } } // then the elements in the container. Index elements are skipped here. @@ -195,7 +195,7 @@ namespace Docnet { // just a link fragments.Add(string.Format(" {2}", - relativePathToRoot, this.GetFinalTargetUrl(navigationContext.PathSpecification), this.Name)); + relativePathToRoot, this.GetFinalTargetUrl(navigationContext), this.Name)); } if (!this.IsRoot) { @@ -277,18 +277,18 @@ namespace Docnet return title; } - public override string GetTargetURL(PathSpecification pathSpecification) + public override string GetTargetURL(NavigationContext navigationContext) { - var defaultElement = this.GetIndexElement(pathSpecification); + var defaultElement = this.GetIndexElement(navigationContext); if (defaultElement == null) { return string.Empty; } - return defaultElement.GetTargetURL(pathSpecification) ?? string.Empty; + return defaultElement.GetTargetURL(navigationContext) ?? string.Empty; } - public SimpleNavigationElement GetIndexElement(PathSpecification pathSpecification) + public SimpleNavigationElement GetIndexElement(NavigationContext navigationContext) { var toReturn = this.Value.FirstOrDefault(e => e.IsIndexElement) as SimpleNavigationElement; if (toReturn == null) @@ -297,11 +297,11 @@ namespace Docnet var path = string.Empty; // Don't check parents when using relative paths since we need to walk the tree manually - if (pathSpecification == PathSpecification.Full) + if (navigationContext.PathSpecification == PathSpecification.Full) { if (this.ParentContainer != null) { - path = Path.GetDirectoryName(this.ParentContainer.GetTargetURL(pathSpecification)); + path = Path.GetDirectoryName(this.ParentContainer.GetTargetURL(navigationContext)); } } @@ -313,7 +313,7 @@ namespace Docnet var value = string.Format("{0}{1}.md", path, nameToUse); - switch (pathSpecification) + switch (navigationContext.PathSpecification) { case PathSpecification.Full: // Default is correct @@ -342,7 +342,7 @@ namespace Docnet var firstChildNavigationLevel = (NavigationLevel)this.Value.FirstOrDefault(x => x is NavigationLevel && ((NavigationLevel)x).Value.Any() && !ReferenceEquals(this, x)); if (firstChildNavigationLevel != null) { - var targetUrl = firstChildNavigationLevel.Value.First().GetTargetURL(pathSpecification); + var targetUrl = firstChildNavigationLevel.Value.First().GetTargetURL(navigationContext); // 3 times since we need 2 parents up preferredPath = Path.GetDirectoryName(targetUrl); @@ -359,7 +359,7 @@ namespace Docnet break; default: - throw new ArgumentOutOfRangeException(nameof(pathSpecification), pathSpecification, null); + throw new ArgumentOutOfRangeException(nameof(navigationContext.PathSpecification), navigationContext.PathSpecification, null); } toReturn = new SimpleNavigationElement diff --git a/src/DocNet/SimpleNavigationElement.cs b/src/DocNet/SimpleNavigationElement.cs index 53e6105..2b02248 100644 --- a/src/DocNet/SimpleNavigationElement.cs +++ b/src/DocNet/SimpleNavigationElement.cs @@ -62,7 +62,7 @@ namespace Docnet } _relativeLinksOnPage.Clear(); var sourceFile = Utils.MakeAbsolutePath(activeConfig.Source, this.Value); - var destinationFile = Utils.MakeAbsolutePath(activeConfig.Destination, this.GetTargetURL(navigationContext.PathSpecification)); + var destinationFile = Utils.MakeAbsolutePath(activeConfig.Destination, this.GetTargetURL(navigationContext)); var sb = new StringBuilder(activeConfig.PageTemplateContents.Length + 2048); var content = string.Empty; this.MarkdownFromFile = string.Empty; @@ -92,7 +92,7 @@ namespace Docnet continue; } defaultMarkdown.AppendFormat("* [{0}]({1}{2}){3}", sibling.Name, relativePathToRoot, - sibling.GetFinalTargetUrl(navigationContext.PathSpecification), Environment.NewLine); + sibling.GetFinalTargetUrl(navigationContext), Environment.NewLine); } defaultMarkdown.Append(Environment.NewLine); content = Utils.ConvertMarkdownToHtml(defaultMarkdown.ToString(), Path.GetDirectoryName(destinationFile), activeConfig.Destination, string.Empty, _relativeLinksOnPage, activeConfig.ConvertLocalLinks); @@ -114,7 +114,7 @@ namespace Docnet sb.Replace("{{Path}}", relativePathToRoot); sb.Replace("{{RelativeSourceFileName}}", Utils.MakeRelativePathForUri(activeConfig.Destination, sourceFile).TrimEnd('/')); sb.Replace("{{RelativeTargetFileName}}", Utils.MakeRelativePathForUri(activeConfig.Destination, destinationFile).TrimEnd('/')); - sb.Replace("{{Breadcrumbs}}", activePath.CreateBreadCrumbsHTML(relativePathToRoot, navigationContext.PathSpecification)); + sb.Replace("{{Breadcrumbs}}", activePath.CreateBreadCrumbsHTML(relativePathToRoot, navigationContext)); sb.Replace("{{ToC}}", activePath.CreateToCHTML(relativePathToRoot, navigationContext)); sb.Replace("{{ExtraScript}}", (this.ExtraScriptProducerFunc == null) ? string.Empty : this.ExtraScriptProducerFunc(this)); @@ -142,7 +142,7 @@ namespace Docnet if (!this.IsIndexElement) { var toAdd = new SearchIndexEntry(); - toAdd.Fill(this.MarkdownFromFile, this.GetTargetURL(navigationContext.PathSpecification), this.Name, activePath); + toAdd.Fill(this.MarkdownFromFile, this.GetTargetURL(navigationContext), this.Name, activePath); collectedEntries.Add(toAdd); } activePath.Pop(); @@ -192,7 +192,7 @@ namespace Docnet string.IsNullOrWhiteSpace(liClass) ? string.Empty : string.Format(" class=\"{0}\"", liClass), string.IsNullOrWhiteSpace(aClass) ? string.Empty : string.Format(" class=\"{0}\"", aClass), relativePathToRoot, - this.GetFinalTargetUrl(navigationContext.PathSpecification), + this.GetFinalTargetUrl(navigationContext), this.Name)); if (isCurrent && _relativeLinksOnPage.SelectMany(x => x.Children).Any(x => x.Level > 1)) { @@ -220,10 +220,9 @@ namespace Docnet /// /// Gets the target URL with respect to the . /// - /// The path specification. + /// The navigation context. /// - /// - public override string GetTargetURL(PathSpecification pathSpecification) + public override string GetTargetURL(NavigationContext navigationContext) { if (_targetURLForHTML == null) { @@ -232,7 +231,7 @@ namespace Docnet var toReplace = ".md"; var replacement = ".htm"; - if (pathSpecification == PathSpecification.RelativeAsFolder) + if (navigationContext.PathSpecification == PathSpecification.RelativeAsFolder) { if (!IsIndexElement && !_targetURLForHTML.EndsWith("index.md", StringComparison.InvariantCultureIgnoreCase)) {