OSDN Git Service

Model::Journals: add article_to_fulltext method
authorhylom <hylom@users.sourceforge.jp>
Fri, 11 Nov 2016 15:18:16 +0000 (00:18 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 11 Nov 2016 15:18:16 +0000 (00:18 +0900)
src/newslash_web/lib/Newslash/Model/Journals.pm

index 7bcc279..61226a2 100644 (file)
@@ -185,6 +185,7 @@ sub _generalize {
     $journal->{primary_topic} = $primary_topic;
     $journal->{topics} = [$primary_topic,];
     $journal->{content_type} = 'journal';
+    $journal->{bodytext} = $self->article_to_bodytext($journal->{article}, $journal->{posttype});
 
     return $journal;
 }
@@ -452,47 +453,8 @@ HTMLを使えないテキスト形式ですが、さらに等幅フォントが
 
 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' => [],
-                  };
-
     my $util = $self->new_instance_of("Newslash::Model::Util");
-    # posttype: 1: PLAINTEXT, 2: HTML, 3: EXTRANS, 4: CODE, 77: FULLHTML
-    if ($posttype == 1) { # PLAINTEXT
-        $html = $util->escape_plaintext($allowed, $article);
-    }
-    if ($posttype == 2) { # HTML
-        $html = $util->clean_html($allowed, $article);
-    }
-    if ($posttype == 3) { # LEGACY HTML (HTML OK)
-        my $t = $article;
-        $t =~ s/\r\n/<br>/g; # conert CRLF
-        $t =~ s/(\r|\n)/<br>/g; # convert CR/LF
-        $html = $util->escape_html($allowed, $t);
-    }
+    my $html = $self->article_to_fulltext($article, $posttype);
     my $min_chars = 50;
     my $max_chars = 500;
     my $intro_text;
@@ -515,6 +477,30 @@ sub article_to_introtext {
 
 sub article_to_bodytext {
     my ($self, $article, $posttype) = @_;
+    my $util = $self->new_instance_of("Newslash::Model::Util");
+    my $html = $self->article_to_fulltext($article, $posttype);
+    my $min_chars = 50;
+    my $max_chars = 500;
+    if (length $html < $min_chars) {
+        return "";
+    }
+    else {
+        my $linebreak = qr{(?:
+                               <br>\s*<br> |
+                               </?p> |
+                               </(?:
+                                   div | (?:block)?quote | [oud]l
+                               )>
+                           )}x;
+        my $intro_text = $1 if $html =~ m/^(.{$min_chars,$max_chars})?$linebreak/s;
+        my $body_text = substr $html, length($intro_text);
+        $body_text = $util->tidy_html($body_text);
+        return $body_text;
+    }
+}
+
+sub article_to_fulltext {
+    my ($self, $article, $posttype) = @_;
     my $html;
     my $allowed = {
                    'b' => [],