MTのプラグインが書けるようになりたいなーと思って、ただいまPerlとMTのお勉強中。その1。
「あたちもMTのプラグイン書けるようになりたいっ」と社内SEに泣きついたら、「しょーがねーなー」とひと肌脱いでくれまして、先週から社内でMTお勉強会してもらってます。
勉強会のおさらいと、 コードネーム’歩実ちゃん’(知人の子供)誕生を記念して、 赤ちゃん語変換を行うプラグインを書いてみた。
ブロックタグ<mt:BabyTalk> を提供し、このタグで囲まれた範囲内に たとえば「~です。」と書いてあったら「~でちゅ。」になるような。。。
一応マイ・ファースト・MTプラグインでちゅ^^;
とはいえ、自力で1からは無理ぽorz…と思ったので
bzbellさんのプラグイン「RemoveBlank」を参考にさせてもらって、Perlの入門書にあったサンプルコードを 合体ぐにゅぐにゅして作ったですよ ^^;
PlugInソース ちなみに、元の「RemoveBlank」プラグインはこんな感じ。
perlの1ファイルだけの構成。
http://sourceforge.jp/projects/nekotama/downloads/31094/RemoveBlank-1.03.zip/
これをベースに、
1) config.yamlで書いてみた(先週やった勉強会のおさらいさ~)。
Blockタグの宣言は↓こんな風に書けば良いらしい。
cf.) http://www.movabletype.org/documentation/developer/declaring-template-tags.html
tags: block: BabyTalk: $convert_babytalk::BabyTalk::convert_babytalk
で、こうなる↓(むちゃくちゃシンプル ^^;)
/plugins/BabyTalk/config.yaml
name: BabyTalk id: BabyTalk version: 1.0 author_link: http://foxbitz.jp/ author_name: fox_b doc_link: http://foxbitz.jp/doc/ description: Convert text to BabyTalk between a tag. tags: block: BabyTalk: $convert_babytalk::BabyTalk::convert_babytalk
2) RemoveBlankではタグや空行があったら削除変換するところを、赤ちゃん語に変えてplugins/BabyTalk/lib配下に置いただけー。
それが↓コレ
plugins/BabyTalk2/lib/BabyTalk.pm
package BabyTalk;
use strict;
use utf8; # utf8プラグマを有効にする ←6/29追加した
use MT::Template::Context;
use base qw(MT::Plugin);
sub convert_babytalk {
my ($ctx, $args, $cond) = @_;
my $builder = $ctx->stash('builder'); #このへん何やってんのかイマイチわかってないがまーヨシとする
my $tokens = $ctx->stash('tokens');
my $out = $builder->build ($ctx, $tokens, $cond);
return $ctx->error ($builder->errstr) if !defined $out;
my @list = split(/[\r\n]/, $out);
$out = do_convert(@list);
$out;
}
sub do_convert {
my @out = @_;
my $txt = '';
for (@out) {
s/a/(=^..^=)/g; #for debug(なんか日本語だと変換されないから orz...)
s/さ/ちゃ/g;
s/し/ち/g;
s/す/ちゅ/g;
s/せ/ちぇ/g;
s/そ/ちょ/g;
s/。/ばぶー。/g;
$txt .= $_ . "\n" if ($_ ne '');
}
return $txt;
}
1; # Every module must return true
yamlで書いた内容が反映されてる。いえー♪
MTテンプレにタグ埋め込み。
変換前後がわかるようにこんな風に書いた。
<div class="asset-body"> <h2>変換前:</h2> <$mt:EntryBody$> <h2>変換後:</h2> <mt:BabyTalk><$mt:EntryBody$></mt:BabyTalk> </div>
http://mt.foxbitz.jp/b1/blog/2010/06/convert-to-babytalk.html
日本語の変換ができてないーorz….
明日会社に行ったら誰かに教えてもらおう To be continued..
6/29 utf8プラグマを有効にする1行を追加して解決デキタ気がする。
use utf8; # utf8プラグマを有効にする
●参考にしたサイト
Encode – 日本語などのマルチバイト文字列を適切に処理する / Perlモジュール徹底解説 :: サンプルコードによるPerl入門
http://d.hatena.ne.jp/perlcodesample/20091118/1246679588
なおよくわからないこと
今回utf8プラグマの1行追加しただけで動いているっぽいのだけれど、 このプラグインに受け渡される文字列は、すでに”内部文字列”ちゅうやつなんかしら?!
$out を最初と最後にdecode/encodeする必要はあるのかな?!
こーいうのホントは必要だった? MTの仕様を確認すればわかるんかな。。。To be continued…
↓
use Encode qw/encode decode/;
# UTF-8バイト文字列を内部文字列にデコード
$str1 = decode('UTF-8', $str1)
タグリファレンス
http://www.movabletype.jp/documentation/appendices/tags/
判定用のタグ(Conditional)
http://movabletype.jp/cgi-bin/mt4/mt-search.cgi?tag=conditional&blog_id=3&IncludeBlogs=3
アーカイブテンプレート:予約変数による判別
http://www.movabletype.jp/documentation/files/arcive_variable.pdf
* メインページ <mt:If name="main_index"> * archive_index <mt:If name="archive_index"> * individual <mt:If name="entry_archive"> * page <mt:If name="page_archive"> * page&entry <mt:If name="feedback_template"> * 各アーカイブのd/w/m/y <mt:If name="datebased_archive"> * d/w/m/y <mt:If name="datebased_only_archive"> * カテゴリ <mt:If name="category_archive"> * ユーザ別 <mt:If name="author_archive"> * 全archives <mt:If name="archive_template"> * 記事リスト系 <mt:If name="archive_listing">
アーカイブテンプレート:変数archive_classによる判別
<mt:If name="archive_class"
eq="entry-archive"> :: individual
<mt:ElseIf eq="page-archive"> :: page
<mt:ElseIf eq="category_archive"> :: category
<mt:ElseIf eq="datebased-monthly-archive"> :: 月別
<mt:ElseIf eq="author-archive"> :: ユーザ別
</mt:If>
グローバル・モディファイア
http://movabletype.jp/documentation/appendices/modifiers/
環境変数
http://www.movabletype.jp/documentation/appendices/config-directives/
日付のフォーマット
http://www.movabletype.jp/documentation/appendices/date-formats.html
<$MTDate format=”※フォーマット指定※”$>
* format_name="iso8601" 例: 2006-05-23T11:44:09+09:00 * format_name="rfc822" 例: Mon, 23 May 2006 11:44:09 +0900 * language="ja" 例: 2006年05月03日 11:44 * format="%x" 例: 2002年09月06日 ※0補完 * format="%Y年%m月%d日" 例: 2002年09月06日 ※0補完 * format=""%Y年%B月%e日" 例: 2002年 9月6日 ※スペース補完 * format="%A" 例: 火曜日
アーカイブマッピング(ファイル名)
http://www.movabletype.jp/documentation/appendices/archive-file-path-specifiers.html
サイトパス生成メモ
<mt:BlogParentWebsite> <$mt:WebsiteURL setvar="website_url"$> <$mt:WebsiteHost setvar="website_host"$> <$mt:WebsiteRelativeURL setvar="website_path"$> </mt:BlogParentWebsite>
ウェブサイトURL: http://example.com/foo/ とした場合、次のように出力される。
| 項目 | 使い方 | 出力例 |
|---|---|---|
| ホストからの相対パス | <$mt:Var name=’website_path’$>index.html | /foo/index.html |
| URL | <$mt:Var name=’website_url’$>index.html | http://example.com/foo/index.html |
| SSL領域のURL | https://<$mt:Var name=’website_host’$><$mt:Var name=’website_path’$>contact.html | https://example.com/foo/contact.html |
パンくずナビ用メモ。フォルダ1階層前提の猛烈シンプルなやつメモっておく。
<li><a href="/">ホーム</a> ></li>
<mt:unless tag="PageBasename" eq="index">
<li><a href="./"><$mt:FolderLabel$></a> ></li>
</mt:unless>
<li><$mt:PageTitle$></li>
<mt:SetVarBlock name="topicpath">
<a href="<$mt:Var name='website_path'$>index.html" title="ホーム">ホーム</a> »
<mt:If name="main_index"><strong><$mt:BlogName$></strong>
<mt:Else><a href="<$mt:Var name='blog_path'$>index.html" title="<$mt:BlogName$>"><$mt:BlogName$></a> »
</mt:If>
</mt:SetVarBlock>
<mt:If name="page_archive">
<mt:SetVarBlock name="topicpath" append="1">
<mt:Unless tag="PageBasename" eq="index">
<mt:ParentFolders>
<a href="<$mt:BlogURL$><$mt:FolderPath$>"><$mt:FolderLabel$></a> »
</mt:ParentFolders>
<strong><$mt:PageTitle remove_html="1"$></strong>
<mt:Else>
<mt:ParentFolders exclude_current="1">
<a href="<$mt:BlogURL$><$mt:FolderPath$>"><$mt:FolderLabel$></a> »
</mt:ParentFolders>
<strong><$mt:FolderLabel$></strong>
</mt:Unless>
</mt:SetVarBlock>
</mt:If>
WordPressのアップグレード, originally uploaded by fox_b.
ダッシュボードに表示されたアップグレード通知のリンクをクリックするだけ。いちいちダウンロードサイトからソースを落としてきたり、それをまた自分のサーバにFTPしたりすることなくその場で最新版にアップグレードしてくれて超楽ちん(^。^)
MTも早くこうなるべきー。
MT>ブログの設定 : 公開設定>アーカイブをサイトパスとは別のパスで公開する, originally uploaded by fox_b.
「アーカイブをサイトパスとは別のパスで公開する」にチェックを入れ以下のように設定してみた。
| アーカイブ種類 | マッピング設定 | 出力結果 |
|---|---|---|
| メインページ | index.html | /b1/index.html |
| アーカイブインデックス | archives.html | /b1/archives.html |
| ウェブページ | folder-path/page-basename.html | /b1/f01/f01-01.html |
| ブログ記事 | yyyy/mm/entry-basename.html | /b1/blog/2010/02/01.html |
| ブログ記事リスト(月別) | yyyy/mm/index.html | /b1/blog/2010/02/index.html |
| ブログ記事リスト(カテゴリ) | category/sub-category/index.html | /b1/blog/cat01/index.html |
「アーカイブをサイトパスとは別のパスで公開する」を設定すると、
出力される。なるほど、純粋にブログ的なコンテンツは/blog配下など別にまとめたいケースは頻繁にあるので妥当な結果と思われる。
「概要」が未入力ならmt:EntryExcerptには「本文」の指定文字数分が自動挿入されるのがデフォルトの動き。そうして欲しくない時があるんだけど、no_generate=”1″をつけてやれば回避できるらしい。
via. http://forums.movabletype.org/2009/05/testing-entryexcerpt-with-mtif.html (Dan Wolfgangセンセイのコメント)
<mt:EntryExcerpt no_generate="1" setvar="excerpt"> <mt:If name="excerpt"> <mt:EntryExcerpt> <mt:Else> <mt:EntryBody> </mt:If>
movabletype.jpのタグリファレンスには書いてないし、 知らなかった。
movabletype.orgにはちゃんとあるのね (-.-)y-~~
http://www.movabletype.org/documentation/appendices/tags/entryexcerpt.html
MT5対応のsitemap.xml生成用テンプレート。
ウェブサイトのカスタムインデックステンプレートとして作るべし。配下ブログも含めて出力する。
<mt:Ignore><!-- 繰り返し処理用 --></mt:Ignore> <mt:SetVarTemplate name="CREATEURL"> <url> <loc><$mt:ArchiveLink encode_xml="1"$></loc> <mt:Entries lastn="1" sort_by="modified_on"><lastmod><$mt:EntryModifiedDate format="%Y-%m-%dT%H:%M:%S"$><$mt:BlogTimezone$></lastmod></mt:Entries> <priority>0.5</priority> </url> </mt:SetVarTemplate> <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <mt:Blogs include_blogs="children" include_with_website="1"> <url> <loc><$mt:BlogURL encode_xml="1"$></loc> <lastmod><$mt:Date format="%Y-%m-%dT%H:%M:%S"$><$mt:BlogTimezone$></lastmod> <changefreq>daily</changefreq> </url> <mt:IfArchiveTypeEnabled archive_type="Individual"><mt:Entries lastn="9999"><url> <loc><$mt:EntryPermalink encode_xml="1"$></loc> <lastmod><$mt:EntryModifiedDate format="%Y-%m-%dT%H:%M:%S"$><$mt:BlogTimezone$></lastmod> <priority>1.0</priority> </url></mt:Entries></mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Page"><mt:Pages lastn="9999"><url> <loc><$mt:PagePermalink encode_xml="1"$></loc> <lastmod><$mt:PageModifiedDate format="%Y-%m-%dT%H:%M:%S"$><$mt:BlogTimezone$></lastmod> <priority>1.0</priority> </url></mt:Pages></mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Yearly"> <mt:ArchiveList archive_type="Yearly"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Monthly"> <mt:ArchiveList archive_type="Monthly"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Weekly"> <mt:ArchiveList archive_type="Weekly"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Daily"> <mt:ArchiveList archive_type="Daily"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Author"> <mt:ArchiveList archive_type="Author"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Author-Daily"> <mt:ArchiveList archive_type="Author-Daily"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Author-Weekly"> <mt:ArchiveList archive_type="Author-Weekly"><$mt:Var name="CREATEURL"$> </mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Author-Monthly"> <mt:ArchiveList archive_type="Author-Monthly"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Author-Yearly"> <mt:ArchiveList archive_type="Author-Yearly"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Category"> <mt:ArchiveList archive_type="Category"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Category-Daily"> <mt:ArchiveList archive_type="Category-Daily"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Category-Weekly"> <mt:ArchiveList archive_type="Category-Weekly"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Category-Monthly"> <mt:ArchiveList archive_type="Category-Monthly"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> <mt:IfArchiveTypeEnabled archive_type="Category-Yearly"> <mt:ArchiveList archive_type="Category-Yearly"><$mt:Var name="CREATEURL"$></mt:ArchiveList> </mt:IfArchiveTypeEnabled> </mt:Blogs> </urlset>
SetVarTemplate:今までどう使うのかピンとこなかったけどようやく理解した。
MT4時代からあるタグで今さら感はありつつも自分用にメモのこすーー(^^)
<div class="path"> <mt:Ignore><!-- パスのリンク生成部分(マクロ定義みたいもん) --></mt:Ignore> <mt:SetVarTemplate name="CREATEPATH"> <a href="<$mt:WebsiteURL$><$mt:FolderPath$>/index.html"><$mt:FolderLabel$></a> ・・・(M) </mt:SetVarTemplate> <mt:SetVarBlock name="this_basename"><$mt:PageBasename$></mt:SetVarBlock> <mt:If name="this_basename" eq="index"> <mt:Ignore><!-- indexファイルならパスに自フォルダを表示しない --></mt:Ignore> <mt:ParentFolders glue=" / " exclude_current="1"> <$mt:GetVar name="CREATEPATH"$> ←ここが青字部分(M)で置換される </mt:ParentFolders> <mt:Else> <mt:ParentFolders glue=" / "> <$mt:Var name="CREATEPATH"$> ←ここが青字部分(M)で置換される </mt:ParentFolders> </mt:If> </div><!-- /div.path -->
mt:GetVarで呼び出すと、ブロック内に書いた内容そのまま(MTタグは解釈されずに文字列として)で置換されるらしい。
同じようなMTタグ群をあちこちに書く時に便利かも。
つまりマクロみたいなもんやね。Cっぽく書くとこんな感じ?
#define CREATEPATH <a href="<$mt:WebsiteURL$><$mt:FolderPath$>/index.html"><$mt:FolderLabel$></a>
movabletype.jp > テンプレートタグリファレンス > MTSetVarTemplate
http://www.movabletype.jp/documentation/appendices/tags/setvartemplate.html
タグリファレンスを見ると配列やハッシュが使えたりするので、単純な置き換え用途と考えるにはもったいないのかも。もうちょっと勉強してみよう。
MTTemplateNoteというコメント用のタグがある。
<$mt:TemplateNote value="コメントhoge"$>
と書いて
<mt:Ignore>コメントhoge</mt:Ignore>
と同じように振る舞うんだけど
MTTemplateNoteってこのタグの長ったらしさはなんとかならないのかなー?!
上の2つを並べてみればわかるとおりMTTemplateNoteの方だとコメント本文の開始位置が恐ろしく後ろになるので、コメントとしての視認性が凄く悪い。全体の長さもMTIgnoreで囲む方がなんぼか短いし(エディタ上で確実に折り返されます。MTTemplateNoteさんw)
mt:Note にするとか、せめてvalue=”"の書式は勘弁してくれるとか。。。ねぇ? ^^;