文章出處

1、OpenLiveWriter安裝

     Windows Live Writer在2012年就停止了更新,Open Live Writer(以下簡稱OLW)是由Windows Live WriterWriter更名而來,是由微軟推出的一款能夠免費使用的博客寫作軟件,主要為用戶提供博客在線撰寫和編輯功能,相比Windows Live Writer,OLW首個版本仍然缺少一些功能,不過團隊已經制訂了更新路線圖,一些新功能會陸續推出。相信以后他將是一個寫博客的好利器。

    但從github源代碼(https://github.com/OpenLiveWriter/OpenLiveWriter)來看,已經有9個月未更新了,而官網更是未見一個插件,“錢途”堪憂呀。

    官網地址:http://openlivewriter.org/ 點擊download下載:https://openlivewriter.azureedge.net/stable/Releases/OpenLiveWriterSetup.exe

    image

    image(界面血漂亮)

   默認安裝到C:\Users\用戶\AppData\Local\OpenLiveWriter目錄,結構如下:

   image

    標紅色的是OLW的主程序:

    image

    雙擊OpenLiveWriter.exe即可打開OLW編輯器:

    image

    作為一個開發人員,對代碼進行著色是不可缺少的,如何在OLW下實現插入代碼并著色呢?

2、如何實現代碼著色

   前奏:從cnblogs的官網獲取,在windows live writer下,可用WindowsLiveWriter.CNBlogs.CodeHighlighter進行代碼著色,是有有效的,詳情查看:http://www.cnblogs.com/cmt/archive/2009/11/27/1611900.html

   實驗:將WindowsLiveWriter.CNBlogs.CodeHighlighter.dll插件放到C:\Users\用戶\AppData\Local\OpenLiveWriter\app-0.6.0.0\Plugins目錄下,啟動并未有見插件

   image

    分析:通過調試OLW的源碼代碼調試,加載插件出現異常;通過ILSpy分析WindowsLiveWriter.CNBlogs.CodeHighlighter.dll,如下圖

image

   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

   插件安裝之后:

   image

    image

3、代碼著色項目下載

3.1 項目下載

   (1)原始項目下載地址:http://sourcecodeplugin.codeplex.com/SourceControl/latest

    image

    點擊“download”下載,只需按照3.2修訂WindowsLiveWriter.SourceCode項目編譯。

   (2)修改后的項目可從github下載:

    https://github.com/zsy619/OpenLiveWriter.SourceCode,下載編輯即可使用。

3.2 編譯配置

   (1)修改類庫引用,WindowsLive.Writer.Api.dll更改為OpenLiveWriter.Api.dll(可以從OLW安裝的目錄下找到)

   (2)修改輸出地址:

   image

    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();
        }

3.3 使用效果

    C#代碼展示如上,目前CNBlogs官網http://util.cnblogs.com/CodeHighlight/LiveWriterHightlight接口還不支持go語言,有點遺憾,期待更新!

    溫馨提示:有用就幫忙推薦一下,謝謝~~


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()