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

Logic Scheme #239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions homework/MarkDownConverter/Enums/NodeState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownConverter.Enums
{
public enum NodeState
{
Bold,
Italic,
Heading,
Symbols,
Root
}
}
19 changes: 19 additions & 0 deletions homework/MarkDownConverter/Enums/TokenState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownConverter.Enums
{
public enum TokenState
{
Space,
NewLine,
Numbers,
Symbols,
Italic,
Bold,
Heading,
}
}
14 changes: 14 additions & 0 deletions homework/MarkDownConverter/Interfaces/IParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using MarkDownConverter.Nodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownConverter.Interfaces
{
public interface IParser
{
public ParentNode Parse(List<IToken> tokens);
}
}
18 changes: 18 additions & 0 deletions homework/MarkDownConverter/Interfaces/IToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using MarkDownConverter.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownConverter.Interfaces
{
public interface IToken
{
public TokenState State { get; }
public int Position { get; }
public int Length { get; }
public string Value { get; }
bool Is(TokenState state);
}
}
13 changes: 13 additions & 0 deletions homework/MarkDownConverter/Interfaces/ITokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownConverter.Interfaces
{
public interface ITokenizer
{
public List<IToken> Tokenize(string text);
}
}
68 changes: 68 additions & 0 deletions homework/MarkDownConverter/MarkDownConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using MarkDownConverter.Interfaces;
using MarkDownConverter.Nodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownConverter
{
public class MarkDownConverter(ITokenizer tokenizer, IParser parser)
{
public ITokenizer Tokenizer { get; } = tokenizer;
public IParser Parser { get; } = parser;

public string Convert(string input)
{
var tokens = Tokenizer.Tokenize(input);
var node = Parser.Parse(tokens);
return ConvertNodeToHtml(node);
}

private string ConvertNodeToHtml(ParentNode node)
{
var output = new StringBuilder();
CreateHtml(node, output);
return output.ToString();
}

private void CreateHtml(Node node, StringBuilder html)
{
switch (node)
{
case ParentNode patent:
{
foreach (var child in patent.Children)
CreateHtml(child, html);
break;
}

case HeadingNode headingNode:
html.Append("<h1>");
foreach (var child in headingNode.Children)
CreateHtml(child, html);
html.Append("</h1>");
break;

case SymbolsNode textNode:
html.Append(textNode.Value);
break;

case ItalicNode italicNode:
html.Append("<em>");
foreach (var child in italicNode.Children)
CreateHtml(child, html);
html.Append("</em>");
break;

case BoldNode boldNode:
html.Append("<strong>");
foreach (var child in boldNode.Children)
CreateHtml(child, html);
html.Append("</strong>");
break;
}
}
}
}
10 changes: 10 additions & 0 deletions homework/MarkDownConverter/MarkDownConverter.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>
31 changes: 31 additions & 0 deletions homework/MarkDownConverter/MarkDownConverter.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35303.130
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MarkDownConverter", "MarkDownConverter.csproj", "{F6D525F6-0754-45FD-86BF-4677B7432233}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarkDownConverterTests", "..\MarkDownConverterTests\MarkDownConverterTests.csproj", "{6B32DAC7-FA8A-473E-BDF9-3FAD9A28F82E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F6D525F6-0754-45FD-86BF-4677B7432233}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6D525F6-0754-45FD-86BF-4677B7432233}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6D525F6-0754-45FD-86BF-4677B7432233}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6D525F6-0754-45FD-86BF-4677B7432233}.Release|Any CPU.Build.0 = Release|Any CPU
{6B32DAC7-FA8A-473E-BDF9-3FAD9A28F82E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B32DAC7-FA8A-473E-BDF9-3FAD9A28F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B32DAC7-FA8A-473E-BDF9-3FAD9A28F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B32DAC7-FA8A-473E-BDF9-3FAD9A28F82E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B8C09EC2-68BD-444E-9228-8D75F49B7190}
EndGlobalSection
EndGlobal
Loading