Changeset 16355

Show
Ignore:
Timestamp:
05/09/08 16:34:27 (2 months ago)
Author:
xfade
Message:

Add more intelligent word count truncation to social news. Based on algo by rambo. Also add configurable html tag filter.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/MidCOM_2_8/org.maemo.socialnews/config/config.inc

    r15990 r16355  
    99'frontpage_show_main_items' => 3, 
    1010'frontpage_show_abstract_length' => 1000, 
     11'frontpage_show_abstract_length_words' => 0, 
     12'frontpage_allowed_tags' => '<p><a><img><b><i><strong><pre><ul><li><br>', 
    1113 
    1214'frontpage_show_secondary_items' => 7, 
  • branches/MidCOM_2_8/org.maemo.socialnews/config/manifest.inc

    r16352 r16355  
    11'name' => 'org.maemo.socialnews', 
    22'purecode' => false, 
    3 'version' => '1.1.4', 
     3'version' => '1.1.5', 
    44'state' => 'stable', 
    55'privileges' => array(), 
  • branches/MidCOM_2_8/org.maemo.socialnews/documentation/CHANGES

    r16352 r16355  
    10102008-05-09 xfade 
    1111  + RSS feed for latest items 
     12  + Add more intelligent word count truncater 
    1213 
    13142007-08-29 bergie 
  • branches/MidCOM_2_8/org.maemo.socialnews/handler/index.php

    r15501 r16355  
    147147    } 
    148148 
     149    private function word_count_truncater(&$content, &$url) 
     150    { 
     151        // Normalize newlines to \n 
     152        $content = preg_replace("/\n\r|\r\n|\r/", "\n", $content); 
     153        $content = strip_tags($content, $this->_config->get('frontpage_allowed_tags')); 
     154 
     155        $word_limit = $this->_config->get('frontpage_show_abstract_length_words'); 
     156        // Naive but works 
     157        $words = count(preg_split('/\b/', strip_tags($content))); 
     158        if ($words <= $word_limit) 
     159        { 
     160            return $content; 
     161        } 
     162        // The difficult part starts here 
     163        // In stead of trying parse the HTML tree to get N words while correctly closing all tags we get first paragraph 
     164 
     165        $content_matches = array(); 
     166        $ret = ""; 
     167        if (preg_match("%((^\s+.*?)|(^.*?))(</p>|(\n\s*){2,}|(<br\s*/?>\s*){2,}|\w+\s*<p>)%ms", $content, $content_matches)) 
     168        { 
     169            $first_paragraph =& $content_matches[1]; 
     170            $first_paragraph_words = count(preg_split('/\b/', strip_tags($first_paragraph))); 
     171 
     172            // Check that our "first paragraph" is of sane size 
     173            if ($first_paragraph_words <= $word_limit) 
     174            { 
     175                $remaining_words = $words - $first_paragraph_words; 
     176                $ret = $first_paragraph; 
     177                if ($remaining_words < 1) 
     178                { 
     179                    return; 
     180                } 
     181                $ret .= "<div class='entry-truncated'><a href='{$url}'>Click to read {$remaining_words} more words</a></div>\n"; 
     182                return $ret; 
     183            } 
     184        } 
     185        else  
     186        { 
     187            //This post doesn't have nice <p> paragraphs 
     188            $ret = substr($content,0,strpos($content,"\n")); 
     189            $words = count(preg_split('/\b/', strip_tags($ret))); 
     190            if ($words < $word_limit) 
     191            { 
     192                $ret .= "<div class='entry-truncated'><a href='{$url}'>Click to read {$remaining_words} more words</a></div>\n"; 
     193                return $ret; 
     194            } 
     195        } 
     196 
     197        // Fallback, show only "xxx words, click here to read" 
     198        $ret .= "<div class='entry-truncated'><a href='{$url}'>Click to read {$words} words</a></div>\n"; 
     199        return $ret; 
     200    } 
     201 
    149202    private function generate_caption($data, $getCnt) 
    150203    { 
     
    221274            if (empty($article->abstract)) 
    222275            { 
    223                 $article->abstract = $this->generate_caption($article->content, $this->_config->get('frontpage_show_abstract_length')); 
     276                if ($this->_config->get('frontpage_show_abstract_length_words') > 0) 
     277                { 
     278                    $article->abstract = $this->word_count_truncater($article->content, $article->url); 
     279                } 
     280                else 
     281                { 
     282                    $article->abstract = $this->generate_caption($article->content, $this->_config->get('frontpage_show_abstract_length')); 
     283                } 
    224284            } 
    225285            else 
    226286            { 
    227                 $article->abstract = $this->generate_caption($article->abstract, $this->_config->get('frontpage_show_abstract_length')); 
     287                if ($this->_config->get('frontpage_show_abstract_length_words') > 0) 
     288                { 
     289                    $article->abstract = $this->word_count_truncater($article->abstract, $article->url); 
     290                } 
     291                else 
     292                { 
     293                    $article->abstract = $this->generate_caption($article->abstract, $this->_config->get('frontpage_show_abstract_length')); 
     294                } 
    228295            } 
    229296