パスワードを忘れた? アカウント作成
690965 journal
JAXA

torusの日記: もうちょっと Movable Type。

日記 by torus
first_n_words というサブルーチンの動作を、 日本語でそれらしく動く修正した。 具体的には、はじめの n × 6 文字だけ抜き出します。

diff -r -u MT-2.6-full-lib.orig/lib/MT/Util.pm MT-2.6-full-lib/lib/MT/Util.pm
--- MT-2.6-full-lib.orig/lib/MT/Util.pm Fri Feb 14 15:38:58 2003
+++ MT-2.6-full-lib/lib/MT/Util.pm Sun Feb 16 02:37:55 2003
@@ -245,9 +245,11 @@
sub encode_html {
my($html) = @_;
return '' unless defined $html;
+ use Jcode;
+ $html = Jcode->new ($html)->euc;
$html =~ tr!\cM!!d;
if ($Have_Entities && !MT::ConfigMgr->instance->NoHTMLEntities) {
- $html = HTML::Entities::encode_entities($html);
+ $html = HTML::Entities::encode_entities($html, '&"');
} else {
## Encode any & not followed by something that looks like
## an entity, numeric or otherwise.
@@ -391,9 +393,29 @@
sub first_n_words {
my($text, $n) = @_;
$text = remove_html($text);
- my @words = split /\s+/, $text;
- my $max = @words > $n ? $n : @words;
- return join ' ', @words[0..$max-1];
+# my @words = split /\s+/, $text;
+# my $max = @words > $n ? $n : @words;
+# return join ' ', @words[0..$max-1];
+ if (length ($text) > 6 * $n) {
+ $text = &_truncate_text ($text, 6 * $n);
+ }
+
+ $text;
+}
+
+sub _truncate_text {
+ my ($text, $num) = @_;
+
+ if (length ($text) > $num) {
+ my $ascii = '[\x00-\x7F]';
+ my $twoBytes = '[\x8E\xA1-\xFE][\xA1-\xFE]';
+ my $threeBytes = '\x8F[\xA1-\xFE][\xA1-\xFE]';
+
+ $text = substr ($text, 0, $num);
+ ($text) = ($text =~ /^((?:$ascii|$twoBytes|$threeBytes)*)/o);
+ $text .= "…";
+ }
+ $text;
}

sub munge_comment {

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

日々是ハック也 -- あるハードコアバイナリアン

読み込み中...