public static async Task <String> TryGetHighlightedCodeAsync([NotNull] String code, [NotNull] String path, SyntaxHighlightStyle style, bool lineNumbers, CancellationToken token) { // Check if the code is possibly invalid const int threshold = 50, length = 1000; if (code.Substring(0, length > code.Length ? code.Length : length).Count( c => char.IsControl(c) && c != '\n' && c != '\r' && c != '\t') > threshold) { return(null); } // Try to extract the code language Match match = Regex.Match(path, @".*([.]\w+)"); if (!match.Success || match.Groups.Count != 2) { return(null); } String extension = match.Groups[1].Value.ToLowerInvariant(), lexer = UncommonExtensions.ContainsKey(extension) ? UncommonExtensions[extension] : extension.Substring(1); // Remove the leading '.' // Prepare the POST request content Dictionary <String, String> values = new Dictionary <String, String> { { "code", code }, // The code to highlight { "lexer", lexer }, // The code language { "style", style.ToString().ToLowerInvariant() }, // The requested syntax highlight style { "divstyles", "border:solid gray;border-width:.0em .0em .0em .0em;padding:.2em .6em;" }, // Default CSS properties { "linenos", lineNumbers ? "pls" : String.Empty } // Includes the line numbers if not empty }; // Make the POST WrappedHTTPWebResult <String> result = await HTTPHelper.POSTWithCacheSupportAsync(APIUrl, values, token); // Check if the lexer is unsupported if (result.StatusCode == HttpStatusCode.InternalServerError) { #if DEBUG //For debugging, inform if an unsupported extesion is found System.Diagnostics.Debug.WriteLine($"Possible unsupported extension: {extension} > {lexer}"); #endif // Retry with the fallback lexer values["lexer"] = FallbackLexer; return((await HTTPHelper.POSTWithCacheSupportAsync(APIUrl, values, token)).Result); } // Return the result return(result.Result); }
/// <summary> /// Calls the default Equals method for the inner result of the wrapped instance /// </summary> /// <param name="other">The other instance to compare</param> public bool Equals(WrappedHTTPWebResult <T> other) => Equals(other.Result);