Windows Live Writer的一个代码语法高亮的插件——CodeInLiveWriter

我平时经常用Windows Live Writer来写博客,但是插入代码很麻烦,用了一些插件也都有些不满意的地方,我就自己写了一个,可以从这里找到如何给Windows Live Writer写plugin。

我这个程序基于hilite.me提供的API,hilite.me内部使用了PygmentsPygments也被用于github的代码显示。

另外SyntaxHighlighter也是个被广泛应用的代码高亮的工具。

使用方法:

下载CodeInLiveWriter.dll,然后放在[WindowsLiveWriterPath]\Plugins\目录下就行了。源代码放在了GitHub上。

目前内建可选的语言有:

  • c#
  • c++
  • java
  • python
  • ruby
  • c
  • sql
  • html
  • xml
  • batch
  • bash

内建支持的格式:

  • vs
  • colorful
  • emacs
  • vim

支持用户自定义语言和格式,分别存在 CodeInLiveWriterStyle.txtCodeInLiveWriterLanguage.txt,然后和CodeInLiveWriter.dll放在同一个文件夹下。具体参见Pygments支持的语言 and 格式

主要的代码如下:(就是用这个插件把代码插进来的:))

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
        private string getHtmlStyleCode(string language, string code, bool showline, string style)
        {
            try
            {
                HttpWebRequest httpWebRequest = WebRequest.Create("http://hilite.me/api") as HttpWebRequest;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";

                string parameters = string.Format("lexer={0}&code={1}&{2}&style={3}",
                                                  HttpUtility.UrlEncode(language),
                                                  HttpUtility.UrlEncode(code),
                                                  showline ? "&linenos=1" : "",
                                                  style);

                using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(parameters);
                }
                string result;
                using (WebResponse response = httpWebRequest.GetResponse())
                {
                    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        result = streamReader.ReadToEnd();
                    }
                }
                return result;
            }
            catch
            {
                return code;
            }
        }
原文地址:https://www.cnblogs.com/fresky/p/3129231.html