From b91577e843dc074d69ca2fb4a651a3d882bdda92 Mon Sep 17 00:00:00 2001 From: Viktor Chernev Date: Tue, 29 Oct 2024 19:01:00 +0300 Subject: [PATCH] Work on features and bugs --- DescribeTranspiler.CLI/Datnik.cs | 7 + DescribeTranspiler.CLI/Functions_Main.cs | 2 + DescribeTranspiler.CLI/Functions_Messages.cs | 4 +- DescribeTranspiler.CLI/Program.cs | 14 +- .../Properties/launchSettings.json | 2 +- .../Translators/Html/HtmlBasicTranslator.cs | 123 +++++++++++++++-- .../Translators/Html/HtmlPageTranslator.cs | 125 ++++++++++++++++-- 7 files changed, 253 insertions(+), 24 deletions(-) diff --git a/DescribeTranspiler.CLI/Datnik.cs b/DescribeTranspiler.CLI/Datnik.cs index 0fbe7633..4746837c 100644 --- a/DescribeTranspiler.CLI/Datnik.cs +++ b/DescribeTranspiler.CLI/Datnik.cs @@ -9,6 +9,11 @@ namespace DescribeTranspiler.Cli { public class Datnik { + /// + /// Wether to censor items marked as "sensitive", "secret" or "hidden". + /// + public static bool isCensored; + /// /// Password to decrypt input files, if parseEncryptedFiles == true /// @@ -119,6 +124,8 @@ public class Datnik static Datnik() { + isCensored = false; + inputPassword = null; outputPassword = null; parseEncryptedFiles = false; diff --git a/DescribeTranspiler.CLI/Functions_Main.cs b/DescribeTranspiler.CLI/Functions_Main.cs index cc7ed46b..6afe5894 100644 --- a/DescribeTranspiler.CLI/Functions_Main.cs +++ b/DescribeTranspiler.CLI/Functions_Main.cs @@ -52,6 +52,7 @@ internal static bool Compile() Messages.ConsoleLog, Messages.ConsoleLogError, Messages.ConsoleLogInfo); + (translator as HtmlBasicTranslator)!.IsCensored = Datnik.isCensored; } else if (Datnik.translatorName.ToLower().StartsWith("html_") || Datnik.translatorName.ToLower() == "html") @@ -60,6 +61,7 @@ internal static bool Compile() Messages.ConsoleLog, Messages.ConsoleLogError, Messages.ConsoleLogInfo); + (translator as HtmlPageTranslator)!.IsCensored = Datnik.isCensored; } else if (Datnik.translatorName.ToLower().StartsWith("xml_") || Datnik.translatorName.ToLower() == "xml") diff --git a/DescribeTranspiler.CLI/Functions_Messages.cs b/DescribeTranspiler.CLI/Functions_Messages.cs index 627a20c7..ea8c1807 100644 --- a/DescribeTranspiler.CLI/Functions_Messages.cs +++ b/DescribeTranspiler.CLI/Functions_Messages.cs @@ -413,7 +413,7 @@ public static void printHelpMessage(bool block = true) ConsoleLogInfo("Display this help message"); ConsoleLog(""); ConsoleLogInfo("-----------------------------------------------------------------"); - ConsoleLog("usage: " + thisName + " parse-file PARSE_PATH RESULT_PATH\n[ input-password=INPUT_PASSWORD ][ output-password=OUTPUT_PASSWORD ][ log-password=LOG_PASSWORD ]\n[ language-version= | lang-ver= ][ translator=(TARGET_LANGUAGE|TRANSLATOR_NAME) ]\n[ beautify= ][ verbosity= | log-verbosity= ][ logfile=LOG_PATH | log-file=LOG_PATH ][ theme= ]"); + ConsoleLog("usage: " + thisName + " parse-file PARSE_PATH RESULT_PATH\n[ input-password=INPUT_PASSWORD ][ output-password=OUTPUT_PASSWORD ][ log-password=LOG_PASSWORD ]\n[ language-version= | lang-ver= ][ translator=(TARGET_LANGUAGE|TRANSLATOR_NAME) ]\n[ beautify= ][ verbosity= | log-verbosity= ][ logfile=LOG_PATH | log-file=LOG_PATH ][ theme= ][ censor ]"); ConsoleLogInfo("TARGET_LANGUAGE - the desired output language: \"JSON\", \"HTML\", \"XML\", \"SQL\""); ConsoleLogInfo("TRANSLATOR_NAME - the name of the translator to be used : \"HTML_PLAIN\", \"HTML_PAGE\", \"XML_PLAIN\", \"SQL_FILEFILL\", \"JSON_BASIC\", \"JSON_LISTIARY\""); ConsoleLogInfo("PARSE_PATH - the path of the file to parse"); @@ -427,7 +427,7 @@ public static void printHelpMessage(bool block = true) ConsoleLogInfo("* theme - \"DBLUE\", \"LBLUE\", \"GREEN\", \"PASTEL\", \"EARTH\", \"CONTRAST\", \"DEFAULT\", \"VIOLET\", \"CYAN\""); ConsoleLog(""); ConsoleLogInfo("-----------------------------------------------------------------"); - ConsoleLog("usage: " + thisName + " parse-folder PARSE_PATH RESULT_PATH\n[ dsonly= ][ toponly= ][ onerror= ( stop | ignore )]\n[ input-password=INPUT_PASSWORD ][ output-password=OUTPUT_PASSWORD ][ log-password=LOG_PASSWORD ]\n[ language-version= | lang-ver= ][ translator=(TARGET_LANGUAGE | TRANSLATOR_NAME) ]\n[ beautify= ][ verbosity= | log-verbosity= ]\n[ logfile=LOG_PATH | log-file=LOG_PATH ][ theme= ]"); + ConsoleLog("usage: " + thisName + " parse-folder PARSE_PATH RESULT_PATH\n[ dsonly= ][ toponly= ][ onerror= ( stop | ignore )]\n[ input-password=INPUT_PASSWORD ][ output-password=OUTPUT_PASSWORD ][ log-password=LOG_PASSWORD ]\n[ language-version= | lang-ver= ][ translator=(TARGET_LANGUAGE | TRANSLATOR_NAME) ]\n[ beautify= ][ verbosity= | log-verbosity= ]\n[ logfile=LOG_PATH | log-file=LOG_PATH ][ theme= ][ censor ]"); ConsoleLogInfo("TARGET_LANGUAGE - the desired output language: \"JSON\", \"HTML\", \"XML\", \"SQL\""); ConsoleLogInfo("TRANSLATOR_NAME - the name of the translator to be used : \"HTML_PLAIN\", \"HTML_PAGE\", \"XML_PLAIN\", \"SQL_FILEFILL\", \"JSON_BASIC\", \"JSON_LISTIARY\""); ConsoleLogInfo("PARSE_PATH - the path of the file to parse"); diff --git a/DescribeTranspiler.CLI/Program.cs b/DescribeTranspiler.CLI/Program.cs index b7ee7b38..793a73bf 100644 --- a/DescribeTranspiler.CLI/Program.cs +++ b/DescribeTranspiler.CLI/Program.cs @@ -52,7 +52,7 @@ static void Main(string[] args) //DescribeCompilerCLI parse-file PARSE_PATH RESULT_PATH //[ input-password=PASSWORD ][ output-password=PASSWORD ][ log-password=PASSWORD ] //[ language-version= | lang-ver= ][ translator = ( TARGET_LANGUAGE | TRANSLATOR_NAME )] [ beautify= ] - //[ verbosity= | log-verbosity= ][ logfile=LOG_PATH | log-file=LOG_PATH ] [ theme= ] + //[ verbosity= | log-verbosity= ][ logfile=LOG_PATH | log-file=LOG_PATH ] [ theme= ] [ censor ] else if (args[0].ToLower() == "parse-file") { parseFile(args); @@ -62,7 +62,7 @@ static void Main(string[] args) //[ dsonly= ][ toponly= ][ onerror= ( stop | ignore )] //[ input-password=PASSWORD ][ output-password=PASSWORD ][ log-password=PASSWORD ] //[ language-version= | lang-ver= ][ translator = ( TARGET_LANGUAGE | TRANSLATOR_NAME )] [ beautify= ] - //[ verbosity= | log-verbosity= ][ logfile=LOG_PATH | log-file=LOG_PATH ] [ theme= ] + //[ verbosity= | log-verbosity= ][ logfile=LOG_PATH | log-file=LOG_PATH ] [ theme= ] [ censor ] else if (args[0].ToLower() == "parse-folder") { parseFolder(args); @@ -72,7 +72,7 @@ static void Main(string[] args) //[ dsonly= ][ toponly= ][ onerror= ( stop | ignore )] //[ input-password=PASSWORD ][ output-password=PASSWORD ][ log-password=PASSWORD ] //[ language-version= | lang-ver= ][ translator = ( TARGET_LANGUAGE | TRANSLATOR_NAME )] [ beautify= ] - //[ verbosity= | log-verbosity= ][ logfile=LOG_PATH | log-file=LOG_PATH ] [ theme= ] + //[ verbosity= | log-verbosity= ][ logfile=LOG_PATH | log-file=LOG_PATH ] [ theme= ] [ censor ] else if (args[0].ToLower() == "parse-many") { Messages.printParseManyNotImplemented(); @@ -364,6 +364,10 @@ static void parseFile(string[] args) { if (Arguments.readBeautifyArgument(cur, i) == false) return; } + else if (cur.ToLower() == "censor") + { + Datnik.isCensored = true; + } else if (cur.StartsWith("verbosity=") && cur.Length > "verbosity=".Length) { if (Arguments.readVerbosityArgument(cur, i) == false) return; @@ -473,6 +477,10 @@ static void parseFolder(string[] args) { if (Arguments.readBeautifyArgument(cur, i) == false) return; } + else if (cur.ToLower() == "censor") + { + Datnik.isCensored = true; + } else if (cur.StartsWith("verbosity=") && cur.Length > "verbosity=".Length) { if (Arguments.readVerbosityArgument(cur, i) == false) return; diff --git a/DescribeTranspiler.CLI/Properties/launchSettings.json b/DescribeTranspiler.CLI/Properties/launchSettings.json index 32e5c39d..e3048472 100644 --- a/DescribeTranspiler.CLI/Properties/launchSettings.json +++ b/DescribeTranspiler.CLI/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "DescribeTranspiler.CLI": { "commandName": "Project", - "commandLineArgs": "parse-folder \"C:\\Users\\Viktor Chernev\\Desktop\\Lists-Cli\\MyData\\Videos\" \"C:\\Users\\Viktor Chernev\\Desktop\\Lists-Cli\\Videos.html\" translator=html_page beautify=true verbosity=low logfile=\"C:\\Users\\Viktor Chernev\\Desktop\\Lists-Cli\\Videos.log\" dsonly=false toponly=false onerror=ignore" + "commandLineArgs": "parse-folder \"C:\\Users\\Viktor Chernev\\Desktop\\Lists-Cli\\MyData\\Videos\" \"C:\\Users\\Viktor Chernev\\Desktop\\Lists-Cli\\Videos.html\" translator=html_page beautify=true verbosity=low logfile=\"C:\\Users\\Viktor Chernev\\Desktop\\Lists-Cli\\Videos.log\" dsonly=false toponly=false onerror=ignore censor" } } } \ No newline at end of file diff --git a/DescribeTranspiler/Translators/Translators/Html/HtmlBasicTranslator.cs b/DescribeTranspiler/Translators/Translators/Html/HtmlBasicTranslator.cs index 2f3eff54..ec3f92b3 100644 --- a/DescribeTranspiler/Translators/Translators/Html/HtmlBasicTranslator.cs +++ b/DescribeTranspiler/Translators/Translators/Html/HtmlBasicTranslator.cs @@ -2,7 +2,10 @@ using DescribeParser.Unfold; using System; using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; using System.Web; +using static Org.BouncyCastle.Asn1.Cmp.Challenge; namespace DescribeTranspiler.Translators @@ -12,6 +15,7 @@ namespace DescribeTranspiler.Translators /// public class HtmlBasicTranslator : DescribeUnfoldTranslator { + public bool IsCensored = false; public override bool IsInitialized { get; @@ -302,6 +306,7 @@ string TranslateProduction(DescribeUnfold u, string id) } //replace in template + string? colval = null; if (u.Decorators.ContainsKey(id)) { List decorators = u.Decorators[id]; @@ -309,18 +314,42 @@ string TranslateProduction(DescribeUnfold u, string id) { if (decorator.Name == "color") { - string res = coloredProductionTemplate!.Replace("{TITLE}", HttpUtility.HtmlEncode(u.Translations[id])); - res = res.Replace("{LINKS}", linkage); - res = res.Replace("{COLOR}", decorator.Value); - res = res.Replace("{ITEMS}", items); - return res; + colval = decorator.Value; + } + else if (decorator.Name == "sensitive" && IsCensored) + { + string text = u.Translations[id]; + string censored = Regex.Replace(text, @"\S", "?"); + u.Translations[id] = censored; + } + else if (decorator.Name == "secret" && IsCensored) + { + string mask = GenerateRandomDarkSquares(random.Next(10, 30)); + u.Translations[id] = mask; + } + else if (decorator.Name == "hidden" && IsCensored) + { + string mask = GenerateRandomGreekText(random.Next(40, 100)); + u.Translations[id] = mask; } } } - string pt = productionTemplate!.Replace("{TITLE}", HttpUtility.HtmlEncode(u.Translations[id])); - pt = pt.Replace("{LINKS}", linkage); - pt = pt.Replace("{ITEMS}", items); - return pt; + + if (colval == null) + { + string pt = productionTemplate!.Replace("{TITLE}", HttpUtility.HtmlEncode(u.Translations[id])); + pt = pt.Replace("{LINKS}", linkage); + pt = pt.Replace("{ITEMS}", items); + return pt; + } + else + { + string res = coloredProductionTemplate!.Replace("{TITLE}", HttpUtility.HtmlEncode(u.Translations[id])); + res = res.Replace("{LINKS}", linkage); + res = res.Replace("{COLOR}", colval); + res = res.Replace("{ITEMS}", items); + return res; + } } string TranslateItem(DescribeUnfold u, string id) { @@ -395,11 +424,87 @@ string TranslateItem(DescribeUnfold u, string id) { res = "" + res + ""; } + else if (decorator.Name == "sensitive" && IsCensored) + { + string text = u.Translations[id]; + string censored = Regex.Replace(text, @"\S", "?"); + + // possible bug here - this is bad way of doing things + // that is hacked into place. Should be refactored + res = res.Replace(u.Translations[id], censored); + u.Translations[id] = censored; + } + else if (decorator.Name == "secret" && IsCensored) + { + // possible bug here - this is bad way of doing things + // that is hacked into place. Should be refactored + string mask = GenerateRandomDarkSquares(random.Next(10, 30)); + res = res.Replace(u.Translations[id], mask); + u.Translations[id] = mask; + } + else if (decorator.Name == "hidden" && IsCensored) + { + // possible bug here - this is bad way of doing things + // that is hacked into place. Should be refactored + string mask = GenerateRandomGreekText(random.Next(40, 100)); + res = res.Replace(u.Translations[id], mask); + u.Translations[id] = mask; + } } } return before + res + after; } + //more + Random random = new Random(); + string GenerateRandomGreekText(int length) + { + // Define a pool of Greek characters and spaces + string pool = "α βγδ εζη θικ λμν ξοπ ρστ υφχ ψω άέή ίόύ ώ "; + + StringBuilder sb = new StringBuilder(); + + // Generate random characters from the pool + for (int i = 0; i < length; i++) + { + char randomChar = pool[random.Next(pool.Length)]; + sb.Append(randomChar); + } + + return sb.ToString(); + } + string GenerateRandomMathText(int length) + { + // Define a pool of Greek characters and spaces + string pool = "+ - × ÷ = ≠ < > ≤ ≥ ∞ π ∑ ∫ √ ∆ ∇ ∝ ⊕ ⊗ ⊥ ⎰"; + + StringBuilder sb = new StringBuilder(); + + // Generate random characters from the pool + for (int i = 0; i < length; i++) + { + char randomChar = pool[random.Next(pool.Length)]; + sb.Append(randomChar); + } + + return sb.ToString(); + } + string GenerateRandomDarkSquares(int length) + { + // Define a pool of Greek characters and spaces + string pool = "▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇"; + + StringBuilder sb = new StringBuilder(); + + // Generate random characters from the pool + for (int i = 0; i < length; i++) + { + char randomChar = pool[random.Next(pool.Length)]; + sb.Append(randomChar); + } + + return sb.ToString(); + } //log diff --git a/DescribeTranspiler/Translators/Translators/Html/HtmlPageTranslator.cs b/DescribeTranspiler/Translators/Translators/Html/HtmlPageTranslator.cs index 96a4ab7b..2d7e01fc 100644 --- a/DescribeTranspiler/Translators/Translators/Html/HtmlPageTranslator.cs +++ b/DescribeTranspiler/Translators/Translators/Html/HtmlPageTranslator.cs @@ -1,9 +1,12 @@ using DescribeParser; using DescribeParser.Unfold; +using Mysqlx.Session; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; +using System.Text; +using System.Text.RegularExpressions; using System.Web; @@ -14,6 +17,8 @@ namespace DescribeTranspiler.Translators /// public class HtmlPageTranslator : DescribeUnfoldTranslator { + public bool IsCensored = false; + public override bool IsInitialized { get; @@ -305,6 +310,7 @@ string TranslateProduction(DescribeUnfold u, string id) } //replace in template + string? colval = null; if (u.Decorators.ContainsKey(id)) { List decorators = u.Decorators[id]; @@ -312,18 +318,42 @@ string TranslateProduction(DescribeUnfold u, string id) { if (decorator.Name == "color") { - string res = coloredProductionTemplate!.Replace("{TITLE}", HttpUtility.HtmlEncode(u.Translations[id])); - res = res.Replace("{LINKS}", linkage); - res = res.Replace("{COLOR}", decorator.Value); - res = res.Replace("{ITEMS}", items); - return res; + colval = decorator.Value; + } + else if (decorator.Name == "sensitive" && IsCensored) + { + string text = u.Translations[id]; + string censored = Regex.Replace(text, @"\S", "?"); + u.Translations[id] = censored; + } + else if (decorator.Name == "secret" && IsCensored) + { + string mask = GenerateRandomDarkSquares(random.Next(10, 30)); + u.Translations[id] = mask; + } + else if (decorator.Name == "hidden" && IsCensored) + { + string mask = GenerateRandomGreekText(random.Next(40, 100)); + u.Translations[id] = mask; } } } - string pt = productionTemplate!.Replace("{TITLE}", HttpUtility.HtmlEncode(u.Translations[id])); - pt = pt.Replace("{LINKS}", linkage); - pt = pt.Replace("{ITEMS}", items); - return pt; + + if(colval == null) + { + string pt = productionTemplate!.Replace("{TITLE}", HttpUtility.HtmlEncode(u.Translations[id])); + pt = pt.Replace("{LINKS}", linkage); + pt = pt.Replace("{ITEMS}", items); + return pt; + } + else + { + string res = coloredProductionTemplate!.Replace("{TITLE}", HttpUtility.HtmlEncode(u.Translations[id])); + res = res.Replace("{LINKS}", linkage); + res = res.Replace("{COLOR}", colval); + res = res.Replace("{ITEMS}", items); + return res; + } } string TranslateItem(DescribeUnfold u, string id) { @@ -399,6 +429,32 @@ string TranslateItem(DescribeUnfold u, string id) { res = "" + res + ""; } + else if (decorator.Name == "sensitive" && IsCensored) + { + string text = u.Translations[id]; + string censored = Regex.Replace(text, @"\S", "?"); + + // possible bug here - this is bad way of doing things + // that is hacked into place. Should be refactored + res = res.Replace(u.Translations[id], censored); + u.Translations[id] = censored; + } + else if (decorator.Name == "secret" && IsCensored) + { + // possible bug here - this is bad way of doing things + // that is hacked into place. Should be refactored + string mask = GenerateRandomDarkSquares(random.Next(10, 30)); + res = res.Replace(u.Translations[id], mask); + u.Translations[id] = mask; + } + else if (decorator.Name == "hidden" && IsCensored) + { + // possible bug here - this is bad way of doing things + // that is hacked into place. Should be refactored + string mask = GenerateRandomGreekText(random.Next(40, 100)); + res = res.Replace(u.Translations[id], mask); + u.Translations[id] = mask; + } } } return before + res + after; @@ -449,6 +505,57 @@ string makeYoutubeLink(string url, int index) return linkLetter; } + //more + Random random = new Random(); + string GenerateRandomGreekText(int length) + { + // Define a pool of Greek characters and spaces + string pool = "α βγδ εζη θικ λμν ξοπ ρστ υφχ ψω άέή ίόύ ώ "; + + StringBuilder sb = new StringBuilder(); + + // Generate random characters from the pool + for (int i = 0; i < length; i++) + { + char randomChar = pool[random.Next(pool.Length)]; + sb.Append(randomChar); + } + + return sb.ToString(); + } + string GenerateRandomMathText(int length) + { + // Define a pool of Greek characters and spaces + string pool = "+ - × ÷ = ≠ < > ≤ ≥ ∞ π ∑ ∫ √ ∆ ∇ ∝ ⊕ ⊗ ⊥ ⎰"; + + StringBuilder sb = new StringBuilder(); + + // Generate random characters from the pool + for (int i = 0; i < length; i++) + { + char randomChar = pool[random.Next(pool.Length)]; + sb.Append(randomChar); + } + + return sb.ToString(); + } + string GenerateRandomDarkSquares(int length) + { + // Define a pool of Greek characters and spaces + string pool = "▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇"; + + StringBuilder sb = new StringBuilder(); + + // Generate random characters from the pool + for (int i = 0; i < length; i++) + { + char randomChar = pool[random.Next(pool.Length)]; + sb.Append(randomChar); + } + + return sb.ToString(); + } + //log public string Log