Skip to content

Commit

Permalink
Refactoring and handling to cleanup unnecessary spaces using CleanupU…
Browse files Browse the repository at this point in the history
…nnecessarySpaces
  • Loading branch information
mysticmind committed Jun 19, 2024
1 parent 2e3d52f commit dfa306a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ If you have used and benefitted from this library. Please feel free to buy me a
Install the package from NuGet using `Install-Package ReverseMarkdown` or clone the repository and built it yourself.

<!-- snippet: Usage -->
<a id='snippet-usage'></a>
<a id='snippet-Usage'></a>
```cs
var converter = new ReverseMarkdown.Converter();

string html = "This a sample <strong>paragraph</strong> from <a href=\"http://test.com\">my site</a>";

string result = converter.Convert(html);
```
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-usage' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-Usage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in:
Expand All @@ -43,7 +43,7 @@ This a sample **paragraph** from [my site](http://test.com)
The conversion can be customized:

<!-- snippet: UsageWithConfig -->
<a id='snippet-usagewithconfig'></a>
<a id='snippet-UsageWithConfig'></a>
```cs
var config = new ReverseMarkdown.Config
{
Expand All @@ -59,7 +59,7 @@ var config = new ReverseMarkdown.Config

var converter = new ReverseMarkdown.Converter(config);
```
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L27-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-usagewithconfig' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L27-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-UsageWithConfig' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Configuration options
Expand Down
8 changes: 4 additions & 4 deletions README.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ If you have used and benefitted from this library. Please feel free to buy me a
Install the package from NuGet using `Install-Package ReverseMarkdown` or clone the repository and build it yourself.

<!-- snippet: Usage -->
<a id='snippet-usage'></a>
<a id='snippet-Usage'></a>
```cs
var converter = new ReverseMarkdown.Converter();

string html = "This a sample <strong>paragraph</strong> from <a href=\"http://test.com\">my site</a>";

string result = converter.Convert(html);
```
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-usage' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-Usage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in:
Expand All @@ -36,7 +36,7 @@ This a sample **paragraph** from [my site](http://test.com)
The conversion can also be customized:

<!-- snippet: UsageWithConfig -->
<a id='snippet-usagewithconfig'></a>
<a id='snippet-UsageWithConfig'></a>
```cs
var config = new ReverseMarkdown.Config
{
Expand All @@ -52,7 +52,7 @@ var config = new ReverseMarkdown.Config

var converter = new ReverseMarkdown.Converter(config);
```
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L27-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-usagewithconfig' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L27-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-UsageWithConfig' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Configuration options
Expand Down
2 changes: 1 addition & 1 deletion src/ReverseMarkdown.Test/ChildConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void WhenConverter_A_IsReplacedByConverter_IgnoreAWhenHasClass()
var converter = new ReverseMarkdown.Converter(new Config(), typeof(IgnoreAWhenHasClass).Assembly);

var type = converter.GetType();
var prop = type.GetField("_converters", BindingFlags.NonPublic | BindingFlags.Instance);
var prop = type.GetField("Converters", BindingFlags.NonPublic | BindingFlags.Instance);

Assert.NotNull(prop);

Expand Down
2 changes: 2 additions & 0 deletions src/ReverseMarkdown/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public enum TableWithoutHeaderRowHandlingOption
/// </summary>
public bool TableHeaderColumnSpanHandling { get; set; } = true;

public bool CleanupUnnecessarySpaces { get; set; } = true;


/// <summary>
/// Determines whether url is allowed: WhitelistUriSchemes contains no elements or contains passed url.
Expand Down
44 changes: 22 additions & 22 deletions src/ReverseMarkdown/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace ReverseMarkdown
{
public class Converter
{
protected readonly IDictionary<string, IConverter> _converters = new Dictionary<string, IConverter>();
protected readonly IConverter _passThroughTagsConverter;
protected readonly IConverter _dropTagsConverter;
protected readonly IConverter _byPassTagsConverter;
protected readonly IDictionary<string, IConverter> Converters = new Dictionary<string, IConverter>();
protected readonly IConverter PassThroughTagsConverter;
protected readonly IConverter DropTagsConverter;
protected readonly IConverter ByPassTagsConverter;

public Converter() : this(new Config()) {}

Expand All @@ -23,19 +23,19 @@ public Converter(Config config, params Assembly[] additionalAssemblies)
{
Config = config;

List<Assembly> assemblies = new List<Assembly>()
var assemblies = new List<Assembly>()
{
typeof(IConverter).GetTypeInfo().Assembly
};

if (!(additionalAssemblies is null))
assemblies.AddRange(additionalAssemblies);

List<Type> types = new List<Type>();
var types = new List<Type>();
// instantiate all converters excluding the unknown tags converters
foreach (var assembly in assemblies)
{
foreach (var ctype in assembly.GetTypes()
foreach (var converterType in assembly.GetTypes()
.Where(t => t.GetTypeInfo().GetInterfaces().Contains(typeof(IConverter)) &&
!t.GetTypeInfo().IsAbstract
&& t != typeof(PassThrough)
Expand All @@ -44,32 +44,32 @@ public Converter(Config config, params Assembly[] additionalAssemblies)
{
// Check to see if any existing types are children/equal to
// the type to add.
if (types.Any(e => ctype.IsAssignableFrom(e)))
if (types.Any(e => converterType.IsAssignableFrom(e)))
// If they are, ignore the type.
continue;

// See if there is a type that is a parent of the
// current type.
Type toRemove = types.FirstOrDefault(e => e.IsAssignableFrom(ctype));
var toRemove = types.FirstOrDefault(e => e.IsAssignableFrom(converterType));
// if there is ...
if (!(toRemove is null))
// ... remove the parent.
types.Remove(toRemove);

// finally, add the type.
types.Add(ctype);
types.Add(converterType);
}
}

// For each type to register ...
foreach (var ctype in types)
foreach (var converterType in types)
// ... activate them
Activator.CreateInstance(ctype, this);
Activator.CreateInstance(converterType, this);

// register the unknown tags converters
_passThroughTagsConverter = new PassThrough(this);
_dropTagsConverter = new Drop(this);
_byPassTagsConverter = new ByPass(this);
PassThroughTagsConverter = new PassThrough(this);
DropTagsConverter = new Drop(this);
ByPassTagsConverter = new ByPass(this);
}

public Config Config { get; protected set; }
Expand All @@ -94,35 +94,35 @@ public virtual string Convert(string html)
// cleanup multiple new lines
result = Regex.Replace( result, @"(^\p{Zs}*(\r\n|\n)){2,}", Environment.NewLine, RegexOptions.Multiline);

return result.Trim().FixMultipleNewlines();
return Config.CleanupUnnecessarySpaces ? result.Trim().FixMultipleNewlines() : result;
}

public virtual void Register(string tagName, IConverter converter)
{
_converters[tagName] = converter;
Converters[tagName] = converter;
}

public virtual IConverter Lookup(string tagName)
{
// if a tag is in the pass through list then use the pass through tags converter
if (Config.PassThroughTags.Contains(tagName))
{
return _passThroughTagsConverter;
return PassThroughTagsConverter;
}

return _converters.ContainsKey(tagName) ? _converters[tagName] : GetDefaultConverter(tagName);
return Converters.TryGetValue(tagName, out var converter) ? converter : GetDefaultConverter(tagName);
}

private IConverter GetDefaultConverter(string tagName)
{
switch (Config.UnknownTags)
{
case Config.UnknownTagsOption.PassThrough:
return _passThroughTagsConverter;
return PassThroughTagsConverter;
case Config.UnknownTagsOption.Drop:
return _dropTagsConverter;
return DropTagsConverter;
case Config.UnknownTagsOption.Bypass:
return _byPassTagsConverter;
return ByPassTagsConverter;
default:
throw new UnknownTagException(tagName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ReverseMarkdown/Converters/ConverterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected string TreatChildren(HtmlNode node)
}

private string Treat(HtmlNode node) {
//TrimNewLine(node);
// TrimNewLine(node);
var converter = Converter.Lookup(node.Name);
return converter.Convert(node);
}
Expand Down
2 changes: 2 additions & 0 deletions src/ReverseMarkdown/Converters/Div.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public override string Convert(HtmlNode node)
"table"
};

content = Converter.Config.CleanupUnnecessarySpaces ? content.Trim() : content;

// if there is a block child then ignore adding the newlines for div
if ((node.ChildNodes.Count == 1 && blockTags.Contains(node.FirstChild.Name)))
{
Expand Down
2 changes: 2 additions & 0 deletions src/ReverseMarkdown/Converters/P.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public override string Convert(HtmlNode node)
var indentation = IndentationFor(node);
var newlineAfter = NewlineAfter(node);

var content = Converter.Config.CleanupUnnecessarySpaces ? TreatChildren(node).Trim() : TreatChildren(node);

return $"{indentation}{TreatChildren(node)}{newlineAfter}";
}

Expand Down

0 comments on commit dfa306a

Please sign in to comment.