-
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
Калентьев Илья tg:@m1nus0ne #241
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Markdown.Tokens.HtmlToken; | ||
using Markdown.Tokens.StringToken; | ||
|
||
namespace Markdown.Maker; | ||
|
||
public class HtmlMaker : IMaker<RootToken> | ||
{ | ||
public RootToken MakeFromTokens(List<StringToken> tokens) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Markdown.Tokens.StringToken; | ||
|
||
namespace Markdown.Maker; | ||
|
||
public interface IMaker<T> | ||
{ | ||
public T MakeFromTokens(List<StringToken> tokens); | ||
} |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Markdown.Maker; | ||
using Markdown.Parser; | ||
using Markdown.Rendered; | ||
|
||
namespace Markdown; | ||
|
||
public class Md<TResult> | ||
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. Честно говоря пока не очень понимаю мотивации использовать тут дженерик. Можешь комментарием к треду или прямо в коде объяснить вкратце мотивацию? |
||
{ | ||
public string Render(string input, IParser parser, IMaker<TResult> maker, IRenderer<TResult> renderer) | ||
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. Довольно много параметров в этом методе передается. Подскажи, почему решил конструктор не использовать? 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. Вынес в конструктор |
||
{ | ||
var tokens = new MarkdownParser().Parse(input); | ||
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. Тут тоже не в конструкторе и не в методе параметром передаем. Подскажи, почему именно так решил сделать? 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. Вынес в конструктор |
||
var model = maker.MakeFromTokens(tokens); | ||
return renderer.Render(model); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Markdown.Tokens.StringToken; | ||
|
||
namespace Markdown.Parser; | ||
|
||
public interface IParser | ||
{ | ||
public List<StringToken> Parse(string input); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Markdown.Parser; | ||
using Markdown.Tokens.StringToken; | ||
|
||
namespace Markdown; | ||
|
||
public class MarkdownParser : IParser | ||
{ | ||
public List<StringToken> Parse(string input) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
|
||
Console.WriteLine("Hello, World!"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Markdown.Rendered; | ||
using Markdown.Tokens.HtmlToken; | ||
using Markdown.Tokens.StringToken; | ||
|
||
namespace Markdown; | ||
|
||
public class HtmlRenderer : IRenderer<RootToken> | ||
{ | ||
public string Render(RootToken root) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Markdown.Rendered; | ||
|
||
public interface IRenderer<T> | ||
{ | ||
public string Render(T input); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Markdown.Tokens; | ||
|
||
public abstract class BaseHtmlToken | ||
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. Точно ли этот класс должен содержать "Html" в названии? Мы же парсим Markdown текст, который уже в последствии преобразуем в HTML, но мы можем захотеть и не только в него преобразовать, а и в другой язык разметки. Получится ли быстро это реализовать в текущей архитектуре? 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. Изначально я хотел использовать модель-посредник для трансляции любого формата разметки текста в другой, но решил оставить IEnumerable ее упрощенной версией. Строка -> IEnumerable -> RootToken (репрезентация DOM) |
||
{ | ||
public List<BaseHtmlToken>? Children { get; private protected set; } | ||
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. А точно ли нам нужен здесь nulluble тип? Может нас устроит и просто пустая коллекция, когда дочерних тегов нет? 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. Исправил |
||
public string Value { get; private protected set; } | ||
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. Подскажи, ты осознанно использовал 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. Всегда использую private, по необходимости расширяю область видимости. Без protected сеттер из дочерних классов становится недоступным |
||
|
||
public BaseHtmlToken(List<BaseHtmlToken>? children) | ||
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. В абстрактном классе у тебя 2 поля, но в конструкторе принимается только одно. Так и задумано? 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. Например в токене, который будет просто хранить текст Children = {StrongCloseToken(), TextToken(), StrongOpenToken()} |
||
{ | ||
Children = children; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Markdown.Tokens.HtmlToken; | ||
|
||
public abstract class HtmlTokenWithTag : BaseHtmlToken | ||
{ | ||
public HtmlTokenWithTag(List<BaseHtmlToken>? children) : base(children) | ||
{ | ||
//проверка на валидность по типам | ||
} | ||
|
||
public abstract Type OpenTypeToken { get; } | ||
public abstract Type CloseTypeToken { get; } | ||
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. Для таких полей обычно используют 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. После паринга StringToken будет преобразовываться в Open/CloseToken, далее в цельный Token |
||
public abstract string TagValue { get; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"<{TagValue}>{string.Join("", Children.Select(token => token.ToString()))}</{TagValue}>"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Markdown.Tokens.HtmlToken; | ||
|
||
public class RootToken : BaseHtmlToken | ||
{ | ||
public RootToken(List<BaseHtmlToken>? children) : base(children) | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Markdown.Tokens.HtmlToken.Strong; | ||
|
||
public class StrongCloseToken : BaseHtmlToken | ||
{ | ||
public StrongCloseToken(string value) : base(null) | ||
{ | ||
Value = value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Markdown.Tokens.HtmlToken.Strong; | ||
|
||
public class StrongOpenToken : BaseHtmlToken | ||
{ | ||
public StrongOpenToken(string value) : base(null) | ||
{ | ||
Value = value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Markdown.Tokens.HtmlToken.Strong; | ||
|
||
public class StrongToken : HtmlTokenWithTag | ||
{ | ||
public StrongToken(List<BaseHtmlToken>? children) : base(children) | ||
{ | ||
} | ||
|
||
public override Type OpenTypeToken => typeof(StrongOpenToken); | ||
public override Type CloseTypeToken => typeof(StrongCloseToken); | ||
public override string TagValue => "strong"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Markdown.Tokens.StringToken; | ||
|
||
public class StringToken(string value, int offset, int length, StringTokenType Type) | ||
{ | ||
public int Length = length; | ||
public int Offset = offset; | ||
public string Value = value; | ||
public StringTokenType Type; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Markdown.Tokens.StringToken; | ||
|
||
public enum StringTokenType | ||
{ | ||
Text, | ||
NewLine, | ||
WhiteSpace, | ||
Hash, | ||
Unexpected | ||
} |
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.
Мелочь. Так же как и вместо собственных классов лучше использовать интерфейсы, так и тут лучше использовать IEnumerable, чтобы не завязываться на конкретную реализацию коллекции, если нам нужно просто по ней проитерироваться
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.
Исправил