-
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
Nov 23, 2024
1 parent
feb67ef
commit d5d124a
Showing
23 changed files
with
292 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Markdown; | ||
|
||
public interface IMd | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,15 @@ | ||
using Markdown.Render; | ||
using Markdown.Tokenizer; | ||
|
||
namespace Markdown; | ||
|
||
public class Md : IMd | ||
{ | ||
private readonly ITokenizer tokenizer = new MarkdownTokenizer(); | ||
private readonly ITokenRenderer renderer = new HtmlRenderer(); | ||
|
||
public string Render(string markdown) | ||
{ | ||
return renderer.Render(tokenizer.Tokenize(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Text; | ||
using Markdown.Render.Renders; | ||
using Markdown.Tokenizer; | ||
|
||
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) | ||
{ | ||
var sb = new StringBuilder(); | ||
foreach (var token in tokens) | ||
{ | ||
sb.Append(Render(token)); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
private string Render(Token token) | ||
{ | ||
return _renders[token.Type].Render(token); | ||
} | ||
|
||
|
||
} |
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,8 @@ | ||
using Markdown.Tokenizer; | ||
|
||
namespace Markdown.Render; | ||
|
||
public interface ITokenRenderer | ||
{ | ||
string Render(List<Token> tokens); | ||
} |
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 @@ | ||
using Markdown.Tokenizer; | ||
|
||
namespace Markdown.Render.Renders; | ||
|
||
public class BoldRender : ITokenRender | ||
{ | ||
public string Render(Token token) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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 @@ | ||
using Markdown.Tokenizer; | ||
|
||
namespace Markdown.Render.Renders; | ||
|
||
public class HeadRender : ITokenRender | ||
{ | ||
public string Render(Token token) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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 @@ | ||
using Markdown.Tokenizer; | ||
|
||
namespace Markdown.Render.Renders; | ||
|
||
public class ItalicRender : ITokenRender | ||
{ | ||
public string Render(Token token) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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,8 @@ | ||
using Markdown.Tokenizer; | ||
|
||
namespace Markdown.Render.Renders; | ||
|
||
public interface ITokenRender | ||
{ | ||
string Render(Token token); | ||
} |
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 @@ | ||
using Markdown.Tokenizer; | ||
|
||
namespace Markdown.Render.Renders; | ||
|
||
public class ItemListRender : ITokenRender | ||
{ | ||
public string Render(Token token) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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 @@ | ||
using Markdown.Tokenizer; | ||
|
||
namespace Markdown.Render.Renders; | ||
|
||
public class TextRender : ITokenRender | ||
{ | ||
public string Render(Token token) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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,6 @@ | ||
namespace Markdown.Tokenizer; | ||
|
||
public interface ITokenizer | ||
{ | ||
List<Token> Tokenize(string text); | ||
} |
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,31 @@ | ||
using Markdown.Tokenizer.Parsers; | ||
|
||
namespace Markdown.Tokenizer; | ||
|
||
public class MarkdownTokenizer : ITokenizer | ||
{ | ||
private static readonly List<ITokenParser> parsers | ||
= new List<ITokenParser> | ||
{ | ||
new HeadParser(), | ||
new BoldParser(), | ||
new ItalicParser(), | ||
new TextParser(), | ||
new ListItemParser() | ||
}; | ||
|
||
public List<Token> Tokenize(string text) | ||
{ | ||
var context = new TokenizerContext(text); | ||
var tokens = new List<Token>(); | ||
|
||
foreach (var parser in parsers) | ||
{ | ||
var token = parser.Parse(context); | ||
if(token is not null) | ||
tokens.Add(token); | ||
} | ||
|
||
return tokens; | ||
} | ||
} |
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,9 @@ | ||
namespace Markdown.Tokenizer.Parsers; | ||
|
||
public class BoldParser : ITokenParser | ||
{ | ||
public Token? Parse(TokenizerContext text) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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,9 @@ | ||
namespace Markdown.Tokenizer.Parsers; | ||
|
||
public class HeadParser : ITokenParser | ||
{ | ||
public Token? Parse(TokenizerContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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,6 @@ | ||
namespace Markdown.Tokenizer.Parsers; | ||
|
||
public interface ITokenParser | ||
{ | ||
Token? Parse(TokenizerContext text); | ||
} |
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,9 @@ | ||
namespace Markdown.Tokenizer.Parsers; | ||
|
||
public class ItalicParser : ITokenParser | ||
{ | ||
public Token? Parse(TokenizerContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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,9 @@ | ||
namespace Markdown.Tokenizer.Parsers; | ||
|
||
public class ListItemParser : ITokenParser | ||
{ | ||
public Token? Parse(TokenizerContext text) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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,9 @@ | ||
namespace Markdown.Tokenizer.Parsers; | ||
|
||
public class TextParser : ITokenParser | ||
{ | ||
public Token? Parse(TokenizerContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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 Markdown.Tokenizer; | ||
|
||
public class Token | ||
{ | ||
public TokenType Type { get; set; } | ||
public string Content { get; set; } = string.Empty; | ||
|
||
public List<Token>? NestedTokens { get; set; } | ||
|
||
public Token(TokenType type, string content, List<Token>? nestedTokens = null) | ||
{ | ||
Type = type; | ||
Content = content; | ||
NestedTokens = nestedTokens; | ||
} | ||
} |
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,10 @@ | ||
namespace Markdown.Tokenizer; | ||
|
||
public enum TokenType | ||
{ | ||
Italic, | ||
Bold, | ||
Header, | ||
Text, | ||
ItemList, | ||
} |
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,36 @@ | ||
namespace Markdown.Tokenizer; | ||
|
||
public class TokenizerContext | ||
{ | ||
private readonly string text; | ||
private int position; | ||
|
||
public TokenizerContext(string text) | ||
{ | ||
this.text = text; | ||
position = 0; | ||
} | ||
|
||
public bool IsEnd => position >= text.Length; | ||
public char Current => text[position]; | ||
public void MoveNext() => position++; | ||
|
||
public string ReadWhile(Func<char, bool> predicate) | ||
{ | ||
var start = position; | ||
while (!IsEnd && predicate(Current)) | ||
{ | ||
MoveNext(); | ||
} | ||
|
||
return text.Substring(start, position - start); | ||
} | ||
|
||
public bool Match(string pattern) | ||
{ | ||
return text.Substring(position).StartsWith(pattern); | ||
} | ||
|
||
public int Position => position; | ||
public void ResetTo(int position) => this.position = position; | ||
} |
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