cakephp view

cakePHPでは、いろいろな政治的関係(?)で
viewのimgタグとcssタグは
$htmlヘルパをつかわにゃならん様です。(当り前?)
※最近気づきました。

それと、aタグもね。

うちはデザインがダメダメで、外注している。
aタグはcotroller作るときに、いっしょに書き換えればいいとしても、
imgタグとcssタグは置換せにゃならん。
この置換作業がメチャメチャめんどい。

ということで、片手間に置換マクロを書いた。
perlなので、インタプリタを入れること。

使いかたは、
perl <下のマクロ> <変換したいファイル>・・・
とする。

ファイルをアップするのが面倒なので、
張り付けw



#!/usr/bin/perl
for ($i = 0; $i <= $#ARGV; $i++) {
$fileName = $ARGV[$i];
print ("exec:" . $fileName . "¥n");
$contents = "";
open(IN, $fileName);
while ($line = <IN>) {
$contents .= $line;
}
close(IN);

# img タグ変換
push(@imgs, $contents =~ /<img(.*?)¥/{0,1}>/mgi);

foreach $img (@imgs) {
$html = '<?php echo $html->image(';



%attributes = $img =~ /(¥S*)=["']{1}(¥S*?)['"]{1}/mg;



$html .= "'". $attributes{"src"} . "'";

delete $attributes{"src"};



@attributeKeys = keys(%attributes);



$html .= ", array(";



foreach $key (@attributeKeys) {

$html .= "'" . $key . "' => '" . $attributes{$key} . "', ";

}

$html .= ")); ?>";



$contents =~ s/<img(.*?)¥/{0,1}>/$html/eimo;

}



# link タグ変換
push(@links, $contents =~ /<link(.*?)¥/{0,1}>/mgi);

foreach $link (@links) {

%attributes = $link =~ /(¥S*)=["']{1}(¥S*?)['"]{1}/mg;

$html = '<?php echo $html->css(';

$html .= "'" . $attributes{"href"} . "'";

$html .= "); ?>";

$contents =~ s/<link(.*?)¥/{0,1}>/$html/eimo;

}


open(OUT, ">" . $fileName);
print(OUT $contents);
close(OUT);
}



超手抜きなのはかんべんしてほしい。