Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cs/Markdown/IMd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Markdown;

public interface IMd
{
string Render(string markdown);
}
9 changes: 9 additions & 0 deletions cs/Markdown/Markdown.csproj
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>
15 changes: 15 additions & 0 deletions cs/Markdown/Md.cs
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

This comment was marked as resolved.

{
private readonly ITokenizer tokenizer = new MarkdownTokenizer();
private readonly ITokenRenderer renderer = new HtmlRenderer();

public string Render(string markdown)
{
return renderer.Render(tokenizer.Tokenize(markdown));
}
}
35 changes: 35 additions & 0 deletions cs/Markdown/Render/HtmlRenderer.cs
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.

}

This comment was marked as resolved.


}
8 changes: 8 additions & 0 deletions cs/Markdown/Render/ITokenRenderer.cs
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);
}
11 changes: 11 additions & 0 deletions cs/Markdown/Render/Renders/BoldRender.cs
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();
}
}
11 changes: 11 additions & 0 deletions cs/Markdown/Render/Renders/HeadRender.cs
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();
}
}
11 changes: 11 additions & 0 deletions cs/Markdown/Render/Renders/IItalicRender.cs
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();
}
}
8 changes: 8 additions & 0 deletions cs/Markdown/Render/Renders/ITokenRender.cs
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);
}
11 changes: 11 additions & 0 deletions cs/Markdown/Render/Renders/ItemListRender.cs
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();
}
}
11 changes: 11 additions & 0 deletions cs/Markdown/Render/Renders/TextRender.cs
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();
}
}
6 changes: 6 additions & 0 deletions cs/Markdown/Tokenizer/ITokenizer.cs
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);
}
31 changes: 31 additions & 0 deletions cs/Markdown/Tokenizer/MarkdownTokenizer.cs
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.

{
var token = parser.Parse(context);
if(token is not null)
tokens.Add(token);
}

return tokens;
}
}
9 changes: 9 additions & 0 deletions cs/Markdown/Tokenizer/Parsers/BoldParser.cs
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();
}
}
9 changes: 9 additions & 0 deletions cs/Markdown/Tokenizer/Parsers/HeadParser.cs
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();
}
}
6 changes: 6 additions & 0 deletions cs/Markdown/Tokenizer/Parsers/ITokenParser.cs
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);
}
9 changes: 9 additions & 0 deletions cs/Markdown/Tokenizer/Parsers/ItalicParser.cs
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();
}
}
9 changes: 9 additions & 0 deletions cs/Markdown/Tokenizer/Parsers/ListItemParser.cs
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();
}
}
9 changes: 9 additions & 0 deletions cs/Markdown/Tokenizer/Parsers/TextParser.cs
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();
}
}
16 changes: 16 additions & 0 deletions cs/Markdown/Tokenizer/Token.cs
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.

public List<Token>? NestedTokens { get; set; }

public Token(TokenType type, string content, List<Token>? nestedTokens = null)
{
Type = type;
Content = content;
NestedTokens = nestedTokens;
}
}
10 changes: 10 additions & 0 deletions cs/Markdown/Tokenizer/TokenType.cs
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,
}
36 changes: 36 additions & 0 deletions cs/Markdown/Tokenizer/TokenizerContext.cs
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.

{
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;
}
6 changes: 6 additions & 0 deletions cs/clean-code.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ControlDigit", "ControlDigi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples", "Samples\Samples.csproj", "{C3EF41D7-50EF-4CE1-B30A-D1D81C93D7FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Markdown", "Markdown\Markdown.csproj", "{BB96272E-4341-4D87-9C3F-98CCFBFF7332}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,5 +29,9 @@ Global
{C3EF41D7-50EF-4CE1-B30A-D1D81C93D7FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3EF41D7-50EF-4CE1-B30A-D1D81C93D7FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3EF41D7-50EF-4CE1-B30A-D1D81C93D7FA}.Release|Any CPU.Build.0 = Release|Any CPU
{BB96272E-4341-4D87-9C3F-98CCFBFF7332}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB96272E-4341-4D87-9C3F-98CCFBFF7332}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB96272E-4341-4D87-9C3F-98CCFBFF7332}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB96272E-4341-4D87-9C3F-98CCFBFF7332}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal