-
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
Мажирин Александр #237
base: master
Are you sure you want to change the base?
Мажирин Александр #237
Changes from 30 commits
913f25a
972bcab
8eabce6
753c342
305ed01
5990650
07b89a5
c419829
4db979e
0082678
e060481
137ccca
98ca9b2
6d1085b
a047313
e1eeee1
ee313cd
3868044
ae342db
37fd3d6
950b252
1cbfbb7
5ad3700
3c126af
ff94e0c
9e7a71b
286d18c
4a5ea48
a3b7e56
492d7e4
6d360a0
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,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--; | ||
} | ||
|
||
return backslashCount % 2 == 1; | ||
} | ||
} |
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> |
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); | ||
} |
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); | ||
} | ||
} |
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)); | ||
} | ||
} |
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) | ||
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 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(); | ||
} | ||
} |
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); | ||
} |
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; | ||
} | ||
} |
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace Markdown.Tags; | ||
|
||
/// <summary> | ||
/// Интерфейс тега | ||
/// </summary> | ||
public interface ITag | ||
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. На мой взгляд тэг - это скорее абстрактный класс с идейной точки зрения |
||
{ | ||
/// <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); | ||
} |
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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); | ||
} |
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.
Потенциально превратит обработку строки в квадратичную сложность 😨
Для подобного анализа лучше всего подходит ДКА