diff --git a/.idea/.idea.Html2Markdown/.idea/git_toolbox_blame.xml b/.idea/.idea.Html2Markdown/.idea/git_toolbox_blame.xml new file mode 100644 index 00000000..7dc12496 --- /dev/null +++ b/.idea/.idea.Html2Markdown/.idea/git_toolbox_blame.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/Html2Markdown/Html2Markdown.csproj b/src/Html2Markdown/Html2Markdown.csproj index aafe5fdc..6d43ba51 100644 --- a/src/Html2Markdown/Html2Markdown.csproj +++ b/src/Html2Markdown/Html2Markdown.csproj @@ -10,10 +10,12 @@ Markdown;Html;Convert True Html2Markdown.snk + true + diff --git a/src/Html2Markdown/Replacement/AnchorTagReplacer.cs b/src/Html2Markdown/Replacement/AnchorTagReplacer.cs index 772bec6c..8a1b0940 100644 --- a/src/Html2Markdown/Replacement/AnchorTagReplacer.cs +++ b/src/Html2Markdown/Replacement/AnchorTagReplacer.cs @@ -1,11 +1,17 @@ -namespace Html2Markdown.Replacement; -/// -/// Replaces an anchor tag with the link text and the link URL in Markdown format. -/// -public class AnchorTagReplacer : CustomReplacer +namespace Html2Markdown.Replacement { - public AnchorTagReplacer() + /// + /// Replaces an anchor tag with the link text and the link URL in Markdown format. + /// + public class AnchorTagReplacer : CustomReplacer { - CustomAction = HtmlParser.ReplaceAnchor; + /// + /// Initializes a new instance of the class. + /// Sets the custom action to replace anchor tags with Markdown formatted links. + /// + public AnchorTagReplacer() + { + CustomAction = HtmlParser.ReplaceAnchor; + } } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/BlockquoteTagReplacer.cs b/src/Html2Markdown/Replacement/BlockquoteTagReplacer.cs index 93ee0ac2..edd37df4 100644 --- a/src/Html2Markdown/Replacement/BlockquoteTagReplacer.cs +++ b/src/Html2Markdown/Replacement/BlockquoteTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// -/// Replaces an anchor tag with the link text and the link URL in Markdown format. +/// Replaces a blockquote tag with the appropriate Markdown format. /// public class BlockquoteTagReplacer : CustomReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the custom action to replace blockquote tags with Markdown formatted blockquotes. + /// public BlockquoteTagReplacer() { CustomAction = HtmlParser.ReplaceBlockquote; diff --git a/src/Html2Markdown/Replacement/BodyTagReplacer.cs b/src/Html2Markdown/Replacement/BodyTagReplacer.cs index 1b3e9d03..2b0776e4 100644 --- a/src/Html2Markdown/Replacement/BodyTagReplacer.cs +++ b/src/Html2Markdown/Replacement/BodyTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the body tag. /// public class BodyTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the pattern to match body tags and the replacement to an empty string. + /// public BodyTagReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/BreakTagReplacer.cs b/src/Html2Markdown/Replacement/BreakTagReplacer.cs index f65b5eb9..86105548 100644 --- a/src/Html2Markdown/Replacement/BreakTagReplacer.cs +++ b/src/Html2Markdown/Replacement/BreakTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replaces the HTML break tag with its Markdown equivalent. /// public class BreakTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the pattern to match break tags and the replacement to a Markdown line break. + /// public BreakTagReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/CodeTagReplacer.cs b/src/Html2Markdown/Replacement/CodeTagReplacer.cs index bad7e74f..611aa19a 100644 --- a/src/Html2Markdown/Replacement/CodeTagReplacer.cs +++ b/src/Html2Markdown/Replacement/CodeTagReplacer.cs @@ -1,14 +1,24 @@ namespace Html2Markdown.Replacement; + /// /// Replaces the HTML code tag with its Markdown equivalent. /// public class CodeTagReplacer : CustomReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the custom action to replace code tags with Markdown formatted code blocks. + /// public CodeTagReplacer() { CustomAction = html => HtmlParser.ReplaceCode(html, false); } + /// + /// Initializes a new instance of the class with an option to support syntax highlighting. + /// Sets the custom action to replace code tags with Markdown formatted code blocks, optionally supporting syntax highlighting. + /// + /// If set to true, supports syntax highlighting in the Markdown output. public CodeTagReplacer(bool supportSyntaxHighlighting) { CustomAction = html => HtmlParser.ReplaceCode(html, supportSyntaxHighlighting); diff --git a/src/Html2Markdown/Replacement/CommonMark/CommonMarkLayoutReplacementGroup.cs b/src/Html2Markdown/Replacement/CommonMark/CommonMarkLayoutReplacementGroup.cs index c0d33452..fa67b4f5 100644 --- a/src/Html2Markdown/Replacement/CommonMark/CommonMarkLayoutReplacementGroup.cs +++ b/src/Html2Markdown/Replacement/CommonMark/CommonMarkLayoutReplacementGroup.cs @@ -1,22 +1,26 @@ namespace Html2Markdown.Replacement.CommonMark; /// -/// A group of IReplacer to deal with converting HTML that is -/// used for layout +/// A group of to deal with converting HTML that is +/// used for layout. /// public class CommonMarkLayoutReplacementGroup : IReplacementGroup { - private readonly IList _replacements = new List { - new HorizontalRuleTagReplacer(), - new CodeTagReplacer(true), - new PreTagReplacer(), - new ParagraphTagReplacer(), - new BreakTagReplacer(), - new BlockquoteTagReplacer() - }; + private readonly IList _replacements = new List { + new HorizontalRuleTagReplacer(), + new CodeTagReplacer(true), + new PreTagReplacer(), + new ParagraphTagReplacer(), + new BreakTagReplacer(), + new BlockquoteTagReplacer() + }; - public IEnumerable Replacers() - { - return _replacements; - } + /// + /// Returns the list of instances. + /// + /// An of . + public IEnumerable Replacers() + { + return _replacements; + } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/CompositeReplacer.cs b/src/Html2Markdown/Replacement/CompositeReplacer.cs index 8f8ae44e..59674a57 100644 --- a/src/Html2Markdown/Replacement/CompositeReplacer.cs +++ b/src/Html2Markdown/Replacement/CompositeReplacer.cs @@ -1,16 +1,26 @@ namespace Html2Markdown.Replacement; + /// /// Allows for multiple replacements to be applied to the HTML. /// public abstract class CompositeReplacer : IReplacer { private readonly IList _replacements = new List(); - + + /// + /// Adds a replacer to the list of replacements. + /// + /// The instance to add. protected void AddReplacer(IReplacer replacer) { _replacements.Add(replacer); } - + + /// + /// Applies all replacements to the given HTML string. + /// + /// The HTML string to process. + /// The processed HTML string with all replacements applied. public string Replace(string html) { return _replacements.Aggregate(html, (current, replacer) => replacer.Replace(current)); diff --git a/src/Html2Markdown/Replacement/CustomReplacer.cs b/src/Html2Markdown/Replacement/CustomReplacer.cs index a01fc0b0..923ecfe3 100644 --- a/src/Html2Markdown/Replacement/CustomReplacer.cs +++ b/src/Html2Markdown/Replacement/CustomReplacer.cs @@ -1,13 +1,25 @@ +using JetBrains.Annotations; + namespace Html2Markdown.Replacement; + /// -/// Allows custom replacement of HTML tags utilising external functions. +/// Allows custom replacement of HTML tags utilizing external functions. /// public class CustomReplacer : IReplacer { - public string Replace(string html) - { - return CustomAction.Invoke(html); - } + /// + /// Replaces the HTML string using the custom action. + /// + /// The HTML string to process. + /// The processed HTML string with the custom replacement applied. + public string Replace(string html) + { + return CustomAction.Invoke(html); + } - public Func CustomAction { get; init; } + /// + /// Gets or sets the custom action to be used for replacing HTML tags. + /// + [PublicAPI] + public Func CustomAction { get; init; } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/DocTypeReplacer.cs b/src/Html2Markdown/Replacement/DocTypeReplacer.cs index 6916baac..5417fec0 100644 --- a/src/Html2Markdown/Replacement/DocTypeReplacer.cs +++ b/src/Html2Markdown/Replacement/DocTypeReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the doctype tag. /// public class DocTypeReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the pattern to match doctype tags and the replacement to an empty string. + /// public DocTypeReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/EmphasisTagReplacer.cs b/src/Html2Markdown/Replacement/EmphasisTagReplacer.cs index 8dd5e4dc..6c2bf355 100644 --- a/src/Html2Markdown/Replacement/EmphasisTagReplacer.cs +++ b/src/Html2Markdown/Replacement/EmphasisTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replaces the HTML emphasis tag with its Markdown equivalent. /// public class EmphasisTagReplacer : CompositeReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the patterns and replacements for converting HTML emphasis tags to Markdown. + /// public EmphasisTagReplacer() { AddReplacer(new PatternReplacer @@ -11,19 +16,19 @@ public EmphasisTagReplacer() Pattern = @"<(?:em|i)>(\s+)", Replacement = " *" }); - + AddReplacer(new PatternReplacer { Pattern = "<(?:em|i)>", Replacement = "*" }); - + AddReplacer(new PatternReplacer { Pattern = @"(\s+)", Replacement = "* " }); - + AddReplacer(new PatternReplacer { Pattern = "", diff --git a/src/Html2Markdown/Replacement/EntitiesReplacementGroup.cs b/src/Html2Markdown/Replacement/EntitiesReplacementGroup.cs index fd5448d2..1689ebda 100644 --- a/src/Html2Markdown/Replacement/EntitiesReplacementGroup.cs +++ b/src/Html2Markdown/Replacement/EntitiesReplacementGroup.cs @@ -1,16 +1,20 @@ namespace Html2Markdown.Replacement; /// -/// A group of IReplacer to deal with converting HTML entities +/// A group of to deal with converting HTML entities. /// public class EntitiesReplacementGroup : IReplacementGroup { - private readonly IList _replacements = new List { - new HtmlEntitiesReplacer() - }; + private readonly IList _replacements = new List { + new HtmlEntitiesReplacer() + }; - public IEnumerable Replacers() - { - return _replacements; - } + /// + /// Returns the list of instances. + /// + /// An of . + public IEnumerable Replacers() + { + return _replacements; + } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/HeadTagReplacer.cs b/src/Html2Markdown/Replacement/HeadTagReplacer.cs index 1a143842..71616dc5 100644 --- a/src/Html2Markdown/Replacement/HeadTagReplacer.cs +++ b/src/Html2Markdown/Replacement/HeadTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the doctype tag. /// public class HeadTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the pattern and replacement for removing HTML head tags. + /// public HeadTagReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/Heading.cs b/src/Html2Markdown/Replacement/Heading.cs index 693f3d78..abff536e 100644 --- a/src/Html2Markdown/Replacement/Heading.cs +++ b/src/Html2Markdown/Replacement/Heading.cs @@ -1,13 +1,37 @@ namespace Html2Markdown.Replacement; + /// /// HTML Heading Tag Enum /// public enum Heading { + /// + /// Represents an HTML H1 tag. + /// H1 = 1, + + /// + /// Represents an HTML H2 tag. + /// H2 = 2, + + /// + /// Represents an HTML H3 tag. + /// H3 = 3, + + /// + /// Represents an HTML H4 tag. + /// H4 = 4, + + /// + /// Represents an HTML H5 tag. + /// H5 = 5, + + /// + /// Represents an HTML H6 tag. + /// H6 = 6 } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/HeadingReplacementGroup.cs b/src/Html2Markdown/Replacement/HeadingReplacementGroup.cs index 762768fc..d13f50e3 100644 --- a/src/Html2Markdown/Replacement/HeadingReplacementGroup.cs +++ b/src/Html2Markdown/Replacement/HeadingReplacementGroup.cs @@ -1,21 +1,25 @@ namespace Html2Markdown.Replacement; /// -/// A group of IReplacer to deal with converting HTML headers +/// A group of to deal with converting HTML headers. /// public class HeadingReplacementGroup : IReplacementGroup { - private readonly IList _replacements = new List { - new HeadingTagReplacer(Heading.H1), - new HeadingTagReplacer(Heading.H2), - new HeadingTagReplacer(Heading.H3), - new HeadingTagReplacer(Heading.H4), - new HeadingTagReplacer(Heading.H5), - new HeadingTagReplacer(Heading.H6) - }; + private readonly IList _replacements = new List { + new HeadingTagReplacer(Heading.H1), + new HeadingTagReplacer(Heading.H2), + new HeadingTagReplacer(Heading.H3), + new HeadingTagReplacer(Heading.H4), + new HeadingTagReplacer(Heading.H5), + new HeadingTagReplacer(Heading.H6) + }; - public IEnumerable Replacers() - { - return _replacements; - } + /// + /// Returns the list of instances. + /// + /// An of . + public IEnumerable Replacers() + { + return _replacements; + } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/HeadingTagReplacer.cs b/src/Html2Markdown/Replacement/HeadingTagReplacer.cs index 0ec1d5d8..0c5213f3 100644 --- a/src/Html2Markdown/Replacement/HeadingTagReplacer.cs +++ b/src/Html2Markdown/Replacement/HeadingTagReplacer.cs @@ -1,18 +1,25 @@ namespace Html2Markdown.Replacement; + /// /// Replaces the HTML heading tag with its Markdown equivalent. /// public class HeadingTagReplacer : CompositeReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the patterns and replacements for converting HTML heading tags to Markdown. + /// + /// The heading level to be replaced (e.g., H1, H2). public HeadingTagReplacer(Heading heading) { var headingNumber = (int) heading; + AddReplacer(new PatternReplacer { Pattern = $"", Replacement = Environment.NewLine + Environment.NewLine }); - + AddReplacer(new PatternReplacer { Pattern = $"]*>", diff --git a/src/Html2Markdown/Replacement/HorizontalRuleTagReplacer.cs b/src/Html2Markdown/Replacement/HorizontalRuleTagReplacer.cs index e013b1a2..59152fda 100644 --- a/src/Html2Markdown/Replacement/HorizontalRuleTagReplacer.cs +++ b/src/Html2Markdown/Replacement/HorizontalRuleTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replaces the HTML horizontal rule tag with its Markdown equivalent. /// public class HorizontalRuleTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the pattern and replacement for converting HTML horizontal rule tags to Markdown. + /// public HorizontalRuleTagReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/HtmlCommentReplacer.cs b/src/Html2Markdown/Replacement/HtmlCommentReplacer.cs index 0a8d0b64..2ba656dd 100644 --- a/src/Html2Markdown/Replacement/HtmlCommentReplacer.cs +++ b/src/Html2Markdown/Replacement/HtmlCommentReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the HTML comment tag. /// public class HtmlCommentReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the pattern and replacement for removing HTML comment tags. + /// public HtmlCommentReplacer() { Pattern = ""; diff --git a/src/Html2Markdown/Replacement/HtmlEntitiesReplacer.cs b/src/Html2Markdown/Replacement/HtmlEntitiesReplacer.cs index 0bc896cc..c3b38f4e 100644 --- a/src/Html2Markdown/Replacement/HtmlEntitiesReplacer.cs +++ b/src/Html2Markdown/Replacement/HtmlEntitiesReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replaces HTML entities with their Markdown equivalent. /// public class HtmlEntitiesReplacer : CustomReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the custom action to replace HTML entities. + /// public HtmlEntitiesReplacer() { CustomAction = HtmlParser.ReplaceEntities; diff --git a/src/Html2Markdown/Replacement/HtmlListReplacer.cs b/src/Html2Markdown/Replacement/HtmlListReplacer.cs index 3f99b623..8ef5a7cb 100644 --- a/src/Html2Markdown/Replacement/HtmlListReplacer.cs +++ b/src/Html2Markdown/Replacement/HtmlListReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replaces HTML lists with their Markdown equivalent. /// public class HtmlListReplacer : CustomReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the custom action to replace HTML lists. + /// public HtmlListReplacer() { CustomAction = HtmlParser.ReplaceLists; diff --git a/src/Html2Markdown/Replacement/HtmlTagReplacer.cs b/src/Html2Markdown/Replacement/HtmlTagReplacer.cs index d6808b09..074134f1 100644 --- a/src/Html2Markdown/Replacement/HtmlTagReplacer.cs +++ b/src/Html2Markdown/Replacement/HtmlTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the HTML tag. /// public class HtmlTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the pattern and replacement for removing HTML tags. + /// public HtmlTagReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/IllegalHtmlReplacementGroup.cs b/src/Html2Markdown/Replacement/IllegalHtmlReplacementGroup.cs index e85fef42..73c826a0 100644 --- a/src/Html2Markdown/Replacement/IllegalHtmlReplacementGroup.cs +++ b/src/Html2Markdown/Replacement/IllegalHtmlReplacementGroup.cs @@ -1,24 +1,28 @@ namespace Html2Markdown.Replacement; /// -/// A group of IReplacer to deal with removing illegal HTML +/// A group of IReplacer to deal with removing illegal HTML. /// public class IllegalHtmlReplacementGroup : IReplacementGroup { - private readonly IList _replacements = new List { - new DocTypeReplacer(), - new HtmlTagReplacer(), - new HeadTagReplacer(), - new BodyTagReplacer(), - new TitleTagReplacer(), - new MetaTagReplacer(), - new LinkTagReplacer(), - new HtmlCommentReplacer(), - new ScriptTagReplacer() - }; + private readonly IList _replacements = new List { + new DocTypeReplacer(), + new HtmlTagReplacer(), + new HeadTagReplacer(), + new BodyTagReplacer(), + new TitleTagReplacer(), + new MetaTagReplacer(), + new LinkTagReplacer(), + new HtmlCommentReplacer(), + new ScriptTagReplacer() + }; - public IEnumerable Replacers() - { - return _replacements; - } + /// + /// Returns the list of IReplacer instances. + /// + /// An IEnumerable of IReplacer. + public IEnumerable Replacers() + { + return _replacements; + } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/ImageTagReplacer.cs b/src/Html2Markdown/Replacement/ImageTagReplacer.cs index 160f5220..b66d2ba8 100644 --- a/src/Html2Markdown/Replacement/ImageTagReplacer.cs +++ b/src/Html2Markdown/Replacement/ImageTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replaces HTML image tags with their Markdown equivalent. /// public class ImageTagReplacer : CustomReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the custom action to replace HTML image tags. + /// public ImageTagReplacer() { CustomAction = HtmlParser.ReplaceImg; diff --git a/src/Html2Markdown/Replacement/LayoutReplacementGroup.cs b/src/Html2Markdown/Replacement/LayoutReplacementGroup.cs index 3adf06d8..4a7ab988 100644 --- a/src/Html2Markdown/Replacement/LayoutReplacementGroup.cs +++ b/src/Html2Markdown/Replacement/LayoutReplacementGroup.cs @@ -2,21 +2,25 @@ namespace Html2Markdown.Replacement; /// /// A group of IReplacer to deal with converting HTML that is -/// used for layout +/// used for layout. /// public class LayoutReplacementGroup : IReplacementGroup { - private readonly IList _replacements = new List { - new HorizontalRuleTagReplacer(), - new CodeTagReplacer(), - new PreTagReplacer(), - new ParagraphTagReplacer(), - new BreakTagReplacer(), - new BlockquoteTagReplacer() - }; + private readonly IList _replacements = new List { + new HorizontalRuleTagReplacer(), + new CodeTagReplacer(), + new PreTagReplacer(), + new ParagraphTagReplacer(), + new BreakTagReplacer(), + new BlockquoteTagReplacer() + }; - public IEnumerable Replacers() - { - return _replacements; - } + /// + /// Returns the list of IReplacer instances. + /// + /// An IEnumerable of IReplacer. + public IEnumerable Replacers() + { + return _replacements; + } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/LinkTagReplacer.cs b/src/Html2Markdown/Replacement/LinkTagReplacer.cs index 12ffa9b4..19aa5989 100644 --- a/src/Html2Markdown/Replacement/LinkTagReplacer.cs +++ b/src/Html2Markdown/Replacement/LinkTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the link tag. /// public class LinkTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the pattern and replacement for removing link tags. + /// public LinkTagReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/MetaTagReplacer.cs b/src/Html2Markdown/Replacement/MetaTagReplacer.cs index a5b49879..00223782 100644 --- a/src/Html2Markdown/Replacement/MetaTagReplacer.cs +++ b/src/Html2Markdown/Replacement/MetaTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the meta tag. /// public class MetaTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the pattern and replacement for removing meta tags. + /// public MetaTagReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/ParagraphTagReplacer.cs b/src/Html2Markdown/Replacement/ParagraphTagReplacer.cs index 4facf8ef..21fc6513 100644 --- a/src/Html2Markdown/Replacement/ParagraphTagReplacer.cs +++ b/src/Html2Markdown/Replacement/ParagraphTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replace the paragraph tag with its Markdown equivalent. /// public class ParagraphTagReplacer : CustomReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the custom action to replace HTML paragraph tags. + /// public ParagraphTagReplacer() { CustomAction = HtmlParser.ReplaceParagraph; diff --git a/src/Html2Markdown/Replacement/PatternReplacer.cs b/src/Html2Markdown/Replacement/PatternReplacer.cs index e414b3c5..96af9b06 100644 --- a/src/Html2Markdown/Replacement/PatternReplacer.cs +++ b/src/Html2Markdown/Replacement/PatternReplacer.cs @@ -1,19 +1,32 @@ using System.Text.RegularExpressions; namespace Html2Markdown.Replacement; + /// /// Allows replacement with a regular expression. /// public class PatternReplacer : IReplacer { - public string Pattern { get; init; } + /// + /// Gets the pattern to match in the HTML. + /// + public string Pattern { get; init; } + + /// + /// Gets the replacement string for the matched pattern. + /// + public string Replacement { get; init; } - public string Replacement { get; init; } - public string Replace(string html) - { - // SECURITY: https://sonarcloud.io/organizations/baynezy/rules?open=csharpsquid%3AS6444&rule_key=csharpsquid%3AS6444 - var regex = new Regex(Pattern, RegexOptions.None, TimeSpan.FromSeconds(1)); + /// + /// Replaces occurrences of the pattern in the provided HTML with the replacement string. + /// + /// The HTML content to process. + /// The processed HTML with replacements. + public string Replace(string html) + { + // SECURITY: https://sonarcloud.io/organizations/baynezy/rules?open=csharpsquid%3AS6444&rule_key=csharpsquid%3AS6444 + var regex = new Regex(Pattern, RegexOptions.None, TimeSpan.FromSeconds(1)); - return regex.Replace(html, Replacement); - } + return regex.Replace(html, Replacement); + } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/PreTagReplacer.cs b/src/Html2Markdown/Replacement/PreTagReplacer.cs index 444d2bca..da98a9fe 100644 --- a/src/Html2Markdown/Replacement/PreTagReplacer.cs +++ b/src/Html2Markdown/Replacement/PreTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replaces the pre tag with a Markdown equivalent. /// public class PreTagReplacer : CustomReplacer { + /// + /// Initializes a new instance of the class. + /// Sets the custom action to replace HTML pre tags. + /// public PreTagReplacer() { CustomAction = HtmlParser.ReplacePre; diff --git a/src/Html2Markdown/Replacement/ScriptTagReplacer.cs b/src/Html2Markdown/Replacement/ScriptTagReplacer.cs index 498a2aee..9b154df7 100644 --- a/src/Html2Markdown/Replacement/ScriptTagReplacer.cs +++ b/src/Html2Markdown/Replacement/ScriptTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the script tag. /// public class ScriptTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the pattern and replacement for removing script tags. + /// public ScriptTagReplacer() { Pattern = "]*>"; diff --git a/src/Html2Markdown/Replacement/StrongTagReplacer.cs b/src/Html2Markdown/Replacement/StrongTagReplacer.cs index 316bc27f..058e2087 100644 --- a/src/Html2Markdown/Replacement/StrongTagReplacer.cs +++ b/src/Html2Markdown/Replacement/StrongTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Replace the strong tag with its Markdown equivalent. /// public class StrongTagReplacer : CompositeReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the replacers for converting strong and b tags to Markdown. + /// public StrongTagReplacer() { AddReplacer(new PatternReplacer @@ -11,19 +16,19 @@ public StrongTagReplacer() Pattern = @"<(?:strong|b)>(\s+)", Replacement = " **" }); - + AddReplacer(new PatternReplacer { Pattern = @"<(?:strong|b)(:?\s[^>]*)?>", Replacement = "**" }); - + AddReplacer(new PatternReplacer { Pattern = @"(\s+)", Replacement = "** " }); - + AddReplacer(new PatternReplacer { Pattern = "", diff --git a/src/Html2Markdown/Replacement/TextFormattingReplacementGroup.cs b/src/Html2Markdown/Replacement/TextFormattingReplacementGroup.cs index 4b801583..0bb182b1 100644 --- a/src/Html2Markdown/Replacement/TextFormattingReplacementGroup.cs +++ b/src/Html2Markdown/Replacement/TextFormattingReplacementGroup.cs @@ -2,20 +2,24 @@ namespace Html2Markdown.Replacement; /// /// A group of IReplacer to deal with converting HTML for -/// formatting text +/// formatting text. /// public class TextFormattingReplacementGroup : IReplacementGroup { - private readonly IList _replacements = new List { - new StrongTagReplacer(), - new EmphasisTagReplacer(), - new ImageTagReplacer(), - new HtmlListReplacer(), - new AnchorTagReplacer() - }; + private readonly IList _replacements = new List { + new StrongTagReplacer(), + new EmphasisTagReplacer(), + new ImageTagReplacer(), + new HtmlListReplacer(), + new AnchorTagReplacer() + }; - public IEnumerable Replacers() - { - return _replacements; - } + /// + /// Gets the collection of IReplacer instances. + /// + /// An IEnumerable of IReplacer instances. + public IEnumerable Replacers() + { + return _replacements; + } } \ No newline at end of file diff --git a/src/Html2Markdown/Replacement/TitleTagReplacer.cs b/src/Html2Markdown/Replacement/TitleTagReplacer.cs index e22845dc..511ccb9b 100644 --- a/src/Html2Markdown/Replacement/TitleTagReplacer.cs +++ b/src/Html2Markdown/Replacement/TitleTagReplacer.cs @@ -1,9 +1,14 @@ namespace Html2Markdown.Replacement; + /// /// Removes the title tag. /// public class TitleTagReplacer : PatternReplacer { + /// + /// Initializes a new instance of the class. + /// Sets up the pattern and replacement for removing title tags. + /// public TitleTagReplacer() { Pattern = "]*>.*?"; diff --git a/src/Html2Markdown/Scheme/AbstractScheme.cs b/src/Html2Markdown/Scheme/AbstractScheme.cs index 187f09b7..5e2cb620 100644 --- a/src/Html2Markdown/Scheme/AbstractScheme.cs +++ b/src/Html2Markdown/Scheme/AbstractScheme.cs @@ -3,18 +3,31 @@ namespace Html2Markdown.Scheme; /// -/// A group of IReplacer to deal with converting HTML entities +/// A group of IReplacer to deal with converting HTML entities. /// public abstract class AbstractScheme : IScheme { - protected readonly IList ReplacerCollection = new List(); + + /// + /// The collection of IReplacer instances used for replacements. + /// + protected readonly IList ReplacerCollection = new List(); - protected static void AddReplacementGroup(IList replacers, IReplacementGroup replacementGroup) - { - replacementGroup.Replacers().ToList().ForEach(replacers.Add); - } + /// + /// Adds a group of replacements to the provided list of replacers. + /// + /// The list of replacers to add to. + /// The group of replacements to add. + protected static void AddReplacementGroup(IList replacers, IReplacementGroup replacementGroup) + { + replacementGroup.Replacers().ToList().ForEach(replacers.Add); + } - public IList Replacers() - { - return ReplacerCollection; - } + /// + /// Gets the collection of IReplacer instances. + /// + /// The list of IReplacer instances. + public IList Replacers() + { + return ReplacerCollection; + } } \ No newline at end of file diff --git a/src/Html2Markdown/Scheme/CommonMark.cs b/src/Html2Markdown/Scheme/CommonMark.cs index 8e0cc6cc..1db7c7e4 100644 --- a/src/Html2Markdown/Scheme/CommonMark.cs +++ b/src/Html2Markdown/Scheme/CommonMark.cs @@ -6,18 +6,26 @@ namespace Html2Markdown.Scheme; /// /// Collection of IReplacer for converting CommonMark Markdown /// https://commonmark.org/ -/// -/// Currently supports : +/// +/// Currently supports : /// * Syntax Highlighting /// public class CommonMark : AbstractScheme { + /// + /// Initializes a new instance of the class. + /// Sets up the replacement groups for converting CommonMark Markdown. + /// public CommonMark() { AddReplacementGroup(ReplacerCollection, new TextFormattingReplacementGroup()); + AddReplacementGroup(ReplacerCollection, new HeadingReplacementGroup()); + AddReplacementGroup(ReplacerCollection, new IllegalHtmlReplacementGroup()); + AddReplacementGroup(ReplacerCollection, new CommonMarkLayoutReplacementGroup()); + AddReplacementGroup(ReplacerCollection, new EntitiesReplacementGroup()); } } \ No newline at end of file diff --git a/src/Html2Markdown/Scheme/Markdown.cs b/src/Html2Markdown/Scheme/Markdown.cs index 55fe2160..f9bc1676 100644 --- a/src/Html2Markdown/Scheme/Markdown.cs +++ b/src/Html2Markdown/Scheme/Markdown.cs @@ -8,6 +8,10 @@ namespace Html2Markdown.Scheme; /// public class Markdown : AbstractScheme { + /// + /// Initializes a new instance of the class. + /// Sets up the replacement groups for converting vanilla Markdown. + /// public Markdown() { AddReplacementGroup(ReplacerCollection, new TextFormattingReplacementGroup());