-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Front matter extractor class * Added support for front matter from command line * Updated docs * Updated version numbers
- Loading branch information
1 parent
700f034
commit a84cd54
Showing
13 changed files
with
537 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using HtmlAgilityPack; | ||
using System; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Html2md | ||
{ | ||
public class FrontMatterExtractor | ||
{ | ||
public string? Extract(FrontMatterOptions options, HtmlDocument document, Uri pageUri) | ||
{ | ||
if (!options.Enabled) | ||
{ | ||
return null; | ||
} | ||
|
||
var builder = new StringBuilder(); | ||
builder.AppendLine(options.Delimiter); | ||
|
||
foreach (var singleValue in options.SingleValueProperties) | ||
{ | ||
builder | ||
.Append(singleValue.Key) | ||
.Append(": ") | ||
.AppendLine(ExtractValue(singleValue.Value, document, pageUri)); | ||
} | ||
|
||
foreach (var singleValue in options.ArrayValueProperties) | ||
{ | ||
builder | ||
.Append(singleValue.Key) | ||
.AppendLine(":"); | ||
|
||
foreach (var match in document.DocumentNode.SelectNodes(singleValue.Value)) | ||
{ | ||
builder | ||
.Append(" - ") | ||
.AppendLine(match.GetDirectInnerText().Trim()); | ||
} | ||
} | ||
|
||
builder.AppendLine(options.Delimiter); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
private static string ExtractValue(string xpathOrMacro, HtmlDocument document, Uri pageUri) | ||
{ | ||
if (xpathOrMacro.StartsWith("{{")) | ||
{ | ||
if (Regex.IsMatch(xpathOrMacro, @"^{{'[^']*'}}$")) | ||
{ | ||
return xpathOrMacro.Substring(3, xpathOrMacro.Length - 6); | ||
} | ||
|
||
return xpathOrMacro switch | ||
{ | ||
"{{RelativeUriPath}}" => pageUri.LocalPath, | ||
_ => throw new Exception("Unknown macro " + xpathOrMacro), | ||
}; | ||
} | ||
else | ||
{ | ||
var node = document.DocumentNode.SelectSingleNode(xpathOrMacro); | ||
var attributeName = Regex.Match(xpathOrMacro, @"/@(\w+)$"); | ||
if (attributeName.Success) | ||
{ | ||
return node.GetAttributeValue(attributeName.Groups[1].Value, string.Empty).Trim(); | ||
} | ||
|
||
return node.GetDirectInnerText().Trim(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Html2md | ||
{ | ||
/// <summary> | ||
/// Configuration for writing Front Matter sections to converted documents. | ||
/// </summary> | ||
public class FrontMatterOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether Front Matter should be written to converted documents. | ||
/// </summary> | ||
public bool Enabled { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the delimiter that should be written to the Front Matter section. Default is ---. | ||
/// </summary> | ||
public string Delimiter { get; set; } = "---"; | ||
|
||
/// <summary> | ||
/// Gets or sets the XPath or macro properties that should be written to the Front Matter section. | ||
/// If an XPath is provided and more than one element matches, then the first is used. | ||
/// </summary> | ||
public Dictionary<string, string> SingleValueProperties { get; set; } = new Dictionary<string, string>(); | ||
|
||
/// <summary> | ||
/// Gets or sets the XPath properties that should be written to the Front Matter section as a list. Each matching | ||
/// value will be written as an entry in the list. | ||
/// </summary> | ||
public Dictionary<string, string> ArrayValueProperties { get; set; } = new Dictionary<string, string>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.