您的位置:首页 > 其它

一个简单的Windows Live Writer插件

2010-05-14 14:37 495 查看
我们在使用Windows Live Writer写技术博客时,粘贴代码最好的方法就是使用VSPaste插件。它的使用方法非常简单,只需要在Visual Studio中复制代码,然后在Windows Live Writer中点击“插入”,再点击“Paste from Visual Studio”就可以了。由于复制的代码为富文本格式(Rich Text Format,RTF),插件会根据文本内容生成相应的HTML,最终粘贴在Writer中的代码配色和你的Visual Studio种的配色是一样的,这样的体验非常不错。而这些代码会放置在一个class为code的pre标记中,我们可以在CSS中进行设置,得到想要的代码块样式。比如:

class SomeClass
{
public SomeClass()
{
SomeMethod();
}

public string SomeMethod()
{
return "Some String";
}
}




由于是对富文本进行处理,因此VSPaste可以粘贴的内容不局限于从Visual Studio中拷贝。Eclipse(或其他IDE)、Word中的内容同样可以通过VSPaste粘贴到Writer中。以下代码来自Eclipse。

public class SomeClass {
public SomeClass() {
someMethod();
}

public String someMethod() {
return "Some String";
}
}




我们在写博客时有时还希望将一些其他内容也显示在这样的块中,如命令行指令、代码的生成结果等等。但这些复制过来很可能不是RTF,这样就不能使用VSPaste来进行粘贴了。因为VSPaste的核心方法是这样的:

public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
{
try
{
if (Clipboard.ContainsData(DataFormats.Rtf))
{
newContent = "<pre class=\"code\">" +
Undent(HTMLRootProcessor.FromRTF((string) Clipboard.GetData(DataFormats.Rtf))) +
"</pre><a href=\"http://11011.net/software/vspaste\"></a>";
return DialogResult.OK;
}
}
catch
{
MessageBox.Show("VS Paste could not convert that content.", "VS Paste Problem",
MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
return DialogResult.Cancel;
}

可见如果不是DataFormats.Rft,VSPaste是不会进行任何处理的。

我之前遇到这样的情况,都是手动在源代码里添加<pre class=”code”></pre>。但加得多了,就厌烦了。也可以将其粘贴到Visual Studio中,然后再复制,但这同样很麻烦。那么何不自己来写一个类似的插件,对所有格式的文本都能将它们放到到一个<pre class=”code”></pre>中。这样的代码写起来简直太容易了。

在Windows Live Writer安装目录下有一个名为WindowsLive.Writer.Api.dll的程序集,我们只要在类库项目中引用这个程序集,就可以进行插件开发了。即使你不知道这些也无所谓,因为反编译VSPaste之后的代码已经明明白白了,我们完全可以在此基础之上进行修改。

namespace KirinStudio.WriterPlugins.PasteAsCode
{
[WriterPlugin("6864d51f-abc8-4fe7-9c64-440210c871f2", "PasteAsCode")]
[InsertableContentSource("PasteAsCode", SidebarText = "PasteAsCode")]
public class Paster : ContentSource
{
public override DialogResult CreateContent(
IWin32Window dialogOwner,
ref string content)
{
try
{
if (Clipboard.ContainsData(DataFormats.Text))
{
content = "<pre class=\"code\">" +
(string)Clipboard.GetData(DataFormats.Text) + "</pre>";
return DialogResult.OK;
}
}
catch
{
MessageBox.Show(
"Cuould not paste the content.",
"PasteAsCode Problem",
MessageBoxButtons.OK,
MessageBoxIcon.Hand);
}
return DialogResult.Cancel;
}
}
}




到这里我们的插件PasteAsCode就写完了。将其编译成dll,放到

C:\Program Files\Windows Live\Writer\Plugins

就可以了(上面这段就是用PasteAsCode粘贴的,怎么样,方便吧?)。

重启Windows Live Writer,在“插入”菜单下可以看到我们的PasteAsCode。

需要注意的是:

如果你是使用Visual Studio 2010进行开发的,注意要在项目属性中把Target framework该为.NET Framework 3.5或以下版本。

WriterPluginAttribute的第一个参数必须为GUID,否则在启动Windows Live Writer时会报错。

要想在“插入”菜单下显示插件,则必须使用InsertableContentSourceAttribute。

通过继承ContentSource并重载其中的方法,就可以进行插件开发了。

更多的类库介绍和示例在Windows Live Writer SDK中。

此外,如果你不希望VSPaste每次都在你的页面上加上广告的话,还可以对VSPaste进行扩展,去除广告链接,如:

namespace KirinStudio.WriterPlugins.VSPasteWithoutAd
{
[WriterPlugin("fe534664-64ea-4048-9d3a-74ae3f51279e", "VSPasteWithoutAd")]
[InsertableContentSource("VSPasteWithoutAd", SidebarText = "VSPasteWithoutAd")]
public class Paster : VSPaste.VSPaste
{
public override DialogResult CreateContent(
IWin32Window dialogOwner,
ref string newContent)
{
DialogResult dr = base.CreateContent(dialogOwner, ref newContent);
newContent = newContent.Remove(
newContent.IndexOf("<a href=\"http://11011.net/software/vspaste\"></a>"));
return dr;
}
}
}

参考资料:

Windows Live Writer 插件开发入门

定制Paste from Visual Studio插件(上)

定制Paste from Visual Studio插件(下)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: