Ejemplo n.º 1
0
 /**
  * Build a hierarchical menu
  *
  * @param object $content \MarkdownExtended\API\ContentInterface
  * @param object $formatter \MarkdownExtended\API\OutputFormatInterface
  *
  * @return string
  */
 public function getToc(ContentInterface $md_content, OutputFormatInterface $formatter, array $attributes = null)
 {
     $cfg_toc_max_level = $this->getConfigOrDefault('toc_max_level');
     $cfg_toc_title = $this->getConfigOrDefault('toc_title');
     $cfg_toc_title_level = $this->getConfigOrDefault('toc_title_level');
     $cfg_toc_id = $this->getConfigOrDefault('toc_id');
     $cfg_toc_class = $this->getConfigOrDefault('toc_class');
     $cfg_toc_item_title = $this->getConfigOrDefault('toc_item_title');
     $menu = $md_content->getMenu();
     $content = $list_content = '';
     $max_level = isset($attributes['max_level']) ? $attributes['max_level'] : $cfg_toc_max_level;
     if (!empty($menu) && count($menu) > 1) {
         $depth = 0;
         $current_level = null;
         foreach ($menu as $item_id => $menu_item) {
             $_item_id = Helper::getSafeIdString($item_id);
             if (isset($max_level) && $menu_item['level'] > $max_level) {
                 continue;
             }
             $diff = $menu_item['level'] - (is_null($current_level) ? $menu_item['level'] : $current_level);
             if ($diff > 0) {
                 $list_content .= str_repeat('<ul><li>', $diff);
             } elseif ($diff < 0) {
                 $list_content .= str_repeat('</li></ul></li>', -$diff);
                 $list_content .= '<li>';
             } else {
                 if (!is_null($current_level)) {
                     $list_content .= '</li>';
                 }
                 $list_content .= '<li>';
             }
             $depth += $diff;
             $list_content .= $formatter->buildTag('link', $menu_item['text'], array('href' => '#' . $_item_id, 'title' => isset($attributes['toc_item_title']) ? $attributes['toc_item_title'] : $cfg_toc_item_title));
             $current_level = $menu_item['level'];
         }
         if ($depth != 0) {
             $list_content .= str_repeat('</ul></li>', $depth);
         }
         $content .= $formatter->buildTag('title', $cfg_toc_title, array('level' => isset($attributes['toc_title_level']) ? $attributes['toc_title_level'] : $cfg_toc_title_level, 'id' => isset($attributes['toc_id']) ? $attributes['toc_id'] : $cfg_toc_id, 'no-addon' => true));
         $content .= $formatter->buildTag('unordered_list', $list_content, array('class' => isset($attributes['class']) ? $attributes['class'] : $cfg_toc_class));
     }
     return $content;
 }
Ejemplo n.º 2
0
 public function buildTitle($text, array $attributes = array())
 {
     if (!isset($attributes['id']) || empty($attributes['id'])) {
         $attributes['id'] = uniqid();
     } else {
         $attributes['id'] = Helper::getSafeIdString($attributes['id']);
     }
     if (!isset($attributes['name']) || empty($attributes['name'])) {
         $attributes['name'] = $attributes['id'];
     }
     if (isset($attributes['level'])) {
         $tag = 'h' . $attributes['level'];
         unset($attributes['level']);
     } else {
         $tag = 'h' . MarkdownExtended::getVar('baseheaderlevel');
     }
     if (!isset($attributes['no-addon']) || $attributes['no-addon'] !== true) {
         $text = $this->addTitleAddon($text, $attributes);
     }
     if (isset($attributes['no-addon'])) {
         unset($attributes['no-addon']);
     }
     $_ttl = $this->getTagString($text, $tag, $attributes);
     return $_ttl;
 }