本家に、WhatsNewブロックなどで文字を適当な長さに切りそろえるときに、マルチバイト文字だと化けてしまう問題を解決するパッチを送信しました。
lib-common.php用です。
少しずつ、プラグインもこれを使うように変更して、送っていく予定です。
ちなみにこの修正、(1)文字が一定数より長ければ、(2)切りそろえて、(3)末尾に"..."を追加する。というはたらきをします。
このときに、マルチバイト用関数(mb_)が入っていれば、こちらを使って、文字コードは自動判定します。
おそらくこうしておけば、文字を切りそろえる必要があるときにはいつでもこれを使えばよくなります。
DRY(Don't Repeat Yourself:同じことを繰り返すな!)という教えにも従っています。
(by RailsによるアジャイルWebアプリケーション開発)
こういう小さなリファクタリングを進めていって、Geeklogをもう少し分かりやすいものにしていきたいなと思います。
(というか、フレームワークっぽくするというか・・・。分かりやすいのはいいけど、コードの繰り返しが結構あるので、いじるときに追いかけるのが大変。)
パッチは続きを参照してください。
*** /tmp/ediff41523T Thu Mar 9 15:51:03 2006
--- /Users/ysakata/workspace/geekjp/public/lib-common.php Thu Mar 9 15:46:14 2006
***************
*** 33,39 ****
// | |
// +---------------------------------------------------------------------------+
//
! // $Id: lib-common.php,v 1.1.1.1 2006/03/09 00:43:39 ysakata Exp $
// Prevent PHP from reporting uninitialized variables
error_reporting( E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR );
--- 33,39 ----
// | |
// +---------------------------------------------------------------------------+
//
! // $Id: lib-common.php,v 1.3 2006/03/09 06:40:41 ysakata Exp $
// Prevent PHP from reporting uninitialized variables
error_reporting( E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR );
***************
*** 3867,3876 ****
if( $_CONF['emailstorieslength'] > 1 )
{
! if( strlen( $storytext ) > $_CONF['emailstorieslength'] )
! {
! $storytext = substr( $storytext, 0, $_CONF['emailstorieslength'] ) . '...';
! }
}
$mailtext .= $storytext . "nn";
--- 3867,3873 ----
if( $_CONF['emailstorieslength'] > 1 )
{
! $storytext=COM_titlesplit($strorytext, $_CONF['emailstorieslength']);
}
$mailtext .= $storytext . "nn";
***************
*** 3998,4014 ****
if(( $A['type'] == 'article' ) || empty( $A['type'] ))
{
- $titletouse = COM_undoSpecialChars( stripslashes( $A['title'] ));
- $itemlen = strlen( $titletouse );
$urlstart = '<a href="' . COM_buildUrl( $_CONF['site_url']
. '/article.php?story=' . $A['sid'] ) . '#comments' . '"';
}
! // Trim the length if over 20 characters
! if( $itemlen > 20 )
{
! $urlstart .= ' title="' . htmlspecialchars( $titletouse ) . '">';
! $titletouse = substr( $titletouse, 0, 17 ) . '...';
}
else
{
--- 3995,4010 ----
if(( $A['type'] == 'article' ) || empty( $A['type'] ))
{
$urlstart = '<a href="' . COM_buildUrl( $_CONF['site_url']
. '/article.php?story=' . $A['sid'] ) . '#comments' . '"';
}
! $title = COM_undoSpecialChars( stripslashes( $A['title'] ));
! $titletouse=COM_titlesplit($title, 20);
!
! if( $title <> $titletouse )
{
! $urlstart .= ' title="' . htmlspecialchars( $title ) . '">';
}
else
{
***************
*** 4060,4074 ****
$A = DB_fetchArray( $result );
$titletouse = COM_undoSpecialChars( stripslashes( $A['title'] ));
- $itemlen = strlen( $titletouse );
$urlstart = '<a href="' . COM_buildUrl( $_CONF['site_url']
. '/article.php?story=' . $A['sid'] ) . '#trackback' . '"';
!
! // Trim the length if over 20 characters
! if( $itemlen > 20 )
{
! $urlstart .= ' title="' . htmlspecialchars( $titletouse ) . '">';
! $titletouse = substr( $titletouse, 0, 17 ) . '...';
}
else
{
--- 4056,4067 ----
$A = DB_fetchArray( $result );
$titletouse = COM_undoSpecialChars( stripslashes( $A['title'] ));
$urlstart = '<a href="' . COM_buildUrl( $_CONF['site_url']
. '/article.php?story=' . $A['sid'] ) . '#trackback' . '"';
! $titletouse = COM_titlesplit($title, 20);
! if( $title <>$titletouse )
{
! $urlstart .= ' title="' . htmlspecialchars( $title ) . '">';
}
else
{
***************
*** 5626,5631 ****
--- 5619,5645 ----
return $iconurl;
}
+ function COM_titlesplit($itemtext, $maxlen="10")
+ {
+ if (!function_exists('mb_substr'))
+ {
+ $itemlen = strlen($itemtext);
+ if ($itemlen > $maxlen)
+ {
+ $itemtext = substr( $titletext, 0, $maxlen) . '...';
+ }
+ }
+ else
+ {
+ $itemlen = mb_strlen($itemtext, mb_detect_encoding($itemtext) );
+ if ($itemlen > $maxlen)
+ {
+ $itemtext = mb_substr( $titletext, 0, $maxlen , mb_detect_encoding($itemtext)) . '...';
+ }
+ }
+ return $itemtext;
+ }
+
// Now include all plugin functions
foreach( $_PLUGINS as $pi_name )
{