danielstjules / Stringy

A PHP string manipulation library with multibyte support
MIT License
2.46k stars 216 forks source link

[question] Finding a block #176

Closed ganar closed 6 years ago

ganar commented 6 years ago

I need to be able to find a block defined by the occurrence of an H2 title followed by a number of html elements and ending just before the opening of following H2, or the end of the container, in order to add a <section> around it. Here is an example of the markdown:

##Ingredients

* Garlic
* Bread
* Cherry tomatos

##Preparation

* Peel the garlic
* Cut the cherry tomatoes in half
* Toast the bread
* Eat the whole thing

The final output from twig should be something like this:

<section>
<h2>Ingredients</h2>
<ul>(…)</ul>
</section>

<section>
<h2>Preparation</h2>
<ul>(…)</ul>
</section>

Can Stringy help me to do that? How?

Any help on the subject is appreciated!

danielstjules commented 6 years ago

Stringy doesn't have any special functions for assisting with that. Are you converting from markdown to HTML? If there's only a single level of nesting as in this example, you could convert the first h2 to <section><h2></h2>, each subsequent to </section><section><h2></h2>, and append a closing </section>.

That said, it sounds like you just want a markdown parser: https://github.com/erusev/parsedown