Exemple #1
0
        public static string FormatCodePrettyPrint(MarkdownDeep.Markdown m, string code)
        {
            // Try to extract the language from the first line
            var    match    = rxExtractLanguage.Match(code);
            string language = null;

            if (match.Success)
            {
                // Save the language
                var g = (Group)match.Groups[2];
                language = g.ToString();

                // Remove the first line
                code = code.Substring(match.Groups[1].Length);
            }

            // If not specified, look for a link definition called "default_syntax" and
            // grab the language from its title
            if (language == null)
            {
                var d = m.GetLinkDefinition("default_syntax");
                if (d != null)
                {
                    language = d.title;
                }
            }

            // Common replacements
            if (language == "C#")
            {
                language = "csharp";
            }
            if (language == "C++")
            {
                language = "cpp";
            }

            // Wrap code in pre/code tags and add PrettyPrint attributes if necessary
            if (string.IsNullOrEmpty(language))
            {
                return(string.Format("<pre><code>{0}</code></pre>\n", code));
            }
            else
            {
                return(string.Format("<textarea class=\"code code-sample data-lang='{0}' \">{1}</textarea>\n",
                                     language.ToLowerInvariant(), code));
            }
        }
Exemple #2
0
        public static string FormatCodePrettyPrint(MarkdownDeep.Markdown m, string code)
        {
            // Try to extract the language from the first line
            var    match    = rxExtractLanguage.Match(code);
            string language = string.Empty;

            if (match.Success)
            {
                var g = match.Groups[2];
                language = g.ToString().Trim().ToLowerInvariant();

                code = code.Substring(match.Groups[1].Length);
            }

            if (string.IsNullOrEmpty(language))
            {
                var d = m.GetLinkDefinition("default_syntax");
                if (d != null)
                {
                    language = d.title;
                }
            }

            // Common replacements
            if (language.Equals("C#", StringComparison.OrdinalIgnoreCase))
            {
                language = "cs";
            }
            else if (language.Equals("csharp", StringComparison.OrdinalIgnoreCase))
            {
                language = "cs";
            }
            else if (language.Equals("C++", StringComparison.OrdinalIgnoreCase))
            {
                language = "cpp";
            }

            if (string.IsNullOrEmpty(language))
            {
                return($"<pre><code>{code}</code></pre>\n");
            }
            else
            {
                return($"<pre class=\"prettyprint lang-{language}\"><code>{code}</code></pre>\n");
            }
        }