Permalink
Browse files

excerpt() for the index and rss views

  • Loading branch information...
1 parent 9d6881e commit 5fc478bef281747ddf753c2538ca2f842617a79d @juliend2 committed Dec 31, 2011
Showing with 54 additions and 2 deletions.
  1. +48 −0 lib/bato.php
  2. +4 −0 posts/2011-08-16-second-post.textile
  3. +1 −1 themes/default/home.php
  4. +1 −1 themes/default/rss.php
View
@@ -102,3 +102,51 @@ function theme_url() {
return $config['base_url'] . '/themes/' . $config['theme'];
}
+function truncate_html($text, $length, $suffix = '…', $isHTML = true){
+ $i = 0;
+ $simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
+ $tags = array();
+ if($isHTML){
+ preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
+ foreach($m as $o){
+ if($o[0][1] - $i >= $length)
+ break;
+ $t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
+ // test if the tag is unpaired, then we mustn't save them
+ if($t[0] != '/' && (!isset($simpleTags[$t])))
+ $tags[] = $t;
+ elseif(end($tags) == substr($t, 1))
+ array_pop($tags);
+ $i += $o[1][1] - $o[0][1];
+ }
+ }
+
+ // output without closing tags
+ $output = substr($text, 0, $length = min(strlen($text), $length + $i));
+ // closing tags
+ $output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');
+
+ // Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
+ $pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
+ // Append closing tags to output
+ $output.=$output2;
+
+ // Get everything until last space
+ $one = substr($output, 0, $pos);
+ // Get the rest
+ $two = substr($output, $pos, (strlen($output) - $pos));
+ // Extract all tags from the last bit
+ preg_match_all('/<(.*?)>/s', $two, $tags);
+ // Add suffix if needed
+ if (strlen($text) > $length) { $one .= $suffix; }
+ // Re-attach tags
+ $output = $one . implode($tags[0]);
+
+ return $output;
+}
+
+function excerpt($content) {
+ global $config;
+ return truncate_html(strip_tags($content, '<p><br>'), $config['max']);
+}
+
@@ -1,3 +1,7 @@
title: Second post
This is my *second* post.
+
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas convallis, massa vel sodales consectetur, enim nulla dignissim lacus, in mattis turpis ligula vitae augue. Etiam hendrerit ultrices laoreet. Fusce eget mi nibh, sit amet venenatis leo. Sed aliquam lorem in massa ornare iaculis. Duis ac purus sapien, ut congue quam. Fusce hendrerit erat tincidunt orci aliquet at eleifend ipsum porttitor. Sed ac turpis faucibus orci sollicitudin scelerisque sed id velit. Morbi a mi a diam commodo consectetur in ut nisl. Fusce sit amet tincidunt metus.
+
+Nam gravida arcu eget sem accumsan fermentum. Mauris ac leo et nisl venenatis tempor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Duis dictum venenatis nibh at molestie. Nunc fermentum molestie posuere. Vestibulum accumsan quam vitae dui dictum id facilisis est lobortis. Phasellus adipiscing metus nec felis lacinia tempor. Ut sem ipsum, eleifend quis bibendum ac, congue in elit. Maecenas viverra orci leo, a vehicula sapien. Aliquam hendrerit sapien id neque tristique eget convallis odio volutpat. Nulla pretium scelerisque nulla, in mattis leo facilisis eget. Sed vitae urna enim, vitae imperdiet sem. Duis varius faucibus volutpat. Sed ac nunc eros, ut tempus lorem. Mauris tristique consectetur lacus, a blandit ante bibendum id.
View
@@ -1,5 +1,5 @@
<?php foreach($posts as $post):?>
<h2><a href="<?php echo post_url($post) ?>"><?php echo $post['title'] ?></a></h2>
<h3><?php echo strftime('%B %e, %Y', strtotime($post['year'].'-'.$post['month'].'-'.$post['day'])) ?></h3>
- <?php echo $post['content'] ?>
+ <?php echo excerpt($post['content']) ?>
<?php endforeach ?>
View
@@ -9,7 +9,7 @@
<?php foreach($posts as $post): ?>
<item>
<title><?php echo $post['title'] ?></title>
- <description><?php echo '<![CDATA['. truncate($post['content'], $config['max']) .']]>'; ?></description>
+ <description><?php echo '<![CDATA['. excerpt($post['content']) .']]>'; ?></description>
<pubDate><?php echo date("D, d M Y H:i:s", strtotime($post['year'].'-'.$post['month'].'-'.$post['day'])) ?></pubDate>
<link><?php echo post_url($post) ?></link>
</item>

0 comments on commit 5fc478b

Please sign in to comment.