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

Шевырин Никита #229

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
883f607
created project Markdown
alexadralt Nov 23, 2024
c1f6b4d
initial design
alexadralt Nov 24, 2024
09601ae
initial design
alexadralt Nov 30, 2024
f27204e
added ArgumentExceptionHelpers
alexadralt Nov 30, 2024
bc62038
implemented abstract syntax tree
alexadralt Nov 30, 2024
f0d106e
did some syntax tree testing
alexadralt Nov 30, 2024
106f113
tokenizer WIP
alexadralt Dec 2, 2024
8de5081
addded tests for Md
alexadralt Dec 2, 2024
6e53af2
deleted tree tests
alexadralt Dec 2, 2024
b67b916
added usage example
alexadralt Dec 2, 2024
ae81615
implementation WIP 1
alexadralt Nov 30, 2024
9005981
renamed IMdTest to IMdTests
alexadralt Dec 4, 2024
1ef1aa0
renamed files for refactoring
alexadralt Dec 6, 2024
887266f
refactored the whole thing
alexadralt Dec 7, 2024
5be7d46
added nesting syntax rule
alexadralt Dec 7, 2024
90e83aa
refactored nesting rule
alexadralt Dec 8, 2024
f106f80
added new syntax rules and fixed potential bugs
alexadralt Dec 8, 2024
8b6a4d8
added descriptions to tests
alexadralt Dec 8, 2024
8efaaf3
deleted files with old version of existing code
alexadralt Dec 8, 2024
8549226
removed old files
alexadralt Dec 8, 2024
b5df139
merged version 1.0
alexadralt Dec 8, 2024
f298b01
deleted old files
alexadralt Dec 8, 2024
9f42573
added factory method for Md
alexadralt Dec 11, 2024
75251ad
refactored MdTokenizer
alexadralt Dec 11, 2024
262d9a2
refactored syntax rules
alexadralt Dec 11, 2024
f7be964
moved private fields declarations
alexadralt Dec 14, 2024
96b2796
moved performance tests to a different project and added more test cases
alexadralt Dec 14, 2024
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
16 changes: 16 additions & 0 deletions Markdown/Markdown.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Markdown", "Markdown\Markdown.csproj", "{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8FD8A48-C2C3-434B-953F-B9AF324E3E95}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions Markdown/Markdown/IMd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Markdown;

public interface IMd
{
public string Render(string markdown);
}
10 changes: 10 additions & 0 deletions Markdown/Markdown/Markdown.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
46 changes: 46 additions & 0 deletions Markdown/Markdown/Md.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Text;
using Markdown.TokenConverters;
using Markdown.Tokenizers;
using Markdown.Tokens;

namespace Markdown;

public class Md : IMd
{
private readonly ITokenizer[] _tokenizers =

This comment was marked as resolved.

{
new BoldTokenizer(),
new ItalicTokenizer(),
new HeadingTokenizer()
};
private readonly ITokenConverter[] _converters =
{
new BoldHtmlConverter(),
new ItalicHtmlConverter(),
new HeadingHtmlConverter()
};

private readonly int _sliceSize = 1024;

public string Render(string markdown)
{
var markdownSpan = markdown.AsSpan();
var context = new StringBuilder();
var stepCount = markdownSpan.Length / _sliceSize;

This comment was marked as resolved.

stepCount = markdownSpan.Length % _sliceSize == 0 ? stepCount : stepCount + 1;
var tokenList = new List<Token>();
for (var step = 0; step < stepCount; step++)
{
var sliceStart = step * _sliceSize;
var sliceSize = Math.Min(_sliceSize, markdownSpan.Length - sliceStart);
var stepSpan = markdownSpan.Slice(sliceStart, sliceSize);
foreach (var tokenizer in _tokenizers)

This comment was marked as resolved.

tokenList.AddRange(tokenizer.Tokenize(stepSpan));
foreach (var converter in _converters)
converter.ProcessTokens(tokenList, context);
tokenList.Clear();
}

return context.ToString();
}
}
1 change: 1 addition & 0 deletions Markdown/Markdown/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

13 changes: 13 additions & 0 deletions Markdown/Markdown/TokenConverters/BoldHtmlConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Text;
using Markdown.Tokens;

namespace Markdown.TokenConverters;

public class BoldHtmlConverter : HtmlTokenConverter
{
protected override MarkdownTokenType MarkdownTokenType { get; } = MarkdownTokenType.ToBold;
public override void ProcessTokens(IEnumerable<Token> tokens, StringBuilder context)
{
throw new NotImplementedException();
}
}
13 changes: 13 additions & 0 deletions Markdown/Markdown/TokenConverters/HeadingHtmlConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Text;
using Markdown.Tokens;

namespace Markdown.TokenConverters;

public class HeadingHtmlConverter : HtmlTokenConverter
{
protected override MarkdownTokenType MarkdownTokenType { get; } = MarkdownTokenType.ToHeading;
public override void ProcessTokens(IEnumerable<Token> tokens, StringBuilder context)
{
throw new NotImplementedException();
}
}
11 changes: 11 additions & 0 deletions Markdown/Markdown/TokenConverters/HtmlTokenConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text;
using Markdown.Tokens;

namespace Markdown.TokenConverters;

public abstract class HtmlTokenConverter : ITokenConverter
{
protected IEnumerable<Token>? TokensOnPreviousSlice { get; set; }
protected abstract MarkdownTokenType MarkdownTokenType { get; }
public abstract void ProcessTokens(IEnumerable<Token> tokens, StringBuilder context);
}
9 changes: 9 additions & 0 deletions Markdown/Markdown/TokenConverters/ITokenConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text;
using Markdown.Tokens;

namespace Markdown.TokenConverters;

public interface ITokenConverter
{
public void ProcessTokens(IEnumerable<Token> tokens, StringBuilder context);
}
13 changes: 13 additions & 0 deletions Markdown/Markdown/TokenConverters/ItalicHtmlConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Text;
using Markdown.Tokens;

namespace Markdown.TokenConverters;

public class ItalicHtmlConverter : HtmlTokenConverter
{
protected override MarkdownTokenType MarkdownTokenType { get; } = MarkdownTokenType.ToItalic;
public override void ProcessTokens(IEnumerable<Token> tokens, StringBuilder context)
{
throw new NotImplementedException();
}
}
13 changes: 13 additions & 0 deletions Markdown/Markdown/Tokenizers/BoldTokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Markdown.Tokens;

namespace Markdown.Tokenizers;

public class BoldTokenizer : MarkdownTokenizer
{
protected override MarkdownTokenType MarkdownTokenType { get; } = MarkdownTokenType.ToBold;

public override IEnumerable<Token> Tokenize(ReadOnlySpan<char> input)
{
throw new NotImplementedException();
}
}
13 changes: 13 additions & 0 deletions Markdown/Markdown/Tokenizers/HeadingTokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Markdown.Tokens;

namespace Markdown.Tokenizers;

public class HeadingTokenizer : MarkdownTokenizer
{
protected override MarkdownTokenType MarkdownTokenType { get; } = MarkdownTokenType.ToHeading;

public override IEnumerable<Token> Tokenize(ReadOnlySpan<char> input)
{
throw new NotImplementedException();
}
}
8 changes: 8 additions & 0 deletions Markdown/Markdown/Tokenizers/ITokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Markdown.Tokens;

namespace Markdown.Tokenizers;

public interface ITokenizer
{
public IEnumerable<Token> Tokenize(ReadOnlySpan<char> input);
}
14 changes: 14 additions & 0 deletions Markdown/Markdown/Tokenizers/ItalicTokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text;
using Markdown.Tokens;

namespace Markdown.Tokenizers;

public class ItalicTokenizer : MarkdownTokenizer
{
protected override MarkdownTokenType MarkdownTokenType { get; } = MarkdownTokenType.ToItalic;

public override IEnumerable<Token> Tokenize(ReadOnlySpan<char> input)
{
throw new NotImplementedException();
}
}
10 changes: 10 additions & 0 deletions Markdown/Markdown/Tokenizers/MarkdownTokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text;
using Markdown.Tokens;

namespace Markdown.Tokenizers;

public abstract class MarkdownTokenizer : ITokenizer
{
protected abstract MarkdownTokenType MarkdownTokenType { get; }
public abstract IEnumerable<Token> Tokenize(ReadOnlySpan<char> input);
}
9 changes: 9 additions & 0 deletions Markdown/Markdown/Tokens/MarkdownTokenType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Markdown.Tokens;

public enum MarkdownTokenType
{
NoConversion,

This comment was marked as resolved.

ToItalic,
ToBold,

This comment was marked as resolved.

ToHeading
}
13 changes: 13 additions & 0 deletions Markdown/Markdown/Tokens/Token.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Markdown.Tokens;

public readonly struct Token(
ReadOnlyMemory<char> content,
MarkdownTokenType markdownTokenType,
int startIndex,
int endIndex)
{
public ReadOnlyMemory<char> Content { get; } = content;
public MarkdownTokenType MarkdownTokenType { get; } = markdownTokenType;
public int StartIndex { get; } = startIndex;
public int EndIndex { get; } = endIndex;
}