You are here

public function Toc::__construct in TOC API 8

Constructs a new TOC object.

Parameters

string $source: The HTML content that contains header tags used to create a table of contents.

array $options: (optional) An associative array of options used to generate a table of contents and bookmarked headers. elements:

  • 'template': Template for table of contents. Possible values: 'tree', 'menu', 'responsive' Default value is responsive.
  • 'title': (optional) Title for table of contents. Default value is 'Table of contents'.
  • 'block': (optional) If TRUE table of contents will be displayed in a block.
  • 'header_count': The minimum number of top level headers required to create a table of contents. Default value is 2
  • 'header_min': The minimum level of header to be included. Default value is 4
  • 'header_max': The maximum level of header to be included. Default value is 2
  • 'header_allowed_tags': List of HTML tags allowed inside a header.
  • 'header_id': Type of header id. Possible values 'title', 'path', 'key'.
  • 'header_id_prefix': Prefix to be prepended to header id when 'path' or 'key' is selected. Default value is 'section'
  • 'top_min': The minimum level of header to include a back to top link. Default value is 2
  • 'top_max': The maximum level of header to include a back to top link. Default value is 2
  • 'top_label': The text to be displayed in back to top links. Default value is 'Back to top'
  • 'number_path': Display the full path inside the header tag. Default value is TRUE.
  • 'number_path_separator': (optional) Path separator used to display the full hierarchy in a header.
  • 'number_path_truncate': (optional) If TRUE empty value (ie 0) will be removed from path.

    • 'default.top': Displays back to top link. If FALSE back to link is hidden.
  • 'default': A associative array containing options for header indexes.

    • 'default.number_type': List style type. Possible values: 'decimal', 'upper-alpha', 'lower-alpha', 'upper-roman', 'lower-roman', 'disc', 'circle', 'square', or 'none' Defaults to 'decimal',
    • 'default.number_prefix': Text to added before the header type. Defaults to ''
    • 'default.number_suffix': Text to added after the header type. Defaults to ') '
  • 'h1 - h2': Header specific settings that override the 'default' settings.

File

src/Toc.php, line 168

Class

Toc
Defines A class that parses the header tags from an HTML document.

Namespace

Drupal\toc_api

Code

public function __construct($source, $options = []) {
  $this->source = $source;

  // Set default options for each header tag.
  $this->options = NestedArray::mergeDeep($this->defaultOptions, $options);
  for ($i = 1; $i <= 6; $i++) {
    $tag = 'h' . $i;
    if ($i >= $this->options['header_min'] && $i <= $this->options['header_max']) {
      $this->options['headers'][$tag] = NestedArray::mergeDeep($this->options['default'], $this->options['headers'][$tag]);
    }
    else {
      unset($this->options['headers'][$tag]);
    }
  }
  $this->allowedTags = $this
    ->formatter()
    ->convertAllowedTagsToArray($this->options['header_allowed_tags']);
  $this
    ->initialize();

  // DEBUG:
  // dsm($this->getIndex());
  // dsm($this->getTree());
}