Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace VII_2656.TOC
- {
- public static class MarkdownTOC
- {
- private static readonly string _pattern = @"^(#{1,6}.*)"; // @"(\#+\s\w+).*$"
- private static Regex _regex;
- private static MatchCollection _matchCollection;
- private static int _start;
- private static string _markdownTOC;
- private static string _markdownWithIdentifiers;
- private static string _markdownSub;
- private static readonly string _pathMD = @"E:\Nazca\VII-2656\VII-2656\MD\Variable.pl.md";
- private static readonly string _pathSave = @"E:\Nazca\VII-2656\VII-2656\MD\Markdown_R.md";
- public static string GetMarkdownTOC()
- {
- return _markdownTOC;
- }
- public static string GetMarkdownWithIdentifiers()
- {
- return _markdownWithIdentifiers;
- }
- public static string GetMarkdownSub()
- {
- return _markdownSub;
- }
- public static void SaveMarkdownToFile()
- {
- File.WriteAllText(_pathSave, _markdownSub, Encoding.UTF8);
- }
- public static string GenerateMarkdownIdentifiers()
- {
- var stringFromFile = File.ReadAllText(_pathMD);
- _regex = new Regex(_pattern, RegexOptions.Multiline);
- _matchCollection = _regex.Matches(stringFromFile);
- foreach (var match in _matchCollection)
- {
- _start = stringFromFile.IndexOf(match.ToString()) + match.ToString().Length - 1;
- var stringBuilder = new StringBuilder(match.ToString());
- stringBuilder.Replace("#", "");
- stringBuilder.Replace("ą", "a");
- stringBuilder.Replace("ć", "c");
- stringBuilder.Replace("ę", "e");
- stringBuilder.Replace("ł", "l");
- stringBuilder.Replace("ń", "n");
- stringBuilder.Replace("ó", "o");
- stringBuilder.Replace("ś", "s");
- stringBuilder.Replace("ź", "z");
- stringBuilder.Replace("ż", "z");
- stringBuilder.Remove(0, 1);
- stringBuilder.Remove(stringBuilder.ToString().Length - 1, 1);
- stringFromFile
- = stringFromFile
- .Insert(_start, " <a name="
- + @""
- + stringBuilder
- .ToString()
- .ToLower()
- .Replace(" ", "_")
- .Trim()
- + @""
- + "></a>");
- }
- _markdownWithIdentifiers = stringFromFile;
- return _markdownWithIdentifiers;
- }
- public static string GenerateMarkdownTOC()
- {
- var stringFromFile = File.ReadAllText(_pathMD);
- _regex = new Regex(_pattern, RegexOptions.Multiline);
- _matchCollection = _regex.Matches(stringFromFile);
- foreach (var match in _matchCollection)
- {
- _start = stringFromFile.IndexOf(match.ToString()) + match.ToString().Length - 1;
- var stringBuilder = new StringBuilder(match.ToString());
- stringBuilder.Replace("#", "");
- stringBuilder.Replace("ą", "a");
- stringBuilder.Replace("ć", "c");
- stringBuilder.Replace("ę", "e");
- stringBuilder.Replace("ł", "l");
- stringBuilder.Replace("ń", "n");
- stringBuilder.Replace("ó", "o");
- stringBuilder.Replace("ś", "s");
- stringBuilder.Replace("ź", "z");
- stringBuilder.Replace("ż", "z");
- stringBuilder.Remove(0, 1);
- stringBuilder.Remove(stringBuilder.ToString().Length - 1, 1);
- var listElement = "["
- + stringBuilder
- + "](#"
- + stringBuilder
- .ToString()
- .ToLower()
- .Replace(" ", "_")
- .Trim()
- + ") \r\n";
- _markdownTOC += listElement;
- }
- return _markdownTOC;
- }
- public static string GenerateMarkdownNoNestedTOC()
- {
- _markdownSub = GenerateMarkdownTOC() + GenerateMarkdownIdentifiers();
- return _markdownSub;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement