WordPress:CoolCodeプラグイン
2008.6.3 火曜日ひとまず、blogの内容的に、ソースコード等を載せることになることは確実だと思われるので、
表示方法を考えてみる。
最初はcssで対応しようかと思っていたけど、
探してみると、コード表示補助のプラグインをいくつか発見。
ひとまずCoolCodeを導入してみる。
こちらからダウンロードして来て、解凍して/wp-content/plugins/にアップロード。
管理画面の「設定」→「プラグイン」で有効化したら使用可能。
記事を投稿する際に
<coolcode>
[コード]
</coolcode>
と囲むとその部分に適用される。
オプションは3つあって、
- lang=”言語名”(PHP/ACTIONSCRIPT 等)
- linenum=”on/off” (行番号の表示/非表示)
- download=”ファイルのパス”
例えば
- <coolcode lang="php">
- <?php
- echo "Hello World";
- ?>
- </coolcode>
こんな感じで使用。
言語名のオプションは、つけると、その言語に合わせてコードをカラーリングしてくれる。
無しだとそのまま表示。
現在
* actionscript
* cpp
* css
* diff
* dtd
* html
* java
* javascript
* mysql
* perl
* php
* python
* ruby
* sql
* xml
に対応してるらしい。
とりあえず、download=””のオプションをつけると、ダウンロードのリンクの文字が中国語になってるので、該当部分を探して修正。
coolcode.phpの298行目
を
に変更。
段々欲が出てきた。
長いソースを貼り付けるとページがもの凄く長くなってしまうので、WP-syntaxのようにスクロールで表示できるようにしてみよう。
DIVタグで囲んで出力するようにして、overflowで制御すればいいかな、と思って軽く考えてたけど、
全部にそれを適用してしまうと、今度は短いコード貼りたいときに困るので、オプションで指定できるように変更してみる。
coolcode.phpのdo_CoolCode関数を
- function do_CoolCode($content, $txt, $options)
- {
- global $post;
- $options = str_replace(array("\\\"", "\\\'"), array("\"", "\'"), $options);
- if (preg_match('/lang="(\w*?)"/i', $options, $match)) {
- $lang = $match[1];
- }
- else {
- $lang = "";
- }
- if (preg_match('/linenum="(\w*?)"/i', $options, $match)) {
- $linenum = $match[1];
- }
- else {
- $linenum = "on";
- }
- if (preg_match('/download="(.*?)"/i', $options, $match)) {
- $download = $match[1];
- }
- else {
- $download = "";
- }
- if (preg_match('/scroll="(.*?)"/i', $options, $match)) {
- $scroll = $match[1];
- }
- else {
- $scroll = "";
- }
- $txt = str_replace("\\\"", "\"", $txt);
- $txt = trim($txt);
- $txt = str_replace("\r\n", "\n", $txt);
- $txt = str_replace("\r", "\n", $txt);
- $blockID = $this->getBlockID($content);
- if ($download == "") {
- $this->blocks[$blockID] = '';
- }
- else {
- $this->blocks[$blockID] .= '<div class="hl-title">Download: <a href="'
- . get_settings('home')
- . '/wp-content/plugins/coolcode/coolcode.php?p=' . $post->ID
- . '&download=' . htmlspecialchars($download)
- . '">' . htmlspecialchars($download) . '</a></div>';
- }
- if (strtolower($scroll) == 'on') {
- $this->blocks[$blockID] .= '<div class="coolcode codeScroll">';
- }else{
- $this->blocks[$blockID] .= '<div class="coolcode">';
- }
- $hackphp = false;
- if (strtolower($lang) == 'php') {
- if (strpos($txt, '<' . '?') === false) {
- $txt = '<' . "?php\n" . $txt . "\n?" . '>';
- $hackphp = true;
- }
- }
- if ((strtolower($linenum) == 'on') or (strtolower($linenum) == 'open')) {
- if(!in_array(strtolower($lang), $this->acceptable_lang)) {
- $this->blocks[$blockID] .= '<div class="hl-surround"><ol class="hl-main ln-show" '
- . 'title="Double click to hide line number." '
- . 'ondblclick = "linenumber(this)"><li class="hl-firstline">'
- . str_replace("\n", "</li>\n<li>", htmlspecialchars($txt))
- . '</li></ol></div>';
- $this->blocks[$blockID] = str_replace("<li></li>", "<li> </li>", $this->blocks[$blockID]);
- $this->blocks[$blockID] = str_replace('<li> ', '<li> ', $this->blocks[$blockID]);
- }
- else
- {
- $options = array(
- 'numbers' => HL_NUMBERS_LI,
- );
- $hl =& Text_Highlighter::factory($lang, $options);
- $this->blocks[$blockID] .= '<div class="hl-surround">' . str_replace($this->hl_class, $this->hl_style, $hl->highlight($txt)) . '</div>';
- $this->blocks[$blockID] = preg_replace('/<span style=\"[^\"]*?\"><\/span>/', '', $this->blocks[$blockID]);
- $this->blocks[$blockID] = str_replace('<ol class="hl-main">',
- '<ol class="hl-main ln-show" title="Double click to hide line number." ondblclick = "linenumber(this)">',
- $this->blocks[$blockID]);
- $this->blocks[$blockID] = str_replace("\"> </span></li>", "\"> </span></li>", $this->blocks[$blockID]);
- $this->blocks[$blockID] = preg_replace('/<li><span style=(.*?)> </si', '<li><span style=\\1> <', $this->blocks[$blockID]);
- if ($hackphp) {
- $this->blocks[$blockID] = str_replace("<span style=\"color: Blue;\"><?php</span></li>\n<li>", '', $this->blocks[$blockID]);
- $this->blocks[$blockID] = str_replace('<li><span style="color: Blue;">?></span></li>', '', $this->blocks[$blockID]);
- }
- }
- }
- else {
- if(!in_array(strtolower($lang), $this->acceptable_lang)) {
- $this->blocks[$blockID] .= '<div class="hl-surround"><div class="hl-main">'
- . str_replace("\n", "<br />", htmlspecialchars($txt))
- . '</div></div>';
- }
- else
- {
- $hl =& Text_Highlighter::factory($lang);
- $this->blocks[$blockID] .= '<div class="hl-surround">' . str_replace("\n", "<br />", str_replace("</pre>", "", str_replace("<pre>", "", str_replace($this->hl_class, $this->hl_style, $hl->highlight($txt))))) . '</div>';
- if ($hackphp) {
- $this->blocks[$blockID] = str_replace('<span style="color: Blue;"><?php</span><span style="color: Gray;"><br /></span>', '', $this->blocks[$blockID]);
- $this->blocks[$blockID] = str_replace('<br /></span><span style="color: Blue;">?></span>', '</span>', $this->blocks[$blockID]);
- }
- }
- $this->blocks[$blockID] = str_replace('<br /> ', '<br /> ', $this->blocks[$blockID]);
- }
- // correct the indent
- $this->blocks[$blockID] = str_replace(" ", ' ', $this->blocks[$blockID]);
- $this->blocks[$blockID] = str_replace(" ", ' ', $this->blocks[$blockID]);
- $this->blocks[$blockID] .= '</div>';
- return $blockID;
- }
このように変更。※赤字が追加した箇所
これで、scroll=”" というオプションを認識して、囲むDIVを書き分けるようになった。
あとはcoolcode.cssに、
- .coolcode {
- border: 1px solid #666666;
- margin-bottom: 10px;
- }
- .codeScroll {
- height:400px;
- }
- /* for IE */
- .codeScroll {
- overflow: auto;
- _overflow: scroll;
- padding-bottom: expression(this.scrollWidth > this.offsetWidth ? 15 : 0);
- }
- .codeScroll ol {
- overflow: visible;
- }
と追記(部分的に、WP-syntaxのcssから拝借)
これで、<coolcode scroll=”on”>とすると、スクロールバーが表示されるようになる。
やっつけだけどひとまずこんな感じで。
一応ソース
coolcode-34-c01.zip
※cssとかコード変換の文字色とかも、このサイト用に若干いじっちゃってます。