forked from kontur-courses/clean-code
-
Notifications
You must be signed in to change notification settings - Fork 1
PR #1
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
Open
Griboedoff
wants to merge
17
commits into
To-PR
Choose a base branch
from
master
base: To-PR
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
PR #1
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fd09786
initial
1957c0c
add partial parse for em tag
bcddd09
обновил спеку
0e02fb4
закончил
9de2d0d
fix review
917b8a6
add test for tags inside
8d9b3e0
fix parseTag
eb28b3a
remove try-catch
a637fac
add performance test
01ac99c
start 2 task
4749f92
add simple url parsing
6d74b92
add css and building urls
afe8c9b
add paragraphs parsing
aabcc15
add header parsing
b4292c3
parce code blocks
05b8c35
add list parsing
842986f
fix p tag in all test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace Markdown | ||
| { | ||
| public class HtmlToken | ||
| { | ||
| private readonly Tag tag; | ||
| private readonly List<HtmlToken> parsedTokens; | ||
| private readonly string data; | ||
| private bool IsTagged => !tag.Equals(Tag.Empty); | ||
| public int Length => parsedTokens.Sum(x => x.Length) + (data ?? "").Length + escapedCharacters + tag.Md.Length * 2; | ||
| private readonly int escapedCharacters; | ||
|
|
||
| public HtmlToken(Tag tag, string data, int escapedCharacters) | ||
| { | ||
| this.tag = tag; | ||
| this.data = data; | ||
| this.escapedCharacters = escapedCharacters; | ||
| parsedTokens = new List<HtmlToken>(); | ||
| } | ||
|
|
||
| public HtmlToken(Tag tag, List<HtmlToken> parsedTokens, int escapedCharacters) | ||
| { | ||
| this.tag = tag; | ||
| this.parsedTokens = parsedTokens; | ||
| this.escapedCharacters = escapedCharacters; | ||
| } | ||
|
|
||
| private string InsertInToTags(string dataToInsert) => IsTagged | ||
| ? $"<{tag.Html}>{dataToInsert}</{tag.Html}>" | ||
| : dataToInsert; | ||
|
|
||
| public override string ToString() | ||
| { | ||
| return parsedTokens.Count > 0 | ||
| ? InsertInToTags(string.Join("", parsedTokens.Select(token => token.ToString()))) | ||
| : InsertInToTags(data); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
| <PropertyGroup> | ||
| <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
| <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
| <ProjectGuid>{E6825F19-EE20-40A7-AE68-22051E2AF040}</ProjectGuid> | ||
| <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | ||
| <OutputType>Exe</OutputType> | ||
| <AppDesignerFolder>Properties</AppDesignerFolder> | ||
| <RootNamespace>Markdown</RootNamespace> | ||
| <AssemblyName>Markdown</AssemblyName> | ||
| <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
| <FileAlignment>512</FileAlignment> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
| <PlatformTarget>AnyCPU</PlatformTarget> | ||
| <DebugSymbols>true</DebugSymbols> | ||
| <DebugType>full</DebugType> | ||
| <Optimize>false</Optimize> | ||
| <OutputPath>bin\Debug\</OutputPath> | ||
| <DefineConstants>DEBUG;TRACE</DefineConstants> | ||
| <ErrorReport>prompt</ErrorReport> | ||
| <WarningLevel>4</WarningLevel> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
| <PlatformTarget>AnyCPU</PlatformTarget> | ||
| <DebugType>pdbonly</DebugType> | ||
| <Optimize>true</Optimize> | ||
| <OutputPath>bin\Release\</OutputPath> | ||
| <DefineConstants>TRACE</DefineConstants> | ||
| <ErrorReport>prompt</ErrorReport> | ||
| <WarningLevel>4</WarningLevel> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Reference Include="FluentAssertions, Version=4.16.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a"> | ||
| <HintPath>..\packages\FluentAssertions.4.16.0\lib\net45\FluentAssertions.dll</HintPath> | ||
| </Reference> | ||
| <Reference Include="FluentAssertions.Core, Version=4.16.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a"> | ||
| <HintPath>..\packages\FluentAssertions.4.16.0\lib\net45\FluentAssertions.Core.dll</HintPath> | ||
| </Reference> | ||
| <Reference Include="nunit.framework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> | ||
| <HintPath>..\packages\NUnit.3.5.0\lib\net45\nunit.framework.dll</HintPath> | ||
| </Reference> | ||
| <Reference Include="System" /> | ||
| <Reference Include="System.Core" /> | ||
| <Reference Include="System.Xml.Linq" /> | ||
| <Reference Include="System.Data.DataSetExtensions" /> | ||
| <Reference Include="System.Data" /> | ||
| <Reference Include="System.Xml" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Compile Include="Md.cs" /> | ||
| <Compile Include="HtmlToken.cs" /> | ||
| <Compile Include="Program.cs" /> | ||
| <Compile Include="Tag.cs" /> | ||
| <Compile Include="Test\HtmlToken_Should.cs" /> | ||
| <Compile Include="Test\MD_Should.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Content Include="packages.config" /> | ||
| <Content Include="README.md" /> | ||
| <Content Include="Spec.md" /> | ||
| </ItemGroup> | ||
| <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
| <!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
| Other similar extension points exist, see Microsoft.Common.targets. | ||
| <Target Name="BeforeBuild"> | ||
| </Target> | ||
| <Target Name="AfterBuild"> | ||
| </Target> | ||
| --> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
||
| namespace Markdown | ||
| { | ||
| public class Md | ||
| { | ||
| private readonly string plainMd; | ||
|
|
||
| private readonly Dictionary<Tag, Func<int, string, int, HtmlToken>> mdTagParserFuncMatch; | ||
| private readonly Dictionary<Tag, Func<int, bool, bool>> validateFunctions; | ||
|
|
||
| public Md(string plainMd) | ||
| { | ||
| this.plainMd = plainMd; | ||
| mdTagParserFuncMatch = new Dictionary<Tag, Func<int, string, int, HtmlToken>> | ||
| { | ||
| [Tag.Em] = ParseEmToken, | ||
| [Tag.Empty] = ParseNoMarkup, | ||
| [Tag.Strong] = ParseStrongToken | ||
| }; | ||
| validateFunctions = new Dictionary<Tag, Func<int, bool, bool>> | ||
| { | ||
| [Tag.Em] = IsValidEmTag, | ||
| [Tag.Strong] = IsValidStrongTag, | ||
| [Tag.Empty] = (i, b) => false | ||
| }; | ||
| } | ||
|
|
||
| private HtmlToken ParseEmToken(int index, string alreadyParsed = "", int alreadyEscaped = 0) | ||
| { | ||
| if (!IsValidEmTag(index, true)) | ||
| return ParseNoMarkup(index); | ||
|
|
||
| index++; | ||
| var tokenData = new StringBuilder(alreadyParsed); | ||
|
|
||
| while (index < plainMd.Length && !IsValidEmTag(index, false)) | ||
| { | ||
| var tag = ParseTag(index); | ||
|
|
||
| if (tag.Equals(Tag.Strong)) | ||
| { | ||
| tokenData.Append("__"); | ||
| index += 2; | ||
| continue; | ||
| } | ||
|
|
||
| if (plainMd[index] == '\\') | ||
| { | ||
| index++; | ||
| alreadyEscaped++; | ||
| } | ||
|
|
||
| tokenData.Append(plainMd[index]); | ||
| index++; | ||
| } | ||
|
|
||
| return index != plainMd.Length | ||
| ? new HtmlToken(Tag.Em, tokenData.ToString(), alreadyEscaped) | ||
| : new HtmlToken(Tag.Empty, tokenData.Insert(0, '_').ToString(), alreadyEscaped); | ||
| } | ||
|
|
||
| private HtmlToken ParseStrongToken(int index, string alreadyParsed = "", int alreadyEscaped = 0) | ||
| { | ||
| if (!IsValidStrongTag(index, true)) | ||
| return ParseNoMarkup(index); | ||
|
|
||
| var parsedTokens = new List<HtmlToken>(); | ||
| index += 2; | ||
| var tokenData = new StringBuilder(alreadyParsed); | ||
|
|
||
| while (index < plainMd.Length && !IsValidStrongTag(index, false)) | ||
| { | ||
| var tag = ParseTag(index); | ||
|
|
||
| if (Equals(tag, Tag.Em)) | ||
| { | ||
| parsedTokens.Add(ParseEmInStrong(ref index, ref alreadyEscaped, parsedTokens, tokenData)); | ||
| if (index == plainMd.Length) | ||
| break; | ||
| } | ||
|
|
||
| if (plainMd[index] == '\\') | ||
| { | ||
| index++; | ||
| alreadyEscaped++; | ||
| } | ||
|
|
||
| tokenData.Append(plainMd[index]); | ||
| index++; | ||
| } | ||
|
|
||
| parsedTokens.Add(new HtmlToken(Tag.Empty, tokenData.ToString(), alreadyEscaped)); | ||
| return index != plainMd.Length | ||
| ? new HtmlToken(Tag.Strong, parsedTokens, 0) | ||
| : new HtmlToken(Tag.Empty, tokenData.Insert(0, "__").ToString(), alreadyEscaped); | ||
| } | ||
|
|
||
| private HtmlToken ParseEmInStrong(ref int index, ref int alreadyEscaped, | ||
| ICollection<HtmlToken> parsedTokens, StringBuilder tokenData) | ||
| { | ||
| parsedTokens.Add(new HtmlToken(Tag.Empty, tokenData.ToString(), alreadyEscaped)); | ||
| alreadyEscaped = 0; | ||
| tokenData.Clear(); | ||
| var htmlToken = ParseEmToken(index); | ||
| index += htmlToken.Length; | ||
| return htmlToken; | ||
| } | ||
|
|
||
| private HtmlToken ParseNoMarkup(int index, string alreadyParsed = "", int alreadyEscaped = 0) | ||
| { | ||
| var tokenData = new StringBuilder(alreadyParsed); | ||
| var escaped = alreadyEscaped; | ||
| while (index < plainMd.Length) | ||
| { | ||
| var tag = ParseTag(index); | ||
|
|
||
| if (validateFunctions[tag].Invoke(index, true)) | ||
| break; | ||
| if (plainMd[index] == '\\') | ||
| { | ||
| index++; | ||
| escaped++; | ||
| } | ||
| tokenData.Append(plainMd[index]); | ||
| index++; | ||
| } | ||
| return new HtmlToken(Tag.Empty, tokenData.ToString(), escaped); | ||
| } | ||
|
|
||
| private bool NotInsideDigits(int tagIndex) | ||
| => !char.IsDigit(plainMd[tagIndex - 1]) && !char.IsDigit(plainMd[tagIndex + 1]); | ||
|
|
||
| private bool IsNotStrongTag(int tagIndex) | ||
| => !(plainMd[tagIndex - 1] == '_' || plainMd[tagIndex + 1] == '_'); | ||
|
|
||
| private bool NoSpaceNearMdTag(int tagIndex, int tagLength, bool isOpenTag) | ||
| => plainMd[tagIndex + (isOpenTag ? tagLength : -1)] != ' '; | ||
|
|
||
| private bool IsNotOpenTagInEndOfString(int tagIndex, int tagLength, bool isOpenTag) | ||
| => !(tagIndex == plainMd.Length - tagLength && isOpenTag); | ||
|
|
||
| private bool IsValidEmTag(int tagIndex, bool isOpenTag) | ||
| { | ||
| if (plainMd[tagIndex] != '_') | ||
| return false; | ||
| try | ||
|
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. чёт лучше try-catch наверное не использовать, а проверить |
||
| { | ||
| return IsNotOpenTagInEndOfString(tagIndex, 1, isOpenTag) | ||
| && NoSpaceNearMdTag(tagIndex, 1, isOpenTag) | ||
| && IsNotStrongTag(tagIndex) | ||
| && NotInsideDigits(tagIndex); | ||
| } | ||
| catch (IndexOutOfRangeException) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private bool IsValidStrongTag(int tagIndex, bool isOpenTag) | ||
| { | ||
| if (plainMd[tagIndex] != '_' || !ParseTag(tagIndex).Equals(Tag.Strong)) | ||
| return false; | ||
| try | ||
| { | ||
| return IsNotOpenTagInEndOfString(tagIndex, 2, isOpenTag) | ||
| && NoSpaceNearMdTag(tagIndex, 2, isOpenTag); | ||
| } | ||
| catch (IndexOutOfRangeException) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private Tag ParseTag(int tagIndex) | ||
| { | ||
| if (plainMd[tagIndex] == '_') | ||
| { | ||
| if (tagIndex != plainMd.Length - 1) | ||
| return plainMd[tagIndex + 1] == '_' ? Tag.Strong : Tag.Em; | ||
| return Tag.Em; | ||
| } | ||
| return Tag.Empty; | ||
| } | ||
|
|
||
| private IEnumerable<HtmlToken> TryParseToHtml() | ||
| { | ||
| var i = 0; | ||
| var root = new List<HtmlToken>(); | ||
| while (i < plainMd.Length) | ||
| { | ||
| var tag = ParseTag(i); | ||
| var parsedToken = mdTagParserFuncMatch[tag].Invoke(i, "", 0); | ||
| i += parsedToken.Length; | ||
| root.Add(parsedToken); | ||
| } | ||
| return root; | ||
| } | ||
|
|
||
| public string Render() | ||
| { | ||
| var htmlTokens = TryParseToHtml(); | ||
| return string.Join("", htmlTokens.Select(x => x.ToString())); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace Markdown | ||
| { | ||
| public class Program | ||
| { | ||
| public static void Main() | ||
| { | ||
|
|
||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
гитхаб до сих пор не научился в интерполяцию строк :(
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.
:(