Skip to content

Commit

Permalink
Merge pull request #426 from baynezy/feature/issue-425-docs
Browse files Browse the repository at this point in the history
feature/issue 425 docs
  • Loading branch information
baynezy authored Aug 2, 2024
2 parents 43f2740 + 2b07e99 commit a3759f7
Show file tree
Hide file tree
Showing 37 changed files with 353 additions and 119 deletions.
6 changes: 6 additions & 0 deletions .idea/.idea.Html2Markdown/.idea/git_toolbox_blame.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Html2Markdown/Html2Markdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
<PackageTags>Markdown;Html;Convert</PackageTags>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>Html2Markdown.snk</AssemblyOriginatorKeyFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.61" />
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" />
</ItemGroup>

</Project>
20 changes: 13 additions & 7 deletions src/Html2Markdown/Replacement/AnchorTagReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
namespace Html2Markdown.Replacement;
/// <summary>
/// Replaces an anchor tag with the link text and the link URL in Markdown format.
/// </summary>
public class AnchorTagReplacer : CustomReplacer
namespace Html2Markdown.Replacement
{
public AnchorTagReplacer()
/// <summary>
/// Replaces an anchor tag with the link text and the link URL in Markdown format.
/// </summary>
public class AnchorTagReplacer : CustomReplacer
{
CustomAction = HtmlParser.ReplaceAnchor;
/// <summary>
/// Initializes a new instance of the <see cref="AnchorTagReplacer"/> class.
/// Sets the custom action to replace anchor tags with Markdown formatted links.
/// </summary>
public AnchorTagReplacer()
{
CustomAction = HtmlParser.ReplaceAnchor;
}
}
}
7 changes: 6 additions & 1 deletion src/Html2Markdown/Replacement/BlockquoteTagReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// Replaces an anchor tag with the link text and the link URL in Markdown format.
/// Replaces a blockquote tag with the appropriate Markdown format.
/// </summary>
public class BlockquoteTagReplacer : CustomReplacer
{
/// <summary>
/// Initializes a new instance of the <see cref="BlockquoteTagReplacer"/> class.
/// Sets the custom action to replace blockquote tags with Markdown formatted blockquotes.
/// </summary>
public BlockquoteTagReplacer()
{
CustomAction = HtmlParser.ReplaceBlockquote;
Expand Down
5 changes: 5 additions & 0 deletions src/Html2Markdown/Replacement/BodyTagReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// Removes the body tag.
/// </summary>
public class BodyTagReplacer : PatternReplacer
{
/// <summary>
/// Initializes a new instance of the <see cref="BodyTagReplacer"/> class.
/// Sets the pattern to match body tags and the replacement to an empty string.
/// </summary>
public BodyTagReplacer()
{
Pattern = "</?body[^>]*>";
Expand Down
5 changes: 5 additions & 0 deletions src/Html2Markdown/Replacement/BreakTagReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// Replaces the HTML break tag with its Markdown equivalent.
/// </summary>
public class BreakTagReplacer : PatternReplacer
{
/// <summary>
/// Initializes a new instance of the <see cref="BreakTagReplacer"/> class.
/// Sets the pattern to match break tags and the replacement to a Markdown line break.
/// </summary>
public BreakTagReplacer()
{
Pattern = "<br[^>]*>";
Expand Down
10 changes: 10 additions & 0 deletions src/Html2Markdown/Replacement/CodeTagReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// Replaces the HTML code tag with its Markdown equivalent.
/// </summary>
public class CodeTagReplacer : CustomReplacer
{
/// <summary>
/// Initializes a new instance of the <see cref="CodeTagReplacer"/> class.
/// Sets the custom action to replace code tags with Markdown formatted code blocks.
/// </summary>
public CodeTagReplacer()
{
CustomAction = html => HtmlParser.ReplaceCode(html, false);
}

/// <summary>
/// Initializes a new instance of the <see cref="CodeTagReplacer"/> 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.
/// </summary>
/// <param name="supportSyntaxHighlighting">If set to <c>true</c>, supports syntax highlighting in the Markdown output.</param>
public CodeTagReplacer(bool supportSyntaxHighlighting)
{
CustomAction = html => HtmlParser.ReplaceCode(html, supportSyntaxHighlighting);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
namespace Html2Markdown.Replacement.CommonMark;

/// <summary>
/// A group of IReplacer to deal with converting HTML that is
/// used for layout
/// A group of <see cref="IReplacer"/> to deal with converting HTML that is
/// used for layout.
/// </summary>
public class CommonMarkLayoutReplacementGroup : IReplacementGroup
{
private readonly IList<IReplacer> _replacements = new List<IReplacer> {
new HorizontalRuleTagReplacer(),
new CodeTagReplacer(true),
new PreTagReplacer(),
new ParagraphTagReplacer(),
new BreakTagReplacer(),
new BlockquoteTagReplacer()
};
private readonly IList<IReplacer> _replacements = new List<IReplacer> {
new HorizontalRuleTagReplacer(),
new CodeTagReplacer(true),
new PreTagReplacer(),
new ParagraphTagReplacer(),
new BreakTagReplacer(),
new BlockquoteTagReplacer()
};

public IEnumerable<IReplacer> Replacers()
{
return _replacements;
}
/// <summary>
/// Returns the list of <see cref="IReplacer"/> instances.
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="IReplacer"/>.</returns>
public IEnumerable<IReplacer> Replacers()
{
return _replacements;
}
}
14 changes: 12 additions & 2 deletions src/Html2Markdown/Replacement/CompositeReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
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>();


/// <summary>
/// Adds a replacer to the list of replacements.
/// </summary>
/// <param name="replacer">The <see cref="IReplacer"/> instance to add.</param>
protected void AddReplacer(IReplacer replacer)
{
_replacements.Add(replacer);
}


/// <summary>
/// Applies all replacements to the given HTML string.
/// </summary>
/// <param name="html">The HTML string to process.</param>
/// <returns>The processed HTML string with all replacements applied.</returns>
public string Replace(string html)
{
return _replacements.Aggregate(html, (current, replacer) => replacer.Replace(current));
Expand Down
24 changes: 18 additions & 6 deletions src/Html2Markdown/Replacement/CustomReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using JetBrains.Annotations;

namespace Html2Markdown.Replacement;

/// <summary>
/// Allows custom replacement of HTML tags utilising external functions.
/// Allows custom replacement of HTML tags utilizing external functions.
/// </summary>
public class CustomReplacer : IReplacer
{
public string Replace(string html)
{
return CustomAction.Invoke(html);
}
/// <summary>
/// Replaces the HTML string using the custom action.
/// </summary>
/// <param name="html">The HTML string to process.</param>
/// <returns>The processed HTML string with the custom replacement applied.</returns>
public string Replace(string html)
{
return CustomAction.Invoke(html);
}

public Func<string, string> CustomAction { get; init; }
/// <summary>
/// Gets or sets the custom action to be used for replacing HTML tags.
/// </summary>
[PublicAPI]
public Func<string, string> CustomAction { get; init; }
}
5 changes: 5 additions & 0 deletions src/Html2Markdown/Replacement/DocTypeReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// Removes the doctype tag.
/// </summary>
public class DocTypeReplacer : PatternReplacer
{
/// <summary>
/// Initializes a new instance of the <see cref="DocTypeReplacer"/> class.
/// Sets the pattern to match doctype tags and the replacement to an empty string.
/// </summary>
public DocTypeReplacer()
{
Pattern = "<!DOCTYPE[^>]*>";
Expand Down
11 changes: 8 additions & 3 deletions src/Html2Markdown/Replacement/EmphasisTagReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// Replaces the HTML emphasis tag with its Markdown equivalent.
/// </summary>
public class EmphasisTagReplacer : CompositeReplacer
{
/// <summary>
/// Initializes a new instance of the <see cref="EmphasisTagReplacer"/> class.
/// Sets up the patterns and replacements for converting HTML emphasis tags to Markdown.
/// </summary>
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)>",
Expand Down
20 changes: 12 additions & 8 deletions src/Html2Markdown/Replacement/EntitiesReplacementGroup.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// A group of IReplacer to deal with converting HTML entities
/// A group of <see cref="IReplacer"/> to deal with converting HTML entities.
/// </summary>
public class EntitiesReplacementGroup : IReplacementGroup
{
private readonly IList<IReplacer> _replacements = new List<IReplacer> {
new HtmlEntitiesReplacer()
};
private readonly IList<IReplacer> _replacements = new List<IReplacer> {
new HtmlEntitiesReplacer()
};

public IEnumerable<IReplacer> Replacers()
{
return _replacements;
}
/// <summary>
/// Returns the list of <see cref="IReplacer"/> instances.
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="IReplacer"/>.</returns>
public IEnumerable<IReplacer> Replacers()
{
return _replacements;
}
}
5 changes: 5 additions & 0 deletions src/Html2Markdown/Replacement/HeadTagReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// Removes the doctype tag.
/// </summary>
public class HeadTagReplacer : PatternReplacer
{
/// <summary>
/// Initializes a new instance of the <see cref="HeadTagReplacer"/> class.
/// Sets up the pattern and replacement for removing HTML head tags.
/// </summary>
public HeadTagReplacer()
{
Pattern = "</?head[^>]*>";
Expand Down
24 changes: 24 additions & 0 deletions src/Html2Markdown/Replacement/Heading.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// HTML Heading Tag Enum
/// </summary>
public enum Heading
{
/// <summary>
/// Represents an HTML H1 tag.
/// </summary>
H1 = 1,

/// <summary>
/// Represents an HTML H2 tag.
/// </summary>
H2 = 2,

/// <summary>
/// Represents an HTML H3 tag.
/// </summary>
H3 = 3,

/// <summary>
/// Represents an HTML H4 tag.
/// </summary>
H4 = 4,

/// <summary>
/// Represents an HTML H5 tag.
/// </summary>
H5 = 5,

/// <summary>
/// Represents an HTML H6 tag.
/// </summary>
H6 = 6
}
30 changes: 17 additions & 13 deletions src/Html2Markdown/Replacement/HeadingReplacementGroup.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
namespace Html2Markdown.Replacement;

/// <summary>
/// A group of IReplacer to deal with converting HTML headers
/// A group of <see cref="IReplacer"/> to deal with converting HTML headers.
/// </summary>
public class HeadingReplacementGroup : IReplacementGroup
{
private readonly IList<IReplacer> _replacements = new List<IReplacer> {
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<IReplacer> _replacements = new List<IReplacer> {
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<IReplacer> Replacers()
{
return _replacements;
}
/// <summary>
/// Returns the list of <see cref="IReplacer"/> instances.
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="IReplacer"/>.</returns>
public IEnumerable<IReplacer> Replacers()
{
return _replacements;
}
}
Loading

0 comments on commit a3759f7

Please sign in to comment.