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

Брозовский Максим #234

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions cs/Markdown/HtmlConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Markdown.Tokens;

namespace Markdown;

public class HtmlConverter : IConverter
{
public string Convert(List<IToken> tokens) => throw new NotImplementedException();
}
8 changes: 8 additions & 0 deletions cs/Markdown/IConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Markdown.Tokens;

namespace Markdown;

public interface IConverter

Choose a reason for hiding this comment

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

Конвертер чего?

{
string Convert(List<IToken> tokens);
}
10 changes: 10 additions & 0 deletions cs/Markdown/Markdown.csproj
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>
13 changes: 13 additions & 0 deletions cs/Markdown/Md.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Markdown.Tags;

namespace Markdown;

public static class Md
{
private static readonly IEnumerable<ITag> Tags = [new BoldTag(), new HeaderTag(), new ItalicTag()];
public static string Render(string markdownText)
{
var tokens = new Tokenizer(Tags).Tokenize(markdownText);
return new HtmlConverter().Convert(tokens);
}
}
9 changes: 9 additions & 0 deletions cs/Markdown/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Markdown;

class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
7 changes: 7 additions & 0 deletions cs/Markdown/Tags/BoldTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Tags;

public class BoldTag : PairTag, ITag
{
public override string MdTag => "__";
public override string HtmlTag => "<strong>";
}
7 changes: 7 additions & 0 deletions cs/Markdown/Tags/HeaderTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Tags;

public class HeaderTag : SingleTag, ITag
{
public override string MdTag => "#";
public override string HtmlTag => "<h1>";
}
10 changes: 10 additions & 0 deletions cs/Markdown/Tags/ITag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Markdown.Tags;

public interface ITag
{
string MdTag { get; }
string HtmlTag { get; }

bool IsOpenedCorrectly((char left, char right) contextChars);
bool IsClosedCorrectly((char left, char right) contextChars);

Choose a reason for hiding this comment

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

Выдели мб отдельную модельку под это? Таплы передавать публично плохая практика. У тебя уже два места, где одна и та же модель используется. При изменении одно из этих мест надо отдельно помнить и о другом

}
9 changes: 9 additions & 0 deletions cs/Markdown/Tags/ItalicTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Markdown.Tags;

public class ItalicTag : PairTag, ITag

Choose a reason for hiding this comment

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

А зачем интерфейс еще раз указывать?

{
public override string MdTag => "_";
public override string HtmlTag => "<em>";

protected override IEnumerable<ITag> ForbiddenInside => [new BoldTag()];
}
12 changes: 12 additions & 0 deletions cs/Markdown/Tags/PairTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Markdown.Tags;

public abstract class PairTag : ITag

Choose a reason for hiding this comment

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

Чет не отразил чем отличается от одинарного

Choose a reason for hiding this comment

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

И как определяем закрываюищий тег?

Copy link
Author

@BMV989 BMV989 Nov 25, 2024

Choose a reason for hiding this comment

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

для MdTag добавил, а HtmlTag там симметричный, т.е. <HtmlTag></HtmlTag>

Copy link
Author

@BMV989 BMV989 Nov 25, 2024

Choose a reason for hiding this comment

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

Чет не отразил чем отличается от одинарного

Главное отличие в том, что у одинарного не будет закрвающего MdTag и он действует на всю строчку, а для HtmlTag мы делаем как в коментарии выше - симметрично

Copy link
Author

Choose a reason for hiding this comment

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

Но там тоже могут быть особые случаи типа <img> тега....

Copy link
Author

Choose a reason for hiding this comment

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

но мне кажется это уже отвественность IMdConverter, как он будет эти токены укладывать в строку

{
public abstract string MdTag { get; }
public abstract string HtmlTag { get; }

public virtual bool IsOpenedCorrectly((char left, char right) contextChars) => contextChars.left != ' ';
public virtual bool IsClosedCorrectly((char left, char right) contextChars) => contextChars.right != ' ';

protected virtual IEnumerable<ITag> ForbiddenInside => [];
}
10 changes: 10 additions & 0 deletions cs/Markdown/Tags/SingleTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Markdown.Tags;

public abstract class SingleTag : ITag
{
public abstract string MdTag { get; }
public abstract string HtmlTag { get; }

public virtual bool IsOpenedCorrectly((char left, char right) contextChars) => contextChars.left == '\n';
public bool IsClosedCorrectly((char left, char right) contextChars) => true;
}
7 changes: 7 additions & 0 deletions cs/Markdown/Tags/TagStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Tags;

public enum TagStatus
{
Opened,
Closed
}
9 changes: 9 additions & 0 deletions cs/Markdown/Tokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Markdown.Tags;
using Markdown.Tokens;

namespace Markdown;

public class Tokenizer(IEnumerable<ITag> tags)
{
public List<IToken> Tokenize(string markdownText) => throw new NotImplementedException();
}
7 changes: 7 additions & 0 deletions cs/Markdown/Tokens/EscapeToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Tokens;

public class EscapeToken : IToken
{
public string Value => "\\";
public int Length => 1;
}
7 changes: 7 additions & 0 deletions cs/Markdown/Tokens/IToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Tokens;

public interface IToken
{
string Value { get; }
int Length { get; }
}
7 changes: 7 additions & 0 deletions cs/Markdown/Tokens/NewLineToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Tokens;

public class NewLineToken : IToken
{
public string Value => "\n";
public int Length => 1;
}
13 changes: 13 additions & 0 deletions cs/Markdown/Tokens/TagToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Markdown.Tags;

namespace Markdown.Tokens;

public class TagToken(ITag tag, char left, char right) : IToken

Choose a reason for hiding this comment

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

А почему char? Например, __ - это два подчеркивания.

{
public ITag Tag => tag;
public TagStatus Status { get; set; }
// TODO: we should get value of tag by his status?
public string Value => throw new NotImplementedException();
public int Length => Tag.MdTag.Length;
public (char left, char right) ContextChars => (left, right);
}
7 changes: 7 additions & 0 deletions cs/Markdown/Tokens/TextToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Tokens;

public class TextToken(string value) : IToken
{
public string Value => value;
public int Length => Value.Length;
}
29 changes: 29 additions & 0 deletions cs/MarkdownTests/MarkdownTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<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="FluentAssertions" Version="6.12.2" />
<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>
<Using Include="NUnit.Framework"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Markdown\Markdown.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions cs/clean-code.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ControlDigit", "ControlDigi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples", "Samples\Samples.csproj", "{C3EF41D7-50EF-4CE1-B30A-D1D81C93D7FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Markdown", "Markdown\Markdown.csproj", "{71910566-C5DE-4C56-93E4-16976D5D4FC6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarkdownTests", "MarkdownTests\MarkdownTests.csproj", "{742ED593-BF23-4F69-BD7C-255777424108}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,5 +31,13 @@ Global
{C3EF41D7-50EF-4CE1-B30A-D1D81C93D7FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3EF41D7-50EF-4CE1-B30A-D1D81C93D7FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3EF41D7-50EF-4CE1-B30A-D1D81C93D7FA}.Release|Any CPU.Build.0 = Release|Any CPU
{71910566-C5DE-4C56-93E4-16976D5D4FC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71910566-C5DE-4C56-93E4-16976D5D4FC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71910566-C5DE-4C56-93E4-16976D5D4FC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71910566-C5DE-4C56-93E4-16976D5D4FC6}.Release|Any CPU.Build.0 = Release|Any CPU
{742ED593-BF23-4F69-BD7C-255777424108}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{742ED593-BF23-4F69-BD7C-255777424108}.Debug|Any CPU.Build.0 = Debug|Any CPU
{742ED593-BF23-4F69-BD7C-255777424108}.Release|Any CPU.ActiveCfg = Release|Any CPU
{742ED593-BF23-4F69-BD7C-255777424108}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
3 changes: 3 additions & 0 deletions cs/clean-code.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=TypesAndNamespaces/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=4a98fdf6_002D7d98_002D4f5a_002Dafeb_002Dea44ad98c70c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a0b4bc4d_002Dd13b_002D4a37_002Db37e_002Dc9c6864e4302/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Types and namespaces"&gt;&lt;ElementKinds&gt;&lt;Kind Name="NAMESPACE" /&gt;&lt;Kind Name="CLASS" /&gt;&lt;Kind Name="STRUCT" /&gt;&lt;Kind Name="ENUM" /&gt;&lt;Kind Name="DELEGATE" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EPredefinedNamingRulesToUserRulesUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=13FAEF5AD10371439A650EAFEB60E612/@KeyIndexDefined">True</s:Boolean>
<s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=13FAEF5AD10371439A650EAFEB60E612/Applicability/=Live/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=13FAEF5AD10371439A650EAFEB60E612/Categories/=Imported_002010_002E10_002E2016/@EntryIndexedValue">Imported 10.10.2016</s:String>
Expand Down