コード例 #1
0
ファイル: ExamParser.cs プロジェクト: AntiTcb/Exam-Formatter
        public static string ConvertToHtml(string text) {
            var finalResult = text;
            try
            {
                var patterns = new[ ]
                               {
                                   @"(?: +)?<card>(?:[\\ ]+)?([a-z0-9""][&\-=\p{Lu}\p{Ll}!.:""',#/・ 0-9]*\s*)(?:[!\\""'/_@#$%^&*()]+)?</card>(?: +)?",
                                   @"\[policy-ygo\]", @"\WPOLICY-KDE\W", @"\WPOLICY-PENALTY\W"
                               };
                var replacements = new[ ]
                                   {
                                       @"<font color='blue'><u><a href='http://antitcb.com/cardlookup?CardName=$1' target='_blank'>$1</a></u></font>",
                                       $"{YUGIOH_POLICY}", $"{KDE_POLICY}", $"{PENALTY_POLICY}"
                                   };

                for ( var i = 0 ; i <= patterns.GetUpperBound(0) ; i++ )
                {
                    var regex = new Regex(patterns[ i ], RegexOptions.IgnoreCase);
                    if (!regex.IsMatch(finalResult)) { continue; }
                    finalResult = regex.Replace(finalResult, replacements[ i ]);
                }
            }
            catch ( Exception e )
            {
                Console.WriteLine(e);
                finalResult = "An error occurred. PM Anti with the text that should be here.";
            }

            var replaceDict = Matches(finalResult, @"'_blank'>(.+?)</a></u></font>").
                Cast< Match >().
                ToDictionary
                (match => match.Groups[ 1 ].ToString(), match => match.Groups[ 1 ].ToString().Replace(" ", "+"));

            return replaceDict.Aggregate
                (finalResult,
                 (current, cardNameReplace) =>
                 Replace(current, $"CardName={cardNameReplace.Key}", $"CardName={cardNameReplace.Value}")).Trim();
        }