public function parse(&$source) { list($ol, $ul) = $this->params; // Find all lists in the source $pattern = preg_quote(trim($ol)) . '|' . preg_quote(trim($ul)); if (preg_match_all('/(<(ol|ul)\\b[^>]*>)(.*?)(<\\/(ol|ul)>)/is', $source, $matches)) { foreach (array_unique($matches[0]) as $key => $replace) { $char = strcasecmp($matches[2][$key], 'ul') === 0 ? $ul : $ol; // list items $strip = new HTMLTagParseAction('li', $char, PHP_EOL); $strip->parse($replace); // list tag $strip = new HTMLTagParseAction($matches[2][$key], ''); $strip->parse($replace); // Whitespace cleanup $replace = preg_replace('/\\s+(' . $pattern . ')/s', PHP_EOL . "\$1", $replace); $replace = PHP_EOL . trim($replace) . PHP_EOL . PHP_EOL; $source = str_replace($matches[0][$key], $replace, $source); } } return $this; }
public function testHTMLTags() { $a = new HTMLTagParseAction('h1', '='); $control = '<h1>Some Title</h1>'; $return = $a->parse($control); $this->assertEquals('=Some Title=', $control); $this->assertInstanceOf('aklump\\loft_parser\\ParseAction', $return); $a = new HTMLTagParseAction('h1', '='); $control = '<h1 id="page-title" class=\'red\'>Some Title</h1>'; $a->parse($control); $this->assertEquals('=Some Title=', $control); $b = new HTMLTagParseAction('strong', "'''"); $control = '<h1><strong>A Strong Title</strong></h1>'; $a->parse($control); $b->parse($control); $this->assertEquals("='''A Strong Title'''=", $control); $a = new HTMLTagParseAction('h1', '<H1>', '</H1>'); $control = '<h1>Some Title</h1>'; $a->parse($control); $this->assertEquals('<H1>Some Title</H1>', $control); $a = new HTMLTagParseAction('ol', '@code', '@endcode'); $control = '<ol id="some-list-id"><li>do</li><li>re</li></ol>'; $a->parse($control); $this->assertEquals('@code<li>do</li><li>re</li>@endcode', $control); }