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

Мажирин Александр #237

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
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
41 changes: 41 additions & 0 deletions cs/Markdown/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Markdown.Extensions;

public static class StringExtensions
{
/// <summary>
/// Производит проверку наличия строки в строке на позиции position без копирования
/// </summary>
/// <param name="origin">Исходная строка</param>
/// <param name="substring">Проверяемая строка</param>
/// <param name="position">Позиция в исходной строке</param>
/// <returns>True, если строка содержится в исходной строке на позиции position, иначе false</returns>
public static bool ContainsSubstringOnIndex(this string origin, string substring, int position)
{
for (var j = 0; j < substring.Length; j++)
{
if (position + j >= origin.Length || origin[position + j] != substring[j])
return false;
}

return true;
}

/// <summary>
/// Проверяет, является ли символ экранированным
/// </summary>
/// <param name="str">Строка</param>
/// <param name="position">Индекс символа</param>
/// <returns>True, если символ экранирован, иначе false</returns>
public static bool IsEscaped(this string str, int position)
{
var previousIndex = position - 1;
var backslashCount = 0;
while (previousIndex >= 0 && str[previousIndex] == '\\')
{
backslashCount++;
previousIndex--;
}
Comment on lines +33 to +37

Choose a reason for hiding this comment

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

Потенциально превратит обработку строки в квадратичную сложность 😨
Для подобного анализа лучше всего подходит ДКА


return backslashCount % 2 == 1;
}
}
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</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
9 changes: 9 additions & 0 deletions cs/Markdown/Markdown/IMd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Markdown.Markdown;

/// <summary>
/// Интерфейс для конвертера Markdown в HTML
/// </summary>
public interface IMd
{
string Render(string md);
}
17 changes: 17 additions & 0 deletions cs/Markdown/Markdown/Md.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Markdown.Renderer;
using Markdown.Tokenizer;

namespace Markdown.Markdown;

/// <summary>
/// Конвертер markdown в HTML
/// </summary>
public class Md : IMd
{
private readonly IRenderer renderer = new HtmlRenderer();
public string Render(string md)
{
var tokens = new MarkdownTokenizer().Tokenize(md);
return renderer.Render(tokens);
}
}
14 changes: 14 additions & 0 deletions cs/Markdown/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Markdown.Markdown;

namespace Markdown;

class Program
{
public static void Main()
{
var mdFile = File.ReadAllText("Markdown.md");
var md = new Md();
//write to file
File.WriteAllText("Markdown.html", md.Render(mdFile));
}
}
58 changes: 58 additions & 0 deletions cs/Markdown/Renderer/HtmlRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Text;
using System.Web;
using Markdown.Tokenizer;
using Markdown.Tokenizer.Tokens;

namespace Markdown.Renderer;

/// <summary>
/// HTML-рендерер. Преобразует токены в HTML-текст, экранируя спецсимволы в тексте
/// </summary>
public class HtmlRenderer : IRenderer
{
public string Render(IEnumerable<IToken> tokens)

Choose a reason for hiding this comment

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

Если делать прямо по честному дерево, то оно должно лежать в одном корне. Но так тоже можно

{
var sb = new StringBuilder();
foreach (var token in tokens)
{
sb.Append(RenderToken(token));
}

return sb.ToString();
}

private string? RenderToken(IToken token)
{
return token switch
{
TextToken textToken => HttpUtility.HtmlEncode(textToken.TextContent),
TagToken tagToken => RenderTagToken(tagToken),
_ => null
};
}

private string RenderTagToken(TagToken tagToken)
{
var sb = new StringBuilder();
sb.Append($"<{tagToken.Tag.HtmlTag}");
foreach (var (key, value) in tagToken.Attributes)
{
sb.Append($" {key}=\"{value}\"");
}

if (tagToken.Tag.SelfClosing)
{
sb.Append(" />");
return sb.ToString();
}

sb.Append('>');
foreach (var child in tagToken.Children)
{
sb.Append(RenderToken(child));
}

sb.Append($"</{tagToken.Tag.HtmlTag}>");
return sb.ToString();
}
}
17 changes: 17 additions & 0 deletions cs/Markdown/Renderer/IRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Markdown.Tokenizer;
using Markdown.Tokenizer.Tokens;

namespace Markdown.Renderer;

/// <summary>
/// Универсальный интерфейс рендерера
/// </summary>
public interface IRenderer
{
/// <summary>
/// Переводит набор токенов в текст языка разметки
/// </summary>
/// <param name="tokens">Набор токенов</param>
/// <returns>Сгенерированный текст</returns>
string Render(IEnumerable<IToken> tokens);
}
20 changes: 20 additions & 0 deletions cs/Markdown/Tags/CursiveTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Markdown.Tags;

/// <summary>
/// Тег для курсивного текста
/// </summary>
public class CursiveTag : ITag
{
public string MdTag { get; } = "_";

public string MdClosingTag => MdTag;

public string HtmlTag { get; } = "em";

public IReadOnlyCollection<ITag> DisallowedChildren { get; } = new List<ITag> { new StrongTag() };

public bool Matches(ITag tag)
{
return tag is CursiveTag;
}
}
17 changes: 17 additions & 0 deletions cs/Markdown/Tags/HeaderTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Markdown.Tags;

/// <summary>
/// Тег для заголовка. Может быть использован только в начале строки
/// </summary>
public class HeaderTag : ITag
{
public string MdTag { get; } = "#";
public string MdClosingTag { get; } = "\n";
public string HtmlTag { get; } = "h1";
public IReadOnlyCollection<ITag> DisallowedChildren { get; } = new List<ITag>();

public bool Matches(ITag tag)
{
return tag is HeaderTag;
}
}
47 changes: 47 additions & 0 deletions cs/Markdown/Tags/ITag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Markdown.Tags;

/// <summary>
/// Интерфейс тега
/// </summary>
public interface ITag

Choose a reason for hiding this comment

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

На мой взгляд тэг - это скорее абстрактный класс с идейной точки зрения

{
/// <summary>
/// Тег, который используется в Markdown
/// </summary>
string MdTag { get; }

/// <summary>
/// Закрывающий тег в Markdown
/// </summary>
string MdClosingTag { get; }

/// <summary>
/// Тег в HTML, соответсвующий тегу Markdown
/// </summary>
/// <see cref="MdTag"/>
string HtmlTag { get; }

/// <summary>
/// Самозакрывание тега в HTML
/// </summary>
bool SelfClosing => false;

/// <summary>
/// Запрет на вложение дочерних элементов определенного типа
/// </summary>
IReadOnlyCollection<ITag> DisallowedChildren { get; }

/// <summary>
/// Получить атрибуты для рендера в HTML
/// </summary>
/// <param name="tagContents">Содержание тега</param>
/// <returns>Строка с атрибутами для вставки в тег</returns>
static Dictionary<string, string> GetHtmlTadAttributes(string tagContents) => new Dictionary<string, string>() { };

/// <summary>
/// Проверка, что тег соответствует переданному тегу
/// </summary>
/// <param name="tag">Тег для сравнения</param>
/// <returns></returns>
bool Matches(ITag tag);
}
39 changes: 39 additions & 0 deletions cs/Markdown/Tags/ImageTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Markdown.Tags;

/// <summary>
/// Тег для картинки
/// </summary>
public class ImageTag : ITag
{
public string MdTag { get; } = "![";

public string MdClosingTag { get; } = ")";

public string HtmlTag { get; } = "img";

public IReadOnlyCollection<ITag> DisallowedChildren => new List<ITag>()
{ new CursiveTag(), new HeaderTag(), new ImageTag(), new StrongTag() };

public bool SelfClosing { get; } = false;

public static Dictionary<string, string> GetHtmlTadAttributes(string tagContents)
{
var attributes = new Dictionary<string, string>();

var altStart = tagContents.IndexOf("![", StringComparison.Ordinal) + 2;
var altEnd = tagContents.IndexOf(']', altStart);
var srcStart = tagContents.IndexOf('(', altEnd) + 1;
var srcEnd = tagContents.IndexOf(')', srcStart);

if (altStart < 2 || altEnd <= altStart || srcStart <= altEnd || srcEnd <= srcStart) return attributes;
attributes["alt"] = tagContents.Substring(altStart, altEnd - altStart);
attributes["src"] = tagContents.Substring(srcStart, srcEnd - srcStart);

return attributes;
}

public bool Matches(ITag tag)
{
return tag is ImageTag;
}
}
18 changes: 18 additions & 0 deletions cs/Markdown/Tags/NewLineTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Markdown.Tags;

/// <summary>
/// Тег для переноса строки
/// </summary>
public class NewLineTag : ITag
{
public string MdTag { get; } = "\n";
public string MdClosingTag { get; } = null;
public string HtmlTag { get; } = "br";
public bool SelfClosing { get; } = true;
public IReadOnlyCollection<ITag> DisallowedChildren { get; } = new List<ITag>();

public bool Matches(ITag tag)
{
return tag is NewLineTag;
}
}
20 changes: 20 additions & 0 deletions cs/Markdown/Tags/StrongTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Markdown.Tags;

/// <summary>
/// Тег для полужирного текста
/// </summary>
public class StrongTag : ITag
{
public string MdTag { get; } = "__";

public string MdClosingTag => MdTag;

public string HtmlTag { get; } = "strong";

public IReadOnlyCollection<ITag> DisallowedChildren { get; } = new List<ITag>();

public bool Matches(ITag tag)
{
return tag is StrongTag;
}
}
11 changes: 11 additions & 0 deletions cs/Markdown/Tokenizer/ITokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Markdown.Tokenizer.Tokens;

namespace Markdown.Tokenizer;

/// <summary>
/// Интерфейс токенайзера - переводчика строки в токены
/// </summary>
public interface ITokenizer
{
public List<IToken> Tokenize(string content);
}
Loading