<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SharpLab. &#187; InvalidSourceModifier</title>
	<atom:link href="http://blog.sharplab.net/category/computer/cprograming/windowslivewriter/invalidsourcemodifier/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sharplab.net</link>
	<description>Shiroicaが学び、経験したことを綴る個人サイト。</description>
	<lastBuildDate>Thu, 12 Jan 2012 00:57:03 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WLW編集画面のHTMLDocumentを操作する上での障害</title>
		<link>http://blog.sharplab.net/blog/2008/06/21/251/</link>
		<comments>http://blog.sharplab.net/blog/2008/06/21/251/#comments</comments>
		<pubDate>Sat, 21 Jun 2008 06:23:12 +0000</pubDate>
		<dc:creator>shiroica</dc:creator>
				<category><![CDATA[InvalidSourceModifier]]></category>

		<guid isPermaLink="false">http://blog.sharplab.net/computer/cprograming/windowslivewriter/invalidsourcemodifier/251/</guid>
		<description><![CDATA[このまえ「Windows Live WriterプラグインからエントリのHTMLDocumentを取得する方法」というエントリでWLW編集画面のHTMLDocumentオブジェクトを取得する方法を紹介したけれども、その方法にはまだ解決できていない問題があり、今回は自分がどこで躓いているのかを説明しつつ嘆くエントリ(笑)。 さて、現在のInvalidSourceModifierは以下のようなソースになっている。 extern alias MicrosoftMshtml; extern alias WriterInteropMshtml; using System; using WindowsLive.Writer.Api; using System.Windows.Forms; using MicrosoftMshtml.mshtml; using WindowsLive.Writer.PostEditor; using WriterMshtml = WindowsLive.Writer.Mshtml; namespace InvalidSourceModifier { //TODO:ImagePathの設定！ [WriterPlugin("9388e56b-c684-4e04-b7f6-b8cc224d70b2", "InvalidSourceModifier", PublisherUrl = "http://www.sharplab.net/")] public class InvalidSourceModifier : PublishNotificationHook { public override bool OnPrePublish(System.Windows.Forms.IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish) { PostEditorForm postEditorForm = getPostEditorForm(dialogOwner); [...]]]></description>
			<content:encoded><![CDATA[<p>このまえ「<a href="http://blog.sharplab.net/computer/cprograming/windowslivewriter/217/">Windows Live WriterプラグインからエントリのHTMLDocumentを取得する方法</a>」というエントリでWLW編集画面のHTMLDocumentオブジェクトを取得する方法を紹介したけれども、その方法にはまだ解決できていない問題があり、今回は自分がどこで躓いているのかを説明しつつ嘆くエントリ(笑)。</p>
<p>さて、現在のInvalidSourceModifierは以下のようなソースになっている。</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:df7a0bf0-3fe8-421c-b631-0f38269a3a81" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">extern alias MicrosoftMshtml;
extern alias WriterInteropMshtml;

using System;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
using MicrosoftMshtml.mshtml;
using WindowsLive.Writer.PostEditor;

using WriterMshtml = WindowsLive.Writer.Mshtml;

namespace InvalidSourceModifier {
    //TODO:ImagePathの設定！
    [WriterPlugin("9388e56b-c684-4e04-b7f6-b8cc224d70b2", "InvalidSourceModifier", PublisherUrl = "http://www.sharplab.net/")]
    public class InvalidSourceModifier : PublishNotificationHook {
        public override bool OnPrePublish(System.Windows.Forms.IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish) {

            PostEditorForm postEditorForm = getPostEditorForm(dialogOwner);
            WriterMshtml.MshtmlControl mshtmlControl = (WriterMshtml.MshtmlControl)findControlByName(postEditorForm, "MshtmlControl", "WindowsLive.Writer.Mshtml");

            IHTMLDocument3 doc = (IHTMLDocument3)mshtmlControl.HTMLDocument;

            IHTMLElementCollection allElement = doc.getElementsByTagName("*");
            foreach (IHTMLElement elem in allElement) {
                object align = elem.getAttribute("align", 0);
                object border = elem.getAttribute("border", 0);
                if (!(align == null || align is DBNull)) {
                    elem.removeAttribute("align", 0);
                    elem.style.styleFloat = (string)align;
                }
                if (!(border == null || border is DBNull)) {
                    elem.removeAttribute("border", 0);
                    elem.style.borderWidth = (string)border;
                }
            }

            updateTextEditorControl(postEditorForm);

            return true;
        }

        private void updateTextEditorControl(PostEditorForm postEditorForm) {

            WriterMshtml.MshtmlEditor mshtmlEditor = (WriterMshtml.MshtmlEditor)findControlByName(postEditorForm, "MshtmlEditor", "WindowsLive.Writer.Mshtml");
            mshtmlEditor.Focus();

            Control test = findControlByName(postEditorForm, "PostEditorMainControl", "WindowsLive.Writer.PostEditor");

        }

        private PostEditorForm getPostEditorForm(IWin32Window postProgressForm) {
            return (PostEditorForm)((Form)Form.FromHandle(postProgressForm.Handle)).Owner;
        }

        private Control findControlByName(Control control, string controlName, string controlNamespace) {

            foreach (Control cChild in control.Controls) {

                Type t = cChild.GetType();

                if (t.Name == controlName &amp;&amp; t.Namespace == controlNamespace) {
                    return cChild;
                }

                if (cChild.HasChildren) {
                    Control returned = findControlByName(cChild, controlName, controlNamespace);
                    if (returned != null) {
                        return returned;
                    }
                }
            }

            return null;
        }

    }

}</pre>
</div>
<p>まず、getPostEditorFormメソッドで投稿の進捗状況を表すプログレスバーを載せたFormを基に、親のPostEditorFormを取得、その後、findControlByNameメソッドを使い、PostEditorForm内から、nameとnamespaceを頼りにMshtmlControlコントロールを探し、そのHTMLDocumentプロパティから取得する、というやり方。</p>
<p>これでめでたく「標準」モードや、「Webプレビュー」モードのHTMLDocumentオブジェクトが習得できるのだけれども、HTMLDocumentオブジェクトに対して操作を加えると、「HTMLコード」画面での内容と、HTMLDocumentの不整合が起きてしまう。特に、表示モードを「HTMLコード」としていた場合、HTMLDocumentに対してプラグインから操作後に表示モードを「標準」に戻すと、HTMLDocumentに対する操作内容が飛んでしまう。</p>
<p>む～。なんとかならんものか。誰か知恵をお貸しくださいませ。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sharplab.net/blog/2008/06/21/251/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>publish notification hooksではpost内容を書き換えられない</title>
		<link>http://blog.sharplab.net/blog/2008/06/15/216/</link>
		<comments>http://blog.sharplab.net/blog/2008/06/15/216/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 15:27:54 +0000</pubDate>
		<dc:creator>shiroica</dc:creator>
				<category><![CDATA[InvalidSourceModifier]]></category>

		<guid isPermaLink="false">http://blog.sharplab.net/computer/cprograming/windowslivewriter/invalidsourcemodifier/216/</guid>
		<description><![CDATA[自分の間抜けさに笑える。 publish notification hooksを使ったプラグインを作ってみようということで、LiveWriterの出力するソースを投稿時に書き換えるプラグイン（InvalidSourceModifier）をつくることにした。 とか言って実際に作っていたのだけれども、いざ形になってきてコンパイルしてみると、 エラー&#160;&#160;&#160; 1&#160;&#160;&#160; プロパティまたはインデクサ &#8216;WindowsLive.Writer.Api.IPostInfo.Contents&#8217; は読み取り専用なので、割り当てることはできません。 というオチ。OnPrePublishメソッドをオーバーライドすれば投稿前にコンテンツの中身を書き換えられるという訳じゃなかったんかい！勘違いでした…。 &#160; どーするかなー。後はhookする対象をOnPostPublishメソッドに変更したうえで、投稿後直ぐXML-RPCで直接WordPress上のコンテンツを書きかえる位しかやりようがないかな･･･。つうかそれならWordPressのプラグインをPHPで書いた方が早いか(笑)]]></description>
			<content:encoded><![CDATA[<p>自分の間抜けさに笑える。</p>
<blockquote><p>publish notification hooksを使ったプラグインを作ってみようということで、LiveWriterの出力するソースを投稿時に書き換えるプラグイン（InvalidSourceModifier）をつくることにした。</p>
</blockquote>
<p>とか<a href="http://blog.sharplab.net/computer/cprograming/windowslivewriter/invalidsourcemodifier/199/">言って</a>実際に作っていたのだけれども、いざ形になってきてコンパイルしてみると、</p>
<blockquote><p>エラー&#160;&#160;&#160; 1&#160;&#160;&#160; プロパティまたはインデクサ &#8216;WindowsLive.Writer.Api.IPostInfo.Contents&#8217; は読み取り専用なので、割り当てることはできません。</p>
</blockquote>
<p>というオチ。OnPrePublishメソッドをオーバーライドすれば投稿前にコンテンツの中身を書き換えられるという訳じゃなかったんかい！勘違いでした…。</p>
</p>
<p>&#160;</p>
<p>どーするかなー。後はhookする対象をOnPostPublishメソッドに変更したうえで、投稿後直ぐXML-RPCで直接WordPress上のコンテンツを書きかえる位しかやりようがないかな･･･。つうかそれならWordPressのプラグインをPHPで書いた方が早いか(笑)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sharplab.net/blog/2008/06/15/216/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>publish notification hooks</title>
		<link>http://blog.sharplab.net/blog/2008/06/10/199/</link>
		<comments>http://blog.sharplab.net/blog/2008/06/10/199/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 16:01:15 +0000</pubDate>
		<dc:creator>shiroica</dc:creator>
				<category><![CDATA[InvalidSourceModifier]]></category>

		<guid isPermaLink="false">http://blog.sharplab.net/computer/cprograming/windowslivewriter/invalidsourcemodifier/199/</guid>
		<description><![CDATA[publish notification hooksを使ったプラグインを作ってみようということで、LiveWriterの出力するソースを投稿時に書き換えるプラグイン（InvalidSourceModifier）をつくることにした。 using System; using System.Collections.Generic; using System.Text; using WindowsLive.Writer.Api; namespace InvalidSourceModifier { //TODO:ImagePathの設定！ [WriterPlugin("9388e56b-c684-4e04-b7f6-b8cc224d70b2", "InvalidSourceModifier", PublisherUrl = "http://www.sharplab.net/")] public class InvalidSourceModifier : PublishNotificationHook { public override bool OnPrePublish(System.Windows.Forms.IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish) { //publishingContext.PostInfo.Contents System.Windows.Forms.MessageBox.Show("test"); return true; } } } …まだこれだけだけど。 さて、実際にコンパイルしてLiveWriterに登録してみると、SDKに付属していたNew SDK Features.docの以下の記述通り、 Unlike content source plugins, publish notification [...]]]></description>
			<content:encoded><![CDATA[<p>publish notification hooksを使ったプラグインを作ってみようということで、LiveWriterの出力するソースを投稿時に書き換えるプラグイン（InvalidSourceModifier）をつくることにした。</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:61748bad-1671-464c-a4d8-e74482f9830e" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;

namespace InvalidSourceModifier {
    //TODO:ImagePathの設定！
    [WriterPlugin("9388e56b-c684-4e04-b7f6-b8cc224d70b2", "InvalidSourceModifier", PublisherUrl = "http://www.sharplab.net/")]
    public class InvalidSourceModifier : PublishNotificationHook {
        public override bool OnPrePublish(System.Windows.Forms.IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish) {
            //publishingContext.PostInfo.Contents
            System.Windows.Forms.MessageBox.Show("test");
           return true;
        }
    }
}</pre>
</div>
<p>…まだこれだけだけど。</p>
<p>さて、実際にコンパイルしてLiveWriterに登録してみると、SDKに付属していたNew SDK Features.docの以下の記述通り、</p>
<blockquote>
<p>Unlike content source plugins, publish notification hooks can be enabled or disabled by the user on a per-blog basis. The first time a publish operation occurs for a particular blog since a publish notification hook was installed, the user will be asked if the hook should be enabled or disabled for that blog.</p>
<p>New SDK Features.doc</p>
</blockquote>
<p><a href="http://blog.sharplab.net/wp-content/uploads/2008/06/image1.png" rel="lightbox"><img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="228" alt="image" src="http://blog.sharplab.net/wp-content/uploads/2008/06/image-thumb.png" width="244" align="left" border="0" /></a>Blog別にプラグインの有効/無効が管理されているのが確認できた。上手く出来てるなー。ちょっとしたことだけど、これは便利だ。</p>
<p>&#160; </p>
<p><a href="http://blog.sharplab.net/wp-content/uploads/2008/06/image2.png" rel="lightbox">&#160;</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sharplab.net/blog/2008/06/10/199/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

