public virtual string Code(string code, string lang, bool escaped) { var transformedCode = code; if (Options.Highlight != null) { var output = Options.Highlight(code, lang); if (output != null && output != code) { escaped = true; transformedCode = output; } } transformedCode = escaped ? transformedCode : StringHelper.Escape(transformedCode, true); var langClass = Options.LangPrefix + StringHelper.Escape(lang ?? string.Empty, true); return($"<pre{AttributesToString(this.Options.PreformattedTextAttributes)}><code class='{langClass}'>{transformedCode}\n</code></pre>\n"); }
public virtual string Code(string code, string lang, bool escaped) { if (Options.Highlight != null) { var @out = Options.Highlight(code, lang); if (@out != null && @out != code) { escaped = true; code = @out; } } if (String.IsNullOrEmpty(lang)) { return("<pre><code>" + (escaped ? code : StringHelper.Escape(code, true)) + "\n</code></pre>"); } return("<pre><code class=\"" + Options.LangPrefix + StringHelper.Escape(lang, true) + "\">" + (escaped ? code : StringHelper.Escape(code, true)) + "\n</code></pre>\n"); }
/// <summary> /// Lexing/Compiling /// </summary> public virtual string Output(string src) { var @out = String.Empty; LinkObj link; string text; string href; IList <string> cap; while (!String.IsNullOrEmpty(src)) { // escape cap = this._rules.Escape.Exec(src); if (cap.Any()) { src = src.Substring(cap[0].Length); @out += cap[1]; continue; } // autolink cap = this._rules.AutoLink.Exec(src); if (cap.Any()) { src = src.Substring(cap[0].Length); if (cap[2] == "@") { text = cap[1][6] == ':' ? this.Mangle(cap[1].Substring(7)) : this.Mangle(cap[1]); href = this.Mangle("mailto:") + text; } else { text = StringHelper.Escape(cap[1]); href = text; } @out += _options.Renderer.Link(href, null, text); continue; } // url (gfm) if (!this.inLink && (cap = this._rules.Url.Exec(src)).Any()) { src = src.Substring(cap[0].Length); text = StringHelper.Escape(cap[1]); href = text; @out += _options.Renderer.Link(href, null, text); continue; } // tag cap = this._rules.Tag.Exec(src); if (cap.Any()) { if (!this.inLink && Regex.IsMatch(cap[0], "^<a ", RegexOptions.IgnoreCase)) { this.inLink = true; } else if (this.inLink && Regex.IsMatch(cap[0], @"^<\/a>", RegexOptions.IgnoreCase)) { this.inLink = false; } src = src.Substring(cap[0].Length); @out += this._options.Sanitize ? (this._options.Sanitizer != null) ? this._options.Sanitizer(cap[0]) : StringHelper.Escape(cap[0]) : cap[0]; continue; } // link cap = this._rules.Link.Exec(src); if (cap.Any()) { src = src.Substring(cap[0].Length); this.inLink = true; @out += this.OutputLink(cap, new LinkObj { Href = cap[2], Title = cap[3] }); this.inLink = false; continue; } // reflink, nolink if ((cap = this._rules.RefLink.Exec(src)).Any() || (cap = this._rules.NoLink.Exec(src)).Any()) { src = src.Substring(cap[0].Length); var linkStr = (StringHelper.NotEmpty(cap, 2, 1)).ReplaceRegex(@"\s+", " "); this.links.TryGetValue(linkStr.ToLower(), out link); if (link == null || String.IsNullOrEmpty(link.Href)) { @out += cap[0][0]; src = cap[0].Substring(1) + src; continue; } this.inLink = true; @out += this.OutputLink(cap, link); this.inLink = false; continue; } // strong if ((cap = this._rules.Strong.Exec(src)).Any()) { src = src.Substring(cap[0].Length); @out += _options.Renderer.Strong(this.Output(StringHelper.NotEmpty(cap, 2, 1))); continue; } // em cap = this._rules.Em.Exec(src); if (cap.Any()) { src = src.Substring(cap[0].Length); @out += _options.Renderer.Em(this.Output(StringHelper.NotEmpty(cap, 2, 1))); continue; } // code cap = this._rules.Code.Exec(src); if (cap.Any()) { src = src.Substring(cap[0].Length); @out += _options.Renderer.Codespan(StringHelper.Escape(cap[2], true)); continue; } // br cap = this._rules.Br.Exec(src); if (cap.Any()) { src = src.Substring(cap[0].Length); @out += _options.Renderer.Br(); continue; } // del (gfm) cap = this._rules.Del.Exec(src); if (cap.Any()) { src = src.Substring(cap[0].Length); @out += _options.Renderer.Del(this.Output(cap[1])); continue; } // text cap = this._rules.Text.Exec(src); if (cap.Any()) { src = src.Substring(cap[0].Length); @out += _options.Renderer.Text(StringHelper.Escape(this.Smartypants(cap[0]))); continue; } if (!String.IsNullOrEmpty(src)) { throw new Exception("Infinite loop on byte: " + (int)src[0]); } } return(@out); }