Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ignore] Added support for @@include tag #24

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DocNet/NavigatedPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public string CreateBreadCrumbsHTML(string relativePathToRoot)
}
else
{
fragments.Add(string.Format("<li><a href=\"{0}{1}\">{2}</a></li>", relativePathToRoot, HttpUtility.UrlEncode(targetURL), element.Name));
fragments.Add(string.Format("<li><a href=\"{0}{1}\">{2}</a></li>", relativePathToRoot, targetURL, element.Name));
}
}
return string.Format("<ul>{0}</ul>{1}", string.Join(" / ", fragments.ToArray()), Environment.NewLine);
Expand Down
11 changes: 7 additions & 4 deletions src/DocNet/SimpleNavigationElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ public override void GenerateOutput(Config activeConfig, NavigatedPath activePat
sb.Replace("{{Breadcrumbs}}", activePath.CreateBreadCrumbsHTML(relativePathToRoot));
sb.Replace("{{ToC}}", activePath.CreateToCHTML(relativePathToRoot));
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
sb.Replace("{{Content}}", content);

// Check if the content contains @@include tag
content = Utils.IncludeProcessor(content, Path.Combine(activeConfig.Source, "_partials"));

// 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
sb.Replace("{{Content}}", content);
Utils.CreateFoldersIfRequired(destinationFile);
File.WriteAllText(destinationFile, sb.ToString());
if(!this.IsIndexElement)
Expand Down Expand Up @@ -210,7 +213,7 @@ public override string TargetURL
_targetURLForHTML = (this.Value ?? string.Empty);
if(_targetURLForHTML.ToLowerInvariant().EndsWith(".md"))
{
_targetURLForHTML = _targetURLForHTML.Substring(0, _targetURLForHTML.Length-3) + ".htm";
_targetURLForHTML = _targetURLForHTML.Substring(0, _targetURLForHTML.Length-3) + ".html";
}
_targetURLForHTML = _targetURLForHTML.Replace("\\", "/");
}
Expand Down
35 changes: 34 additions & 1 deletion src/DocNet/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Docnet
Expand Down Expand Up @@ -184,5 +185,37 @@ public static string MakeRelativePath(string fromPath, string toPath)

return relativePath;
}
}

/// <summary>
/// Regex expression used to parse @@include(filename.html) tag.
/// </summary>
private static Regex includeRegex = new Regex(@"@@include\((.*)\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

/// <summary>
/// Process the input for @@include tags and embeds the included content
/// into the output.
/// </summary>
/// <param name="content"></param>
/// <param name="partialPath"></param>
/// <returns></returns>
public static string IncludeProcessor(string content, string partialPath)
{
Match m = includeRegex.Match(content);
while (m.Success)
{
if (m.Groups.Count > 1)
{
string tagToReplace = m.Groups[0].Value;
string fileName = m.Groups[1].Value;
string filePath = Path.Combine(partialPath, fileName);
if (File.Exists(filePath))
{
content = content.Replace(tagToReplace, File.ReadAllText(filePath));
}
}
m = m.NextMatch();
}
return content;
}
}
}