iG:Syntax Hiliterでファイル名を出力する
09/05/11修正 - igSynHilite関数の正規表現にA-Z0-9を追加
ご質問頂いたのでついでにネタにしちゃいます。
iG:Syntax Hiliterでファイル名を出力させる方法です。wp-content/plugins/syntax_hilite.phpの四つの関数を跨いでいます。
igSynHilite
まずは、取得部分。204行目でnum=*の取得と同様にname=*を取得します。
- PHP
- syntax_hilite.php
- Source
// main function that calls the highlighter
function igSynHilite($inData) {
$inData = preg_replace_callback('/¥[(¥w{1,})((?:¥s+num+=([0-9]{1,})+)*)((?:¥s+name+=([a-zA-Z0-9¥.¥-¥_]{1,})+)*)¥](.+?)¥[¥/¥1¥]/ims', 'igSynHilite_code', $inData); // call code hiliter
return $inData;
}- // main function that calls the highlighter
- function igSynHilite($inData) {
- $inData = preg_replace_callback('/¥[(¥w{1,})((?:¥s+num+=([0-9]{1,})+)*)((?:¥s+name+=([a-zA-Z0-9¥.¥-¥_]{1,})+)*)¥](.+?)¥[¥/¥1¥]/ims', 'igSynHilite_code', $inData); // call code hiliter
- return $inData;
- }
igSynHilite_code
igSynHiliteのコールバック。187行目でファイル名を配列から抜き出し、197行目でdoHiliteに第四引数として渡す。
- PHP
- syntax_hilite.php
- Source
//Hilite CODE
function igSynHilite_code($hCode) {
global $igsh,$tOffAutoFmt;
$startTag = strtolower(trim($hCode[1]));
$inTxt = $hCode[6];
$pVal = (int) $hCode[3]; // get the starting line number
$filename = $hCode[5]; // get filename
$hilitedCode = "";
if(!empty($startTag)) {
if(strlen($inTxt)>1) {
$tOffAutoFmt = 1; // if code is there, disable auto formatting
}
$arrSearch = array("< ", "< ", " >", " >", "< ", "< ", " >", " >");
$arrReplace = array("<", "<", ">", ">", "<", "<", ">", ">");
$inTxt = str_replace($arrSearch, $arrReplace, $inTxt);
$pVal = ((empty($pVal)) || ($pVal<1)) ? 1 : $pVal;
$hilitedCode = $igsh->doHilite($inTxt, $startTag, $pVal, $filename);
}
return $hilitedCode;
}- //Hilite CODE
- function igSynHilite_code($hCode) {
- $inTxt = $hCode[6];
- $pVal = (int) $hCode[3]; // get the starting line number
- $filename = $hCode[5]; // get filename
- $hilitedCode = "";
- $tOffAutoFmt = 1; // if code is there, disable auto formatting
- }
- $hilitedCode = $igsh->doHilite($inTxt, $startTag, $pVal, $filename);
- }
- return $hilitedCode;
- }
doHilite
第四引数でファイル名を受け取って、pFixに渡してあげるだけ。
- PHP
- syntax_hilite.php
- Source
function doHilite($mTxt, $mType='html', $sNum=1, $filename) {- function doHilite($mTxt, $mType='html', $sNum=1, $filename) {
- PHP
- syntax_hilite.php
- Source
$hCode = $this->pFix($mTypeShow, $filename, $mType.'-'.$cbId).$hCode.$this->sFix();
- $hCode = $this->pFix($mTypeShow, $filename, $mType.'-'.$cbId).$hCode.$this->sFix();
pFix
ヘッダのHTMLを整形している関数pFixの第二引数としてファイル名が渡ってくるので、お好みに合わせて出力して下さい。
syntax_hilite.phpを触った当初の目的がヘッダのHTMLをリストで出力することだったので、この例だと全面的に変更してあります。この辺りは好みの問題だと思うんで、デフォルト通りspanに$filenameを出力してもいいかもしれません。
- PHP
- syntax_hilite.php
- Source
function pFix($hLang='PHP', $filename, $bId, $bCls='syntax_hilite') {
$bBody = "";
$bId = strtolower($bId);
if(IG_PLAIN_TEXT_TYPE=="inbox") {
$ig_jsPlainTxt = "showPlainTxt";
} else {
$ig_jsPlainTxt = "showCodeTxt";
}
$bBody .= "¥t- ";
$bBody .= "¥t¥t
- {$hLang} "; $bBody .= "¥t¥t
- {$filename} "; $bBody .= "¥t
- Source "; $bBody .= "
- function pFix($hLang='PHP', $filename, $bId, $bCls='syntax_hilite') {
- $bBody = "";
- if(IG_PLAIN_TEXT_TYPE=="inbox") {
- $ig_jsPlainTxt = "showPlainTxt";
- } else {
- $ig_jsPlainTxt = "showCodeTxt";
- }
- $bBody .= "¥t<ul class=¥"syntax_hilite_header¥">";
- $bBody .= "¥t¥t<li class=¥"language¥">{$hLang}</li>";
- $bBody .= "¥t¥t<li class=¥"filename¥">{$filename}</li>";
- $bBody .= "¥t<li class=¥"source¥"><a href=¥"#¥" onclick=¥"javascript:{$ig_jsPlainTxt}('{$bId}'); return false;¥">Source</a></li>";
- $bBody .= "</ul>";
- $bBody .= "<div class=¥"{$bCls}¥">¥n";
- $bBody .= "<div id=¥"{$bId}¥">¥n";
- return $bBody;
- }
問題点
igSynHiliteで使った正規表現上、行数とファイル名の両方を出力する場合、[php name=* num=*]の順だと正しく動きません。
[php num=* name=*]の順か、単体の[php name=*]か[php num=*]でお使い下さい。
こう書けば逆順でも動くよってコードあったら教えて下さい。
About this entry
You’re currently reading “iG:Syntax Hiliterでファイル名を出力する,” an entry on jp.ferv.blog
- Published:
- Mon, Jan 12th, 2009 at 3:28 AM
- Author:
- dsk
- Category:
- Web
- Tags:
- iG:Syntax Hiliter
-
Jan 13th, 2009
2:11 GMT-9記事にまでしてくれて、ありがとうございました。
無事ファイル名を出力出来ました。
GeSHiも最新版にして、バブリーになりました。 -
Jan 13th, 2009
2:27 GMT-9こちらこそ、ネタ提供ありがとうございました。
引き続きハイライトライフをお楽しみください(謎
Have your say
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Tags
Entries
- 08.15 最速 PNGEncoder を教えてもらったよ
- 08.14 初期値を与えてからトランジション
- 08.10 Touch オブジェクトの注意点
- 08.09 iOS Safari Examples
- 05.10 Player10.0 でも purePDF で日本語PDFを作る
- 05.10 ByteArray に 'unicodeFFFE' を書き込む際のバグ
- 03.13 Stratus2 でチャットアプリ
- 02.23 Twitterを今更はじめました
- 02.04 Alchemy PNG encoder のデモ
- 02.03 Alchemy PNG encoder を Spark
- 01.20 Alchemy PNG encoder
- 01.19 Alchemy JPG encoder
- 01.08 Optimized PNGEncoder with filters
- 01.07 Optimized PNGEncoder
- 12.25 FlashのboldがWin/Macで違うよ
- 12.18 iCone1.1.1リリース
- 11.11 DisplayObject3D.buttonMode
- 11.03 ぐるぐるしたら、部屋が暖まる法則
- 10.29 二次ベジェから三次ベジェへの変換
- 10.29 GraphicsPathで円・正多角形・星型正多角形
2 Comments
Jump to comment form | comments rss | trackback uri