Browse Source

Add PathSpecification.RelativeAsFolder to generate a single folder for each file

pull/65/head
Geert van Horrik 8 years ago
parent
commit
f143279394
6 changed files with 96 additions and 39 deletions
  1. +1
    -0
      src/DocNet/INavigationElement.cs
  2. +2
    -0
      src/DocNet/NavigationElement.cs
  3. +30
    -18
      src/DocNet/NavigationLevel.cs
  4. +3
    -1
      src/DocNet/PathSpecification.cs
  5. +16
    -2
      src/DocNet/SimpleNavigationElement.cs
  6. +44
    -18
      src/MarkdownDeep/MardownDeep.cs

+ 1
- 0
src/DocNet/INavigationElement.cs View File

@@ -65,6 +65,7 @@ namespace Docnet
/// Gets a value indicating whether this element is the __index element
/// </summary>
bool IsIndexElement { get; set; }
bool IsAutoGenerated { get; set; }
string Name { get; set; }
object Value { get; set; }
NavigationLevel ParentContainer { get; set; }


+ 2
- 0
src/DocNet/NavigationElement.cs View File

@@ -67,6 +67,8 @@ namespace Docnet
/// </summary>
public abstract bool IsIndexElement { get; set; }

public bool IsAutoGenerated { get; set; }

public string Name { get; set; }
/// <summary>
/// Gets or sets the value of this element, which can either be a string or a NavigationLevel


+ 30
- 18
src/DocNet/NavigationLevel.cs View File

@@ -180,8 +180,14 @@ namespace Docnet
}
else
{
fragments.Add(string.Format("{0}<a href=\"{1}{2}\">{3}</a></span></li>", 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}<a href=\"{1}{2}\">{3}</a></span></li>",
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;



+ 3
- 1
src/DocNet/PathSpecification.cs View File

@@ -4,6 +4,8 @@
{
Full,

Relative
Relative,

RelativeAsFolder
}
}

+ 16
- 2
src/DocNet/SimpleNavigationElement.cs View File

@@ -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;
}



+ 44
- 18
src/MarkdownDeep/MardownDeep.cs View File

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


Loading…
Cancel
Save