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