-
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
Зиновьева Милана #224
Open
crycrash
wants to merge
14
commits into
kontur-courses:master
Choose a base branch
from
crycrash:master
base: master
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
Зиновьева Милана #224
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f70afab
Added structure of project
crycrash f3b8fc8
Added tag processing
crycrash 2f9afb4
Improved algorithm, eliminated code duplication
crycrash 01e48b5
The header logic has been corrected
crycrash b43171f
Fixed screening
crycrash 294f785
extended tests have been added, the logic of working with spaces and …
crycrash cb9a405
Fixed strings with numbers
crycrash 9142d1f
Fixed header in header
crycrash bb22cb9
Added extra tests for headings
crycrash 3a0d8b9
Fixed formatting
crycrash 2201109
Small fixes
crycrash 883722f
Added links
crycrash c887712
Processing link names with nested tags
crycrash 02c538a
Specification added
crycrash 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 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
This file contains 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 |
---|---|---|
@@ -1,15 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<LangVersion>8</LangVersion> | ||
<LangVersion>6</LangVersion> | ||
<TargetFramework>net6</TargetFramework> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.1"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
<PackageReference Include="NUnit" Version="3.12.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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 |
---|---|---|
@@ -1,12 +1,65 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework.Internal.Execution; | ||
using System.Linq; | ||
|
||
namespace ControlDigit | ||
{ | ||
public static class SnilsExtensions | ||
{ | ||
public static int CalculateSnils(this long number) | ||
{ | ||
throw new NotImplementedException(); | ||
public static int CalculateSnils(this long number){ | ||
var dict = NumberDecompotion(number); | ||
var M = SummDecompotion(dict); | ||
var result = MakeControlDigit(M); | ||
return result; | ||
} | ||
|
||
private static Dictionary<int, int> NumberDecompotion(this long number){ | ||
var counter = 1; | ||
var dict = new Dictionary<int, int>(); | ||
while(number / 10 >= 1){ | ||
dict.Add(counter, (int)number % 10); | ||
counter++; | ||
number = number / 10; | ||
} | ||
dict.Add(counter, (int)number % 10); | ||
return dict; | ||
} | ||
|
||
private static int SummDecompotion(Dictionary<int, int> dict){ | ||
var summ = 0; | ||
foreach(var pair in dict){ | ||
summ += pair.Key * pair.Value; | ||
} | ||
return summ; | ||
} | ||
|
||
private static IEnumerable<int> MakeFactorsSnils(int length){ | ||
return Enumerable.Range(1, length); | ||
} | ||
|
||
private static IEnumerable<int> MakeFactorsUpc(int length){ | ||
return Enumerable.Range(1, length); | ||
} | ||
|
||
private static int CountControlDigit(IEnumerable<int> values, Func<int, IEnumerable<int>> evaluateFactors, int length){ | ||
var factors = evaluateFactors(length); | ||
var summ = 0; | ||
//for(int i = 0;i < length;i++){ | ||
//summ += factors[i] * values[i]; | ||
//} | ||
var ggygu = factors.Zip(values) | ||
} | ||
|
||
private static int MakeControlDigit(int summ){ | ||
if (summ > 101) | ||
summ = summ % 101; | ||
if (summ < 100) | ||
return summ; | ||
else | ||
return 0; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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,55 @@ | ||
namespace Markdown; | ||
|
||
public class HelperFunctions | ||
{ | ||
private static readonly char[] forbiddenChars = ['_', '#']; | ||
public static int ScreeningCheck(ref string text, int startIndex) | ||
{ | ||
if (IsScreening(text, startIndex)) | ||
{ | ||
int endIndex = text.IndexOf('\\', startIndex + 1); | ||
if (endIndex == -1) | ||
return startIndex + 1; | ||
text = text.Remove(startIndex - 1, 1).Remove(endIndex - 1, 1); | ||
return endIndex - 1; | ||
} | ||
return startIndex; | ||
} | ||
|
||
public static int FindCorrectCloseSymbolForItalic(string text, int startIndex) | ||
{ | ||
for (int i = startIndex + 1; i < text.Length; i++) | ||
{ | ||
if (text[i] == '_' && !IsScreening(text, i) | ||
&& !IsPartOfDoubleUnderscore(text, i) && !IsSurroundedByWhitespaceOrDigit(text, i)) | ||
{ | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
private static bool IsScreening(string text, int index) => | ||
index - 1 >= 0 && text[index - 1] == '\\'; | ||
|
||
public static bool IsDigit(string text, int index) => | ||
index - 1 >= 0 && char.IsDigit(text[index - 1]); | ||
|
||
public static string ProcessNestedTag(ref string text) => | ||
Md.Render(text); | ||
|
||
public static bool ContainsWhiteSpaces(string text) => | ||
text.Contains(' ') || string.IsNullOrWhiteSpace(text); | ||
|
||
public static bool ContainsOnlyDash(string text) => | ||
text.All(symbol => forbiddenChars.Contains(symbol)); | ||
|
||
private static bool IsPartOfDoubleUnderscore(string text, int index) => | ||
(index + 1 < text.Length && text[index + 1] == '_') || | ||
(index > 0 && text[index - 1] == '_'); | ||
|
||
private static bool IsSurroundedByWhitespaceOrDigit(string text, int index) => | ||
(index > 0 && char.IsWhiteSpace(text[index - 1])) || | ||
(index + 1 < text.Length && char.IsWhiteSpace(text[index + 1])) || | ||
(index > 0 && char.IsDigit(text[index - 1])); | ||
} |
This file contains 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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>false</IsTestProject> | ||
</PropertyGroup> | ||
</Project> |
This file contains 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,42 @@ | ||
using Markdown.Tags; | ||
|
||
namespace Markdown; | ||
|
||
public class Md | ||
{ | ||
private static readonly List<ITagHandler> TagHandlers = | ||
[ | ||
new BoldTag(), | ||
new ItalicTag(), | ||
new HeadingTag() | ||
]; | ||
|
||
public static string Render(string markdownString) | ||
{ | ||
if (string.IsNullOrEmpty(markdownString) || HelperFunctions.ContainsOnlyDash(markdownString)) | ||
return markdownString; | ||
|
||
int index = 0; | ||
while (index < markdownString.Length) | ||
{ | ||
if (TryProcessTag(ref markdownString, ref index)) | ||
continue; | ||
index++; | ||
} | ||
|
||
return markdownString; | ||
} | ||
|
||
private static bool TryProcessTag(ref string markdownString, ref int index) | ||
{ | ||
foreach (var handler in TagHandlers) | ||
{ | ||
if (handler.IsTagStart(markdownString, index)) | ||
{ | ||
index = handler.ProcessTag(ref markdownString, index); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains 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,56 @@ | ||
namespace Markdown.Tags; | ||
|
||
public abstract class BaseTagHandler : ITagHandler | ||
{ | ||
protected abstract string Symbol { get; } | ||
protected abstract string HtmlTag { get; } | ||
|
||
public abstract bool IsTagStart(string text, int index); | ||
|
||
public int ProcessTag(ref string text, int startIndex) | ||
{ | ||
var newIndex = HelperFunctions.ScreeningCheck(ref text, startIndex); | ||
if (newIndex != startIndex) | ||
return newIndex; | ||
|
||
int endIndex = FindEndIndex(text, startIndex); | ||
|
||
if (endIndex == -1) | ||
return startIndex + Symbol.Length; | ||
|
||
string content = ExtractContent(text, startIndex, endIndex); | ||
if (HelperFunctions.ContainsWhiteSpaces(content)) | ||
return startIndex + content.Length; | ||
|
||
content = ProcessNestedTag(ref content); | ||
string replacement = WrapWithHtmlTag(content); | ||
text = ReplaceText(text, startIndex, endIndex, replacement); | ||
|
||
return startIndex + replacement.Length; | ||
} | ||
|
||
protected virtual string ProcessNestedTag(ref string text) | ||
{ | ||
return HelperFunctions.ProcessNestedTag(ref text); | ||
} | ||
|
||
protected virtual int FindEndIndex(string text, int startIndex) | ||
{ | ||
return text.IndexOf(Symbol, startIndex + Symbol.Length); | ||
} | ||
|
||
protected virtual string ExtractContent(string text, int startIndex, int endIndex) | ||
{ | ||
return text.Substring(startIndex + Symbol.Length, endIndex - startIndex - Symbol.Length); | ||
} | ||
|
||
protected virtual string WrapWithHtmlTag(string content) | ||
{ | ||
return $"<{HtmlTag}>{content}</{HtmlTag}>"; | ||
} | ||
|
||
protected virtual string ReplaceText(string text, int startIndex, int endIndex, string replacement) | ||
{ | ||
return text.Substring(0, startIndex) + replacement + text.Substring(endIndex + Symbol.Length); | ||
} | ||
} |
This file contains 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,12 @@ | ||
namespace Markdown.Tags; | ||
public class BoldTag : BaseTagHandler | ||
{ | ||
protected override string Symbol => "__"; | ||
protected override string HtmlTag => "strong"; | ||
|
||
public override bool IsTagStart(string text, int index) | ||
{ | ||
return index + 2 < text.Length && text.Substring(index, 2) == Symbol | ||
&& !char.IsWhiteSpace(text[index + 1]); | ||
} | ||
} |
This file contains 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,26 @@ | ||
namespace Markdown.Tags; | ||
public class HeadingTag : BaseTagHandler | ||
{ | ||
protected override string Symbol => "#"; | ||
protected override string HtmlTag => "h1"; | ||
|
||
public override bool IsTagStart(string text, int index) | ||
{ | ||
return text[index].ToString() == Symbol; | ||
} | ||
|
||
protected override int FindEndIndex(string text, int startIndex) | ||
{ | ||
int endIndex = text.IndexOf('\n', startIndex + 1); | ||
return endIndex == -1 ? text.Length : endIndex; | ||
} | ||
|
||
protected override string ReplaceText(string text, int startIndex, int endIndex, string replacement) | ||
{ | ||
if (endIndex < text.Length && text[endIndex] == '\n') | ||
{ | ||
return text.Substring(0, startIndex) + replacement + '\n' + text.Substring(endIndex + 1); | ||
} | ||
return text.Substring(0, startIndex) + replacement + text.Substring(endIndex); | ||
} | ||
} |
This file contains 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,7 @@ | ||
namespace Markdown.Tags; | ||
public interface ITagHandler | ||
{ | ||
bool IsTagStart(string text, int index); | ||
|
||
int ProcessTag(ref string text, int startIndex); | ||
} |
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.
Булы тут немного не к месту, хочется от них избавиться. Как вариант, можно добавить дефолтный хендлер для символов, которые не являются тегами, перестать опираться на tagChars и использовать handler.IsTagStart. Чтобы всё заработало, хендлеры должны быть расположены в списке в корректном порядке. Тогда получится, что для каждого символа, мы берем первый подходящий хендлер. Так же придется вынести хендлеры в конструктор, для гибкой конфигурации.