silverstripe-autotoc 

silverstripe-autotoc Commit Details

Date:2017-12-30 10:54:52 (3 years 9 months ago)
Author:Nicola Fontana
Branch:dev, master
Commit:eac462e522b9267708643229df754bc97aad850e
Parents: 9a48657e78da4b4661b714aac5a690c90b193cd3
Message:Using new PHP array notation [] instead of array()

Changes:
Msrc/Tocifier.php (7 diffs)
Mtests/TocifierTest.php (3 diffs)

File differences

src/Tocifier.php
2525
2626
2727
28
28
2929
3030
3131
......
7777
7878
7979
80
80
8181
8282
83
83
8484
8585
8686
......
118118
119119
120120
121
121
122122
123123
124124
......
126126
127127
128128
129
129
130130
131131
132132
......
165165
166166
167167
168
168
169169
170170
171171
......
228228
229229
230230
231
232
233
234
235
236
231
232
233
234
235
236
237
237238
238
239
239
240
240241
241
242
243
244
245
246
247
248
242
243
244
245
246
247
248
249
250
251
252
253
254
255
249256
250
251
257
258
252259
253
254
260
261
262
263
255264
256265
257266
......
260269
261270
262271
263
272
264273
265274
266275
private $tree;
// Array of references to the potential parents
private $dangling = array();
private $dangling = [];
// Callback for augmenting a single DOMElement
private $augment_callback;
*/
private function &newNode($id, $text, $level)
{
$node = array(
$node = [
'id' => $id,
'title' => $text
);
];
// Clear the trailing dangling parents after level, if any
end($this->dangling);
$parent = & $this->getParent($level);
$node = & $this->newNode($id, $text, $level);
if (!isset($parent['children'])) {
$parent['children'] = array();
$parent['children'] = [];
}
$parent['children'][] = & $node;
}
$body = $doc->getElementsByTagName('body')->item(0);
$this->html = str_replace(array("<body>\n", '<body>', '</body>'), '', $doc->saveHTML($body));
$this->html = str_replace(["<body>\n", '<body>', '</body>'], '', $doc->saveHTML($body));
}
/**
public function __construct($html)
{
$this->raw_html = $html;
$this->setAugmentCallback(array(static::class, 'setId'));
$this->setAugmentCallback([static::class, 'setId']);
}
/**
*
* The TOC is represented in the form of:
*
* <code>
* array(
* array('id' => 'TOC-1',
* 'title' => 'Item 1',
* 'children' => array(
* array('id' => 'TOC-2',
* [
* [
* 'id' => 'TOC-1',
* 'title' => 'Item 1',
* 'children' => [
* [
* 'id' => 'TOC-2',
* 'title' => 'Subitem 1.1'
* ),
* array('id' => 'TOC-3',
* ], [
* 'id' => 'TOC-3',
* 'title' => 'Subitem 1.2',
* 'children' => array(
* array('id' => 'TOC-4',
* 'title => 'Subsubitem 1.2.1'
* ))))),
* array('id' => 'TOC-5,
* 'title' => 'Item 2',
* 'children' => array(
* array('id' => 'TOC-6',
* 'children' => [
* [
* 'id' => 'TOC-4',
* 'title => 'Subsubitem 1.2.1'
* ]
* ]
* ]
* ],
* ], [
* 'id' => 'TOC-5,
* 'title' => 'Item 2',
* 'children' => [
* [
* 'id' => 'TOC-6',
* 'title' => 'Subitem 2.1'
* ),
* array('id' => 'TOC-7',
* ], [
* 'id' => 'TOC-7',
* 'title' => 'Subitem 2.2'
* ))));
* </code>
* ]
* ]
* ]
* ]
*
* The TOC is cached, so subsequent calls will return the same tree.
*
*/
public function getTOC()
{
return isset($this->tree['children']) ? $this->tree['children'] : array();
return isset($this->tree['children']) ? $this->tree['children'] : [];
}
/**
tests/TocifierTest.php
1818
1919
2020
21
21
2222
2323
2424
......
3030
3131
3232
33
33
3434
3535
3636
......
4848
4949
5050
51
51
5252
5353
5454
$tocifier = new Tocifier(null);
$this->assertFalse($tocifier->process());
$tocifier = new Tocifier(array('1234'));
$tocifier = new Tocifier(['1234']);
$this->assertFalse($tocifier->process());
$tocifier = new Tocifier('1234');
$tocifier = new Tocifier(file_get_contents(__DIR__.'/test1'));
$this->assertEquals('', $tocifier->getHtml());
$tocifier->setAugmentCallback(array('\eNTiDi\Autotoc\Tocifier', 'prependAnchor'));
$tocifier->setAugmentCallback(['\eNTiDi\Autotoc\Tocifier', 'prependAnchor']);
$this->assertTrue($tocifier->process());
$this->assertStringEqualsFile(__DIR__.'/html1', $tocifier->getHtml());
}
public function testTOC()
{
$tocifier = new Tocifier(file_get_contents(__DIR__.'/test1'));
$this->assertEquals(array(), $tocifier->getTOC());
$this->assertEquals([], $tocifier->getTOC());
$this->assertTrue($tocifier->process());
$this->assertNotNull($tocifier->getTOC());

Archive Download the corresponding diff file