Passed
Push — 2.0 ( b7ed7b...934f6b )
by Colin
35:11 queued 31:18
created

TableOfContentsGenerator::generate()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.0046

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 45
ccs 21
cts 22
cp 0.9545
rs 8.6506
cc 7
nc 8
nop 1
crap 7.0046
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\TableOfContents;
15
16
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
17
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
18
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
19
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
20
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
21
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
22
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
23
use League\CommonMark\Extension\TableOfContents\Normalizer\AsIsNormalizerStrategy;
24
use League\CommonMark\Extension\TableOfContents\Normalizer\FlatNormalizerStrategy;
25
use League\CommonMark\Extension\TableOfContents\Normalizer\NormalizerStrategyInterface;
26
use League\CommonMark\Extension\TableOfContents\Normalizer\RelativeNormalizerStrategy;
27
use League\CommonMark\Node\Block\Document;
28
use League\CommonMark\Node\RawMarkupContainerInterface;
29
use League\CommonMark\Node\StringContainerHelper;
30
use League\Config\Exception\InvalidConfigurationException;
31
32
final class TableOfContentsGenerator implements TableOfContentsGeneratorInterface
33
{
34
    public const STYLE_BULLET  = ListBlock::TYPE_BULLET;
35
    public const STYLE_ORDERED = ListBlock::TYPE_ORDERED;
36
37
    public const NORMALIZE_DISABLED = 'as-is';
38
    public const NORMALIZE_RELATIVE = 'relative';
39
    public const NORMALIZE_FLAT     = 'flat';
40
41
    /** @psalm-readonly */
42
    private string $style;
43
44
    /** @psalm-readonly */
45
    private string $normalizationStrategy;
46
47
    /** @psalm-readonly */
48
    private int $minHeadingLevel;
49
50
    /** @psalm-readonly */
51
    private int $maxHeadingLevel;
52
53 87
    public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel)
54
    {
55 87
        $this->style                 = $style;
56 87
        $this->normalizationStrategy = $normalizationStrategy;
57 87
        $this->minHeadingLevel       = $minHeadingLevel;
58 87
        $this->maxHeadingLevel       = $maxHeadingLevel;
59 87
    }
60
61 87
    public function generate(Document $document): ?TableOfContents
62
    {
63 87
        $toc = $this->createToc($document);
64
65 87
        $normalizer = $this->getNormalizer($toc);
66
67 87
        $firstHeading = null;
68
69 87
        foreach ($this->getHeadingLinks($document) as $headingLink) {
70 81
            $heading = $headingLink->parent();
71
            // Make sure this is actually tied to a heading
72 81
            if (! $heading instanceof Heading) {
73
                continue;
74
            }
75
76
            // Skip any headings outside the configured min/max levels
77 81
            if ($heading->getLevel() < $this->minHeadingLevel || $heading->getLevel() > $this->maxHeadingLevel) {
78 6
                continue;
79
            }
80
81
            // Keep track of the first heading we see - we might need this later
82 81
            $firstHeading ??= $heading;
83
84
            // Keep track of the start and end lines
85 81
            $toc->setStartLine($firstHeading->getStartLine());
86 81
            $toc->setEndLine($heading->getEndLine());
87
88
            // Create the new link
89 81
            $link = new Link('#' . $headingLink->getSlug(), StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]));
90
91 81
            $listItem = new ListItem($toc->getListData());
92 81
            $listItem->setStartLine($heading->getStartLine());
93 81
            $listItem->setEndLine($heading->getEndLine());
94 81
            $listItem->appendChild($link);
95
96
            // Add it to the correct place
97 81
            $normalizer->addItem($heading->getLevel(), $listItem);
98
        }
99
100
        // Don't add the TOC if no headings were present
101 87
        if (! $toc->hasChildren() || $firstHeading === null) {
102 12
            return null;
103
        }
104
105 81
        return $toc;
106
    }
107
108 87
    private function createToc(Document $document): TableOfContents
109
    {
110 87
        $listData = new ListData();
111
112 87
        if ($this->style === self::STYLE_BULLET) {
113 81
            $listData->type = ListBlock::TYPE_BULLET;
114 6
        } elseif ($this->style === self::STYLE_ORDERED) {
115 6
            $listData->type = ListBlock::TYPE_ORDERED;
116
        } else {
117
            throw new InvalidConfigurationException(\sprintf('Invalid table of contents list style: "%s"', $this->style));
118
        }
119
120 87
        $toc = new TableOfContents($listData);
121
122 87
        $toc->setStartLine($document->getStartLine());
123 87
        $toc->setEndLine($document->getEndLine());
124
125 87
        return $toc;
126
    }
127
128
    /**
129
     * @return iterable<HeadingPermalink>
130
     */
131 87
    private function getHeadingLinks(Document $document): iterable
132
    {
133 87
        $walker = $document->walker();
134 87
        while ($event = $walker->next()) {
135 87
            if ($event->isEntering() && ($node = $event->getNode()) instanceof HeadingPermalink) {
136 81
                yield $node;
137
            }
138
        }
139 87
    }
140
141 87
    private function getNormalizer(TableOfContents $toc): NormalizerStrategyInterface
142
    {
143 87
        switch ($this->normalizationStrategy) {
144 87
            case self::NORMALIZE_DISABLED:
145 6
                return new AsIsNormalizerStrategy($toc);
146 81
            case self::NORMALIZE_RELATIVE:
147 75
                return new RelativeNormalizerStrategy($toc);
148 6
            case self::NORMALIZE_FLAT:
149 6
                return new FlatNormalizerStrategy($toc);
150
            default:
151
                throw new InvalidConfigurationException(\sprintf('Invalid table of contents normalization strategy: "%s"', $this->normalizationStrategy));
152
        }
153
    }
154
}
155