Changeset 16355
- Timestamp:
- 05/09/08 16:34:27 (2 months ago)
- Files:
-
- branches/MidCOM_2_8/org.maemo.socialnews/config/config.inc (modified) (1 diff)
- branches/MidCOM_2_8/org.maemo.socialnews/config/manifest.inc (modified) (1 diff)
- branches/MidCOM_2_8/org.maemo.socialnews/documentation/CHANGES (modified) (1 diff)
- branches/MidCOM_2_8/org.maemo.socialnews/handler/index.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/MidCOM_2_8/org.maemo.socialnews/config/config.inc
r15990 r16355 9 9 'frontpage_show_main_items' => 3, 10 10 '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>', 11 13 12 14 'frontpage_show_secondary_items' => 7, branches/MidCOM_2_8/org.maemo.socialnews/config/manifest.inc
r16352 r16355 1 1 'name' => 'org.maemo.socialnews', 2 2 'purecode' => false, 3 'version' => '1.1. 4',3 'version' => '1.1.5', 4 4 'state' => 'stable', 5 5 'privileges' => array(), branches/MidCOM_2_8/org.maemo.socialnews/documentation/CHANGES
r16352 r16355 10 10 2008-05-09 xfade 11 11 + RSS feed for latest items 12 + Add more intelligent word count truncater 12 13 13 14 2007-08-29 bergie branches/MidCOM_2_8/org.maemo.socialnews/handler/index.php
r15501 r16355 147 147 } 148 148 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 149 202 private function generate_caption($data, $getCnt) 150 203 { … … 221 274 if (empty($article->abstract)) 222 275 { 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 } 224 284 } 225 285 else 226 286 { 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 } 228 295 } 229 296
