-
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
base: master
Are you sure you want to change the base?
Рыбин Леонид #236
Changes from 2 commits
dd6c865
f58bebf
2953301
3098027
0e78fce
d954e38
ec240a3
7d337ad
db4069a
8dc7f24
71a7e22
7b4d9db
e92b5fc
d5ed0a0
ea13850
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,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,32 @@ | ||
using Markdown.Tokens; | ||
using System.Text; | ||
|
||
namespace Markdown; | ||
|
||
public class Md | ||
{ | ||
public string Render(string text) | ||
{ | ||
var textLines = text.Split(Environment.NewLine).ToList(); | ||
var sb = new StringBuilder(); | ||
|
||
foreach (var line in textLines) | ||
{ | ||
sb.Append(RenderCurrentString(line)); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
private string RenderCurrentString(string line) | ||
{ | ||
var tokenizer = new Tokenizer(line); | ||
return GenerateHtml(line, tokenizer.TokenizeLine()); | ||
} | ||
|
||
private string GenerateHtml(string line, List<ITokenPosition> Tokens) | ||
{ | ||
// TODO some logic | ||
throw new NotImplementedException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
using Markdown; | ||
|
||
Md md = new Md(); | ||
md.Render("text \n\n\n text123"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Markdown.Tokens; | ||
|
||
namespace Markdown; | ||
|
||
public class Tokenizer | ||
{ | ||
private Stack<BoldToken> boldTokens; | ||
private Stack<ItalicToken> italicTokens; | ||
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 Tokenizer(string line) | ||
{ | ||
// TODO some logic | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public List<ITokenPosition> TokenizeLine() | ||
{ | ||
// TODO some logic | ||
throw new NotImplementedException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Markdown.Tokens; | ||
|
||
public class BoldToken : PairToken | ||
{ | ||
public override string MdView => "__"; | ||
public override string HtmlTagOpen => "<strong>"; | ||
public override string HtmlTagClose => "</strong>"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Markdown.Tokens; | ||
|
||
public class EscapeToken : SingleToken | ||
{ | ||
public override string MdView => """\"""; | ||
public override string HtmlTagOpen => ""; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Markdown.Tokens; | ||
|
||
public class HeaderToken : PairToken | ||
{ | ||
public override string MdView => "# "; | ||
public override string HtmlTagOpen => "<h1>"; | ||
public override string HtmlTagClose => "</h1>"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Markdown.Tokens; | ||
|
||
public interface ITokenPosition | ||
{ | ||
public string MdView { get; } | ||
public int Position { get; set; } | ||
public string HtmlView { get; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Markdown.Tokens; | ||
|
||
public class ItalicToken : PairToken | ||
{ | ||
public override string MdView => "_"; | ||
public override string HtmlTagOpen => "<em>"; | ||
public override string HtmlTagClose => "</em>"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Markdown.Tokens; | ||
|
||
public abstract class PairToken : ITokenPosition | ||
{ | ||
public abstract string MdView { get; } | ||
public abstract string HtmlTagOpen { get; } | ||
public abstract string HtmlTagClose { get; } | ||
public string HtmlView => IsClosed ? HtmlTagOpen : HtmlTagClose; | ||
|
||
public bool IsClosed { get; set; } | ||
public int Position { get; 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. Я специально не разделял тэг и токен, потому что я вижу работу парсера так: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Markdown.Tokens; | ||
|
||
public abstract class SingleToken : ITokenPosition | ||
{ | ||
public abstract string MdView { get; } | ||
public abstract string HtmlTagOpen { get; } | ||
|
||
public string HtmlView => HtmlTagOpen; | ||
public int Position { get; set; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace MarkdownTests | ||
{ | ||
public class Tests | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
} | ||
|
||
[Test] | ||
public void Test1() | ||
{ | ||
Assert.Pass(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Markdown\Markdown.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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.
У тебя пока не написана логика, поэтому можешь сказать своё видение, какие токены вернуться для этой строки:
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.
В начале строки мы видим "# " поэтому добавляется HeaderToken(HtmlView = <h1>), потом "_" добавляет ItalicToken(HtmlView = <em>), после мы видим "_", но уже закрывающий, благодаря стэкам мы понимаем какой это тип(закрывающий или открывающий), поэтому добавиться ItalicToken(HtmlView = </em>) и наконец мы дойдём до конца строки, поскольку в стэке будет открывающий HeaderToken, то необходимо добавить HeaderToken(HtmlView = </h1>).
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.
Так-с, т.е. в этой строке нам придет список токенов, условно говоря, "какой тэг и в какой позиции".
Я вижу тут 2 проблемы: