I see that there are Trimmer and Truncate out there that have been created for truncating text but I don't believe either does what it is I'm looking for.

I'd like to truncate text similar to how Hacksaw for ExpressionEngine works. Allowing me to allow or disallow html tags, and making sure that even if it cuts off before a tag is closed, that it automatically properly closes the tag. I wouldn't want to turn the entire lower half of my page into a hyperlink for instance if i happen to truncate mid-link. I do want to maintain the ability for links to be in the text that is being truncated, and multiple paragraphs, etc. however.

3 Answers
3

This hasn't been fully tested for production, or documented for that matter, but I have migrated it over and tested basic usage as per the docs on devot-ee. It should, at the very least, give you a nice start.

You can use the Twig filter with: {{ entry.fieldHandle|hacksaw(words="100") }}

Rock on Ryan, thanks for porting it over. Get it up on straightupcraft.com when you get a chance. This does exactly what i need (as far as my current needs), haven't tested it extensively.
– Christopher HealeyJul 1 '14 at 21:20

After posting it originally, I went back and modified a few things after testing it a bit more thoroughly. I noticed one of the downfalls was as you said leaving HTML elements unclosed so I added in a new method to take care of that. I believe it now to function as the EE version does. I will work at getting it up on Straight up Craft as well as getting it fully documented here in the next couple days. Glad to help.
– Ryan ShrumJul 1 '14 at 21:44

We see an issue with current version of Hacksaw where the content contains non-breaking spaces. They're being converted into little black diamonds with questions marks in them. Redactor loves to add non-breaking spaces, in my experience.
– Ryan MasugaJul 21 '16 at 15:25

There's also another issue that it's not working currently well with text in Cyrillic (or maybe any Unicode enabled range of characters).
– certainlyakeyNov 1 '16 at 20:55