• 00
  • :
  • 00
  • :
  • 00

Windows Live Writer代码高亮插件对比

一、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&#39;,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&#39;,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&#39;,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&#39;,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&#39;,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&#39;,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&#39;,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&#39;,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&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">  9:         {
  10. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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&#39;,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&#39;,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&#39;,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&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 14:             {
  15. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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&#39;,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&#39;,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&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 18:             }
  19. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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&#39;,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&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 21:             {
  22. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 23:                 {
  24. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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&#39;,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&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 26:                 }
  27. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> 27:             }
  28. </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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&#39;,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&#39;,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&#39;,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&#39;,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&#39;,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&#39;,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&#39;,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代码
  1. <div onclick="cnblogs_code_show(&#39;2f7c88c0-3057-4a33-a098-9af74ed82b16&#39;)" 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(&#39;2f7c88c0-3057-4a33-a098-9af74ed82b16&#39;,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.  
  4.     <div id="cnblogs_code_open_2f7c88c0-3057-4a33-a098-9af74ed82b16" class="cnblogs_code_hide">
  5.         <pre><span style="color: #008080">  1</span>  <span style="color: #808080">/// &lt;summary&gt;</span>
  6. <span style="color: #008080">  2</span>         <span style="color: #808080">/// ??????</span>
  7. <span style="color: #008080">  3</span>         <span style="color: #808080">/// &lt;/summary&gt;</span>
  8. <span style="color: #008080">  4</span>         <span style="color: #808080">/// &lt;param name=&quot;code&quot;&gt;??&lt;/param&gt;</span>
  9. <span style="color: #008080">  5</span>         <span style="color: #808080">/// &lt;param name=&quot;languageKey&quot;&gt;????&lt;/param&gt;</span>
  10. <span style="color: #008080">  6</span>         <span style="color: #808080">/// &lt;param name=&quot;isShowLineNum&quot;&gt;??????&lt;/param&gt;</span>
  11. <span style="color: #008080">  7</span>         <span style="color: #808080">/// &lt;returns&gt;???????????HTML????&lt;/returns&gt;</span>
  12. <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)
  13. <span style="color: #008080">  9</span>         {
  14. <span style="color: #008080"> 10</span>             SyntaxLanguage lang = <span style="color: #0000ff">null</span>;
  15. <span style="color: #008080"> 11</span>             <span style="color: #008000">//??????????</span>
  16. <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;
  17. <span style="color: #008080"> 13</span>             <span style="color: #0000ff">if</span> (config == <span style="color: #0000ff">null</span>)
  18. <span style="color: #008080"> 14</span>             {
  19. <span style="color: #008080"> 15</span>                 <span style="color: #008000">//????????? web.config ???????</span>
  20. <span style="color: #008080"> 16</span>                 config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection(&quot;<span style="color: #8b0000">codeHighlighter</span>&quot;);
  21. <span style="color: #008080"> 17</span>                 HttpContext.Current.Cache.Insert(&quot;<span style="color: #8b0000">CodeHighlighterConfig</span>&quot;, config);
  22. <span style="color: #008080"> 18</span>             }
  23. <span style="color: #008080"> 19</span>             <span style="color: #008000">//????</span>
  24. <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)
  25. <span style="color: #008080"> 21</span>             {
  26. <span style="color: #008080"> 22</span>                 <span style="color: #0000ff">if</span> (key.ToLower() == languageKey.ToLower())
  27. <span style="color: #008080"> 23</span>                 {
  28. <span style="color: #008080"> 24</span>                     lang = CodeHighlighter.GetLanguage(config, key);
  29. <span style="color: #008080"> 25</span>                     <span style="color: #0000ff">break</span>;
  30. <span style="color: #008080"> 26</span>                 }
  31. <span style="color: #008080"> 27</span>             }
  32. <span style="color: #008080"> 28</span>             <span style="color: #008000">//??????????</span>
  33. <span style="color: #008080"> 29</span>             <span style="color: #0000ff">if</span> (lang == <span style="color: #0000ff">null</span>)
  34. <span style="color: #008080"> 30</span>                 <span style="color: #0000ff">return</span> code;
  35. <span style="color: #008080"> 31</span>             CodeHighlighterEngine engine = <span style="color: #0000ff">new</span> CodeHighlighterEngine();
  36. <span style="color: #008080"> 32</span>             engine.OutliningEnabled = <span style="color: #0000ff">false</span>;
  37. <span style="color: #008080"> 33</span>             engine.LineNumberMarginVisible = isShowLineNum;
  38. <span style="color: #008080"> 34</span>             <span style="color: #0000ff">return</span> engine.GenerateHtmlInline(<span style="color: #0000ff">string</span>.Empty, code, lang);
  39. <span style="color: #008080"> 35</span>         }</pre>
  40.     </div>
  41.     <span class="cnblogs_code_collapse">View Code</span>
  42. </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>

 

写在最后的话

11

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

 

 

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

 

 

 

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

posted @ 2015-03-24 11:03  Garson_Zhang  阅读(183)  评论(0编辑  收藏  举报