Forked EntityRefButton plugin 0.0.3
Movable Type というブログシステムのための、プラグインのお話。
EntityRefButton は、エントリ編集ページに、XHTML で都合の悪いいくつかの文字を実体参照化するボタンを追加してくれるプラグインです。プラグインで実体参照化するアイデアはあったのですが、このプラグインを見て JavaScript で実現してしまう方がより適しているかなと気付かされた次第。
ところで MT 3.3 と wheeljack の間には既にエントリ編集ページのテンプレートに差が生じているので、このプラグインはそのままでは正常に動作しません。Wheeljack 向けに少し改造しました。ニッチだなぁ。
それと、このプラグインはなぜかスペース文字まで改行禁止スペース文字 ( ) に実体参照化してしまうので、これを除去しました。改行禁止では意味が違ってしまいますので。
早速、以下の差分コードを貼り付けて、EntityRefButton にて実体参照化してみました。これは便利便利。
diff --unified --recursive EntityRefButton/plugins/EntityRefButton/entityrefbutton.pl EntityRefButton.fork/plugins/EntityRefButton/entityrefbutton.pl
--- EntityRefButton/plugins/EntityRefButton/entityrefbutton.pl 2006-12-08 13:47:57.000000000 +0900
--- EntityRefButton.fork/plugins/EntityRefButton/entityrefbutton.pl 2006-12-08 13:47:57.000000000 +0900
@@ -15,7 +15,7 @@
version => $VERSION,
description => '<MT_TRANS phrase=\'This plugin adds a convert button to the entry body and the extended body fields on the edit entry screen, which converts special characters to character entity references.\'>',
author_name => 'M-Logic, Inc.',
- author_link => 'http://labs.m-logic.gr.jp/',
+ author_link => 'http://labs.m-logic.jp/',
doc_link => 'http://labs.m-logic.jp/plugins/entityrefbutton/docs/entityrefbutton.html',
l10n_class => 'EntityRefButton::L10N',
});
@@ -27,11 +27,11 @@
edit_entry => [
# body
{
- pattern_s => <<'PATTERN_END'
- write('<img title="<MT_TRANS phrase="Quote">" onclick="return formatStr(document.entry_form.text, \'blockquote\')" src="<TMPL_VAR NAME=STATIC_URI>images/formatting-icons/quote.gif" alt="<MT_TRANS phrase="Quote">" width="26" height="19" />');
-PATTERN_END
-,
- pattern_e => '\s+\}\n\}',
+ pattern => quotemeta(q{ write('<img title="<MT_TRANS phrase="Quote"}) .
+ q{(?:\ escape="singlequotes")?} .
+ quotemeta(q{>" onclick="return formatStr(document.entry_form.text, \'blockquote\')" src="<TMPL_VAR NAME=STATIC_URI>images/formatting-icons/quote.gif" alt="<MT_TRANS phrase="Quote"}) .
+ q{(?:\ escape="singlequotes")?} .
+ quotemeta(q{>" width="26" height="19" />');}),
replacement => <<'REPLACEMENT_END'
write('<img title="_BUTTON_LABEL_" onclick="return convertToCharEntityRef(document.entry_form.text)" src="<TMPL_VAR NAME=STATIC_URI>plugins/EntityRefButton/images/formatting-icons/ref.gif" alt="_BUTTON_LABEL_" width="26" height="19" />');
REPLACEMENT_END
@@ -39,11 +39,11 @@
},
# extended body
{
- pattern_s => <<'PATTERN_END'
- write('<img title="<MT_TRANS phrase="Quote">" onclick="return formatStr(document.entry_form.text_more, \'blockquote\')" src="<TMPL_VAR NAME=STATIC_URI>images/formatting-icons/quote.gif" alt="<MT_TRANS phrase="Quote">" width="26" height="19" />');
-PATTERN_END
-,
- pattern_e => '\s+\}\n\}',
+ pattern => quotemeta(q{ write('<img title="<MT_TRANS phrase="Quote"}) .
+ q{(?:\ escape="singlequotes")?} .
+ quotemeta(q{>" onclick="return formatStr(document.entry_form.text_more, \'blockquote\')" src="<TMPL_VAR NAME=STATIC_URI>images/formatting-icons/quote.gif" alt="<MT_TRANS phrase="Quote"}) .
+ q{(?:\ escape="singlequotes")?} .
+ quotemeta(q{>" width="26" height="19" />');}),
replacement => <<'REPLACEMENT_END'
write('<img title="_BUTTON_LABEL_" onclick="return convertToCharEntityRef(document.entry_form.text_more)" src="<TMPL_VAR NAME=STATIC_URI>plugins/EntityRefButton/images/formatting-icons/ref.gif" alt="_BUTTON_LABEL_" width="26" height="19" />');
REPLACEMENT_END
@@ -52,11 +52,10 @@
bm_entry => [
# body
{
- pattern_s => <<'PATTERN_END'
+ pattern => quotemeta <<'PATTERN_END'
write('<a title="<MT_TRANS phrase="Quote">" href="#" onclick="return formatStr(document.entry_form.text, \'blockquote\')"><img src="<TMPL_VAR NAME=STATIC_URI>images/html-quote.gif" alt="<MT_TRANS phrase="Quote">" width="22" height="16" /></a>');
PATTERN_END
,
- pattern_e => '\s+\}\n\}',
replacement => <<'REPLACEMENT_END'
write('<a title="_BUTTON_LABEL_" href="#" onclick="return convertToCharEntityRef(document.entry_form.text)"><img src="<TMPL_VAR NAME=STATIC_URI>plugins/EntityRefButton/images/html-ref.gif" alt="_BUTTON_LABEL_" width="22" height="16" /></a>');
REPLACEMENT_END
@@ -64,11 +63,10 @@
},
# extended body
{
- pattern_s => <<'PATTERN_END'
+ pattern => quotemeta <<'PATTERN_END'
write('<a title="<MT_TRANS phrase="Quote">" href="#" onclick="return formatStr(document.entry_form.text_more, \'blockquote\')"><img src="<TMPL_VAR NAME=STATIC_URI>images/html-quote.gif" alt="<MT_TRANS phrase="Quote">" width="22" height="16" /></a>');
PATTERN_END
,
- pattern_e => '\s+\}\n\}',
replacement => <<'REPLACEMENT_END'
write('<a title="_BUTTON_LABEL_" href="#" onclick="return convertToCharEntityRef(document.entry_form.text_more)"><img src="<TMPL_VAR NAME=STATIC_URI>plugins/EntityRefButton/images/html-ref.gif" alt="_BUTTON_LABEL_" width="22" height="16" /></a>');
REPLACEMENT_END
@@ -96,18 +94,17 @@
if(!canFormat) return;
var str = getSelected(e);
if(str) {
- str = str.replace(/&/g, "&");
- str = str.replace(/ /g, " ");
- str = str.replace(/</g, "<");
- str = str.replace(/>/g, ">");
- str = str.replace(/"/g, """);
+ str = str.replace(/&/g, "&")
+ .replace(/</g, "<")
+ .replace(/>/g, ">")
+ .replace(/"/g, """);
setSelection(e, str);
}
return false;
}
SCRIPT
- $$tmpl =~ s!(\n\n)(//-->\n</script>)!$1$script$2!is;
+ $$tmpl =~ s!(?<=\n\n)(?=//-->\n</script>)!$script!is;
}
sub add_convert_button {
@@ -117,11 +114,10 @@
my $alt_str = $plugin->translate('Convert to character entity reference');
my $entries = $BUTTONS{$template_name};
foreach my $item (@$entries) {
- my $pattern_s = $item->{pattern_s};
- my $pattern_e = $item->{pattern_e};
+ my $pattern = $item->{pattern};
my $replacement = $item->{replacement};
$replacement =~ s!_BUTTON_LABEL_!$alt_str!isg;
- $$tmpl =~ s!(\Q$pattern_s\E)($pattern_e)!$1$replacement$2!is;
+ $$tmpl =~ s!$pattern!$&$replacement!is;
}
}
- タグ
- Movable Type
- plugins
- templates
- 公開日時
- 2006-12-09T21:49:35+09:00 @576
- Permalink URI & TrackBack URL
- http://blog.drry.jp/2006/12/09/forked-entityrefbutton
TrackBack ( 1 )
- カンタンに実体参照文字に変換する from MovableType備忘録
- 2006-12-12T22:07:06+09:00 @576
- XHTMLでは、「 & 」や「 " 」、「 < 」などの、ある特定の文字はそのまま使ってはいけません…とされています。 なので、「...
コメント