From 5dc2ecbeb16b672879ee8e72a37249fb8843cadb Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Mon, 26 Jun 2017 15:37:51 +0200 Subject: [PATCH 1/8] Introduce PathSpecification to config and migrate properties to methods so PathSpecification can be passed in --- src/DocNet/Config.cs | 23 +++++- src/DocNet/Docnet.csproj | 1 + src/DocNet/Engine.cs | 7 +- src/DocNet/INavigationElement.cs | 17 +++-- src/DocNet/NavigatedPath.cs | 14 ++-- src/DocNet/NavigationElement.cs | 17 +++-- src/DocNet/NavigationLevel.cs | 132 +++++++++++++++++++--------------- src/DocNet/PathSpecification.cs | 9 +++ src/DocNet/SimpleNavigationElement.cs | 54 +++++++------- 9 files changed, 173 insertions(+), 101 deletions(-) create mode 100644 src/DocNet/PathSpecification.cs diff --git a/src/DocNet/Config.cs b/src/DocNet/Config.cs index 482e665..bd21cc3 100644 --- a/src/DocNet/Config.cs +++ b/src/DocNet/Config.cs @@ -131,7 +131,7 @@ namespace Docnet private void GenerateSearchDataIndex() { var collectedSearchEntries = new List(); - this.Pages.CollectSearchIndexEntries(collectedSearchEntries, new NavigatedPath()); + this.Pages.CollectSearchIndexEntries(collectedSearchEntries, new NavigatedPath(), this.PathSpecification); JObject searchIndex = new JObject(new JProperty("docs", new JArray( collectedSearchEntries.Select(e=>new JObject( @@ -164,7 +164,7 @@ namespace Docnet searchSimpleElement.ExtraScriptProducerFunc = e=> @" "; - searchSimpleElement.GenerateOutput(this, activePath); + searchSimpleElement.GenerateOutput(this, activePath, this.PathSpecification); activePath.Pop(); } @@ -256,6 +256,25 @@ namespace Docnet get { return _templateContents ?? string.Empty; } } + public PathSpecification PathSpecification + { + get + { + var pathSpecification = PathSpecification.Full; + + var pathSpecificationAsString = _configData.PathSpecification; + if (!string.IsNullOrWhiteSpace(pathSpecificationAsString)) + { + if (!Enum.TryParse(pathSpecificationAsString, true, out pathSpecification)) + { + pathSpecification = PathSpecification.Full; + } + } + + return pathSpecification; + } + } + public NavigationLevel Pages { get diff --git a/src/DocNet/Docnet.csproj b/src/DocNet/Docnet.csproj index ff52d74..2340235 100644 --- a/src/DocNet/Docnet.csproj +++ b/src/DocNet/Docnet.csproj @@ -60,6 +60,7 @@ + diff --git a/src/DocNet/Engine.cs b/src/DocNet/Engine.cs index 28435cf..5078bd1 100644 --- a/src/DocNet/Engine.cs +++ b/src/DocNet/Engine.cs @@ -70,11 +70,14 @@ namespace Docnet Console.WriteLine("Errors occurred, can't continue!"); return null; } - if(config.Pages.IndexElement == null) + + var indexElement = config.Pages.GetIndexElement(config.PathSpecification); + if(indexElement == null) { Console.WriteLine("[ERROR] Root __index not found. The root navigationlevel is required to have an __index element"); return null; } + return config; } @@ -95,7 +98,7 @@ namespace Docnet Console.WriteLine("Copying source folders to copy."); _loadedConfig.CopySourceFoldersToCopy(); Console.WriteLine("Generating pages in '{0}'", _loadedConfig.Destination); - _loadedConfig.Pages.GenerateOutput(_loadedConfig, new NavigatedPath()); + _loadedConfig.Pages.GenerateOutput(_loadedConfig, new NavigatedPath(), _loadedConfig.PathSpecification); Console.WriteLine("Generating search index"); _loadedConfig.GenerateSearchData(); Console.WriteLine("Done!"); diff --git a/src/DocNet/INavigationElement.cs b/src/DocNet/INavigationElement.cs index 0e97aac..c665d16 100644 --- a/src/DocNet/INavigationElement.cs +++ b/src/DocNet/INavigationElement.cs @@ -35,23 +35,31 @@ namespace Docnet /// /// The active configuration to use for the output. /// The active path navigated through the ToC to reach this element. - void GenerateOutput(Config activeConfig, NavigatedPath activePath); - + /// The path specification. + void GenerateOutput(Config activeConfig, NavigatedPath activePath, PathSpecification pathSpecification); /// /// Generates the ToC fragment for this element, which can either be a simple line or a full expanded menu. /// /// The navigated path to the current element, which doesn't necessarily have to be this element. /// 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. /// - string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot); + string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, PathSpecification pathSpecification); /// /// Collects the search index entries. These are created from simple navigation elements found in this container, which aren't index element. /// /// The collected entries. /// The active path currently navigated. - void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath); + /// The path specification. + void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath, PathSpecification pathSpecification); + /// + /// Gets the target URL with respect to the . + /// + /// The path specification. + /// + string GetTargetURL(PathSpecification pathSpecification); /// /// Gets a value indicating whether this element is the __index element @@ -59,7 +67,6 @@ namespace Docnet bool IsIndexElement { get; set; } string Name { get; set; } object Value { get; set; } - string TargetURL { get; } NavigationLevel ParentContainer { get; set; } } } diff --git a/src/DocNet/NavigatedPath.cs b/src/DocNet/NavigatedPath.cs index b53f6c1..c93f21c 100644 --- a/src/DocNet/NavigatedPath.cs +++ b/src/DocNet/NavigatedPath.cs @@ -35,17 +35,18 @@ namespace Docnet public class NavigatedPath : Stack { /// - /// Creates the bread crumbs HTML of the elements in this path, delimited by '/' characters. + /// 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. /// - public string CreateBreadCrumbsHTML(string relativePathToRoot) + public string CreateBreadCrumbsHTML(string relativePathToRoot, PathSpecification pathSpecification) { 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.TargetURL; + var targetURL = element.GetTargetURL(pathSpecification); if(string.IsNullOrWhiteSpace(targetURL)) { fragments.Add(string.Format("
  • {0}
  • ", element.Name)); @@ -73,11 +74,12 @@ namespace Docnet /// /// Creates the ToC HTML for the element reached by the elements in this path. All containers in this path are expanded, all elements inside these containers which - /// aren't, are not expanded. + /// aren't, are not expanded. /// /// 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. /// - public string CreateToCHTML(string relativePathToRoot) + public string CreateToCHTML(string relativePathToRoot, PathSpecification pathSpecification) { // the root container is the bottom element of this path. We use that container to build the root and navigate any node open along the navigated path. var rootContainer = this.Reverse().FirstOrDefault() as NavigationLevel; @@ -86,7 +88,7 @@ namespace Docnet // no root container, no TOC return string.Empty; } - return rootContainer.GenerateToCFragment(this, relativePathToRoot); + return rootContainer.GenerateToCFragment(this, relativePathToRoot, pathSpecification); } } } diff --git a/src/DocNet/NavigationElement.cs b/src/DocNet/NavigationElement.cs index dd86175..9ca86d5 100644 --- a/src/DocNet/NavigationElement.cs +++ b/src/DocNet/NavigationElement.cs @@ -36,25 +36,32 @@ namespace Docnet ///
    /// The active configuration to use for the output. /// The active path navigated through the ToC to reach this element. - /// true if everything went well, false otherwise - public abstract void GenerateOutput(Config activeConfig, NavigatedPath activePath); + /// The path specification. + public abstract void GenerateOutput(Config activeConfig, NavigatedPath activePath, PathSpecification pathSpecification); /// /// Generates the ToC fragment for this element, which can either be a simple line or a full expanded menu. /// /// The navigated path to the current element, which doesn't necessarily have to be this element. /// 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. /// - public abstract string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot); + public abstract string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, PathSpecification pathSpecification); /// /// Collects the search index entries. These are created from simple navigation elements found in this container, which aren't index element. /// /// The collected entries. /// The active path currently navigated. - public abstract void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath); + /// The path specification. + public abstract void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath, PathSpecification pathSpecification); + /// + /// Gets the target URL with respect to the . + /// + /// The path specification. + /// + public abstract string GetTargetURL(PathSpecification pathSpecification); #region Properties - public abstract string TargetURL { get; } /// /// Gets / sets a value indicating whether this element is the __index element /// diff --git a/src/DocNet/NavigationLevel.cs b/src/DocNet/NavigationLevel.cs index bfc2ba1..b607c8e 100644 --- a/src/DocNet/NavigationLevel.cs +++ b/src/DocNet/NavigationLevel.cs @@ -77,20 +77,20 @@ namespace Docnet else { toAdd = new SimpleNavigationElement - { - Name = nameToUse, - Value = childValue, - IsIndexElement = isIndexElement - }; + { + Name = nameToUse, + Value = childValue, + IsIndexElement = isIndexElement + }; } } else { var subLevel = new NavigationLevel(_rootDirectory) - { - Name = child.Key, - IsRoot = false - }; + { + Name = child.Key, + IsRoot = false + }; subLevel.Load((JObject)child.Value); toAdd = subLevel; } @@ -105,12 +105,13 @@ namespace Docnet /// /// The collected entries. /// The active path currently navigated. - public override void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath) + /// The path specification. + public override void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath, PathSpecification pathSpecification) { activePath.Push(this); foreach (var element in this.Value) { - element.CollectSearchIndexEntries(collectedEntries, activePath); + element.CollectSearchIndexEntries(collectedEntries, activePath, pathSpecification); } activePath.Pop(); } @@ -121,14 +122,15 @@ namespace Docnet /// /// The active configuration to use for the output. /// The active path navigated through the ToC to reach this element. - public override void GenerateOutput(Config activeConfig, NavigatedPath activePath) + /// The path specification. + public override void GenerateOutput(Config activeConfig, NavigatedPath activePath, PathSpecification pathSpecification) { activePath.Push(this); int i = 0; while (i < this.Value.Count) { var element = this.Value[i]; - element.GenerateOutput(activeConfig, activePath); + element.GenerateOutput(activeConfig, activePath, pathSpecification); i++; } activePath.Pop(); @@ -140,8 +142,9 @@ namespace Docnet /// /// The navigated path to the current element, which doesn't necessarily have to be this element. /// 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. /// - public override string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot) + public override string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, PathSpecification pathSpecification) { var fragments = new List(); if (!this.IsRoot) @@ -163,7 +166,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.IndexElement; + var indexElement = this.GetIndexElement(pathSpecification); if (indexElement == null) { fragments.Add(string.Format("{0}{1}
  • ", elementStartTag, this.Name)); @@ -172,18 +175,18 @@ namespace Docnet { if (this.IsRoot) { - fragments.Add(indexElement.PerformGenerateToCFragment(navigatedPath, relativePathToRoot)); + fragments.Add(indexElement.PerformGenerateToCFragment(navigatedPath, relativePathToRoot, pathSpecification)); } else { - fragments.Add(string.Format("{0}{3}", elementStartTag, relativePathToRoot, HttpUtility.UrlPathEncode(indexElement.TargetURL), + fragments.Add(string.Format("{0}{3}", elementStartTag, relativePathToRoot, HttpUtility.UrlPathEncode(indexElement.GetTargetURL(pathSpecification)), this.Name)); } } // then the elements in the container. Index elements are skipped here. foreach (var element in this.Value) { - fragments.Add(element.GenerateToCFragment(navigatedPath, relativePathToRoot)); + fragments.Add(element.GenerateToCFragment(navigatedPath, relativePathToRoot, pathSpecification)); } fragments.Add(""); } @@ -191,7 +194,7 @@ namespace Docnet { // just a link fragments.Add(string.Format(" {2}", - relativePathToRoot, HttpUtility.UrlPathEncode(this.TargetURL), this.Name)); + relativePathToRoot, HttpUtility.UrlPathEncode(this.GetTargetURL(pathSpecification)), this.Name)); } if (!this.IsRoot) { @@ -204,9 +207,9 @@ namespace Docnet private NavigationLevel CreateGeneratedLevel(string path) { var root = new NavigationLevel(_rootDirectory) - { - ParentContainer = this - }; + { + ParentContainer = this + }; foreach (var mdFile in Directory.GetFiles(path, "*.md", SearchOption.TopDirectoryOnly)) { @@ -217,11 +220,11 @@ namespace Docnet } var item = new SimpleNavigationElement - { - Name = name, - Value = Utils.MakeRelativePath(mdFile, _rootDirectory), - ParentContainer = root - }; + { + Name = name, + Value = Utils.MakeRelativePath(mdFile, _rootDirectory), + ParentContainer = root + }; root.Value.Add(item); } @@ -270,49 +273,64 @@ namespace Docnet return title; } - - #region Properties - public override string TargetURL + public override string GetTargetURL(PathSpecification pathSpecification) { - get + var defaultElement = this.GetIndexElement(pathSpecification); + if (defaultElement == null) { - var defaultElement = this.IndexElement; - if (defaultElement == null) - { - return string.Empty; - } - return defaultElement.TargetURL ?? string.Empty; + return string.Empty; } + return defaultElement.GetTargetURL(pathSpecification) ?? string.Empty; } - - public SimpleNavigationElement IndexElement + public SimpleNavigationElement GetIndexElement(PathSpecification pathSpecification) { - get + var toReturn = this.Value.FirstOrDefault(e => e.IsIndexElement) as SimpleNavigationElement; + if (toReturn == null) { - var toReturn = this.Value.FirstOrDefault(e => e.IsIndexElement) as SimpleNavigationElement; - if (toReturn == null) + // no index element, add an artificial one. + var path = string.Empty; + if (this.ParentContainer != null) { - // no index element, add an artificial one. - var path = string.Empty; - if (this.ParentContainer != null) - { - path = Path.GetDirectoryName(this.ParentContainer.TargetURL); - } - var nameToUse = this.Name.Replace(".", "").Replace('/', '_').Replace("\\", "_").Replace(":", "").Replace(" ", ""); - if (string.IsNullOrWhiteSpace(nameToUse)) - { - return null; - } - toReturn = new SimpleNavigationElement() { ParentContainer = this, Value = string.Format("{0}{1}.md", path, nameToUse), Name = this.Name, IsIndexElement = true }; - this.Value.Add(toReturn); + path = Path.GetDirectoryName(this.ParentContainer.GetTargetURL(pathSpecification)); + } + var nameToUse = this.Name.Replace(".", "").Replace('/', '_').Replace("\\", "_").Replace(":", "").Replace(" ", ""); + if (string.IsNullOrWhiteSpace(nameToUse)) + { + return null; } - return toReturn; + var value = string.Empty; + + switch (pathSpecification) + { + case PathSpecification.Full: + value = string.Format("{0}{1}.md", path, nameToUse); + break; + + case PathSpecification.Relative: + value = Path.Combine(path ?? string.Empty, nameToUse + ".md"); + break; + + default: + throw new ArgumentOutOfRangeException(nameof(pathSpecification), pathSpecification, null); + } + + toReturn = new SimpleNavigationElement + { + ParentContainer = this, + Value = value, + Name = this.Name, + IsIndexElement = true + }; + + this.Value.Add(toReturn); } - } + return toReturn; + } + #region Properties /// /// Gets / sets a value indicating whether this element is the __index element /// diff --git a/src/DocNet/PathSpecification.cs b/src/DocNet/PathSpecification.cs new file mode 100644 index 0000000..bfe2736 --- /dev/null +++ b/src/DocNet/PathSpecification.cs @@ -0,0 +1,9 @@ +namespace Docnet +{ + public enum PathSpecification + { + Full, + + Relative + } +} \ No newline at end of file diff --git a/src/DocNet/SimpleNavigationElement.cs b/src/DocNet/SimpleNavigationElement.cs index c47f2b7..1c9f877 100644 --- a/src/DocNet/SimpleNavigationElement.cs +++ b/src/DocNet/SimpleNavigationElement.cs @@ -49,7 +49,9 @@ namespace Docnet /// /// The active configuration to use for the output. /// The active path navigated through the ToC to reach this element. - public override void GenerateOutput(Config activeConfig, NavigatedPath activePath) + /// The path specification. + /// + public override void GenerateOutput(Config activeConfig, NavigatedPath activePath, PathSpecification pathSpecification) { // if we're the __index element, we're not pushing ourselves on the path, as we're representing the container we're in, which is already on the path. if(!this.IsIndexElement) @@ -58,7 +60,7 @@ namespace Docnet } _relativeH2LinksOnPage.Clear(); var sourceFile = Utils.MakeAbsolutePath(activeConfig.Source, this.Value); - var destinationFile = Utils.MakeAbsolutePath(activeConfig.Destination, this.TargetURL); + var destinationFile = Utils.MakeAbsolutePath(activeConfig.Destination, this.GetTargetURL(pathSpecification)); var sb = new StringBuilder(activeConfig.PageTemplateContents.Length + 2048); var content = string.Empty; this.MarkdownFromFile = string.Empty; @@ -87,7 +89,7 @@ namespace Docnet { continue; } - defaultMarkdown.AppendFormat("* [{0}]({1}{2}){3}", sibling.Name, relativePathToRoot, HttpUtility.UrlPathEncode(sibling.TargetURL), Environment.NewLine); + defaultMarkdown.AppendFormat("* [{0}]({1}{2}){3}", sibling.Name, relativePathToRoot, HttpUtility.UrlPathEncode(sibling.GetTargetURL(pathSpecification)), Environment.NewLine); } defaultMarkdown.Append(Environment.NewLine); content = Utils.ConvertMarkdownToHtml(defaultMarkdown.ToString(), Path.GetDirectoryName(destinationFile), activeConfig.Destination, string.Empty, _relativeH2LinksOnPage, activeConfig.ConvertLocalLinks); @@ -109,8 +111,8 @@ 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)); - sb.Replace("{{ToC}}", activePath.CreateToCHTML(relativePathToRoot)); + sb.Replace("{{Breadcrumbs}}", activePath.CreateBreadCrumbsHTML(relativePathToRoot, pathSpecification)); + sb.Replace("{{ToC}}", activePath.CreateToCHTML(relativePathToRoot, pathSpecification)); sb.Replace("{{ExtraScript}}", (this.ExtraScriptProducerFunc == null) ? string.Empty : this.ExtraScriptProducerFunc(this)); // the last action has to be replacing the content marker, so markers in the content which we have in the template as well aren't replaced @@ -129,14 +131,15 @@ namespace Docnet /// /// The collected entries. /// The active path currently navigated. - public override void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath) + /// The path specification. + public override void CollectSearchIndexEntries(List collectedEntries, NavigatedPath activePath, PathSpecification pathSpecification) { activePath.Push(this); // simply convert ourselves into an entry if we're not an index if(!this.IsIndexElement) { var toAdd = new SearchIndexEntry(); - toAdd.Fill(this.MarkdownFromFile, this.TargetURL, this.Name, activePath); + toAdd.Fill(this.MarkdownFromFile, this.GetTargetURL(pathSpecification), this.Name, activePath); collectedEntries.Add(toAdd); } activePath.Pop(); @@ -148,8 +151,9 @@ namespace Docnet /// /// The navigated path to the current element, which doesn't necessarily have to be this element. /// 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. /// - public override string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot) + public override string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, PathSpecification pathSpecification) { // index elements are rendered in the parent container. if(this.IsIndexElement) @@ -157,7 +161,7 @@ namespace Docnet return string.Empty; } - return PerformGenerateToCFragment(navigatedPath, relativePathToRoot); + return PerformGenerateToCFragment(navigatedPath, relativePathToRoot, pathSpecification); } @@ -167,8 +171,9 @@ namespace Docnet /// /// The navigated path. /// The relative path to root. + /// The path specification. /// - public string PerformGenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot) + public string PerformGenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, PathSpecification pathSpecification) { // we can't navigate deeper from here. If we are the element being navigated to, we are the current and will have to emit any additional relative URLs too. bool isCurrent = navigatedPath.Contains(this); @@ -184,7 +189,7 @@ namespace Docnet string.IsNullOrWhiteSpace(liClass) ? string.Empty : string.Format(" class=\"{0}\"", liClass), string.IsNullOrWhiteSpace(aClass) ? string.Empty : string.Format(" class=\"{0}\"", aClass), relativePathToRoot, - HttpUtility.UrlPathEncode(this.TargetURL), + HttpUtility.UrlPathEncode(this.GetTargetURL(pathSpecification)), this.Name)); if(isCurrent && _relativeH2LinksOnPage.Any()) { @@ -203,26 +208,27 @@ namespace Docnet return string.Join(Environment.NewLine, fragments.ToArray()); } - - #region Properties - public override string TargetURL + /// + /// Gets the target URL with respect to the . + /// + /// The path specification. + /// + /// + public override string GetTargetURL(PathSpecification pathSpecification) { - get + if (_targetURLForHTML == null) { - if(_targetURLForHTML==null) + _targetURLForHTML = (this.Value ?? string.Empty); + if (_targetURLForHTML.ToLowerInvariant().EndsWith(".md")) { - _targetURLForHTML = (this.Value ?? string.Empty); - if(_targetURLForHTML.ToLowerInvariant().EndsWith(".md")) - { - _targetURLForHTML = _targetURLForHTML.Substring(0, _targetURLForHTML.Length-3) + ".htm"; - } - _targetURLForHTML = _targetURLForHTML.Replace("\\", "/"); + _targetURLForHTML = _targetURLForHTML.Substring(0, _targetURLForHTML.Length - 3) + ".htm"; } - return _targetURLForHTML; + _targetURLForHTML = _targetURLForHTML.Replace("\\", "/"); } + return _targetURLForHTML; } - + #region Properties /// /// Gets / sets a value indicating whether this element is the __index element /// From 8cd24cfd732f5b14c8f1530fe05ebd94835ea519 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Mon, 26 Jun 2017 19:34:03 +0200 Subject: [PATCH 2/8] First working version of newly generated indexes (PathSpecification: relative) --- src/DocNet/Config.cs | 2 +- src/DocNet/NavigationLevel.cs | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/DocNet/Config.cs b/src/DocNet/Config.cs index bd21cc3..55b15d6 100644 --- a/src/DocNet/Config.cs +++ b/src/DocNet/Config.cs @@ -262,7 +262,7 @@ namespace Docnet { var pathSpecification = PathSpecification.Full; - var pathSpecificationAsString = _configData.PathSpecification; + var pathSpecificationAsString = (string)_configData.PathSpecification; if (!string.IsNullOrWhiteSpace(pathSpecificationAsString)) { if (!Enum.TryParse(pathSpecificationAsString, true, out pathSpecification)) diff --git a/src/DocNet/NavigationLevel.cs b/src/DocNet/NavigationLevel.cs index b607c8e..d093b6b 100644 --- a/src/DocNet/NavigationLevel.cs +++ b/src/DocNet/NavigationLevel.cs @@ -71,6 +71,7 @@ namespace Docnet { path = Path.Combine(_rootDirectory, path); } + toAdd = CreateGeneratedLevel(path); toAdd.Name = nameToUse; } @@ -222,7 +223,7 @@ namespace Docnet var item = new SimpleNavigationElement { Name = name, - Value = Utils.MakeRelativePath(mdFile, _rootDirectory), + Value = Path.Combine(Utils.MakeRelativePath(_rootDirectory, path), Path.GetFileName(mdFile)), ParentContainer = root }; @@ -237,6 +238,7 @@ namespace Docnet root.Value.Add(subDirectoryNavigationElement); } + return root; } @@ -294,22 +296,39 @@ namespace Docnet { path = Path.GetDirectoryName(this.ParentContainer.GetTargetURL(pathSpecification)); } + var nameToUse = this.Name.Replace(".", "").Replace('/', '_').Replace("\\", "_").Replace(":", "").Replace(" ", ""); if (string.IsNullOrWhiteSpace(nameToUse)) { return null; } - var value = string.Empty; + var value = string.Format("{0}{1}.md", path, nameToUse); switch (pathSpecification) { case PathSpecification.Full: - value = string.Format("{0}{1}.md", path, nameToUse); + // Default is correct break; case PathSpecification.Relative: - value = Path.Combine(path ?? string.Empty, nameToUse + ".md"); + var preferredPath = path; + + // We're making a big assumption here, but we can get the first page and assume it's + // in the right folder. + + // Find first (simple) child and use 1 level up. A SimpleNavigationElement mostly represents a folder + // thus is excellent to be used as a folder name + var firstSimpleChildPage = (SimpleNavigationElement)this.Value.FirstOrDefault(x => x is SimpleNavigationElement && !ReferenceEquals(this, x)); + if (firstSimpleChildPage != null) + { + preferredPath = Path.GetDirectoryName(firstSimpleChildPage.Value); + } + + if (!string.IsNullOrWhiteSpace(preferredPath)) + { + value = Path.Combine(preferredPath, "index.md"); + } break; default: From f1432793948e9c821104f71b71ac75a61b09a923 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Mon, 26 Jun 2017 23:12:58 +0200 Subject: [PATCH 3/8] Add PathSpecification.RelativeAsFolder to generate a single folder for each file --- src/DocNet/INavigationElement.cs | 1 + src/DocNet/NavigationElement.cs | 2 ++ src/DocNet/NavigationLevel.cs | 48 +++++++++++++++++---------- src/DocNet/PathSpecification.cs | 4 ++- src/DocNet/SimpleNavigationElement.cs | 18 ++++++++-- src/MarkdownDeep/MardownDeep.cs | 62 +++++++++++++++++++++++++---------- 6 files changed, 96 insertions(+), 39 deletions(-) diff --git a/src/DocNet/INavigationElement.cs b/src/DocNet/INavigationElement.cs index c665d16..6a524be 100644 --- a/src/DocNet/INavigationElement.cs +++ b/src/DocNet/INavigationElement.cs @@ -65,6 +65,7 @@ namespace Docnet /// Gets a value indicating whether this element is the __index element /// bool IsIndexElement { get; set; } + bool IsAutoGenerated { get; set; } string Name { get; set; } object Value { get; set; } NavigationLevel ParentContainer { get; set; } diff --git a/src/DocNet/NavigationElement.cs b/src/DocNet/NavigationElement.cs index 9ca86d5..11f210c 100644 --- a/src/DocNet/NavigationElement.cs +++ b/src/DocNet/NavigationElement.cs @@ -67,6 +67,8 @@ namespace Docnet /// public abstract bool IsIndexElement { get; set; } + public bool IsAutoGenerated { get; set; } + public string Name { get; set; } /// /// Gets or sets the value of this element, which can either be a string or a NavigationLevel diff --git a/src/DocNet/NavigationLevel.cs b/src/DocNet/NavigationLevel.cs index d093b6b..d28f533 100644 --- a/src/DocNet/NavigationLevel.cs +++ b/src/DocNet/NavigationLevel.cs @@ -180,8 +180,14 @@ namespace Docnet } else { - fragments.Add(string.Format("{0}{3}", elementStartTag, relativePathToRoot, HttpUtility.UrlPathEncode(indexElement.GetTargetURL(pathSpecification)), - this.Name)); + var link = HttpUtility.UrlPathEncode(indexElement.GetTargetURL(pathSpecification)); + if (link.EndsWith("index.htm",StringComparison.InvariantCultureIgnoreCase)) + { + link = link.Substring(0, link.Length - "index.htm".Length); + } + + fragments.Add(string.Format("{0}{3}", + elementStartTag, relativePathToRoot, link, this.Name)); } } // then the elements in the container. Index elements are skipped here. @@ -209,7 +215,8 @@ namespace Docnet { var root = new NavigationLevel(_rootDirectory) { - ParentContainer = this + ParentContainer = this, + IsAutoGenerated = true }; foreach (var mdFile in Directory.GetFiles(path, "*.md", SearchOption.TopDirectoryOnly)) @@ -224,7 +231,8 @@ namespace Docnet { Name = name, Value = Path.Combine(Utils.MakeRelativePath(_rootDirectory, path), Path.GetFileName(mdFile)), - ParentContainer = root + ParentContainer = root, + IsAutoGenerated = true }; root.Value.Add(item); @@ -282,6 +290,7 @@ namespace Docnet { return string.Empty; } + return defaultElement.GetTargetURL(pathSpecification) ?? string.Empty; } @@ -312,22 +321,25 @@ namespace Docnet break; case PathSpecification.Relative: - var preferredPath = path; - - // We're making a big assumption here, but we can get the first page and assume it's - // in the right folder. - - // Find first (simple) child and use 1 level up. A SimpleNavigationElement mostly represents a folder - // thus is excellent to be used as a folder name - var firstSimpleChildPage = (SimpleNavigationElement)this.Value.FirstOrDefault(x => x is SimpleNavigationElement && !ReferenceEquals(this, x)); - if (firstSimpleChildPage != null) + case PathSpecification.RelativeAsFolder: + if (!IsRoot) { - preferredPath = Path.GetDirectoryName(firstSimpleChildPage.Value); - } + var preferredPath = value; - if (!string.IsNullOrWhiteSpace(preferredPath)) - { - value = Path.Combine(preferredPath, "index.md"); + // We're making a big assumption here, but we can get the first page and assume it's + // in the right folder. + + // Find first (simple) child and use 1 level up. A SimpleNavigationElement mostly represents a folder + // thus is excellent to be used as a folder name + var firstSimpleChildPage = (SimpleNavigationElement) this.Value.FirstOrDefault(x => x is SimpleNavigationElement && !ReferenceEquals(this, x)); + if (firstSimpleChildPage != null) + { + preferredPath = Path.GetDirectoryName(firstSimpleChildPage.Value); + if (!string.IsNullOrWhiteSpace(preferredPath)) + { + value = Path.Combine(preferredPath, "index.md"); + } + } } break; diff --git a/src/DocNet/PathSpecification.cs b/src/DocNet/PathSpecification.cs index bfe2736..254c664 100644 --- a/src/DocNet/PathSpecification.cs +++ b/src/DocNet/PathSpecification.cs @@ -4,6 +4,8 @@ { Full, - Relative + Relative, + + RelativeAsFolder } } \ No newline at end of file diff --git a/src/DocNet/SimpleNavigationElement.cs b/src/DocNet/SimpleNavigationElement.cs index 1c9f877..052bf5f 100644 --- a/src/DocNet/SimpleNavigationElement.cs +++ b/src/DocNet/SimpleNavigationElement.cs @@ -219,12 +219,26 @@ namespace Docnet if (_targetURLForHTML == null) { _targetURLForHTML = (this.Value ?? string.Empty); - if (_targetURLForHTML.ToLowerInvariant().EndsWith(".md")) + + var toReplace = ".md"; + var replacement = ".htm"; + + if (pathSpecification == PathSpecification.RelativeAsFolder) { - _targetURLForHTML = _targetURLForHTML.Substring(0, _targetURLForHTML.Length - 3) + ".htm"; + if (!IsIndexElement && !_targetURLForHTML.EndsWith("index.md", StringComparison.InvariantCultureIgnoreCase)) + { + replacement = "/index.htm"; + } } + + if (_targetURLForHTML.EndsWith(toReplace, StringComparison.InvariantCultureIgnoreCase)) + { + _targetURLForHTML = _targetURLForHTML.Substring(0, _targetURLForHTML.Length - toReplace.Length) + replacement; + } + _targetURLForHTML = _targetURLForHTML.Replace("\\", "/"); } + return _targetURLForHTML; } diff --git a/src/MarkdownDeep/MardownDeep.cs b/src/MarkdownDeep/MardownDeep.cs index 9b496fe..796525c 100644 --- a/src/MarkdownDeep/MardownDeep.cs +++ b/src/MarkdownDeep/MardownDeep.cs @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; @@ -279,8 +280,10 @@ namespace MarkdownDeep } // Override to supply the size of an image - public virtual bool OnGetImageSize(string url, bool TitledImage, out int width, out int height) + public virtual bool OnGetImageSize(string url, bool TitledImage, out int width, out int height, out string finalUrl) { + finalUrl = url; + if (GetImageSizeFunc != null) { var info = new ImageInfo() { url = url, titled_image=TitledImage }; @@ -314,30 +317,51 @@ namespace MarkdownDeep url=url.Substring(1); } - str=str + "\\" + url.Replace("/", "\\"); + var success = false; + // Because PathSpecification.RelativeAsFolder creates an additional layer of directories, + // this trial & error code was implemented to ensure that images could be found + var count = 0; + while (count < 2) + { + //Create an image object from the uploaded file + try + { + var fileName = str + "\\"; + var currentUrl = url; - // + for (int i = 0; i < count; i++) + { + currentUrl = "../" + currentUrl; + } - //Create an image object from the uploaded file - try - { - var img = System.Drawing.Image.FromFile(str); - width=img.Width; - height=img.Height; + fileName += currentUrl.Replace("/", "\\"); + + if (File.Exists(fileName)) + { + var img = System.Drawing.Image.FromFile(fileName); + width = img.Width; + height = img.Height; + finalUrl = currentUrl; + + if (MaxImageWidth != 0 && width > MaxImageWidth) + { + height = (int)((double)height * (double)MaxImageWidth / (double)width); + width = MaxImageWidth; + } - if (MaxImageWidth != 0 && width>MaxImageWidth) + success = true; + break; + } + } + catch (Exception) { - height=(int)((double)height * (double)MaxImageWidth / (double)width); - width=MaxImageWidth; } - return true; - } - catch (Exception) - { - return false; + count++; } + + return success; } @@ -386,9 +410,11 @@ namespace MarkdownDeep } // Try to determine width and height + var url = tag.attributes["src"]; int width, height; - if (OnGetImageSize(tag.attributes["src"], TitledImage, out width, out height)) + if (OnGetImageSize(url, TitledImage, out width, out height, out url)) { + tag.attributes["src"] = url; tag.attributes["width"] = width.ToString(); tag.attributes["height"] = height.ToString(); } From 1ef728edd42cd49b27ca4f2f4e5df2d62f729292 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Tue, 27 Jun 2017 09:51:58 +0200 Subject: [PATCH 4/8] Put "folder-only" navigation levels inside the right folder when using one of the relative path specifications --- src/DocNet/NavigationLevel.cs | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/DocNet/NavigationLevel.cs b/src/DocNet/NavigationLevel.cs index d28f533..ab0a633 100644 --- a/src/DocNet/NavigationLevel.cs +++ b/src/DocNet/NavigationLevel.cs @@ -199,9 +199,15 @@ namespace Docnet } else { + var link = HttpUtility.UrlPathEncode(this.GetTargetURL(pathSpecification)); + if (link.EndsWith("index.htm", StringComparison.InvariantCultureIgnoreCase)) + { + link = link.Substring(0, link.Length - "index.htm".Length); + } + // just a link fragments.Add(string.Format(" {2}", - relativePathToRoot, HttpUtility.UrlPathEncode(this.GetTargetURL(pathSpecification)), this.Name)); + relativePathToRoot, link, this.Name)); } if (!this.IsRoot) { @@ -301,9 +307,14 @@ namespace Docnet { // no index element, add an artificial one. var path = string.Empty; - if (this.ParentContainer != null) + + // Don't check parents when using relative paths since we need to walk the tree manually + if (pathSpecification == PathSpecification.Full) { - path = Path.GetDirectoryName(this.ParentContainer.GetTargetURL(pathSpecification)); + if (this.ParentContainer != null) + { + path = Path.GetDirectoryName(this.ParentContainer.GetTargetURL(pathSpecification)); + } } var nameToUse = this.Name.Replace(".", "").Replace('/', '_').Replace("\\", "_").Replace(":", "").Replace(" ", ""); @@ -324,7 +335,7 @@ namespace Docnet case PathSpecification.RelativeAsFolder: if (!IsRoot) { - var preferredPath = value; + string preferredPath = null; // We're making a big assumption here, but we can get the first page and assume it's // in the right folder. @@ -335,11 +346,27 @@ namespace Docnet if (firstSimpleChildPage != null) { preferredPath = Path.GetDirectoryName(firstSimpleChildPage.Value); - if (!string.IsNullOrWhiteSpace(preferredPath)) + } + else + { + // This is representing an empty folder. Search for first child navigation that has real childs, + // then retrieve the path by going levels up. + var firstChildNavigationLevel = (NavigationLevel)this.Value.FirstOrDefault(x => x is NavigationLevel && ((NavigationLevel)x).Value.Any() && !ReferenceEquals(this, x)); + if (firstChildNavigationLevel != null) { - value = Path.Combine(preferredPath, "index.md"); + var targetUrl = firstChildNavigationLevel.Value.First().GetTargetURL(pathSpecification); + + // 3 times since we need 2 parents up + preferredPath = Path.GetDirectoryName(targetUrl); + preferredPath = Path.GetDirectoryName(preferredPath); + preferredPath = Path.GetDirectoryName(preferredPath); } } + + if (!string.IsNullOrWhiteSpace(preferredPath)) + { + value = Path.Combine(preferredPath, "index.md"); + } } break; From d9d4594b3435783eb47681154977c18c06946e7c Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Tue, 27 Jun 2017 10:09:14 +0200 Subject: [PATCH 5/8] Remove index.htm from the target url (since navigating to the folder auto-navigates to the index.htm) --- src/DocNet/Docnet.csproj | 1 + src/DocNet/INavigationElementExtensions.cs | 27 +++++++++++++++++++++++++++ src/DocNet/NavigationLevel.cs | 16 ++-------------- src/DocNet/SimpleNavigationElement.cs | 5 +++-- 4 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 src/DocNet/INavigationElementExtensions.cs diff --git a/src/DocNet/Docnet.csproj b/src/DocNet/Docnet.csproj index 2340235..99b3653 100644 --- a/src/DocNet/Docnet.csproj +++ b/src/DocNet/Docnet.csproj @@ -65,6 +65,7 @@ + diff --git a/src/DocNet/INavigationElementExtensions.cs b/src/DocNet/INavigationElementExtensions.cs new file mode 100644 index 0000000..ce7138d --- /dev/null +++ b/src/DocNet/INavigationElementExtensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Web; + +namespace Docnet +{ + public static class INavigationElementExtensions + { + /// + /// Gets the final URL by encoding the path and by removing the filename if it equals index.htm. + /// + /// The navigation element. + /// The path specification. + /// + public static string GetFinalTargetUrl(this INavigationElement navigationElement, PathSpecification pathSpecification) + { + var targetUrl = navigationElement.GetTargetURL(pathSpecification); + var link = HttpUtility.UrlPathEncode(targetUrl); + + if (link.EndsWith("index.htm", StringComparison.InvariantCultureIgnoreCase)) + { + link = link.Substring(0, link.Length - "index.htm".Length); + } + + return link; + } + } +} \ No newline at end of file diff --git a/src/DocNet/NavigationLevel.cs b/src/DocNet/NavigationLevel.cs index ab0a633..ba1d781 100644 --- a/src/DocNet/NavigationLevel.cs +++ b/src/DocNet/NavigationLevel.cs @@ -180,14 +180,8 @@ namespace Docnet } else { - var link = HttpUtility.UrlPathEncode(indexElement.GetTargetURL(pathSpecification)); - if (link.EndsWith("index.htm",StringComparison.InvariantCultureIgnoreCase)) - { - link = link.Substring(0, link.Length - "index.htm".Length); - } - fragments.Add(string.Format("{0}{3}", - elementStartTag, relativePathToRoot, link, this.Name)); + elementStartTag, relativePathToRoot, indexElement.GetFinalTargetUrl(pathSpecification), this.Name)); } } // then the elements in the container. Index elements are skipped here. @@ -199,15 +193,9 @@ namespace Docnet } else { - var link = HttpUtility.UrlPathEncode(this.GetTargetURL(pathSpecification)); - if (link.EndsWith("index.htm", StringComparison.InvariantCultureIgnoreCase)) - { - link = link.Substring(0, link.Length - "index.htm".Length); - } - // just a link fragments.Add(string.Format(" {2}", - relativePathToRoot, link, this.Name)); + relativePathToRoot, this.GetFinalTargetUrl(pathSpecification), this.Name)); } if (!this.IsRoot) { diff --git a/src/DocNet/SimpleNavigationElement.cs b/src/DocNet/SimpleNavigationElement.cs index 052bf5f..db9df9b 100644 --- a/src/DocNet/SimpleNavigationElement.cs +++ b/src/DocNet/SimpleNavigationElement.cs @@ -89,7 +89,8 @@ namespace Docnet { continue; } - defaultMarkdown.AppendFormat("* [{0}]({1}{2}){3}", sibling.Name, relativePathToRoot, HttpUtility.UrlPathEncode(sibling.GetTargetURL(pathSpecification)), Environment.NewLine); + defaultMarkdown.AppendFormat("* [{0}]({1}{2}){3}", sibling.Name, relativePathToRoot, + sibling.GetFinalTargetUrl(pathSpecification), Environment.NewLine); } defaultMarkdown.Append(Environment.NewLine); content = Utils.ConvertMarkdownToHtml(defaultMarkdown.ToString(), Path.GetDirectoryName(destinationFile), activeConfig.Destination, string.Empty, _relativeH2LinksOnPage, activeConfig.ConvertLocalLinks); @@ -189,7 +190,7 @@ namespace Docnet string.IsNullOrWhiteSpace(liClass) ? string.Empty : string.Format(" class=\"{0}\"", liClass), string.IsNullOrWhiteSpace(aClass) ? string.Empty : string.Format(" class=\"{0}\"", aClass), relativePathToRoot, - HttpUtility.UrlPathEncode(this.GetTargetURL(pathSpecification)), + this.GetFinalTargetUrl(pathSpecification), this.Name)); if(isCurrent && _relativeH2LinksOnPage.Any()) { From 409e4de67968661b1bd9891121cfc225ec2b3456 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Tue, 27 Jun 2017 18:05:23 +0200 Subject: [PATCH 6/8] Don't remove index.htm when it's the only content of a link --- src/DocNet/INavigationElementExtensions.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/DocNet/INavigationElementExtensions.cs b/src/DocNet/INavigationElementExtensions.cs index ce7138d..0b60fb3 100644 --- a/src/DocNet/INavigationElementExtensions.cs +++ b/src/DocNet/INavigationElementExtensions.cs @@ -5,6 +5,8 @@ namespace Docnet { public static class INavigationElementExtensions { + private const string IndexHtmFileName = "index.htm"; + /// /// Gets the final URL by encoding the path and by removing the filename if it equals index.htm. /// @@ -16,9 +18,9 @@ namespace Docnet var targetUrl = navigationElement.GetTargetURL(pathSpecification); var link = HttpUtility.UrlPathEncode(targetUrl); - if (link.EndsWith("index.htm", StringComparison.InvariantCultureIgnoreCase)) + if (link.Length > IndexHtmFileName.Length && link.EndsWith(IndexHtmFileName, StringComparison.InvariantCultureIgnoreCase)) { - link = link.Substring(0, link.Length - "index.htm".Length); + link = link.Substring(0, link.Length - IndexHtmFileName.Length); } return link; From d099b7973966839d81b1e882cdcc93e62b3bd5e9 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Wed, 28 Jun 2017 17:08:14 +0200 Subject: [PATCH 7/8] Disable index.htm replacement for now --- src/DocNet/INavigationElementExtensions.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/DocNet/INavigationElementExtensions.cs b/src/DocNet/INavigationElementExtensions.cs index 0b60fb3..b49a3f5 100644 --- a/src/DocNet/INavigationElementExtensions.cs +++ b/src/DocNet/INavigationElementExtensions.cs @@ -18,12 +18,18 @@ namespace Docnet var targetUrl = navigationElement.GetTargetURL(pathSpecification); var link = HttpUtility.UrlPathEncode(targetUrl); - if (link.Length > IndexHtmFileName.Length && link.EndsWith(IndexHtmFileName, StringComparison.InvariantCultureIgnoreCase)) - { - link = link.Substring(0, link.Length - IndexHtmFileName.Length); - } + // 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); + // } + //} - return link; + return link; } } } \ No newline at end of file From 7e11dc1f9915d9cae82e8ce996a2aae56f5f876a Mon Sep 17 00:00:00 2001 From: Frans Bouma Date: Mon, 3 Jul 2017 10:15:34 +0200 Subject: [PATCH 8/8] version bump --- src/DocNet/Properties/AssemblyInfo.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DocNet/Properties/AssemblyInfo.cs b/src/DocNet/Properties/AssemblyInfo.cs index 4b7ae3c..03e610a 100644 --- a/src/DocNet/Properties/AssemblyInfo.cs +++ b/src/DocNet/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.15.0.0")] -[assembly: AssemblyFileVersion("0.15.0")] +[assembly: AssemblyVersion("0.16.0.0")] +[assembly: AssemblyFileVersion("0.16.0")]