-
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
Рыбин Леонид #236
Open
JavaScriptHaters
wants to merge
15
commits into
kontur-courses:master
Choose a base branch
from
JavaScriptHaters:Markdown
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
Рыбин Леонид #236
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dd6c865
create Markdown architecture
JavaScriptHaters f58bebf
small fix ITokenPosition and Tokenizer
JavaScriptHaters 2953301
rebuilt architecture
JavaScriptHaters 3098027
small refactor
JavaScriptHaters 0e78fce
base realization
JavaScriptHaters d954e38
add escape
JavaScriptHaters ec240a3
small fixes
JavaScriptHaters 7d337ad
raw variant of state machine
JavaScriptHaters db4069a
non refactor works bold and italic
JavaScriptHaters 8dc7f24
Add tests
JavaScriptHaters 71a7e22
big refactor
JavaScriptHaters 7b4d9db
all tags work stable
JavaScriptHaters e92b5fc
big refactor
JavaScriptHaters d5ed0a0
small refactor
JavaScriptHaters ea13850
small fix bold tag
JavaScriptHaters 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 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 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<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,30 @@ | ||
using System.Text; | ||
using Markdown.Token; | ||
|
||
namespace Markdown; | ||
|
||
public static class Md | ||
{ | ||
public static string Render(string text) | ||
{ | ||
var parser = new TagParser(); | ||
return GenerateHtml(text, parser.GetTokens(text)); | ||
} | ||
|
||
private static string GenerateHtml(string text, List<IToken> tokens) | ||
{ | ||
var currentTokens = tokens.OrderBy(t => t.Position).ToList(); | ||
int position = 0; | ||
var sb = new StringBuilder(); | ||
foreach (var token in currentTokens) | ||
{ | ||
sb.Append(text[position..token.Position]); | ||
sb.Append(token.ConvertedText); | ||
position = token.Position + token.SourceText.Length > text.Length ? text.Length : token.Position + token.SourceText.Length; | ||
} | ||
|
||
sb.Append(text[position..text.Length]); | ||
|
||
return sb.ToString(); | ||
} | ||
} |
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,3 @@ | ||
using Markdown; | ||
|
||
Console.WriteLine(Md.Render("some 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,94 @@ | ||
using Markdown.Tags; | ||
|
||
namespace Markdown.Rules; | ||
|
||
public class BoldRule : IRule | ||
{ | ||
private readonly ITag tag = new BoldTag(); | ||
private List<Token.Token> tokens = new(); | ||
private bool isItalicInStart; | ||
private bool isItalicInMiddle; | ||
private bool isTagClosed; | ||
private readonly int[] italicStartsInBoldState = [10, 12]; | ||
private readonly int boldStartsInItalicState = 9; | ||
private int currentState; | ||
|
||
public TagKind MoveByRule(char ch, int position) | ||
{ | ||
var symbol = SymbolStatusParser.ParseSymbolStatus(ch); | ||
|
||
if (!tag.UsedSymbols.Contains(symbol)) | ||
{ | ||
symbol = SymbolStatus.anotherSymbol; | ||
} | ||
|
||
currentState = tag.States[currentState][symbol]; | ||
|
||
if (currentState == 0) | ||
{ | ||
ClearTokens(); | ||
} | ||
|
||
if (currentState == tag.InputStateNumber) | ||
{ | ||
tokens.Add(new Token.Token(tag.MdView, tag.Head, position - (tag.MdView.Length - 1))); | ||
} | ||
else if (currentState == tag.OutputStateNumber) | ||
{ | ||
if (symbol == SymbolStatus.eof) | ||
{ | ||
tokens.Add(new Token.Token(tag.MdView, tag.Tail, position - (tag.MdView.Length - 1))); | ||
} | ||
else | ||
{ | ||
tokens.Add(new Token.Token(tag.MdView, tag.Tail, position - tag.MdView.Length)); | ||
} | ||
|
||
isTagClosed = true; | ||
} | ||
else if (currentState == boldStartsInItalicState) | ||
{ | ||
if (isItalicInMiddle) | ||
{ | ||
isItalicInStart = false; | ||
isItalicInMiddle = false; | ||
isTagClosed = false; | ||
currentState = 0; | ||
tokens.Clear(); | ||
} | ||
else | ||
{ | ||
isItalicInStart = !isItalicInStart; | ||
} | ||
} | ||
else if (italicStartsInBoldState.Contains(currentState)) | ||
{ | ||
if (isItalicInStart) | ||
{ | ||
isItalicInStart = false; | ||
isItalicInMiddle = false; | ||
isTagClosed = false; | ||
currentState = 0; | ||
tokens.Clear(); | ||
} | ||
else | ||
{ | ||
isItalicInMiddle = !isItalicInMiddle; | ||
} | ||
|
||
} | ||
|
||
if (isTagClosed && (!isItalicInMiddle && !isItalicInStart || symbol == SymbolStatus.eof)) | ||
{ | ||
isTagClosed = false; | ||
currentState = 0; | ||
return TagKind.Close; | ||
} | ||
|
||
return TagKind.None; | ||
} | ||
|
||
public List<Token.Token> GetTokens() { return tokens; } | ||
|
||
public void ClearTokens() { tokens.Clear(); } | ||
} |
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,40 @@ | ||
using Markdown.Tags; | ||
|
||
namespace Markdown.Rules; | ||
|
||
public class EscapeRule : IRule | ||
{ | ||
private readonly ITag tag = new EscapeTag(); | ||
private List<Token.Token> tokens = new(); | ||
private int currentState; | ||
|
||
public TagKind MoveByRule(char ch, int position) | ||
{ | ||
var symbol = SymbolStatusParser.ParseSymbolStatus(ch); | ||
|
||
if (!tag.UsedSymbols.Contains(symbol)) | ||
{ | ||
symbol = SymbolStatus.anotherSymbol; | ||
} | ||
|
||
currentState = tag.States[currentState][symbol]; | ||
|
||
if (currentState == tag.InputStateNumber) | ||
{ | ||
tokens.Add(new Token.Token(tag.MdView, tag.Head, position - (tag.MdView.Length - 1))); | ||
return TagKind.Open; | ||
} | ||
else if (currentState == tag.OutputStateNumber) | ||
{ | ||
tokens.Add(new Token.Token(ch.ToString(), ch.ToString(), position - (tag.MdView.Length - 1))); | ||
currentState = 0; | ||
return TagKind.Close; | ||
} | ||
|
||
return TagKind.None; | ||
} | ||
|
||
public List<Token.Token> GetTokens() { return tokens; } | ||
|
||
public void ClearTokens() { tokens.Clear(); } | ||
} |
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,47 @@ | ||
using Markdown.Tags; | ||
|
||
namespace Markdown.Rules; | ||
|
||
public class H1Rule : IRule | ||
{ | ||
private readonly ITag tag = new H1Tag(); | ||
private List<Token.Token> tokens = new(); | ||
private int currentState; | ||
|
||
public TagKind MoveByRule(char ch, int position) | ||
{ | ||
var symbol = SymbolStatusParser.ParseSymbolStatus(ch); | ||
|
||
if (!tag.UsedSymbols.Contains(symbol)) | ||
{ | ||
symbol = SymbolStatus.anotherSymbol; | ||
} | ||
|
||
currentState = tag.States[currentState][symbol]; | ||
|
||
if (currentState == tag.InputStateNumber) | ||
{ | ||
tokens.Add(new Token.Token(tag.MdView, tag.Head, position - (tag.MdView.Length - 1))); | ||
} | ||
else if (currentState == tag.OutputStateNumber) | ||
{ | ||
if (symbol == SymbolStatus.eof) | ||
{ | ||
tokens.Add(new Token.Token("", tag.Tail, position + 1)); | ||
} | ||
else | ||
{ | ||
tokens.Add(new Token.Token("", tag.Tail, position)); | ||
} | ||
|
||
currentState = 0; | ||
return TagKind.Close; | ||
} | ||
|
||
return TagKind.None; | ||
} | ||
|
||
public List<Token.Token> GetTokens() { return tokens; } | ||
|
||
public void ClearTokens() { tokens.Clear(); } | ||
} |
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,12 @@ | ||
using Markdown.Tags; | ||
|
||
namespace Markdown.Rules; | ||
|
||
public interface IRule | ||
{ | ||
public TagKind MoveByRule(char ch, int position); | ||
|
||
public List<Token.Token> GetTokens(); | ||
|
||
public void ClearTokens(); | ||
} |
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,95 @@ | ||
using Markdown.Tags; | ||
|
||
namespace Markdown.Rules; | ||
|
||
public class ItalicRule : IRule | ||
{ | ||
private readonly ITag tag = new ItalicTextTag(); | ||
private List<Token.Token> tokens = new(); | ||
private bool isBoldInMiddle; | ||
private bool isBoldInStart; | ||
private bool isTagClosed; | ||
private readonly int anotherStartState = 18; | ||
private readonly int italicStartsInBoldState = 7; | ||
private readonly int boldStartsInItalicState = 10; | ||
private int currentState; | ||
|
||
public TagKind MoveByRule(char ch, int position) | ||
{ | ||
|
||
var symbol = SymbolStatusParser.ParseSymbolStatus(ch); | ||
|
||
if (!tag.UsedSymbols.Contains(symbol)) | ||
{ | ||
symbol = SymbolStatus.anotherSymbol; | ||
} | ||
|
||
currentState = tag.States[currentState][symbol]; | ||
|
||
if (currentState == 0) | ||
{ | ||
ClearTokens(); | ||
} | ||
|
||
if (currentState == tag.InputStateNumber || currentState == anotherStartState) | ||
{ | ||
tokens.Add(new Token.Token(tag.MdView, tag.Head, position - tag.MdView.Length)); | ||
} | ||
else if (currentState == tag.OutputStateNumber) | ||
{ | ||
if (symbol == SymbolStatus.eof) | ||
{ | ||
tokens.Add(new Token.Token(tag.MdView, tag.Tail, position)); | ||
} | ||
else | ||
{ | ||
tokens.Add(new Token.Token(tag.MdView, tag.Tail, position - tag.MdView.Length)); | ||
} | ||
isTagClosed = true; | ||
} | ||
else if (currentState == boldStartsInItalicState) | ||
{ | ||
if (isBoldInMiddle) | ||
{ | ||
isBoldInMiddle = false; | ||
isBoldInStart = false; | ||
isTagClosed = false; | ||
currentState = 0; | ||
tokens.Clear(); | ||
} | ||
else | ||
{ | ||
isBoldInStart = !isBoldInStart; | ||
} | ||
} | ||
else if (currentState == italicStartsInBoldState) | ||
{ | ||
if (isBoldInStart) | ||
{ | ||
isBoldInMiddle = false; | ||
isBoldInStart = false; | ||
isTagClosed = false; | ||
currentState = 0; | ||
tokens.Clear(); | ||
|
||
} | ||
else | ||
{ | ||
isBoldInMiddle = !isBoldInMiddle; | ||
} | ||
} | ||
|
||
if (isTagClosed && (!isBoldInMiddle || symbol == SymbolStatus.eof)) | ||
{ | ||
isTagClosed = false; | ||
currentState = 0; | ||
return TagKind.Close; | ||
} | ||
|
||
return TagKind.None; | ||
} | ||
|
||
public List<Token.Token> GetTokens() { return tokens; } | ||
|
||
public void ClearTokens() { tokens.Clear(); } | ||
} |
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 @@ | ||
namespace Markdown; | ||
|
||
public enum SymbolStatus | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. По код-стайлу обычно enum-чики с большой буквы именуются |
||
{ | ||
text, | ||
digit, | ||
underscore, | ||
backslash, | ||
eof, | ||
newline, | ||
space, | ||
sharp, | ||
anotherSymbol, | ||
none | ||
} |
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,25 @@ | ||
namespace Markdown; | ||
|
||
public static class SymbolStatusParser | ||
{ | ||
public static SymbolStatus ParseSymbolStatus(char symbol) | ||
{ | ||
if (char.IsLetter(symbol)) | ||
return SymbolStatus.text; | ||
if (char.IsDigit(symbol)) | ||
return SymbolStatus.digit; | ||
if (symbol == ' ') | ||
return SymbolStatus.space; | ||
if (symbol == '_') | ||
return SymbolStatus.underscore; | ||
if (symbol == '\\') | ||
return SymbolStatus.backslash; | ||
if (symbol == '\n') | ||
return SymbolStatus.newline; | ||
if (symbol == '\0') | ||
return SymbolStatus.eof; | ||
if (symbol == '#') | ||
return SymbolStatus.sharp; | ||
return SymbolStatus.none; | ||
} | ||
} |
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.
This comment was marked as resolved.
Sorry, something went wrong.
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.
Ну если про GetTokens говорить, то он возращает, что-то такое:
"# ", <h1>, position = 0
"_", <em>, position = 8
А GenerateHtml пробегает по тексту и меняет в нужной позиции символы на HTML тэги, при этом у него может быть глобальный сдвиг, чтобы позиции символов оставались актуальными