Parsoid
A bidirectional parser between wikitext and HTML5
Loading...
Searching...
No Matches
Wikimedia\Parsoid\Core\TOCData Class Reference

Table of Contents data, including an array of section metadata. More...

+ Inheritance diagram for Wikimedia\Parsoid\Core\TOCData:
+ Collaboration diagram for Wikimedia\Parsoid\Core\TOCData:

Public Member Functions

 __construct (... $sections)
 Create a new TOCData object with the given sections and no extension data.
 
 getCurrentTOCLevel ()
 Return current TOC level while headings are being processed and section metadata is being constructed.
 
 addSection (SectionMetadata $s)
 Add a new section to this TOCData.
 
 getSections ()
 Get the list of sections in the TOCData.
 
 setExtensionData (string $key, $value)
 Attaches arbitrary data to this TOCData object.
 
 appendExtensionData (string $key, $value)
 Appends arbitrary data to this TOCData.
 
 getExtensionData ( $key)
 Gets extension data previously attached to this TOCData.
 
 toLegacy ()
 Return as associative array, in the legacy format returned by the action API.
 
 processHeading (int $oldLevel, int $level, SectionMetadata $metadata)
 
 jsonSerialize ()
 Serialize all data in the TOCData as JSON.
 
 toJsonArray ()
 
 prettyPrint ()
 For use in parser tests and wherever else humans might appreciate some formatting in the JSON encoded output.
 
 __clone ()
 

Static Public Member Functions

static fromLegacy (array $data)
 Create a new TOCData object from the legacy associative array format.
 
static newFromJsonArray (array $json)
 
static jsonClassHintFor (string $keyName)
 

Detailed Description

Table of Contents data, including an array of section metadata.

This is simply an array of SectionMetadata objects for now along with extension data, but may include additional ToC properties in the future.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Parsoid\Core\TOCData::__construct ( $sections)

Create a new TOCData object with the given sections and no extension data.

Parameters
SectionMetadata...$sections

Member Function Documentation

◆ addSection()

Wikimedia\Parsoid\Core\TOCData::addSection ( SectionMetadata $s)

Add a new section to this TOCData.

Parameters
SectionMetadata$s

◆ appendExtensionData()

Wikimedia\Parsoid\Core\TOCData::appendExtensionData ( string $key,
$value )

Appends arbitrary data to this TOCData.

This can be used to store some information about the table of contents in the ParserOutput object for later use during page output.

See ::setExtensionData() for more details on rationale and use.

Parameters
string$keyThe key for accessing the data. Extensions should take care to avoid conflicts in naming keys. It is suggested to use the extension's name as a prefix.
int | string$valueThe value to append to the list.
Returns
never This method is not yet implemented.

◆ fromLegacy()

static Wikimedia\Parsoid\Core\TOCData::fromLegacy ( array $data)
static

Create a new TOCData object from the legacy associative array format.

This is used for backward compatibility, but the associative array format does not include any properties of the TOCData other than the section list.

Parameters
array$dataAssociative array with ToC data in legacy format
Returns
TOCData

◆ getCurrentTOCLevel()

Wikimedia\Parsoid\Core\TOCData::getCurrentTOCLevel ( )

Return current TOC level while headings are being processed and section metadata is being constructed.

Returns
int

◆ getExtensionData()

Wikimedia\Parsoid\Core\TOCData::getExtensionData ( $key)

Gets extension data previously attached to this TOCData.

Parameters
string$keyThe key to look up
Returns
mixed|null The value(s) previously set for the given key using ::setExtensionData() or ::appendExtensionData(), or null if no value was set for this key.

◆ getSections()

Wikimedia\Parsoid\Core\TOCData::getSections ( )

Get the list of sections in the TOCData.

Returns
SectionMetadata[]

◆ jsonSerialize()

Wikimedia\Parsoid\Core\TOCData::jsonSerialize ( )

Serialize all data in the TOCData as JSON.

Unlike the :toLegacy() method, this method will include all of the properties in the TOCData so that the serialization is reversible.

◆ prettyPrint()

Wikimedia\Parsoid\Core\TOCData::prettyPrint ( )

For use in parser tests and wherever else humans might appreciate some formatting in the JSON encoded output.

Returns
string

◆ processHeading()

Wikimedia\Parsoid\Core\TOCData::processHeading ( int $oldLevel,
int $level,
SectionMetadata $metadata )
Parameters
int$oldLevellevel of the heading (H1/H2, etc.)
int$levellevel of the heading (H1/H2, etc.)
SectionMetadata$metadataThis metadata will be updated This logic is copied from Parser.php::finalizeHeadings

◆ setExtensionData()

Wikimedia\Parsoid\Core\TOCData::setExtensionData ( string $key,
$value )

Attaches arbitrary data to this TOCData object.

This can be used to store some information about the table of contents in the ParserOutput object for later use during page output. The data will be cached along with the ParserOutput object.

See ParserOutput::setExtensionData() in core for further information about typical usage in hooks, and SectionMetadata::setExtensionData() for a similar method appropriate for information about a specific section of the ToC.

Setting conflicting values for the same key is not allowed. If you call ::setExtensionData() multiple times with the same key on a TOCData, is is expected that the value will be identical each time. If you want to collect multiple pieces of data under a single key, use ::appendExtensionData().

Note
Only scalar values (numbers, strings, or arrays) are supported as a value. (A future revision will allow anything that core's JsonCodec can handle.) Attempts to set other types as extension data values will break ParserCache for the page.
Todo
When values more complex than scalar values get supported, __clone needs to be updated accordingly.
Parameters
string$keyThe key for accessing the data. Extensions should take care to avoid conflicts in naming keys. It is suggested to use the extension's name as a prefix. Using the prefix mw: is reserved for core.
mixed$valueThe value to set. Setting a value to null is equivalent to removing the value.

◆ toLegacy()

Wikimedia\Parsoid\Core\TOCData::toLegacy ( )

Return as associative array, in the legacy format returned by the action API.

This is helpful as b/c support while we transition to objects, but it drops some properties from this class and shouldn't be used in new code.

Returns
array

The documentation for this class was generated from the following file: