Windows Live Writer在2012年就中止了更新,Open Live Writer(如下簡稱OLW)是由Windows Live WriterWriter改名而來,是由微軟推出的一款可以無償使用的博客寫做軟件,主要爲用戶提供博客在線撰寫和編輯功能,相比Windows Live Writer,OLW首個版本仍然缺乏一些功能,不過團隊已經制訂了更新路線圖,一些新功能會陸續推出。相信之後他將是一個寫博客的好利器。 html
但從github源代碼(https://github.com/OpenLiveWriter/OpenLiveWriter)來看,已經有9個月未更新了,而官網更是未見一個插件,「錢途」堪憂呀。 git
官網地址:http://openlivewriter.org/ 點擊download下載:https://openlivewriter.azureedge.net/stable/Releases/OpenLiveWriterSetup.exe github
默認安裝到C:\Users\用戶\AppData\Local\OpenLiveWriter目錄,結構以下: app
標紅色的是OLW的主程序: 優化
雙擊OpenLiveWriter.exe便可打開OLW編輯器: this
做爲一個開發人員,對代碼進行着色是不可缺乏的,如何在OLW下實現插入代碼並着色呢? url
前奏:從cnblogs的官網獲取,在windows live writer下,可用WindowsLiveWriter.CNBlogs.CodeHighlighter進行代碼着色,是有有效的,詳情查看:http://www.cnblogs.com/cmt/archive/2009/11/27/1611900.html。 .net
實驗:將WindowsLiveWriter.CNBlogs.CodeHighlighter.dll插件放到C:\Users\用戶\AppData\Local\OpenLiveWriter\app-0.6.0.0\Plugins目錄下,啓動並未有見插件
分析:經過調試OLW的源碼代碼調試,加載插件出現異常;經過ILSpy分析WindowsLiveWriter.CNBlogs.CodeHighlighter.dll,以下圖
WindowsLiveWriter.CNBlogs.CodeHighlighter.dll引用WindowsLive.Writer.Api,與現有OLW的新接口OpenLiveWriter.Api不匹配。
解決方案一:反編譯WindowsLiveWriter.CNBlogs.CodeHighlighter.dll修改引用類庫,將WindowsLive.Writer.Api.dll更改成OpenLiveWriter.Api.dll,不建議使用本方法。
解決方案二:因爲是從Windows Live Writer Source Code plugin for SyntaxHighlighter(http://sourcecodeplugin.codeplex.com/)進行優化而來,能夠從本開源項目進行優化。
封裝編譯以後的dll爲:OpenLiveWriter.CNBlogs.SourceCode.dll,下載地址爲:OpenLiveWriter.CNBlogs.SourceCode.zip
插件安裝以後:
(1)原始項目下載地址:http://sourcecodeplugin.codeplex.com/SourceControl/latest
點擊「download」下載,只需按照3.2修訂WindowsLiveWriter.SourceCode項目編譯。
(2)修改後的項目可從github下載:
https://github.com/zsy619/OpenLiveWriter.SourceCode,下載編輯便可使用。
(1)修改類庫引用,WindowsLive.Writer.Api.dll更改成OpenLiveWriter.Api.dll(能夠從OLW安裝的目錄下找到)
(2)修改輸出地址:
OpenLiveWriter.CNBlogs.SourceCode類庫輸出地址:copy "$(TargetPath)" "C:\Users\xxtt\AppData\Local\OpenLiveWriter\app-0.6.0.0\Plugins"
(3)CodeForm窗體代碼修訂
修改Code屬性:
public string Code { get { return this._code; } set { this._code = value; } }
新增博客園的遠程代碼着色方法(可參考WindowsLiveWriter.CNBlogs.CodeHighlighter.dll):
private string RemoteCodeHighlight() { string requestUriString = "http://util.cnblogs.com/CodeHighlight/LiveWriterHightlight"; HttpWebRequest httpWebRequest = WebRequest.Create(requestUriString) as HttpWebRequest; httpWebRequest.Method = "POST"; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; string value = string.Format("language={0}&code={1}", HttpUtility.UrlEncode(this.comboBrush.Text.Trim()), HttpUtility.UrlEncode(this.textCode.Text.Trim())); using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write(value); } string result; using (WebResponse response = httpWebRequest.GetResponse()) { using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { result = streamReader.ReadToEnd(); } } return result; }
修改buttonOK_Click方法:
private void buttonOK_Click(object sender, EventArgs e) { this._configDb.Config.Brush = this.comboBrush.Text; this._configDb.Config.MainFormX = base.Left; this._configDb.Config.MainFormY = base.Top; this._configDb.Config.MainFormWidth = base.Width; this._configDb.Config.MainFormHeight = base.Height; this._configDb.SavePluginConfigurationData(); try { this._code = this.RemoteCodeHighlight(); } catch (Exception exception) { MessageBox.Show(exception.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } base.DialogResult = DialogResult.OK; base.Close(); }
C#代碼展現如上,目前CNBlogs官網http://util.cnblogs.com/CodeHighlight/LiveWriterHightlight接口還不支持go語言,有點遺憾,期待更新!