-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Кашин Александр #223
base: master
Are you sure you want to change the base?
Кашин Александр #223
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Markdown; | ||
|
||
public interface IMd | ||
{ | ||
string Render(string markdown); | ||
} |
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> |
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)); | ||
} | ||
} |
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 comment was marked as resolved.
Sorry, something went wrong. |
||
} | ||
|
||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
} |
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); | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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); | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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); | ||
} |
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) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
var token = parser.Parse(context); | ||
if(token is not null) | ||
tokens.Add(token); | ||
} | ||
|
||
return tokens; | ||
} | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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); | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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; | ||
|
||
This comment was marked as resolved.
Sorry, something went wrong. |
||
public List<Token>? NestedTokens { get; set; } | ||
|
||
public Token(TokenType type, string content, List<Token>? nestedTokens = null) | ||
{ | ||
Type = type; | ||
Content = content; | ||
NestedTokens = nestedTokens; | ||
} | ||
} |
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, | ||
} |
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) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
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 comment was marked as resolved.
Sorry, something went wrong.