TopShelfCraft / Wordsmith

A plugin for Craft CMS to help you manage and manipulate text.
Other
30 stars 20 forks source link

chop with words should add a space after each paragraph. #28

Closed outline4 closed 4 years ago

outline4 commented 4 years ago

If there is more then one paragraph, it should add a space after each paragraph

With this:

{% set text %}
    <p>She sells seashells by the seashore.</p><p>The shells that she sells are seashells, I'm <em>sure</em>.</p><p>So, if she sells shells she sees on the shore,</p><p>Yes, I say she sells seashore shells, for sure.</p>
{% endset %}

{{ text|chop(limit=40, unit='w', append="…") }}

the output is the following:

She sells seashells by the seashore.The shells that she sells are seashells, I'm sure.So, if she sells shells she sees on the shore,Yes, I say she sells seashore shells, for sure.

You see, there should be a space after seashore and sure. etc.

If you separate the paragraphs like you did in your example, everything works fine:

{% set text %}
    <p>She sells seashells by the seashore.</p>
    <p>The shells that she sells are seashells, I'm <em>sure</em>.</p>
    <p>So, if she sells shells she sees on the shore,</p>
    <p>Yes, I say she sells seashore shells, for sure.</p>
{% endset %}

Is this a bug?

Is there a way around it?

Cheers
Stefan

outline4 commented 4 years ago

Ok, I've found a way around this problem: {{ text|replace({'</p>': '</p> '})|chop(limit=40, unit='w', append="…") }}. But it would be nice if chopper could do this itself...

michaelrog commented 4 years ago

Stefan — Thanks for pointing this out.

Unfortunately, I think it'd be tricky to implement your remedy in a consistent way. For example, we would only consider adding whitespace between consecutive block elements; it would be incorrect to add extra space around inline elements. So now chop needs to know/care about different HTML tags? (Seems kind of outside the scope of that method.) And, which whitespace character(s) would the user prefer? A space? A line break?

So, probably chop is not the right place for this feature. I like to keep the mandate of individual methods as tight as possible.

...

However... Maybe we could add a asPlainText method, to extract plain text from HTML, which could normalize whitespace around block elements... And then methods like chop could lean on that asPlainText functionality when a user wants to operate on plain text from an HTML passage.

Perhaps somebody would like to think through the params/implementation of such a method and strike up a FR/PR for it... 😇