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

Вильданов Савелий #226

Open
wants to merge 7 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
2 changes: 1 addition & 1 deletion cs/Markdown/ListOfTokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class ListOfTokens<T> : List<T>
{
public new void Add(T token)
{
if(!(token is null))
if (!(token is null))
base.Add(token);
}
}
5 changes: 2 additions & 3 deletions cs/Markdown/Md.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ public class Md
{
public static string Render(string markdown)
{
Preparator preparator = new Preparator();
TokenGenerator tokenGenerator = new TokenGenerator();
Converter converter = new Converter();
preparator.Paragrapher(markdown);
return converter.ConvertWithTokens(preparator.GetTokens());
return converter.ConvertWithTokens(tokenGenerator.SplitParagraphs(markdown));
}
}
37 changes: 28 additions & 9 deletions cs/Markdown/Parsers/BoldParser.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections;
using System.Security.Principal;
using Markdown.TagsType;

namespace Markdown.Parsers;

public class BoldParser
public class BoldParser : IParser
{
private readonly Stack<Token> tagsStack;
private readonly GeneralParser.IsValidTag isValidOpenBoldTag;
Expand All @@ -24,7 +25,7 @@ public BoldParser()
index = 0;
}

public Token BoldParse(string text, int i)
private Token BoldParse(string text, int i)
{
index = i;
index++;
Expand All @@ -34,35 +35,43 @@ public Token BoldParse(string text, int i)
if (IsEmptyStringInsideBold(text, i))
{
index = text.Length;
return new Token("____");
return new Token("____", new TextTag());
}

if (IsBoldClosed)
{
if (!isValidOpenBoldTag(text, i+1))
if (!isValidOpenBoldTag(text, i + 1))
{
return (new Token("__"));
return (new Token("__", new TextTag()));
}
}
else
{
if (!isValidCloseBoldTag(text, i))
{
return new Token("__");
if (isValidOpenBoldTag(text, i))
{
if (tagsStack.Count != 0)
tagsStack.Pop();
return addBoldTag(new BoldTag(), true);
}

return new Token("__", new TextTag());
}
}


if (tagsStack.Count > 1 &&
tagsStack.Any(p => p.Type is {MarkdownTag: "_"}) &&
tagsStack.Peek().Type is {MarkdownTag: "__"})
{
tagsStack.Pop();
}

if (!IsBoldClosed && i == text.Length - 1)
{
tagsStack.Pop();
}

var boldTag = addBoldTag(new BoldTag(), IsBoldClosed);
IsBoldClosed = !IsBoldClosed;

Expand All @@ -84,8 +93,6 @@ public bool IsNextSymbolBold(string text, int i)
return false;
}

public int GetNewIndex() => index;

private bool IsEmptyStringInsideBold(string text, int i)
{
int nextIndex = i + 1;
Expand All @@ -96,4 +103,16 @@ private bool IsEmptyStringInsideBold(string text, int i)

return false;
}

public bool TryParse(char symbol, string text, int i)
{
return symbol == '_' && IsNextSymbolBold(text, i);
}

public Token Parse(string text, ref int i)
{
var token = BoldParse(text, i);
i = index;
return token;
}
}
4 changes: 2 additions & 2 deletions cs/Markdown/Parsers/GeneralParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public static Token GenerateTagToken(ITagsType tag, bool isClose)
{
if (isClose)
{
var temp = new Token(tag.GetHtmlOpenTag(), tag);
var temp = new Token(tag.GetHtmlOpenTag, tag);
tagsStack.Push(temp);
return temp;
}

if (tagsStack.Count != 0 && tagsStack.Peek().Type?.MarkdownTag == tag.MarkdownTag)
tag.HasPair = true;
return new Token(tag.GetHtmlCloseTag(), tag);
return new Token(tag.GetHtmlCloseTag, tag);
}

public static void ClosePairTag(Token token)
Expand Down
17 changes: 17 additions & 0 deletions cs/Markdown/Parsers/HeadingParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Markdown.TagsType;

namespace Markdown.Parsers;

public class HeadingParser : IParser
{
public bool TryParse(char symbol, string text, int index)
{
return symbol == '#';
}

public Token Parse(string text, ref int index)
{
var headingTag = new HeadingTag();
return new Token(headingTag.GetHtmlOpenTag, headingTag);
}
}
7 changes: 7 additions & 0 deletions cs/Markdown/Parsers/IParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Parsers;

public interface IParser
{
bool TryParse(char symbol, string text, int index);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это скорее CanParse наверное? Обычно методы Try... пытаются вернуть какой-то результат в out параметре, то есть "пытаются что-то сделать и вернуть успешность этого действия" - например, int.TryParse(..., out value), dictionary.TryGetValue(..., out value), а в твоем случае этого параметра нет

Token Parse(string text, ref int index);
}
27 changes: 22 additions & 5 deletions cs/Markdown/Parsers/ItalicParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Markdown.Parsers;

public class ItalicParser
public class ItalicParser : IParser
{
private readonly GeneralParser.IsValidTag isValidOpenItalicTag;
private readonly GeneralParser.IsValidTag isValidCloseItalicTag;
Expand All @@ -22,25 +22,32 @@ public ItalicParser()
IsHaveInsideItalicTag = false;
}

public Token ItalicParse(string text, int i)
private Token ItalicParse(string text, int i)
{
if (i != 0 && i != text.Length - 1)
IsHaveInsideItalicTag = true;
if (isItalicClosed)
{
if (!isValidOpenItalicTag(text, i))
{
return new Token("_");
return new Token("_", new TextTag());
}
}
else
{
if (!isValidCloseItalicTag(text, i))
{
return new Token("_");
if (isValidOpenItalicTag(text, i))
{
if (tagsStack.Count != 0)
tagsStack.Pop();
return addItalicTag(new ItalicTag(), true);
}

return new Token("_", new TextTag());
}
}

if (tagsStack.Count > 1 && !isItalicClosed && BoldParser.IsBoldClosed)
{
tagsStack.Pop();
Expand All @@ -55,4 +62,14 @@ public Token ItalicParse(string text, int i)

return result;
}

public bool TryParse(char symbol, string text, int i)
{
return symbol == '_';
}

public Token Parse(string text, ref int index)
{
return ItalicParse(text, index);
}
}
16 changes: 14 additions & 2 deletions cs/Markdown/Parsers/LinkParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Markdown.Parsers;

public class LinkParser
public class LinkParser : IParser
{
private LinkTag linkTag;
private StringBuilder nameOfLink;
Expand Down Expand Up @@ -43,9 +43,21 @@ public Token LinkParse(string text)

private Token GenerateLinkToken()
{
var context = linkTag.GetHtmlOpenTag() + linkTag.LinkUrl + "\">" + linkTag.LinkName + linkTag.GetHtmlCloseTag();
var context = linkTag.GetHtmlOpenTag + linkTag.LinkName + linkTag.GetHtmlCloseTag;
urlOfLink.Clear();
nameOfLink.Clear();
return new Token(context, linkTag);
}

public bool TryParse(char symbol, string text, int i)
{
return symbol == '[';
}

public Token Parse(string text, ref int i)
{
var token = LinkParse(text);
i = text.Length-1;
return token;
}
}
52 changes: 33 additions & 19 deletions cs/Markdown/Parsers/SlashParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,53 @@

namespace Markdown.Parsers;

public class SlashParser
public class SlashParser : IParser
{
private int index;
private BoldParser boldParser;

private int Index;

public SlashParser()
public SlashParser(BoldParser boldParser)
{
Index = 0;
this.boldParser = boldParser;
index = 0;
}
public Token QuotedToken(string text, int i, bool isNextSymbolBold)

private Token QuotedToken(string text, int i, bool isNextSymbolBold)
{
i++;
Index = i;
if (Index < text.Length)
index = i;
if (index < text.Length)
{
switch (text[Index])
switch (text[index])
{
case '_':
if (Index + 1 < text.Length && isNextSymbolBold)
if (index + 1 < text.Length && isNextSymbolBold)
{
Index++;
return new Token(new BoldTag().MarkdownTag);
index++;
return new Token(new BoldTag().MarkdownTag, new TextTag());
}
return new Token(new ItalicTag().MarkdownTag);

return new Token(new ItalicTag().MarkdownTag, new TextTag());
case '#':
return new Token(new HeadingTag().MarkdownTag);
return new Token(new HeadingTag().MarkdownTag, new TextTag());
}
}

Index--;
return new Token("\\");
index--;
return new Token("\\", new TextTag());
}

public int GetIndex() => index;

public bool TryParse(char symbol, string text, int i)
{
return symbol == '\\';
}

public Token Parse(string text, ref int i)
{
var token = QuotedToken(text, i, boldParser.IsNextSymbolBold(text, i + 1));
i = index;
return token;
}

public int GetIndex() => Index;

}
Loading