Beispiel #1
0
 /**
  * Replace callback for fix function
  * 
  * @param array $value 
  * @return string Markup with added IDs
  */
 private function fix_callback($value)
 {
     $sluggifier = new UniqueSluggifier();
     if (preg_match('/id/ui', $value[1]) == false) {
         if (preg_match('/title/ui', $value[1])) {
             $title = preg_replace_callback('/.*title[\\t,\\ ]*?=[\\t,\\ ]*?[\'|"](.*?)[\'|"]/ui', function ($m) {
                 return $m[1];
             }, $value[1]);
             return $value[1] . ' id="' . preg_replace('/^[-0-9._:]/', 'N', $sluggifier->slugify($title)) . '">' . $value[3] . '</h';
         }
         return $value[1] . ' id="' . preg_replace('/^[-0-9._:]/', 'N', $this->CheckUniqId($sluggifier->slugify($value[3]))) . '">' . $value[3] . '</h';
     }
     return $value[0];
 }
Beispiel #2
0
 /**
  * Fix markup
  *
  * @param string $markup
  * @param int    $topLevel
  * @param int    $depth
  * @return string Markup with added IDs
  * @throws RuntimeException
  */
 public function fix($markup, $topLevel = 1, $depth = 6)
 {
     if (!$this->isFullHtmlDocument($markup)) {
         $partialID = 'toc_generator_' . mt_rand(1000, 4000);
         $markup = sprintf("<body id='%s'>%s</body>", $partialID, $markup);
     }
     $domDocument = $this->htmlParser->loadHTML($markup);
     $domDocument->preserveWhiteSpace = true;
     // do not clobber whitespace
     $sluggifier = new UniqueSluggifier();
     /** @var \DOMElement $node */
     foreach ($this->traverseHeaderTags($domDocument, $topLevel, $depth) as $node) {
         if ($node->getAttribute('id')) {
             continue;
         }
         $node->setAttribute('id', $sluggifier->slugify($node->getAttribute('title') ?: $node->textContent));
     }
     return $this->htmlParser->saveHTML(isset($partialID) ? $domDocument->getElementById($partialID)->childNodes : $domDocument);
 }