一、Paste ASVisual Studio Code

参考:http://www.cnblogs.com/mikelij/archive/2010/11/13/1876199.html

插件下载:http://pan.baidu.com/s/1i3iUO53

个人备注:可能存在VS SQL版本兼容问题(中文乱码),网上有解决方案

代码效果,中文乱码请无视
  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代码:

Paste ASVisual Studio Code
  1. <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">
  2.     <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
  3.         <div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px">Code Snippet</div>
  4.         <div style="background: #ddd; max-height: 300px; overflow: auto">
  5.             <ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;">
  6.                 <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">&lt;summary&gt;</span></li>
  7.                 <li><span style="background:#ffffff;color:#000000">????????</span><span style="background:#ffffff;color:#808080">///</span><span style="background:#ffffff;color:#008000"> ??????</span></li>
  8.                 <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">&lt;/summary&gt;</span></li>
  9.                 <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">&lt;param name=&quot;code&quot;&gt;</span><span style="background:#ffffff;color:#008000">??</span><span style="background:#ffffff;color:#808080">&lt;/param&gt;</span></li>
  10.                 <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">&lt;param name=&quot;languageKey&quot;&gt;</span><span style="background:#ffffff;color:#008000">????</span><span style="background:#ffffff;color:#808080">&lt;/param&gt;</span></li>
  11.                 <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">&lt;param name=&quot;isShowLineNum&quot;&gt;</span><span style="background:#ffffff;color:#008000">??????</span><span style="background:#ffffff;color:#808080">&lt;/param&gt;</span></li>
  12.                 <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">&lt;returns&gt;</span><span style="background:#ffffff;color:#008000">???????????HTML????</span><span style="background:#ffffff;color:#808080">&lt;/returns&gt;</span></li>
  13.                 <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>
  14.                 <li><span style="background:#ffffff;color:#000000">????????{</span></li>
  15.                 <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>
  16.                 <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#008000">//??????????</span></li>
  17.                 <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">&quot;CodeHighlighterConfig&quot;</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>
  18.                 <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>
  19.                 <li><span style="background:#ffffff;color:#000000">????????????{</span></li>
  20.                 <li><span style="background:#ffffff;color:#000000">????????????????</span><span style="background:#ffffff;color:#008000">//????????? web.config ???????</span></li>
  21.                 <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">&quot;codeHighlighter&quot;</span><span style="background:#ffffff;color:#000000">);</span></li>
  22.                 <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">&quot;CodeHighlighterConfig&quot;</span><span style="background:#ffffff;color:#000000">, config);</span></li>
  23.                 <li><span style="background:#ffffff;color:#000000">????????????}</span></li>
  24.                 <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#008000">//????</span></li>
  25.                 <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>
  26.                 <li><span style="background:#ffffff;color:#000000">????????????{</span></li>
  27.                 <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>
  28.                 <li><span style="background:#ffffff;color:#000000">????????????????{</span></li>
  29.                 <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>
  30.                 <li><span style="background:#ffffff;color:#000000">????????????????????</span><span style="background:#ffffff;color:#0000ff">break</span><span style="background:#ffffff;color:#000000">;</span></li>
  31.                 <li><span style="background:#ffffff;color:#000000">????????????????}</span></li>
  32.                 <li><span style="background:#ffffff;color:#000000">????????????}</span></li>
  33.                 <li><span style="background:#ffffff;color:#000000">????????????</span><span style="background:#ffffff;color:#008000">//??????????</span></li>
  34.                 <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>
  35.                 <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>
  36.                 <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>
  37.                 <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>
  38.                 <li><span style="background:#ffffff;color:#000000">????????????engine.LineNumberMarginVisible = isShowLineNum;</span></li>
  39.                 <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>
  40.                 <li><span style="background:#ffffff;color:#000000">????????}</span></li>
  41.             </ol>
  42.         </div>
  43.     </div>
  44. </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
  1. <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">/// &lt;summary&gt;
  2. <span style="font-weight: bold; color: black">02:  </span>        /// </span><span style="background: white; color: green">??????
  3. <span style="font-weight: bold; color: black">03:  </span>        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
  4. <span style="font-weight: bold; color: black">04:  </span>        /// &lt;param name=&quot;code&quot;&gt;</span><span style="background: white; color: green">??</span><span style="background: white; color: gray">&lt;/param&gt;
  5. <span style="font-weight: bold; color: black">05:  </span>        /// &lt;param name=&quot;languageKey&quot;&gt;</span><span style="background: white; color: green">????</span><span style="background: white; color: gray">&lt;/param&gt;
  6. <span style="font-weight: bold; color: black">06:  </span>        /// &lt;param name=&quot;isShowLineNum&quot;&gt;</span><span style="background: white; color: green">??????</span><span style="background: white; color: gray">&lt;/param&gt;
  7. <span style="font-weight: bold; color: black">07:  </span>        /// &lt;returns&gt;</span><span style="background: white; color: green">???????????HTML????</span><span style="background: white; color: gray">&lt;/returns&gt;
  8. <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)
  9. <span style="font-weight: bold; color: black">09:  </span>        {
  10. <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">;
  11. <span style="font-weight: bold; color: black">11:  </span>            </span><span style="background: white; color: green">//??????????
  12. <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">&quot;CodeHighlighterConfig&quot;</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">;
  13. <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">)
  14. <span style="font-weight: bold; color: black">14:  </span>            {
  15. <span style="font-weight: bold; color: black">15:  </span>                </span><span style="background: white; color: green">//????????? web.config ???????
  16. <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">&quot;codeHighlighter&quot;</span><span style="background: white; color: black">);
  17. <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">&quot;CodeHighlighterConfig&quot;</span><span style="background: white; color: black">, config);
  18. <span style="font-weight: bold; color: black">18:  </span>            }
  19. <span style="font-weight: bold; color: black">19:  </span>            </span><span style="background: white; color: green">//????
  20. <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)
  21. <span style="font-weight: bold; color: black">21:  </span>            {
  22. <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())
  23. <span style="font-weight: bold; color: black">23:  </span>                {
  24. <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);
  25. <span style="font-weight: bold; color: black">25:  </span>                    </span><span style="background: white; color: blue">break</span><span style="background: white; color: black">;
  26. <span style="font-weight: bold; color: black">26:  </span>                }
  27. <span style="font-weight: bold; color: black">27:  </span>            }
  28. <span style="font-weight: bold; color: black">28:  </span>            </span><span style="background: white; color: green">//??????????
  29. <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">)
  30. <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;
  31. <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">();
  32. <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">;
  33. <span style="font-weight: bold; color: black">33:  </span>            engine.LineNumberMarginVisible = isShowLineNum;
  34. <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);
  35. <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
  1. <pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  1:  <span style="color: #808080">/// &lt;summary&gt;</span>
  2. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  2:         <span style="color: #808080">/// ??????</span>
  3. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  3:         <span style="color: #808080">/// &lt;/summary&gt;</span>
  4. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  4:         <span style="color: #808080">/// &lt;param name=&quot;code&quot;&gt;??&lt;/param&gt;</span>
  5. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  5:         <span style="color: #808080">/// &lt;param name=&quot;languageKey&quot;&gt;????&lt;/param&gt;</span>
  6. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  6:         <span style="color: #808080">/// &lt;param name=&quot;isShowLineNum&quot;&gt;??????&lt;/param&gt;</span>
  7. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  7:         <span style="color: #808080">/// &lt;returns&gt;???????????HTML????&lt;/returns&gt;</span>
  8. </pre><pre style="font-size: 12px; font-family: consolas,&#39;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)
  9. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  9:         {
  10. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 10:             SyntaxLanguage lang = <span style="color: #0000ff">null</span>;
  11. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 11:             <span style="color: #008000">//??????????</span>
  12. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 12:             CodeHighlighterConfiguration config = HttpContext.Current.Cache[&quot;<span style="color: #8b0000">CodeHighlighterConfig</span>&quot;] <span style="color: #0000ff">as</span> CodeHighlighterConfiguration;
  13. </pre><pre style="font-size: 12px; font-family: consolas,&#39;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>)
  14. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 14:             {
  15. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 15:                 <span style="color: #008000">//????????? web.config ???????</span>
  16. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 16:                 config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection(&quot;<span style="color: #8b0000">codeHighlighter</span>&quot;);
  17. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 17:                 HttpContext.Current.Cache.Insert(&quot;<span style="color: #8b0000">CodeHighlighterConfig</span>&quot;, config);
  18. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 18:             }
  19. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 19:             <span style="color: #008000">//????</span>
  20. </pre><pre style="font-size: 12px; font-family: consolas,&#39;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)
  21. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 21:             {
  22. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 22:                 <span style="color: #0000ff">if</span> (key.ToLower() == languageKey.ToLower())
  23. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 23:                 {
  24. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 24:                     lang = CodeHighlighter.GetLanguage(config, key);
  25. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 25:                     <span style="color: #0000ff">break</span>;
  26. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 26:                 }
  27. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 27:             }
  28. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 28:             <span style="color: #008000">//??????????</span>
  29. </pre><pre style="font-size: 12px; font-family: consolas,&#39;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>)
  30. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 30:                 <span style="color: #0000ff">return</span> code;
  31. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 31:             CodeHighlighterEngine engine = <span style="color: #0000ff">new</span> CodeHighlighterEngine();
  32. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 32:             engine.OutliningEnabled = <span style="color: #0000ff">false</span>;
  33. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 33:             engine.LineNumberMarginVisible = isShowLineNum;
  34. </pre><pre style="font-size: 12px; font-family: consolas,&#39;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);
  35. </pre><pre style="font-size: 12px; font-family: consolas,&#39;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 }

生成HTML代码

Memento.SourceCode生成的HTML代码
  1. <div onclick="cnblogs_code_show('2f7c88c0-3057-4a33-a098-9af74ed82b16')" class="cnblogs_code">
  2.     <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" />
  3.     <div id="cnblogs_code_open_2f7c88c0-3057-4a33-a098-9af74ed82b16" class="cnblogs_code_hide">
  4.         <pre><span style="color: #008080">  1</span>  <span style="color: #808080">/// &lt;summary&gt;</span>
  5. <span style="color: #008080">  2</span>         <span style="color: #808080">/// ??????</span>
  6. <span style="color: #008080">  3</span>         <span style="color: #808080">/// &lt;/summary&gt;</span>
  7. <span style="color: #008080">  4</span>         <span style="color: #808080">/// &lt;param name=&quot;code&quot;&gt;??&lt;/param&gt;</span>
  8. <span style="color: #008080">  5</span>         <span style="color: #808080">/// &lt;param name=&quot;languageKey&quot;&gt;????&lt;/param&gt;</span>
  9. <span style="color: #008080">  6</span>         <span style="color: #808080">/// &lt;param name=&quot;isShowLineNum&quot;&gt;??????&lt;/param&gt;</span>
  10. <span style="color: #008080">  7</span>         <span style="color: #808080">/// &lt;returns&gt;???????????HTML????&lt;/returns&gt;</span>
  11. <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)
  12. <span style="color: #008080">  9</span>         {
  13. <span style="color: #008080"> 10</span>             SyntaxLanguage lang = <span style="color: #0000ff">null</span>;
  14. <span style="color: #008080"> 11</span>             <span style="color: #008000">//??????????</span>
  15. <span style="color: #008080"> 12</span>             CodeHighlighterConfiguration config = HttpContext.Current.Cache[&quot;<span style="color: #8b0000">CodeHighlighterConfig</span>&quot;] <span style="color: #0000ff">as</span> CodeHighlighterConfiguration;
  16. <span style="color: #008080"> 13</span>             <span style="color: #0000ff">if</span> (config == <span style="color: #0000ff">null</span>)
  17. <span style="color: #008080"> 14</span>             {
  18. <span style="color: #008080"> 15</span>                 <span style="color: #008000">//????????? web.config ???????</span>
  19. <span style="color: #008080"> 16</span>                 config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection(&quot;<span style="color: #8b0000">codeHighlighter</span>&quot;);
  20. <span style="color: #008080"> 17</span>                 HttpContext.Current.Cache.Insert(&quot;<span style="color: #8b0000">CodeHighlighterConfig</span>&quot;, config);
  21. <span style="color: #008080"> 18</span>             }
  22. <span style="color: #008080"> 19</span>             <span style="color: #008000">//????</span>
  23. <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)
  24. <span style="color: #008080"> 21</span>             {
  25. <span style="color: #008080"> 22</span>                 <span style="color: #0000ff">if</span> (key.ToLower() == languageKey.ToLower())
  26. <span style="color: #008080"> 23</span>                 {
  27. <span style="color: #008080"> 24</span>                     lang = CodeHighlighter.GetLanguage(config, key);
  28. <span style="color: #008080"> 25</span>                     <span style="color: #0000ff">break</span>;
  29. <span style="color: #008080"> 26</span>                 }
  30. <span style="color: #008080"> 27</span>             }
  31. <span style="color: #008080"> 28</span>             <span style="color: #008000">//??????????</span>
  32. <span style="color: #008080"> 29</span>             <span style="color: #0000ff">if</span> (lang == <span style="color: #0000ff">null</span>)
  33. <span style="color: #008080"> 30</span>                 <span style="color: #0000ff">return</span> code;
  34. <span style="color: #008080"> 31</span>             CodeHighlighterEngine engine = <span style="color: #0000ff">new</span> CodeHighlighterEngine();
  35. <span style="color: #008080"> 32</span>             engine.OutliningEnabled = <span style="color: #0000ff">false</span>;
  36. <span style="color: #008080"> 33</span>             engine.LineNumberMarginVisible = isShowLineNum;
  37. <span style="color: #008080"> 34</span>             <span style="color: #0000ff">return</span> engine.GenerateHtmlInline(<span style="color: #0000ff">string</span>.Empty, code, lang);
  38. <span style="color: #008080"> 35</span>         }</pre>
  39.     </div>
  40.     <span class="cnblogs_code_collapse"></span>
  41. </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代码
  1. <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">
  2.     <pre><span style="color: #808080">///</span> <span style="color: #808080">&lt;summary&gt;</span>
  3.     <span style="color: #808080">///</span><span style="color: #008000"> ??????
  4.         </span><span style="color: #808080">///</span> <span style="color: #808080">&lt;/summary&gt;</span>
  5.     <span style="color: #808080">///</span> <span style="color: #808080">&lt;param name=&quot;code&quot;&gt;</span><span style="color: #008000">??</span><span style="color: #808080">&lt;/param&gt;</span>
  6.     <span style="color: #808080">///</span> <span style="color: #808080">&lt;param name=&quot;languageKey&quot;&gt;</span><span style="color: #008000">????</span><span style="color: #808080">&lt;/param&gt;</span>
  7.     <span style="color: #808080">///</span> <span style="color: #808080">&lt;param name=&quot;isShowLineNum&quot;&gt;</span><span style="color: #008000">??????</span><span style="color: #808080">&lt;/param&gt;</span>
  8.     <span style="color: #808080">///</span> <span style="color: #808080">&lt;returns&gt;</span><span style="color: #008000">???????????HTML????</span><span style="color: #808080">&lt;/returns&gt;</span>
  9.     <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)
  10.         {
  11.             SyntaxLanguage lang </span>= <span style="color: #0000ff">null</span><span style="color: #000000">;
  12.             </span><span style="color: #008000">//</span><span style="color: #008000">??????????</span>
  13.             CodeHighlighterConfiguration config = HttpContext.Current.Cache[<span style="color: #800000">&quot;</span><span style="color: #800000">CodeHighlighterConfig</span><span style="color: #800000">&quot;</span>] <span style="color: #0000ff">as</span><span style="color: #000000"> CodeHighlighterConfiguration;
  14.             </span><span style="color: #0000ff">if</span> (config == <span style="color: #0000ff">null</span><span style="color: #000000">)
  15.             {
  16.                 </span><span style="color: #008000">//</span><span style="color: #008000">????????? web.config ???????</span>
  17.                 config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection(<span style="color: #800000">&quot;</span><span style="color: #800000">codeHighlighter</span><span style="color: #800000">&quot;</span><span style="color: #000000">);
  18.                 HttpContext.Current.Cache.Insert(</span><span style="color: #800000">&quot;</span><span style="color: #800000">CodeHighlighterConfig</span><span style="color: #800000">&quot;</span><span style="color: #000000">, config);
  19.             }
  20.             </span><span style="color: #008000">//</span><span style="color: #008000">????</span>
  21.     <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)
  22.             {
  23.                 </span><span style="color: #0000ff">if</span> (key.ToLower() ==<span style="color: #000000"> languageKey.ToLower())
  24.                 {
  25.                     lang </span>=<span style="color: #000000"> CodeHighlighter.GetLanguage(config, key);
  26.                     </span><span style="color: #0000ff">break</span><span style="color: #000000">;
  27.                 }
  28.             }
  29.             </span><span style="color: #008000">//</span><span style="color: #008000">??????????</span>
  30.     <span style="color: #0000ff">if</span> (lang == <span style="color: #0000ff">null</span><span style="color: #000000">)
  31.                 </span><span style="color: #0000ff">return</span><span style="color: #000000"> code;
  32.             CodeHighlighterEngine engine </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> CodeHighlighterEngine();
  33.             engine.OutliningEnabled </span>= <span style="color: #0000ff">false</span><span style="color: #000000">;
  34.             engine.LineNumberMarginVisible </span>=<span style="color: #000000"> isShowLineNum;
  35.             </span><span style="color: #0000ff">return</span> engine.GenerateHtmlInline(<span style="color: #0000ff">string</span><span style="color: #000000">.Empty, code, lang);
  36.         }</span></pre>
  37. </div>

 

写在最后的话

这个效果不知道怎么实现,还请知晓的朋友指点

 

 

本文参考:http://www.cnblogs.com/lyj/archive/2008/11/30/1344463.html

 

 

 

来源:http://www.cnblogs.com/GarsonZhang/

Windows Live Writer代码高亮插件对比的更多相关文章

  1. 用 Windows Live Writer 和 SyntaxHighlighter 插件写高亮代码

    博客园内置支持SyntaxHighlighter代码着色,代码着色语法:<pre class='brush:编程语言'>代码</pre>. 需要注意的是:如何你使用Syntax ...

  2. Windows Live Writer 语法高亮

    1.WindowsLiveWriter.CNBlogs.CodeHighlighter.rar 这个插件生成的高亮代码与网页上的一模一样,插入后即可立即显示效果,不过貌似它必须联网才能实时显示效果,因 ...

  3. OLW (Open Live Writer)安装代码高亮插件方法(简明)

    1.首先下载OLW代码高亮插件,请点击--->OLW代码高亮插件 2.在你安装OLW的目录下(顺便说一下默认的安装目录为C:\Users\你的用户名\AppData\Local\OpenLive ...

  4. 一次查找Windows Live Writer的VSPaste插件丢失RTF格式信息的经历

    背景 我在博客园上写博客是使用Windows Live Writer,代码高亮插件是使用Paste from Visual Studio(下文简称VSPaste). Windows Live Writ ...

  5. Windows Live Writer的Markdown插件

    我新写了一个Windows Live Writer的Markdown插件,代码放在了github上. 介绍 这个项目是一个Windows Live Writer的Markdown插件.有了这个插件,你 ...

  6. open live write 代码高亮插件的配置和使用

    第一步:下载open live writer插件,下载地址:http://www.cnblogs.com/memento/p/5995173.html 第二步:找到open live writer的安 ...

  7. 7个高性能JavaScript代码高亮插件

    本文由码农网 – 小峰原创,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 对于喜欢写技术博客的同学来说,一定对代码高亮组件非常熟悉.一款优秀的JavaScript代码高亮插件,将会帮助你渲染 ...

  8. 轻量级jQuery语法高亮代码高亮插件jQuery Litelighter。

    <!DOCTYPE html><html><head><meta charset="UTF-8" /><title>jQ ...

  9. [转]7个高性能JavaScript代码高亮插件

    对于喜欢写技术博客的同学来说,一定对代码高亮组件非常熟悉.一款优秀的JavaScript代码高亮插件,将会帮助你渲染任何一种编程语言,包括一些关键字的着色,以及每行代码的缩进等.今天我们要来分享一些高 ...

随机推荐

  1. PAT乙级 1027. 打印沙漏(20)

    1027. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...

  2. 我的代码观——关于ACM编程风格与librazy网友的对话

    序 在拙文 <高手看了,感觉惨不忍睹——关于“[ACM]杭电ACM题一直WA求高手看看代码”>中,我对ACMer们的一些代码“惯例”发表了我的看法, librazy网友在评论中给出了他的一 ...

  3. scala特质

    package com.ming.test /** * scala 特质,类似与java接口,但是比java接口强大,可以有实现方法,定义字段之类的 */ /** * 定义一个日志的特质 */ tra ...

  4. JS实现复制网页内容自动加入版权内容代码和原文链接

    JS实现复制网页内容自动加入版权内容代码和原文链接 实现代码:在body内放入如下代码即可: <script type="text/javascript"> var S ...

  5. win32 Ui 编程 收集

    ---恢复内容开始--- windows sdk编程系列---- 通用控件 http://www.cnblogs.com/MoreNotepad-plus-plus/articles/3164534. ...

  6. alias function varibales in Linux/GNU and Mac alias命令细说

    细说,在古文言中是”奸细佞臣的话“,现如今成了”详细说明“的缩略. alias是MS-DOC中cmds中doskey的counterpart,是”别名“或者”化名“的意思 alias强大之处在于可以化 ...

  7. How to create UrlSlug in Asp.Net MVC

    转自:http://www.ehsanghanbari.com/Post/20/how-to-create-urlslug-in-aspnet-mvc UrlSlug Is a way of gene ...

  8. 使用compile_scripts.php脚本,生成lua打包的zip,解决加密问题

    @echo off set DIR=%~dp0 set TEMPLATE_ROOT=%DIR%.. echo %TEMPLATE_ROOT%\quick\bin\win32\php.exe echo ...

  9. fastjson和json-lib的区别

    上次把原生json替换成了fastjson,发生很多地方不兼容,对这个也做了一些总结: 1.对于没有赋值的变量处理,json-lib会根据类型给出相应初始值,而fastjson直接忽略这个字段. 解决 ...

  10. HttpConnection方式访问网络

    参考疯狂android讲义,重点在于学习1.HttpConnection访问网络2.多线程下载文件的处理 主activity: package com.example.multithreaddownl ...