From 3fbcba03d681ea01e049506dfb5c676bad925542 Mon Sep 17 00:00:00 2001 From: hylom Date: Wed, 9 Nov 2016 00:34:32 +0900 Subject: [PATCH] Model::Journals: add article_to_introtext --- src/newslash_web/lib/Newslash/Model/Journals.pm | 93 +++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 6 deletions(-) diff --git a/src/newslash_web/lib/Newslash/Model/Journals.pm b/src/newslash_web/lib/Newslash/Model/Journals.pm index e605567d..8db65348 100644 --- a/src/newslash_web/lib/Newslash/Model/Journals.pm +++ b/src/newslash_web/lib/Newslash/Model/Journals.pm @@ -1,6 +1,7 @@ package Newslash::Model::Journals; use Newslash::Model::Base -base; +use Newslash::Model::Util; use URI::Escape; use Data::Dumper; @@ -223,8 +224,8 @@ journal id =cut -my $comment_status = ['disabled', 'enabled', 'friends_only', 'friends_fof_only', - 'no_foe', 'no_foe_eof' ,'logged_in']; +use constant COMMENT_STATUS => ['disabled', 'enabled', 'friends_only', 'friends_fof_only', + 'no_foe', 'no_foe_eof' ,'logged_in']; sub create { my ($self, $params, $user, $extra_params, $opts) = @_; @@ -248,8 +249,8 @@ sub create { $self->set_error("no commentstatus given", 1); return; } - if (!grep {/$params->{commentstatus}/} @$comment_status) { - $self->set_error("invalid commentstatus given", 1); + if (!grep {/$params->{commentstatus}/} COMMENT_STATUS) { + $self->set_error("invalid comment_status given", 1); return; } @@ -262,7 +263,7 @@ sub create { #my $rootdir = $gSkin->{rootdir}; #my $did = $slashdb->createDiscussion({ # kind => 'journal', - # title => $description, + # title => $title, # topic => $form->{tid}, # commentstatus => $form->{journal_discuss}, # url => "$rootdir/~" . fixparam($user->{nickname}) . "/journal/$id", @@ -324,7 +325,8 @@ INSERT INTO journals_text (?, ?, ?) EOSQL - $rs = $dbh->do($sql, undef, $journal_id, $params->{article}, $params->{article}); + my $introtext = $self->article_to_introtext($params->{article}, $params->{posttype}); + $rs = $dbh->do($sql, undef, $journal_id, $params->{article}, $introtext); if (!$rs) { $dbh->rollback; $self->set_error("insert into journals_text failed", $dbh->{mysql_errorno}); @@ -413,6 +415,85 @@ EOSQL return $journal_id; } +=pod + +HTML形式:(HTML) +利用可能なHTMLタグを使って、投稿の体裁を整えることができます。 + +テキスト形式(HTML OK!):(EXTRANS) +HTMLタグを使用できます。HTML形式との違いは、改行のポイントで改行文字として
が +自動的に挿入され、さらに空白文字は多少知的に改行なしのスペースに変わります。 + +ホントのテキスト形式:(PLAINTEXT) +&、 <、>が除外され、HTMLタグの利用ができないホントのテキスト形式です。 + +コード:(CODE) +HTMLを使えないテキスト形式ですが、さらに等幅フォントが使われ、適当にインデントもされます。 + +=cut + +sub article_to_introtext { + my ($self, $article, $posttype) = @_; + my $html; + my $allowed = { + 'b' => [], + 'i' => [], + 'p' => [], + 'br' => [], + 'a' => ['href',], + 'ol' => ['start',], + 'ul' => [], + 'li' => [], + 'dl' => [], + 'dt' => [], + 'dd' => [], + 'em' => [], + 'strong' => [], + 'tt' => [], + 'blockquote' => ['title', 'cite',], + 'div' => [], + 'ecode' => [], + 'del' => [], + 'ins' => [], + 'sub' => [], + 'sup' => [], + 'quote' => [], + 'strike' => [], + }; + + + # posttype: 1: PLAINTEXT, 2: HTML, 3: EXTRANS, 4: CODE, 77: FULLHTML + if ($posttype == 1) { # PLAINTEXT + $html = Newslash::Model::Util::escape_plaintext($allowed, $article); + } + if ($posttype == 2) { # HTML + $html = Newslash::Model::Util::clean_html($allowed, $article); + } + if ($posttype == 3) { # LEGACY HTML (HTML OK) + my $t = $article; + $t =~ s/\r\n/
/g; # conert CRLF + $t =~ s/(\r|\n)/
/g; # convert CR/LF + $html = Newslash::Model::Util::escape_html($allowed, $t); + } + my $min_chars = 50; + my $max_chars = 500; + my $intro_text; + if (length $html < $min_chars) { + $intro_text = $html; + } + else { + my $linebreak = qr{(?: +
\s*
| + | + + )}x; + $intro_text = $1 if $html =~ m/^(.{$min_chars,$max_chars})?$linebreak/s; + } + $intro_text = Newslash::Model::Util::clean_html($intro_text); + return $intro_text; +} sub create_firehose_item { my ($self, $journal, $article, $user, $options) = @_; -- 2.11.0