-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from baynezy/feature/issue-362-refactor
Create concrete implementations of replacers
- Loading branch information
Showing
39 changed files
with
453 additions
and
268 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Replaces an anchor tag with the link text and the link URL in Markdown format. | ||
/// </summary> | ||
public class AnchorTagReplacer : CustomReplacer | ||
{ | ||
public AnchorTagReplacer() | ||
{ | ||
CustomAction = HtmlParser.ReplaceAnchor; | ||
} | ||
} |
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,11 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Replaces an anchor tag with the link text and the link URL in Markdown format. | ||
/// </summary> | ||
public class BlockquoteTagReplacer : CustomReplacer | ||
{ | ||
public BlockquoteTagReplacer() | ||
{ | ||
CustomAction = HtmlParser.ReplaceBlockquote; | ||
} | ||
} |
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,12 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Removes the body tag. | ||
/// </summary> | ||
public class BodyTagReplacer : PatternReplacer | ||
{ | ||
public BodyTagReplacer() | ||
{ | ||
Pattern = "</?body[^>]*>"; | ||
Replacement = ""; | ||
} | ||
} |
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,12 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Replaces the HTML break tag with its Markdown equivalent. | ||
/// </summary> | ||
public class BreakTagReplacer : PatternReplacer | ||
{ | ||
public BreakTagReplacer() | ||
{ | ||
Pattern = "<br[^>]*>"; | ||
Replacement = " " + Environment.NewLine; | ||
} | ||
} |
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,16 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Replaces the HTML code tag with its Markdown equivalent. | ||
/// </summary> | ||
public class CodeTagReplacer : CustomReplacer | ||
{ | ||
public CodeTagReplacer() | ||
{ | ||
CustomAction = HtmlParser.ReplaceCode; | ||
} | ||
|
||
public CodeTagReplacer(bool supportSyntaxHighlighting) | ||
{ | ||
CustomAction = html => HtmlParser.ReplaceCode(html, supportSyntaxHighlighting); | ||
} | ||
} |
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,18 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Allows for multiple replacements to be applied to the HTML. | ||
/// </summary> | ||
public abstract class CompositeReplacer : IReplacer | ||
{ | ||
private readonly IList<IReplacer> _replacements = new List<IReplacer>(); | ||
|
||
protected void AddReplacer(IReplacer replacer) | ||
{ | ||
_replacements.Add(replacer); | ||
} | ||
|
||
public string Replace(string html) | ||
{ | ||
return _replacements.Aggregate(html, (current, replacer) => replacer.Replace(current)); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
namespace Html2Markdown.Replacement; | ||
|
||
internal class CustomReplacer : IReplacer | ||
/// <summary> | ||
/// Allows custom replacement of HTML tags utilising external functions. | ||
/// </summary> | ||
public class CustomReplacer : IReplacer | ||
{ | ||
public string Replace(string html) | ||
{ | ||
return CustomAction.Invoke(html); | ||
} | ||
|
||
public Func<string, string> CustomAction { get; init; } | ||
protected Func<string, string> CustomAction { get; init; } | ||
} |
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,12 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Removes the doctype tag. | ||
/// </summary> | ||
public class DocTypeReplacer : PatternReplacer | ||
{ | ||
public DocTypeReplacer() | ||
{ | ||
Pattern = "<!DOCTYPE[^>]*>"; | ||
Replacement = ""; | ||
} | ||
} |
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,33 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Replaces the HTML emphasis tag with its Markdown equivalent. | ||
/// </summary> | ||
public class EmphasisTagReplacer : CompositeReplacer | ||
{ | ||
public EmphasisTagReplacer() | ||
{ | ||
AddReplacer(new PatternReplacer | ||
{ | ||
Pattern = @"<(?:em|i)>(\s+)", | ||
Replacement = " *" | ||
}); | ||
|
||
AddReplacer(new PatternReplacer | ||
{ | ||
Pattern = "<(?:em|i)>", | ||
Replacement = "*" | ||
}); | ||
|
||
AddReplacer(new PatternReplacer | ||
{ | ||
Pattern = @"(\s+)</(em|i)>", | ||
Replacement = "* " | ||
}); | ||
|
||
AddReplacer(new PatternReplacer | ||
{ | ||
Pattern = "</(em|i)>", | ||
Replacement = "*" | ||
}); | ||
} | ||
} |
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,12 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Removes the doctype tag. | ||
/// </summary> | ||
public class HeadTagReplacer : PatternReplacer | ||
{ | ||
public HeadTagReplacer() | ||
{ | ||
Pattern = "</?head[^>]*>"; | ||
Replacement = ""; | ||
} | ||
} |
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,13 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// HTML Heading Tag Enum | ||
/// </summary> | ||
public enum Heading | ||
{ | ||
H1 = 1, | ||
H2 = 2, | ||
H3 = 3, | ||
H4 = 4, | ||
H5 = 5, | ||
H6 = 6 | ||
} |
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,22 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Replaces the HTML heading tag with its Markdown equivalent. | ||
/// </summary> | ||
public class HeadingTagReplacer : CompositeReplacer | ||
{ | ||
public HeadingTagReplacer(Heading heading) | ||
{ | ||
var headingNumber = (int) heading; | ||
AddReplacer(new PatternReplacer | ||
{ | ||
Pattern = $"</h{headingNumber}>", | ||
Replacement = Environment.NewLine + Environment.NewLine | ||
}); | ||
|
||
AddReplacer(new PatternReplacer | ||
{ | ||
Pattern = $"<h{headingNumber}[^>]*>", | ||
Replacement = Environment.NewLine + Environment.NewLine + new string('#', headingNumber) + " " | ||
}); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Html2Markdown/Replacement/HorizontalRuleTagReplacer.cs
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,12 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Replaces the HTML horizontal rule tag with its Markdown equivalent. | ||
/// </summary> | ||
public class HorizontalRuleTagReplacer : PatternReplacer | ||
{ | ||
public HorizontalRuleTagReplacer() | ||
{ | ||
Pattern = "<hr[^>]*>"; | ||
Replacement = Environment.NewLine + Environment.NewLine + "* * *" + Environment.NewLine; | ||
} | ||
} |
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,12 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Removes the HTML comment tag. | ||
/// </summary> | ||
public class HtmlCommentReplacer : PatternReplacer | ||
{ | ||
public HtmlCommentReplacer() | ||
{ | ||
Pattern = "<!--[^-]+-->"; | ||
Replacement = ""; | ||
} | ||
} |
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,11 @@ | ||
namespace Html2Markdown.Replacement; | ||
/// <summary> | ||
/// Replaces HTML entities with their Markdown equivalent. | ||
/// </summary> | ||
public class HtmlEntitiesReplacer : CustomReplacer | ||
{ | ||
public HtmlEntitiesReplacer() | ||
{ | ||
CustomAction = HtmlParser.ReplaceEntities; | ||
} | ||
} |
Oops, something went wrong.