Kirby Chopper Plugin


#1

Someone asked earlier today about excerpts, and how to keep the tags. Since the built in excerpt() strips everything, @texnixe suggested a custom field method.

It just so happened the project I am working on at the moment needs this, so I Googled around, one thing led to another and Kirby Chopper was born.

I really cant take much credit for this, it’s based on the work described in an excellent article by Patrick Galbraith. I simply wrapped it up into a plugin and tweaked it a little.

It’s a beta plugin, so please, go careful. Feel free to post issues or PR’s over on Github.

Usage

To trim to 200 characters and append ellipsis on the end:

<?= $page->text()->chopper(200, 'chars', '…') ?>

To trim to 50 words and append ellipsis on the end:

<?= $page->text()->chopper(50, 'words', '…') ?>

Defaults to words and an ellipsis, so if you just want 20 words and an ellipsis on the end, its shorter to write:

<?= $page->text()->chopper(20) ?>

To change the default list of kept tags, add this line to your config.php and amend accordingly:

c::set('chopper.keep', '<p><a><strong><em><sub><sup><blockquote><figure><img>');

Excerpts with working styling/tags?
#2

I found a small issue with this but not sure how to fix it really. It doesn’t like things like fancy quotes, they come out weird like the encoding is wrong or something. This showed up on content pasted from Word. Content typed directly into fields seems fine.

If anyone has any idea how i can fix it, please point me in the right direction. For now I did a find and replace on all my content files to change the offending characters, but could do with a real fix.

Im a bit confused how it happened really, because im rendering it with kirbytext before chopping it up, which should have dealt with those characters properly upfront (i think)


#3

I got to the bottom of it. Just pushed an update. Should handle fancy characters just fine now.


#4

@jimbobrjames:

Thank you for your great plugin. I like it very much!

If you want to extend it, you may add:

  1. a new option in the call of the plugin to change the default config option of “chopper.keep” for this one call
<?= $page->text()->chopper(20, $keep='<p><a><strong><em><sub><sup><blockquote><h1><h2><h3><h4><h5><h6>') ?>

e.g. without <figure><img>. Without of this option in the plugin call it should furthermore use the config setting.

  1. another new option to change some tags, e.g. the headings. At the moment I add it to your plugin code like
  $search  = array("<h3", "</h3", "<h2", "</h2", "<h1", "</h1");
  $replace = array("<h5", "</h5", "<h4", "</h4", "<h3", "</h3");
  $result = str_ireplace($search, $replace, $result);

in front of the last line

  return $result;

I need this, if I post a preview of a blog article.

Thank you very much!


#5

@HeinerEF Your second suggestion is rather an edge case and not necessary to add in the plugin as you might as well do your string replacement in the final result if you need it.

$search  = array("<h3", "</h3", "<h2", "</h2", "<h1", "</h1");
$replace = array("<h5", "</h5", "<h4", "</h4", "<h3", "</h3");
$result = str_ireplace($search, $replace, $page->text()->chopper(20));
echo $result;

#6

Thanks, @texnixe … I must say I was on the fence on the second suggestion. Glad theres a way to do it on the fly for those who want it.

@HeinerEF Your first suggestion seems useful, so I will look into that. Thanks for liking my plugin :slight_smile:


#7

Thank you @texnixe and @jimbobrjames .

I will use

<?php echo str_ireplace(array("<h3", "</h3", "<h2", "</h2", "<h1", "</h1"), array("<h5", "</h5", "<h4", "</h4", "<h3", "</h3"), $page->text()->chopper(20)) . "\n";  ?>

in future.

I don’t like the new syntax with <?= or [ , , ], because I’m old fashioned.


#8

@HeinerEF As long as you don’t have to follow any coding standards and just coding along merrily for yourself, you can do whatever you like. However, the short echo tag and the short array notation are less verbose and thus make your code more readable.


#9

Sorry @texnixe, I can not agree to

In my view it is the other way around:

The sign = is less informative than the word echo and the word array tells me (and may be others) direct the type of the following in the round brackets. [ ] says nothing, I have to look in a translation table to know that meaning.


#10

Then get your “translation table” ready for Kirby 3, you won’t find any array() language constructs nor any echo() commands directly following a php opening tags in the new docs( or the source) code anymore.


#11

This is unfortunately already for getkirby.com/docs. It does not always get better, only newer.


#12

@HeinerEF I was like you, but when getting use to it I agree with @texnixe.

Try it and give it some time and you may not look back.