一、Paste ASVisual Studio Code
参考:http://www.cnblogs.com/mikelij/archive/2010/11/13/1876199.html
插件下载:http://pan.baidu.com/s/1i3iUO53
个人备注:可能存在VS SQL版本兼容问题(中文乱码),网上有解决方案
代码效果,中文乱码请无视
- /// <summary>
- /// ??????
- /// </summary>
- /// <param name="code">??</param>
- /// <param name="languageKey">????</param>
- /// <param name="isShowLineNum">??????</param>
- /// <returns>???????????HTML????</returns>
- public static string GeneralCodeHighlight(string code, string languageKey, bool isShowLineNum)
- {
- SyntaxLanguage lang = null;
- //??????????
- CodeHighlighterConfiguration config = HttpContext.Current.Cache["CodeHighlighterConfig"] as CodeHighlighterConfiguration;
- if (config == null)
- {
- //????????? web.config ???????
- config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter");
- HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config);
- }
- //????
- foreach (string key in config.LanguageConfigs.Keys)
- {
- if (key.ToLower() == languageKey.ToLower())
- {
- lang = CodeHighlighter.GetLanguage(config, key);
- break;
- }
- }
- //??????????
- if (lang == null)
- return code;
- CodeHighlighterEngine engine = new CodeHighlighterEngine();
- engine.OutliningEnabled = false;
- engine.LineNumberMarginVisible = isShowLineNum;
- return engine.GenerateHtmlInline(string.Empty, code, lang);
- }
生成的HTML代码:
Paste ASVisual Studio Code
- <div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7a43bc57-663b-4479-88d8-72ca33c46e39" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">
- <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
- <div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px">Code Snippet</div>
- <div style="background: #ddd; max-height: 300px; overflow: auto">
- <ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;">
- <li><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#808080">///</span><span style="background:#ffffff;color:#008000"> </span><span style="background:#ffffff;color:#808080"><summary></span></li>
- <li><span style="background:#ffffff;color:#000000">????????</span><span style="background:#ffffff;color:#808080">///</span><span style="background:#ffffff;color:#008000"> ??????</span></li>
- <li><span style="background:#ffffff;color:#000000">????????</span><span style="background:#ffffff;color:#808080">///</span><span style="background:#ffffff;color:#008000"> </span><span style="background:#ffffff;color:#808080"></summary></span></li>
- <li><span style="background:#ffffff;color:#000000">????????</span><span style="background:#ffffff;color:#808080">///</span><span style="background:#ffffff;color:#008000"> </span><span style="background:#ffffff;color:#808080"><param name="code"></span><span style="background:#ffffff;color:#008000">??</span><span style="background:#ffffff;color:#808080"></param></span></li>
- <li><span style="background:#ffffff;color:#000000">????????</span><span style="background:#ffffff;color:#808080">///</span><span style="background:#ffffff;color:#008000"> </span><span style="background:#ffffff;color:#808080"><param name="languageKey"></span><span style="background:#ffffff;color:#008000">????</span><span style="background:#ffffff;color:#808080"></param></span></li>
- <li><span style="background:#ffffff;color:#000000">????????</span><span style="background:#ffffff;color:#808080">///</span><span style="background:#ffffff;color:#008000"> </span><span style="background:#ffffff;color:#808080"><param name="isShowLineNum"></span><span style="background:#ffffff;color:#008000">??????</span><span style="background:#ffffff;color:#808080"></param></span></li>
- <li><span style="background:#ffffff;color:#000000">????????</span><span style="background:#ffffff;color:#808080">///</span><span style="background:#ffffff;color:#008000"> </span><span style="background:#ffffff;color:#808080"><returns></span><span style="background:#ffffff;color:#008000">???????????HTML????</span><span style="background:#ffffff;color:#808080"></returns></span></li>
- <li><span style="background:#ffffff;color:#000000">????????</span><span style="background:#ffffff;color:#0000ff">public</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#0000ff">static</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#0000ff">string</span><span style="background:#ffffff;color:#000000"> GeneralCodeHighlight(</span><span style="background:#ffffff;color:#0000ff">string</span><span style="background:#ffffff;color:#000000"> code, </span><span style="background:#ffffff;color:#0000ff">string</span><span style="background:#ffffff;color:#000000"> languageKey, </span><span style="background:#ffffff;color:#0000ff">bool</span><span style="background:#ffffff;color:#000000"> isShowLineNum)</span></li>
- <li><span style="background:#ffffff;color:#000000">????????{</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#2b91af">SyntaxLanguage</span><span style="background:#ffffff;color:#000000"> lang = </span><span style="background:#ffffff;color:#0000ff">null</span><span style="background:#ffffff;color:#000000">;</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#008000">//??????????</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#2b91af">CodeHighlighterConfiguration</span><span style="background:#ffffff;color:#000000"> config = </span><span style="background:#ffffff;color:#2b91af">HttpContext</span><span style="background:#ffffff;color:#000000">.Current.Cache[</span><span style="background:#ffffff;color:#a31515">"CodeHighlighterConfig"</span><span style="background:#ffffff;color:#000000">] </span><span style="background:#ffffff;color:#0000ff">as</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#2b91af">CodeHighlighterConfiguration</span><span style="background:#ffffff;color:#000000">;</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#0000ff">if</span><span style="background:#ffffff;color:#000000"> (config == </span><span style="background:#ffffff;color:#0000ff">null</span><span style="background:#ffffff;color:#000000">)</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????{</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????</span><span style="background:#ffffff;color:#008000">//????????? web.config ???????</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????config = (</span><span style="background:#ffffff;color:#2b91af">CodeHighlighterConfiguration</span><span style="background:#ffffff;color:#000000">)</span><span style="background:#ffffff;color:#2b91af">ConfigurationManager</span><span style="background:#ffffff;color:#000000">.GetSection(</span><span style="background:#ffffff;color:#a31515">"codeHighlighter"</span><span style="background:#ffffff;color:#000000">);</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????</span><span style="background:#ffffff;color:#2b91af">HttpContext</span><span style="background:#ffffff;color:#000000">.Current.Cache.Insert(</span><span style="background:#ffffff;color:#a31515">"CodeHighlighterConfig"</span><span style="background:#ffffff;color:#000000">, config);</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????}</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#008000">//????</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#0000ff">foreach</span><span style="background:#ffffff;color:#000000"> (</span><span style="background:#ffffff;color:#0000ff">string</span><span style="background:#ffffff;color:#000000"> key </span><span style="background:#ffffff;color:#0000ff">in</span><span style="background:#ffffff;color:#000000"> config.LanguageConfigs.Keys)</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????{</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????</span><span style="background:#ffffff;color:#0000ff">if</span><span style="background:#ffffff;color:#000000"> (key.ToLower() == languageKey.ToLower())</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????{</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????????lang = </span><span style="background:#ffffff;color:#2b91af">CodeHighlighter</span><span style="background:#ffffff;color:#000000">.GetLanguage(config, key);</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????????</span><span style="background:#ffffff;color:#0000ff">break</span><span style="background:#ffffff;color:#000000">;</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????}</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????}</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#008000">//??????????</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#0000ff">if</span><span style="background:#ffffff;color:#000000"> (lang == </span><span style="background:#ffffff;color:#0000ff">null</span><span style="background:#ffffff;color:#000000">)</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????????</span><span style="background:#ffffff;color:#0000ff">return</span><span style="background:#ffffff;color:#000000"> code;</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#2b91af">CodeHighlighterEngine</span><span style="background:#ffffff;color:#000000"> engine = </span><span style="background:#ffffff;color:#0000ff">new</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#2b91af">CodeHighlighterEngine</span><span style="background:#ffffff;color:#000000">();</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????engine.OutliningEnabled = </span><span style="background:#ffffff;color:#0000ff">false</span><span style="background:#ffffff;color:#000000">;</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????engine.LineNumberMarginVisible = isShowLineNum;</span></li>
- <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#0000ff">return</span><span style="background:#ffffff;color:#000000"> engine.GenerateHtmlInline(</span><span style="background:#ffffff;color:#0000ff">string</span><span style="background:#ffffff;color:#000000">.Empty, code, lang);</span></li>
- <li><span style="background:#ffffff;color:#000000">????????}</span></li>
- </ol>
- </div>
- </div>
- </div>
二、Paste From Visual Studio Code
参考:http://www.cnblogs.com/JeffreyZhao/archive/2009/12/16/vspaste-customization-1.html
插件下载:http://pan.baidu.com/s/1hqkYKCk
个人备注:可能存在VS SQL版本兼容问题(中文乱码),网上有解决方案
01: /// <summary>
02: /// 解析代码高亮
03: /// </summary>
04: /// <param name="code">代码</param>
05: /// <param name="languageKey">语法类型</param>
06: /// <param name="isShowLineNum">是否显示行号</param>
07: /// <returns>将代码解析高亮后组织成HTML的字符串</returns>
08: public static string GeneralCodeHighlight(string code, string languageKey, bool isShowLineNum)
09: {
10: SyntaxLanguage lang = null;
11: //尝试从缓存获取配置节
12: CodeHighlighterConfiguration config = HttpContext.Current.Cache["CodeHighlighterConfig"] as CodeHighlighterConfiguration;
13: if (config == null)
14: {
15: //缓存不存在,重新从 web.config 获取并保存缓存
16: config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter");
17: HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config);
18: }
19: //获取语言
20: foreach (string key in config.LanguageConfigs.Keys)
21: {
22: if (key.ToLower() == languageKey.ToLower())
23: {
24: lang = CodeHighlighter.GetLanguage(config, key);
25: break;
26: }
27: }
28: //不明语言,不理会返回
29: if (lang == null)
30: return code;
31: CodeHighlighterEngine engine = new CodeHighlighterEngine();
32: engine.OutliningEnabled = false;
33: engine.LineNumberMarginVisible = isShowLineNum;
34: return engine.GenerateHtmlInline(string.Empty, code, lang);
35: }
生成HTML代码:
Code Snippet
- <pre class="code"><span style="font-weight: bold; color: black">01: </span><span style="background: white; color: black"> </span><span style="background: white; color: gray">/// <summary>
- <span style="font-weight: bold; color: black">02: </span> /// </span><span style="background: white; color: green">??????
- <span style="font-weight: bold; color: black">03: </span> </span><span style="background: white; color: gray">/// </summary>
- <span style="font-weight: bold; color: black">04: </span> /// <param name="code"></span><span style="background: white; color: green">??</span><span style="background: white; color: gray"></param>
- <span style="font-weight: bold; color: black">05: </span> /// <param name="languageKey"></span><span style="background: white; color: green">????</span><span style="background: white; color: gray"></param>
- <span style="font-weight: bold; color: black">06: </span> /// <param name="isShowLineNum"></span><span style="background: white; color: green">??????</span><span style="background: white; color: gray"></param>
- <span style="font-weight: bold; color: black">07: </span> /// <returns></span><span style="background: white; color: green">???????????HTML????</span><span style="background: white; color: gray"></returns>
- <span style="font-weight: bold; color: black">08: </span> </span><span style="background: white; color: blue">public static string </span><span style="background: white; color: black">GeneralCodeHighlight(</span><span style="background: white; color: blue">string </span><span style="background: white; color: black">code, </span><span style="background: white; color: blue">string </span><span style="background: white; color: black">languageKey, </span><span style="background: white; color: blue">bool </span><span style="background: white; color: black">isShowLineNum)
- <span style="font-weight: bold; color: black">09: </span> {
- <span style="font-weight: bold; color: black">10: </span> </span><span style="background: white; color: #2b91af">SyntaxLanguage </span><span style="background: white; color: black">lang = </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">;
- <span style="font-weight: bold; color: black">11: </span> </span><span style="background: white; color: green">//??????????
- <span style="font-weight: bold; color: black">12: </span> </span><span style="background: white; color: #2b91af">CodeHighlighterConfiguration </span><span style="background: white; color: black">config = </span><span style="background: white; color: #2b91af">HttpContext</span><span style="background: white; color: black">.Current.Cache[</span><span style="background: white; color: #a31515">"CodeHighlighterConfig"</span><span style="background: white; color: black">] </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">CodeHighlighterConfiguration</span><span style="background: white; color: black">;
- <span style="font-weight: bold; color: black">13: </span> </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(config == </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
- <span style="font-weight: bold; color: black">14: </span> {
- <span style="font-weight: bold; color: black">15: </span> </span><span style="background: white; color: green">//????????? web.config ???????
- <span style="font-weight: bold; color: black">16: </span> </span><span style="background: white; color: black">config = (</span><span style="background: white; color: #2b91af">CodeHighlighterConfiguration</span><span style="background: white; color: black">)</span><span style="background: white; color: #2b91af">ConfigurationManager</span><span style="background: white; color: black">.GetSection(</span><span style="background: white; color: #a31515">"codeHighlighter"</span><span style="background: white; color: black">);
- <span style="font-weight: bold; color: black">17: </span> </span><span style="background: white; color: #2b91af">HttpContext</span><span style="background: white; color: black">.Current.Cache.Insert(</span><span style="background: white; color: #a31515">"CodeHighlighterConfig"</span><span style="background: white; color: black">, config);
- <span style="font-weight: bold; color: black">18: </span> }
- <span style="font-weight: bold; color: black">19: </span> </span><span style="background: white; color: green">//????
- <span style="font-weight: bold; color: black">20: </span> </span><span style="background: white; color: blue">foreach </span><span style="background: white; color: black">(</span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key </span><span style="background: white; color: blue">in </span><span style="background: white; color: black">config.LanguageConfigs.Keys)
- <span style="font-weight: bold; color: black">21: </span> {
- <span style="font-weight: bold; color: black">22: </span> </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(key.ToLower() == languageKey.ToLower())
- <span style="font-weight: bold; color: black">23: </span> {
- <span style="font-weight: bold; color: black">24: </span> lang = </span><span style="background: white; color: #2b91af">CodeHighlighter</span><span style="background: white; color: black">.GetLanguage(config, key);
- <span style="font-weight: bold; color: black">25: </span> </span><span style="background: white; color: blue">break</span><span style="background: white; color: black">;
- <span style="font-weight: bold; color: black">26: </span> }
- <span style="font-weight: bold; color: black">27: </span> }
- <span style="font-weight: bold; color: black">28: </span> </span><span style="background: white; color: green">//??????????
- <span style="font-weight: bold; color: black">29: </span> </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(lang == </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
- <span style="font-weight: bold; color: black">30: </span> </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">code;
- <span style="font-weight: bold; color: black">31: </span> </span><span style="background: white; color: #2b91af">CodeHighlighterEngine </span><span style="background: white; color: black">engine = </span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">CodeHighlighterEngine</span><span style="background: white; color: black">();
- <span style="font-weight: bold; color: black">32: </span> engine.OutliningEnabled = </span><span style="background: white; color: blue">false</span><span style="background: white; color: black">;
- <span style="font-weight: bold; color: black">33: </span> engine.LineNumberMarginVisible = isShowLineNum;
- <span style="font-weight: bold; color: black">34: </span> </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">engine.GenerateHtmlInline(</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">.Empty, code, lang);
- <span style="font-weight: bold; color: black">35: </span> }</span></pre>
三、WLWSourceCodePlugin
参考:http://www.amergerzic.com/post/WLWSourceCodePlugin.aspx
下载地址:http://pan.baidu.com/s/1jG7pqyq
1: /// <summary>
2: /// 解析代码高亮
3: /// </summary>
4: /// <param name="code">代码</param>
5: /// <param name="languageKey">语法类型</param>
6: /// <param name="isShowLineNum">是否显示行号</param>
7: /// <returns>将代码解析高亮后组织成HTML的字符串</returns>
8: public static string GeneralCodeHighlight(string code, string languageKey, bool isShowLineNum)
9: {
10: SyntaxLanguage lang = null;
11: //尝试从缓存获取配置节
12: CodeHighlighterConfiguration config = HttpContext.Current.Cache["CodeHighlighterConfig"] as CodeHighlighterConfiguration;
13: if (config == null)
14: {
15: //缓存不存在,重新从 web.config 获取并保存缓存
16: config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter");
17: HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config);
18: }
19: //获取语言
20: foreach (string key in config.LanguageConfigs.Keys)
21: {
22: if (key.ToLower() == languageKey.ToLower())
23: {
24: lang = CodeHighlighter.GetLanguage(config, key);
25: break;
26: }
27: }
28: //不明语言,不理会返回
29: if (lang == null)
30: return code;
31: CodeHighlighterEngine engine = new CodeHighlighterEngine();
32: engine.OutliningEnabled = false;
33: engine.LineNumberMarginVisible = isShowLineNum;
34: return engine.GenerateHtmlInline(string.Empty, code, lang);
35: }
生成HTML代码
Code Snippet
- <pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 1: <span style="color: #808080">/// <summary></span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 2: <span style="color: #808080">/// ??????</span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 3: <span style="color: #808080">/// </summary></span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 4: <span style="color: #808080">/// <param name="code">??</param></span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 5: <span style="color: #808080">/// <param name="languageKey">????</param></span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 6: <span style="color: #808080">/// <param name="isShowLineNum">??????</param></span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 7: <span style="color: #808080">/// <returns>???????????HTML????</returns></span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 8: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GeneralCodeHighlight(<span style="color: #0000ff">string</span> code, <span style="color: #0000ff">string</span> languageKey, <span style="color: #0000ff">bool</span> isShowLineNum)
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 9: {
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 10: SyntaxLanguage lang = <span style="color: #0000ff">null</span>;
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 11: <span style="color: #008000">//??????????</span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 12: CodeHighlighterConfiguration config = HttpContext.Current.Cache["<span style="color: #8b0000">CodeHighlighterConfig</span>"] <span style="color: #0000ff">as</span> CodeHighlighterConfiguration;
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 13: <span style="color: #0000ff">if</span> (config == <span style="color: #0000ff">null</span>)
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 14: {
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 15: <span style="color: #008000">//????????? web.config ???????</span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 16: config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("<span style="color: #8b0000">codeHighlighter</span>");
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 17: HttpContext.Current.Cache.Insert("<span style="color: #8b0000">CodeHighlighterConfig</span>", config);
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 18: }
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 19: <span style="color: #008000">//????</span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 20: <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">string</span> key <span style="color: #0000ff">in</span> config.LanguageConfigs.Keys)
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 21: {
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 22: <span style="color: #0000ff">if</span> (key.ToLower() == languageKey.ToLower())
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 23: {
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 24: lang = CodeHighlighter.GetLanguage(config, key);
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 25: <span style="color: #0000ff">break</span>;
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 26: }
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 27: }
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 28: <span style="color: #008000">//??????????</span>
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 29: <span style="color: #0000ff">if</span> (lang == <span style="color: #0000ff">null</span>)
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 30: <span style="color: #0000ff">return</span> code;
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 31: CodeHighlighterEngine engine = <span style="color: #0000ff">new</span> CodeHighlighterEngine();
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 32: engine.OutliningEnabled = <span style="color: #0000ff">false</span>;
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 33: engine.LineNumberMarginVisible = isShowLineNum;
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 34: <span style="color: #0000ff">return</span> engine.GenerateHtmlInline(<span style="color: #0000ff">string</span>.Empty, code, lang);
- </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 35: }</pre></pre>
四、Memento.SourceCode
参考:http://www.cnblogs.com/memento/p/4307171.html
下载地址: http://pan.baidu.com/s/1pJqkiRh
1 /// <summary>
2 /// 解析代码高亮
3 /// </summary>
4 /// <param name="code">代码</param>
5 /// <param name="languageKey">语法类型</param>
6 /// <param name="isShowLineNum">是否显示行号</param>
7 /// <returns>将代码解析高亮后组织成HTML的字符串</returns>
8 public static string GeneralCodeHighlight(string code, string languageKey, bool isShowLineNum)
9 {
10 SyntaxLanguage lang = null;
11 //尝试从缓存获取配置节
12 CodeHighlighterConfiguration config = HttpContext.Current.Cache["CodeHighlighterConfig"] as CodeHighlighterConfiguration;
13 if (config == null)
14 {
15 //缓存不存在,重新从 web.config 获取并保存缓存
16 config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter");
17 HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config);
18 }
19 //获取语言
20 foreach (string key in config.LanguageConfigs.Keys)
21 {
22 if (key.ToLower() == languageKey.ToLower())
23 {
24 lang = CodeHighlighter.GetLanguage(config, key);
25 break;
26 }
27 }
28 //不明语言,不理会返回
29 if (lang == null)
30 return code;
31 CodeHighlighterEngine engine = new CodeHighlighterEngine();
32 engine.OutliningEnabled = false;
33 engine.LineNumberMarginVisible = isShowLineNum;
34 return engine.GenerateHtmlInline(string.Empty, code, lang);
35 }
View Code
生成HTML代码
Memento.SourceCode生成的HTML代码
- <div onclick="cnblogs_code_show('2f7c88c0-3057-4a33-a098-9af74ed82b16')" class="cnblogs_code">
- <img id="code_img_closed_2f7c88c0-3057-4a33-a098-9af74ed82b16" class="code_img_closed" alt="" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" /><img onclick="cnblogs_code_hide('2f7c88c0-3057-4a33-a098-9af74ed82b16',event)" id="code_img_opened_2f7c88c0-3057-4a33-a098-9af74ed82b16" class="code_img_opened" style="display: none" alt="" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" />
-
- <div id="cnblogs_code_open_2f7c88c0-3057-4a33-a098-9af74ed82b16" class="cnblogs_code_hide">
- <pre><span style="color: #008080"> 1</span> <span style="color: #808080">/// <summary></span>
- <span style="color: #008080"> 2</span> <span style="color: #808080">/// ??????</span>
- <span style="color: #008080"> 3</span> <span style="color: #808080">/// </summary></span>
- <span style="color: #008080"> 4</span> <span style="color: #808080">/// <param name="code">??</param></span>
- <span style="color: #008080"> 5</span> <span style="color: #808080">/// <param name="languageKey">????</param></span>
- <span style="color: #008080"> 6</span> <span style="color: #808080">/// <param name="isShowLineNum">??????</param></span>
- <span style="color: #008080"> 7</span> <span style="color: #808080">/// <returns>???????????HTML????</returns></span>
- <span style="color: #008080"> 8</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GeneralCodeHighlight(<span style="color: #0000ff">string</span> code, <span style="color: #0000ff">string</span> languageKey, <span style="color: #0000ff">bool</span> isShowLineNum)
- <span style="color: #008080"> 9</span> {
- <span style="color: #008080"> 10</span> SyntaxLanguage lang = <span style="color: #0000ff">null</span>;
- <span style="color: #008080"> 11</span> <span style="color: #008000">//??????????</span>
- <span style="color: #008080"> 12</span> CodeHighlighterConfiguration config = HttpContext.Current.Cache["<span style="color: #8b0000">CodeHighlighterConfig</span>"] <span style="color: #0000ff">as</span> CodeHighlighterConfiguration;
- <span style="color: #008080"> 13</span> <span style="color: #0000ff">if</span> (config == <span style="color: #0000ff">null</span>)
- <span style="color: #008080"> 14</span> {
- <span style="color: #008080"> 15</span> <span style="color: #008000">//????????? web.config ???????</span>
- <span style="color: #008080"> 16</span> config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("<span style="color: #8b0000">codeHighlighter</span>");
- <span style="color: #008080"> 17</span> HttpContext.Current.Cache.Insert("<span style="color: #8b0000">CodeHighlighterConfig</span>", config);
- <span style="color: #008080"> 18</span> }
- <span style="color: #008080"> 19</span> <span style="color: #008000">//????</span>
- <span style="color: #008080"> 20</span> <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">string</span> key <span style="color: #0000ff">in</span> config.LanguageConfigs.Keys)
- <span style="color: #008080"> 21</span> {
- <span style="color: #008080"> 22</span> <span style="color: #0000ff">if</span> (key.ToLower() == languageKey.ToLower())
- <span style="color: #008080"> 23</span> {
- <span style="color: #008080"> 24</span> lang = CodeHighlighter.GetLanguage(config, key);
- <span style="color: #008080"> 25</span> <span style="color: #0000ff">break</span>;
- <span style="color: #008080"> 26</span> }
- <span style="color: #008080"> 27</span> }
- <span style="color: #008080"> 28</span> <span style="color: #008000">//??????????</span>
- <span style="color: #008080"> 29</span> <span style="color: #0000ff">if</span> (lang == <span style="color: #0000ff">null</span>)
- <span style="color: #008080"> 30</span> <span style="color: #0000ff">return</span> code;
- <span style="color: #008080"> 31</span> CodeHighlighterEngine engine = <span style="color: #0000ff">new</span> CodeHighlighterEngine();
- <span style="color: #008080"> 32</span> engine.OutliningEnabled = <span style="color: #0000ff">false</span>;
- <span style="color: #008080"> 33</span> engine.LineNumberMarginVisible = isShowLineNum;
- <span style="color: #008080"> 34</span> <span style="color: #0000ff">return</span> engine.GenerateHtmlInline(<span style="color: #0000ff">string</span>.Empty, code, lang);
- <span style="color: #008080"> 35</span> }</pre>
- </div>
- <span class="cnblogs_code_collapse">View Code</span>
- </div>
五、博客园推荐的:
/// <summary>
/// 解析代码高亮
/// </summary>
/// <param name="code">代码</param>
/// <param name="languageKey">语法类型</param>
/// <param name="isShowLineNum">是否显示行号</param>
/// <returns>将代码解析高亮后组织成HTML的字符串</returns>
public static string GeneralCodeHighlight(string code, string languageKey, bool isShowLineNum)
{
SyntaxLanguage lang = null;
//尝试从缓存获取配置节
CodeHighlighterConfiguration config = HttpContext.Current.Cache["CodeHighlighterConfig"] as CodeHighlighterConfiguration;
if (config == null)
{
//缓存不存在,重新从 web.config 获取并保存缓存
config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter");
HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config);
}
//获取语言
foreach (string key in config.LanguageConfigs.Keys)
{
if (key.ToLower() == languageKey.ToLower())
{
lang = CodeHighlighter.GetLanguage(config, key);
break;
}
}
//不明语言,不理会返回
if (lang == null)
return code;
CodeHighlighterEngine engine = new CodeHighlighterEngine();
engine.OutliningEnabled = false;
engine.LineNumberMarginVisible = isShowLineNum;
return engine.GenerateHtmlInline(string.Empty, code, lang);
}
生成的HTML代码
- <div class="cnblogs_code" style="border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; border-bottom: #cccccc 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; border-left: #cccccc 1px solid; padding-right: 5px; background-color: #f5f5f5">
- <pre><span style="color: #808080">///</span> <span style="color: #808080"><summary></span>
- <span style="color: #808080">///</span><span style="color: #008000"> ??????
- </span><span style="color: #808080">///</span> <span style="color: #808080"></summary></span>
- <span style="color: #808080">///</span> <span style="color: #808080"><param name="code"></span><span style="color: #008000">??</span><span style="color: #808080"></param></span>
- <span style="color: #808080">///</span> <span style="color: #808080"><param name="languageKey"></span><span style="color: #008000">????</span><span style="color: #808080"></param></span>
- <span style="color: #808080">///</span> <span style="color: #808080"><param name="isShowLineNum"></span><span style="color: #008000">??????</span><span style="color: #808080"></param></span>
- <span style="color: #808080">///</span> <span style="color: #808080"><returns></span><span style="color: #008000">???????????HTML????</span><span style="color: #808080"></returns></span>
- <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GeneralCodeHighlight(<span style="color: #0000ff">string</span> code, <span style="color: #0000ff">string</span> languageKey, <span style="color: #0000ff">bool</span><span style="color: #000000"> isShowLineNum)
- {
- SyntaxLanguage lang </span>= <span style="color: #0000ff">null</span><span style="color: #000000">;
- </span><span style="color: #008000">//</span><span style="color: #008000">??????????</span>
- CodeHighlighterConfiguration config = HttpContext.Current.Cache[<span style="color: #800000">"</span><span style="color: #800000">CodeHighlighterConfig</span><span style="color: #800000">"</span>] <span style="color: #0000ff">as</span><span style="color: #000000"> CodeHighlighterConfiguration;
- </span><span style="color: #0000ff">if</span> (config == <span style="color: #0000ff">null</span><span style="color: #000000">)
- {
- </span><span style="color: #008000">//</span><span style="color: #008000">????????? web.config ???????</span>
- config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection(<span style="color: #800000">"</span><span style="color: #800000">codeHighlighter</span><span style="color: #800000">"</span><span style="color: #000000">);
- HttpContext.Current.Cache.Insert(</span><span style="color: #800000">"</span><span style="color: #800000">CodeHighlighterConfig</span><span style="color: #800000">"</span><span style="color: #000000">, config);
- }
- </span><span style="color: #008000">//</span><span style="color: #008000">????</span>
- <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">string</span> key <span style="color: #0000ff">in</span><span style="color: #000000"> config.LanguageConfigs.Keys)
- {
- </span><span style="color: #0000ff">if</span> (key.ToLower() ==<span style="color: #000000"> languageKey.ToLower())
- {
- lang </span>=<span style="color: #000000"> CodeHighlighter.GetLanguage(config, key);
- </span><span style="color: #0000ff">break</span><span style="color: #000000">;
- }
- }
- </span><span style="color: #008000">//</span><span style="color: #008000">??????????</span>
- <span style="color: #0000ff">if</span> (lang == <span style="color: #0000ff">null</span><span style="color: #000000">)
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> code;
- CodeHighlighterEngine engine </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> CodeHighlighterEngine();
- engine.OutliningEnabled </span>= <span style="color: #0000ff">false</span><span style="color: #000000">;
- engine.LineNumberMarginVisible </span>=<span style="color: #000000"> isShowLineNum;
- </span><span style="color: #0000ff">return</span> engine.GenerateHtmlInline(<span style="color: #0000ff">string</span><span style="color: #000000">.Empty, code, lang);
- }</span></pre>
- </div>
写在最后的话
这个效果不知道怎么实现,还请知晓的朋友指点
本文参考:http://www.cnblogs.com/lyj/archive/2008/11/30/1344463.html
来源:http://www.cnblogs.com/GarsonZhang/