-
Notifications
You must be signed in to change notification settings - Fork 319
Спроектировал парсер Markdown #256
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
Open
Kpokoko
wants to merge
8
commits into
kontur-courses:master
Choose a base branch
from
Kpokoko:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cbd5e5b
Спроектировал парсер Markdown
Kpokoko 65494cc
Написал тесты, немного поправил архитектуру
Kpokoko 3a8f9ae
реализовал токенизатор + валидатор тегов
Kpokoko 80b1d81
Доделал базовый парсер, немного расширил архитектуру
Kpokoko 3830474
Добавил рендеринг неупорядоченных листов
Kpokoko 321270b
Добавил забытые тесты на производительность
Kpokoko ec9c046
Отрефакторил мелкие моменты, изменил создание итоговой строки
Kpokoko 50d1fa8
Внёс функциональность метода ProcessUnpairLineTags в TokenizeText
Kpokoko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Markdown", "Markdown\Markdown.csproj", "{DB656FAA-A411-48CE-8C50-6F36096580E2}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {DB656FAA-A411-48CE-8C50-6F36096580E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {DB656FAA-A411-48CE-8C50-6F36096580E2}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {DB656FAA-A411-48CE-8C50-6F36096580E2}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {DB656FAA-A411-48CE-8C50-6F36096580E2}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or 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; | ||
|
|
||
| public interface IMarkdownParser | ||
| { | ||
| public string Render(string markdown); | ||
|
|
||
| public List<Token> TokenizeText(string markdown); | ||
|
|
||
| public string BuildHTMLString(List<Token> tokens, string markdown); | ||
| } |
This file contains hidden or 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,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="FluentAssertions" Version="8.8.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0-preview-24080-01" /> | ||
| <PackageReference Include="NUnit" Version="4.4.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="6.0.0-beta.1" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,188 @@ | ||
| using System.Text; | ||
|
|
||
| namespace Markdown; | ||
|
|
||
| public class MarkdownParser : IMarkdownParser | ||
| { | ||
| private readonly Dictionary<string, TokenType> _pairMarkdownToTag = new() | ||
| { | ||
| {"_", TokenType.Italic}, | ||
| {"__", TokenType.Bold}, | ||
| {"#", TokenType.Title}, | ||
| {"*", TokenType.ListItem}, | ||
| }; | ||
|
|
||
| private readonly HashSet<TokenType> _unpairTags = new() { TokenType.Title, TokenType.ListItem, TokenType.List }; | ||
|
|
||
| public string Render(string markdown) | ||
| { | ||
| var tokensList = TokenizeText(markdown); | ||
| var htmlWithPairTags = BuildHTMLString(tokensList, markdown); | ||
| return htmlWithPairTags; | ||
| } | ||
|
|
||
| public List<Token> TokenizeText(string markdown) | ||
| { | ||
| var stack = new Stack<RawToken>(); | ||
| var tokens = new List<Token>(); | ||
| var tagValidator = new MarkdownTagValidator(markdown); | ||
| var isListOpened = false; | ||
| for (var i = 0; i < markdown.Length; ++i) | ||
| { | ||
| if (!_pairMarkdownToTag.ContainsKey(markdown[i].ToString())) | ||
| { | ||
| var isNewLine = tagValidator.IsNewLineStarted(i); | ||
| if (isNewLine && isListOpened && stack.Count > 0 && stack.Peek().Type == TokenType.ListItem | ||
| && i < markdown.Length - 1 && markdown[i + 1] != '*') | ||
| { | ||
| var opening = stack.Pop(); | ||
| if (tagValidator.HasTagContentInside(opening.StartIndex + 1, i - 1) && | ||
| !tagValidator.HasTagDigitsInside(opening.StartIndex + 1, i - 1) && | ||
| !tagValidator.IsTagPartsSplittingWord(opening.StartIndex, i)) | ||
| { | ||
| tokens.Add(CreateToken(opening, i - 1)); | ||
| } | ||
| tokens.Add(CreateToken(stack.Pop(), i)); | ||
| } | ||
| else if (markdown[i] == '\n' && isListOpened && stack.Count > 0 | ||
| && stack.Peek().Type == TokenType.ListItem) | ||
| { | ||
| tokens.Add(CreateToken(stack.Pop(), i)); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| string currentTag = null; | ||
| var tagLength = 1; | ||
| if (markdown[i] == '_' && i + 1 < markdown.Length && markdown[i + 1] == '_') | ||
| { | ||
| currentTag = "__"; | ||
| tagLength = 2; | ||
| } | ||
| else | ||
| currentTag = markdown[i].ToString(); | ||
| if (currentTag == "#" || currentTag == "*") | ||
| tagLength = 2; | ||
|
|
||
| if (currentTag == "__" && stack.Any(s => s.Type == TokenType.Italic)) | ||
| { | ||
| i += tagLength - 1; | ||
| continue; | ||
| } | ||
|
|
||
| var isOpening = stack.Count == 0 || stack.Peek().Type != _pairMarkdownToTag[currentTag]; | ||
| var isTagCorrect = tagValidator.IsTagPartCorrect(i, isOpening, tagLength) | ||
| || tagValidator.IsListOpening(i); | ||
| if (!isTagCorrect) | ||
| { | ||
| i += tagLength - 1; | ||
| continue; | ||
| } | ||
| if (stack.Count == 0 || stack.Peek().Type != _pairMarkdownToTag[currentTag] | ||
| || _unpairTags.Contains(_pairMarkdownToTag[currentTag])) | ||
| { | ||
| if (!isListOpened && tagValidator.IsListOpening(i)) | ||
| { | ||
| var listStartInd = i; | ||
| if (i > 0) | ||
| --listStartInd; | ||
| // Длина марки 1 для отсеивания \n | ||
| stack.Push(new RawToken(TokenType.List, 1, listStartInd)); | ||
| isListOpened = true; | ||
| stack.Push(new RawToken(_pairMarkdownToTag[currentTag], tagLength, i)); | ||
| continue; | ||
| } | ||
| stack.Push(new RawToken(_pairMarkdownToTag[currentTag], tagLength, i)); | ||
| } | ||
| else | ||
| { | ||
| if (_unpairTags.Contains(_pairMarkdownToTag[currentTag])) | ||
| continue; | ||
| var opening = stack.Pop(); | ||
| if (tagValidator.HasTagContentInside(opening.StartIndex + tagLength, i - 1) && | ||
| !tagValidator.HasTagDigitsInside(opening.StartIndex + tagLength, i - 1) && | ||
| !tagValidator.IsTagPartsSplittingWord(opening.StartIndex, i)) | ||
| { | ||
| tokens.Add(CreateToken(opening, i)); | ||
| } | ||
| } | ||
| i += tagLength - 1; | ||
| } | ||
|
|
||
| var isLastCharUsed = false; | ||
| while (stack.Count > 0) | ||
| { | ||
| if (_unpairTags.Contains(stack.Peek().Type) | ||
| || stack.Peek().Type is TokenType.Italic && markdown[^1] == '_' && !isLastCharUsed) | ||
| { | ||
| tokens.Add(CreateToken(stack.Peek(), markdown.Length)); | ||
| isLastCharUsed = true; | ||
| } | ||
|
|
||
| stack.Pop(); | ||
| } | ||
| return tokens; | ||
| } | ||
|
|
||
| public List<Token> AddTokenWrappers(List<Token> tokens) | ||
| { | ||
| var tagBuilder = new TagBuilder(); | ||
| foreach (var token in tokens) | ||
| { | ||
| var wrappers = tagBuilder.GetWrappers(token.Type.ToString()); | ||
| token.SetTokenWrappers(wrappers); | ||
| } | ||
| return tokens; | ||
| } | ||
|
|
||
| public string BuildHTMLString(List<Token> tokens, string markdown) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(markdown) || tokens.Count == 0) | ||
| return markdown; | ||
| tokens = tokens | ||
| .OrderBy(t => t.StartIndex) | ||
| .ToList(); | ||
| tokens = AddTokenWrappers(tokens); | ||
| var htmlString = new StringBuilder(); | ||
| var tagStartPositions = new Dictionary<int, Token>(); | ||
| var tagEndPositions = new Dictionary<int, Token>(); | ||
| var openedTags = new Stack<Token>(); | ||
| foreach (var token in tokens) | ||
| { | ||
| tagStartPositions.Add(token.StartIndex, token); | ||
| tagEndPositions.TryAdd(token.StartIndex + token.TokenLength + token.TokenMarkLength, token); | ||
| } | ||
|
|
||
| for (var i = 0; i < markdown.Length; ++i) | ||
| { | ||
| if (tagStartPositions.ContainsKey(i)) | ||
| { | ||
| htmlString.Append(tagStartPositions[i].TokenWrappers.TokenStart); | ||
| openedTags.Push(tagStartPositions[i]); | ||
| i += tagStartPositions[i].TokenMarkLength - 1; | ||
| continue; | ||
| } | ||
| if (tagEndPositions.ContainsKey(i) && openedTags.Count > 0) | ||
| { | ||
| htmlString.Append(tagEndPositions[i].TokenWrappers.TokenEnd); | ||
| openedTags.Pop(); | ||
| if (tagEndPositions[i].Type == TokenType.List) | ||
| i -= 1; | ||
| else if (tagEndPositions[i].Type != TokenType.ListItem) | ||
| i += tagEndPositions[i].TokenMarkLength - 1; | ||
| continue; | ||
| } | ||
| htmlString.Append(markdown[i]); | ||
| } | ||
|
|
||
| while (openedTags.Count > 0) | ||
| htmlString.Append(openedTags.Pop().TokenWrappers.TokenEnd); | ||
| return htmlString.ToString(); | ||
| } | ||
|
|
||
| private Token CreateToken(RawToken rawToken, int endIndex) | ||
| { | ||
| var startIndex = rawToken.StartIndex; | ||
| return new Token(startIndex, endIndex - rawToken.StartIndex - rawToken.TokenMarkLength, rawToken.Type, rawToken.TokenMarkLength); | ||
| } | ||
| } |
This file contains hidden or 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,49 @@ | ||
| namespace Markdown; | ||
|
|
||
| public class MarkdownTagValidator | ||
| { | ||
| private string _markdown; | ||
|
|
||
| public MarkdownTagValidator(string markdown) | ||
| { | ||
| this._markdown = markdown; | ||
| } | ||
|
|
||
| public bool IsTagPartCorrect(int start, bool isOpeningTag, int length) | ||
| { | ||
| var isTagScreened = (start > 0 && _markdown[start - 1] == '\\') | ||
| && (start > 1 && _markdown[start - 2] != '\\'); | ||
| if (isOpeningTag) | ||
| return !isTagScreened && _markdown[start + length] != ' '; | ||
| return !isTagScreened && _markdown[start - 1] != ' '; | ||
| } | ||
|
|
||
| public bool IsTagPartsSplittingWord(int start, int end) | ||
| { | ||
| var isStartSplittingWord = start > 0 && char.IsLetter(_markdown[start - 1]); | ||
| var isEndSplittingWord = end < _markdown.Length - 1 && char.IsLetter(_markdown[end + 1]); | ||
| if (!isStartSplittingWord || !isEndSplittingWord) | ||
| return false; | ||
| return _markdown.Substring(start, end - start).Contains(' '); | ||
| } | ||
|
|
||
| public bool HasTagContentInside(int start, int end) | ||
| { | ||
| return !string.IsNullOrEmpty(_markdown.Substring(start, end - start + 1)); | ||
| } | ||
|
|
||
| public bool HasTagDigitsInside(int start, int end) | ||
| { | ||
| return _markdown.Substring(start, end - start + 1).Any(char.IsDigit); | ||
| } | ||
|
|
||
| public bool IsNewLineStarted(int index) | ||
| { | ||
| return index == 0 || _markdown[index - 1] == '\n'; | ||
| } | ||
|
|
||
| public bool IsListOpening(int index) | ||
| { | ||
| return _markdown[index] == '*' && IsNewLineStarted(index); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Посмотри на название метода и на возвращаемое значение в return. Ничего не смущает?)