-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kashin.aleksandr
committed
Dec 11, 2024
1 parent
d5d124a
commit 593897b
Showing
58 changed files
with
903 additions
and
215 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace Markdown; | ||
|
||
public interface IMd | ||
public interface IMarkdown | ||
{ | ||
string Render(string markdown); | ||
} |
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,94 @@ | ||
using Markdown.Render; | ||
using Markdown.Tokenizer; | ||
using Markdown.Tokenizer.Nodes; | ||
using Markdown.Tokenizer.Tags; | ||
|
||
namespace Markdown; | ||
|
||
public class MarkdownRenderer : IMarkdown | ||
{ | ||
public string Render(string markdown) | ||
{ | ||
var tokenizer = new MarkdownTokenizer(); | ||
var renderer = new HtmlRenderer(); | ||
var tokens = tokenizer.Tokenize(markdown); | ||
var tree = ToTree(tokens); | ||
return renderer.Render(tree); | ||
} | ||
|
||
private Node ToTree(List<Token> tokens) | ||
{ | ||
Node mainNode = new MainNode(); | ||
Node currentNode = mainNode; | ||
for (int i = 0; i < tokens.Count; i++) | ||
{ | ||
if (tokens[i].TagStatus == TagStatus.Broken) | ||
{ | ||
currentNode.Children.Add(new TextNode{Value = tokens[i].Value}); | ||
continue; | ||
} | ||
|
||
if (tokens[i] is ItalicTag tag) | ||
{ | ||
if(tag.TagStatus == TagStatus.Open) | ||
{ | ||
var node = new ItalicNode(); | ||
currentNode.Children.Add(node); | ||
node.Parent = currentNode; | ||
currentNode = node; | ||
continue; | ||
} | ||
|
||
if (tag.TagStatus == TagStatus.Closed) | ||
{ | ||
currentNode = currentNode.Parent; | ||
continue; | ||
} | ||
} | ||
|
||
if (tokens[i] is BoldTag boldTag) | ||
{ | ||
if(boldTag.TagStatus == TagStatus.Open) | ||
{ | ||
var node = new BoldNode(); | ||
currentNode.Children.Add(node); | ||
node.Parent = currentNode; | ||
currentNode = node; | ||
continue; | ||
} | ||
|
||
if (boldTag.TagStatus == TagStatus.Closed) | ||
{ | ||
currentNode = currentNode.Parent; | ||
continue; | ||
} | ||
} | ||
|
||
if (tokens[i] is HeaderTag) | ||
{ | ||
var node = new HeaderNode(); | ||
currentNode.Children.Add(node); | ||
node.Parent = currentNode; | ||
currentNode = node; | ||
continue; | ||
} | ||
|
||
if (tokens[i] is NewLineToken) | ||
{ | ||
if (currentNode is HeaderNode) | ||
{ | ||
currentNode = currentNode.Parent; | ||
} | ||
continue; | ||
} | ||
|
||
if (tokens[i] is TextToken textToken) | ||
{ | ||
currentNode.Children.Add(new TextNode { Value = textToken.Value }); | ||
continue; | ||
} | ||
} | ||
|
||
return currentNode.Parent ?? currentNode; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,35 +1,28 @@ | ||
using System.Text; | ||
using Markdown.Render.Renders; | ||
using Markdown.Tokenizer; | ||
using Markdown.Tokenizer.Nodes; | ||
|
||
namespace Markdown.Render; | ||
|
||
public class HtmlRenderer : ITokenRenderer | ||
{ | ||
private readonly Dictionary<TokenType, ITokenRender> _renders = new() | ||
{ | ||
{ TokenType.Italic , new ItalicRender() }, | ||
{ TokenType.Bold , new BoldRender() }, | ||
{ TokenType.Header, new HeadRender() }, | ||
{ TokenType.Text, new TextRender() }, | ||
{ TokenType.ItemList, new ItemListRender() } | ||
}; | ||
|
||
public string Render(List<Token> tokens) | ||
public string Render(Node tokens) | ||
{ | ||
var sb = new StringBuilder(); | ||
foreach (var token in tokens) | ||
{ | ||
sb.Append(Render(token)); | ||
} | ||
foreach (var token in tokens.Children) | ||
sb.Append(RenderToken(token)); | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
private string Render(Token token) | ||
private string? RenderToken(Node node) | ||
{ | ||
return _renders[token.Type].Render(token); | ||
return node switch | ||
{ | ||
TextNode textNode => textNode.Value, | ||
HeaderNode => $"<h1>{Render(node)}</h1>", | ||
ItalicNode => $"<em>{Render(node)}</em>", | ||
BoldNode => $"<strong>{Render(node)}</strong>", | ||
_ => throw new Exception($"Unknown token type: {node.GetType()}") | ||
}; | ||
} | ||
|
||
|
||
} |
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,8 +1,8 @@ | ||
using Markdown.Tokenizer; | ||
using Markdown.Tokenizer.Nodes; | ||
|
||
namespace Markdown.Render; | ||
|
||
public interface ITokenRenderer | ||
{ | ||
string Render(List<Token> tokens); | ||
string Render(Node tokens); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,93 @@ | ||
namespace Markdown.Tests.Markdown; | ||
|
||
[TestFixture] | ||
public class MarkdownTests | ||
{ | ||
private static readonly VerifySettings Settings = new(); | ||
private static readonly MarkdownRenderer Renderer = new(); | ||
|
||
[OneTimeSetUp] | ||
public void OneTimeSetUp() | ||
{ | ||
Settings.UseDirectory("snapshots"); | ||
} | ||
|
||
[TestCaseSource(nameof(ItalicTestCases))] | ||
public string Test_1(string input) => Renderer.Render(input); | ||
|
||
private static TestCaseData[] ItalicTestCases = | ||
[ | ||
new TestCaseData("# Header").Returns("<h1>Header</h1>"), | ||
new TestCaseData("\\# Header").Returns("# Header"), | ||
new TestCaseData("\\\\# Header").Returns("\\<h1>Header</h1>"), | ||
new TestCaseData("_Italic text_").Returns("<em>Italic text</em>"), | ||
new TestCaseData("\\_Text_").Returns("_Text_"), | ||
new TestCaseData("\\\\_Italic text_").Returns("\\<em>Italic text</em>"), | ||
new TestCaseData("_Italic text").Returns("_Italic text"), | ||
new TestCaseData("Italic text_").Returns("Italic text_"), | ||
new TestCaseData("Italic_ text_").Returns("Italic_ text_"), | ||
new TestCaseData("_Italic _text").Returns("_Italic _text"), | ||
new TestCaseData("_нач_але").Returns("<em>нач</em>але"), | ||
new TestCaseData("сер_еди_не").Returns("сер<em>еди</em>не"), | ||
new TestCaseData("цифры_1_12_3").Returns("цифры_1_12_3"), | ||
new TestCaseData("кон_це._").Returns("кон<em>це.</em>"), | ||
new TestCaseData("в ра_зных сл_овах не").Returns("в ра_зных сл_овах не"), | ||
new TestCaseData("__bold__").Returns("<strong>bold</strong>"), | ||
new TestCaseData("_Text__").Returns("_Text__"), | ||
new TestCaseData("__Text_").Returns("__Text_"), | ||
new TestCaseData("__Italic __text").Returns("__Italic __text"), | ||
new TestCaseData("__два _один_ может__").Returns("<strong>два <em>один</em> может</strong>"), | ||
new TestCaseData("_одинарного __двойное__ не_").Returns( "<em>одинарного __двойное__ не</em>") | ||
]; | ||
|
||
private static Task Verify(string target) => | ||
Verifier.Verify(target, Settings); | ||
|
||
[Test] | ||
public void SimpleText_Render_Verify() => | ||
Verify(Renderer.Render("Text")); | ||
|
||
[Test] | ||
public void EscapedCharacter_Render_Verify() => | ||
Verify(Renderer.Render(@"\_Text_")); | ||
|
||
[Test] | ||
public void ItalicText_Render_Verify() => | ||
Verify(Renderer.Render("_Italic text_")); | ||
|
||
[Test] | ||
public void BoldText_Render_Verify() => | ||
Verify(Renderer.Render("__Bold text__")); | ||
|
||
[Test] | ||
public void BoldWithItalicText_Render_Verify() => | ||
Verify(Renderer.Render("__Bold _with italic_ text__")); | ||
|
||
[Test] | ||
public void SimpleHeader_Render_Verify() => | ||
Verify(Renderer.Render("# Header")); | ||
|
||
[Test] | ||
public void TwoHeaders_Render_Verify() => | ||
Verify(Renderer.Render("# Header one \n# Header two")); | ||
// | ||
// [Test] | ||
// public void HeaderWithItalic_Render_Verify() => | ||
// Verify(Renderer.Render("# Header with _italic text_")); | ||
// | ||
// [Test] | ||
// public void HeaderWithBoldAndItalic_Render_Verify() => | ||
// Verify(Renderer.Render("# Header with _italic_ and __bold__ text")); | ||
// | ||
// [Test] | ||
// public void HeaderWithItalicInBold_Render_Verify() => | ||
// Verify(Renderer.Render("# Header ___italic_ in bold__ text")); | ||
// | ||
// [Test] | ||
// public void SimpleList_Render_Verify() => | ||
// Verify(Renderer.Render("- item1\n- item2")); | ||
// | ||
// [Test] | ||
// public void ListWithItalicAndBold_Render_Verify() => | ||
// Verify(Renderer.Render("- _item1_\n- __item2__")); | ||
} |
1 change: 1 addition & 0 deletions
1
cs/Markdown/Tests/Markdown/snapshots/MarkdownTests.SimpleHeader_Render_Verify.received.txt
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 @@ | ||
<h1>Header</h1> |
1 change: 1 addition & 0 deletions
1
cs/Markdown/Tests/Markdown/snapshots/MarkdownTests.SimpleHeader_Render_Verify.verified.txt
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 @@ | ||
<h1>Header</h1> |
1 change: 1 addition & 0 deletions
1
cs/Markdown/Tests/Markdown/snapshots/MarkdownTests.TwoHeaders_Render_Verify.received.txt
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 @@ | ||
<h1>Header one </h1><h1>Header two</h1> |
1 change: 1 addition & 0 deletions
1
cs/Markdown/Tests/Markdown/snapshots/MarkdownTests.TwoHeaders_Render_Verify.verified.txt
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 @@ | ||
<h1>Header one </h1><h1>Header two</h1> |
Oops, something went wrong.