Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 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
13 changes: 13 additions & 0 deletions cs/MarkupConverter/AST/Blocks/Block.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MarkupConverter.AST.Inlines;

namespace MarkupConverter.AST.Blocks;

public abstract class Block : Node
{
public List<Inline> Inlines { get; }

protected Block(List<Inline> inlines)
{
Inlines = inlines;
}
}
11 changes: 11 additions & 0 deletions cs/MarkupConverter/AST/Blocks/BlockContainerNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MarkupConverter.AST.Blocks;

public abstract class BlockContainerNode
{
public List<Block> Blocks { get; }

protected BlockContainerNode(List<Block> blocks)
{
Blocks = blocks;
}
}
8 changes: 8 additions & 0 deletions cs/MarkupConverter/AST/Blocks/Document.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MarkupConverter.AST.Blocks;

public class Document : BlockContainerNode
{
public Document(List<Block> blocks) : base(blocks)
{
}
}
13 changes: 13 additions & 0 deletions cs/MarkupConverter/AST/Blocks/Header.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MarkupConverter.AST.Inlines;

namespace MarkupConverter.AST.Blocks;

public class Header : Block
{
public int Level { get; }

public Header(List<Inline> inlines, int level) : base(inlines)
{
Level = level;
}
}
10 changes: 10 additions & 0 deletions cs/MarkupConverter/AST/Blocks/Paragraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MarkupConverter.AST.Inlines;

namespace MarkupConverter.AST.Blocks;

public class Paragraph : Block
{
public Paragraph(List<Inline> inlines) : base(inlines)
{
}
}
11 changes: 11 additions & 0 deletions cs/MarkupConverter/AST/Inlines/Bold.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MarkupConverter.AST.Inlines;

public class Bold : Inline
{
public List<Inline> Inlines { get; }

public Bold(List<Inline> inlines)
{
Inlines = inlines;
}
}
3 changes: 3 additions & 0 deletions cs/MarkupConverter/AST/Inlines/Inline.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace MarkupConverter.AST.Inlines;

public abstract class Inline : Node;
3 changes: 3 additions & 0 deletions cs/MarkupConverter/AST/Inlines/InlineLeaf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace MarkupConverter.AST.Inlines;

public abstract class InlineLeaf : Inline;
11 changes: 11 additions & 0 deletions cs/MarkupConverter/AST/Inlines/Italic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MarkupConverter.AST.Inlines;

public class Italic : InlineLeaf
{
public List<Inline> InlineLeaves { get; }

public Italic(List<Inline> inlineLeaves)
{
InlineLeaves = inlineLeaves;
}
}
11 changes: 11 additions & 0 deletions cs/MarkupConverter/AST/Inlines/Text.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MarkupConverter.AST.Inlines;

public class Text : InlineLeaf
{
public string Content { get; }

public Text(string content)
{
Content = content;
}
}
3 changes: 3 additions & 0 deletions cs/MarkupConverter/AST/Node.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace MarkupConverter.AST;

public class Node;
24 changes: 24 additions & 0 deletions cs/MarkupConverter/MarkupConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using MarkupConverter.Parsers;
using MarkupConverter.Renderers;

namespace MarkupConverter;

public class MarkupConverter
{
private readonly IParser parser;
private readonly IRenderer renderer;

public MarkupConverter(IParser parser, IRenderer renderer)
{
this.parser = parser;
this.renderer = renderer;
}

public string Convert(string text)
{
var ast = parser.Parse(text);
var markup = renderer.Render(ast);

return markup;
}
}
9 changes: 9 additions & 0 deletions cs/MarkupConverter/MarkupConverter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
34 changes: 34 additions & 0 deletions cs/MarkupConverter/Parsers/BlockParsers/HeaderParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using MarkupConverter.Parsers.BlockParsers.OpenBlocks;

namespace MarkupConverter.Parsers.BlockParsers;

public class HeaderParser : IBlockParser
{
public bool CanParse(string line)
{
var trimmed = line.TrimStart();
var headingLevel = CountHeadingLevel(trimmed);

return headingLevel is >= 1 and <= 6
&& (headingLevel == trimmed.Length || char.IsWhiteSpace(trimmed[headingLevel]));
}

public IOpenBlock Parse(string line)
{
var trimmed = line.TrimStart();
var headingLevel = CountHeadingLevel(trimmed);
var content = trimmed.Substring(headingLevel + 1).Trim();

return new HeaderOpenBlock(content, headingLevel);
}

private static int CountHeadingLevel(string line)
{
var level = 0;
foreach (var c in line)
if (c == '#') level++;
else break;

return level;
}
}
9 changes: 9 additions & 0 deletions cs/MarkupConverter/Parsers/BlockParsers/IBlockParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MarkupConverter.Parsers.BlockParsers.OpenBlocks;

namespace MarkupConverter.Parsers.BlockParsers;

public interface IBlockParser
{
bool CanParse(string line);
IOpenBlock Parse(string line);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using MarkupConverter.AST.Blocks;
using MarkupConverter.Parsers.InlineParsers;

namespace MarkupConverter.Parsers.BlockParsers.OpenBlocks;

public class HeaderOpenBlock : IOpenBlock
{
private readonly int level;
public string Content { get; }
public Type BlockType => typeof(Header);

public HeaderOpenBlock(string content, int level)
{
Content = content;
this.level = level;
}

public bool CanAccept(IOpenBlock block)
{
return false;
}

public void Accept(IOpenBlock block)
{
}

public Block Close(IInlineParser parser)
{
return new Header(parser.Parse(Content), level);
}
}
13 changes: 13 additions & 0 deletions cs/MarkupConverter/Parsers/BlockParsers/OpenBlocks/IOpenBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MarkupConverter.AST.Blocks;
using MarkupConverter.Parsers.InlineParsers;

namespace MarkupConverter.Parsers.BlockParsers.OpenBlocks;

public interface IOpenBlock
{
string Content { get; }
Type BlockType { get; }
bool CanAccept(IOpenBlock block);
void Accept(IOpenBlock block);
Block Close(IInlineParser parser);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using MarkupConverter.AST.Blocks;
using MarkupConverter.Parsers.InlineParsers;

namespace MarkupConverter.Parsers.BlockParsers.OpenBlocks;

public class ParagraphOpenBlock : IOpenBlock
{
public string Content { get; private set; }
public Type BlockType => typeof(Paragraph);

public ParagraphOpenBlock(string content)
{
Content = content;
}

public bool CanAccept(IOpenBlock block)
{
return block.BlockType == BlockType;
}

public void Accept(IOpenBlock block)
{
Content += ' ' + block.Content;
}

public Block Close(IInlineParser parser)
{
return new Paragraph(parser.Parse(Content));
}
}
18 changes: 18 additions & 0 deletions cs/MarkupConverter/Parsers/BlockParsers/ParagraphParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using MarkupConverter.Parsers.BlockParsers.OpenBlocks;

namespace MarkupConverter.Parsers.BlockParsers;

public class ParagraphParser : IBlockParser
{
public bool CanParse(string line)
{
return !string.IsNullOrWhiteSpace(line);
}

public IOpenBlock Parse(string line)
{
var content = line.Trim();

return new ParagraphOpenBlock(content);
}
}
8 changes: 8 additions & 0 deletions cs/MarkupConverter/Parsers/IParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MarkupConverter.AST.Blocks;

namespace MarkupConverter.Parsers;

public interface IParser
{
public Document Parse(string text);
}
11 changes: 11 additions & 0 deletions cs/MarkupConverter/Parsers/InlineParsers/Delimiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MarkupConverter.Parsers.InlineParsers;

public class Delimiter
{
public int NodeIndex;
public int Length;
public bool CanOpen;
public bool CanClose;
public bool IsInsideWord;
public bool Matched;
}
8 changes: 8 additions & 0 deletions cs/MarkupConverter/Parsers/InlineParsers/IInlineParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MarkupConverter.AST.Inlines;

namespace MarkupConverter.Parsers.InlineParsers;

public interface IInlineParser
{
public List<Inline> Parse(string text);
}
Loading