Passed
Pull Request — 2.7 (#1065)
by
unknown
22:10 queued 19:35
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 0
Metric Value
cc 7
eloc 21
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 45
ccs 21
cts 22
cp 0.9545
crap 7.0046
rs 8.6506
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\NodeIterator;
29
use League\CommonMark\Node\RawMarkupContainerInterface;
30
use League\CommonMark\Node\StringContainerHelper;
31
use League\Config\Exception\InvalidConfigurationException;
32
33
final class TableOfContentsGenerator implements TableOfContentsGeneratorInterface
34
{
35
    public const STYLE_BULLET  = ListBlock::TYPE_BULLET;
36
    public const STYLE_ORDERED = ListBlock::TYPE_ORDERED;
37
38
    public const NORMALIZE_DISABLED = 'as-is';
39
    public const NORMALIZE_RELATIVE = 'relative';
40
    public const NORMALIZE_FLAT     = 'flat';
41
42
    /** @psalm-readonly */
43
    private string $style;
44
45
    /** @psalm-readonly */
46
    private string $normalizationStrategy;
47
48
    /** @psalm-readonly */
49
    private int $minHeadingLevel;
50
51
    /** @psalm-readonly */
52
    private int $maxHeadingLevel;
53
54
    /** @psalm-readonly */
55
    private string $fragmentPrefix;
56
57 66
    public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel, string $fragmentPrefix)
58
    {
59 66
        $this->style                 = $style;
60 66
        $this->normalizationStrategy = $normalizationStrategy;
61 66
        $this->minHeadingLevel       = $minHeadingLevel;
62 66
        $this->maxHeadingLevel       = $maxHeadingLevel;
63 66
        $this->fragmentPrefix        = $fragmentPrefix;
64
65 66
        if ($fragmentPrefix !== '') {
66 66
            $this->fragmentPrefix .= '-';
67
        }
68
    }
69
70 66
    public function generate(Document $document): ?TableOfContents
71
    {
72 66
        $toc = $this->createToc($document);
73
74 66
        $normalizer = $this->getNormalizer($toc);
75
76 66
        $firstHeading = null;
77
78 66
        foreach ($this->getHeadingLinks($document) as $headingLink) {
79 58
            $heading = $headingLink->parent();
80
            // Make sure this is actually tied to a heading
81 58
            if (! $heading instanceof Heading) {
82
                continue;
83
            }
84
85
            // Skip any headings outside the configured min/max levels
86 58
            if ($heading->getLevel() < $this->minHeadingLevel || $heading->getLevel() > $this->maxHeadingLevel) {
87 4
                continue;
88
            }
89
90
            // Keep track of the first heading we see - we might need this later
91 58
            $firstHeading ??= $heading;
92
93
            // Keep track of the start and end lines
94 58
            $toc->setStartLine($firstHeading->getStartLine());
95 58
            $toc->setEndLine($heading->getEndLine());
96
97
            // Create the new link
98 58
            $link = new Link('#' . $this->fragmentPrefix . $headingLink->getSlug(), StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]));
99
100 58
            $listItem = new ListItem($toc->getListData());
101 58
            $listItem->setStartLine($heading->getStartLine());
102 58
            $listItem->setEndLine($heading->getEndLine());
103 58
            $listItem->appendChild($link);
104
105
            // Add it to the correct place
106 58
            $normalizer->addItem($heading->getLevel(), $listItem);
107
        }
108
109
        // Don't add the TOC if no headings were present
110 66
        if (! $toc->hasChildren() || $firstHeading === null) {
111 8
            return null;
112
        }
113
114 58
        return $toc;
115
    }
116
117 66
    private function createToc(Document $document): TableOfContents
118
    {
119 66
        $listData = new ListData();
120
121 66
        if ($this->style === self::STYLE_BULLET) {
122 62
            $listData->type = ListBlock::TYPE_BULLET;
123 4
        } elseif ($this->style === self::STYLE_ORDERED) {
124 4
            $listData->type = ListBlock::TYPE_ORDERED;
125
        } else {
126
            throw new InvalidConfigurationException(\sprintf('Invalid table of contents list style: "%s"', $this->style));
127
        }
128
129 66
        $toc = new TableOfContents($listData);
130
131 66
        $toc->setStartLine($document->getStartLine());
132 66
        $toc->setEndLine($document->getEndLine());
133
134 66
        return $toc;
135
    }
136
137
    /**
138
     * @return iterable<HeadingPermalink>
139
     */
140 66
    private function getHeadingLinks(Document $document): iterable
141
    {
142 66
        foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
143 66
            if (! $node instanceof Heading) {
144 66
                continue;
145
            }
146
147 58
            foreach ($node->children() as $child) {
148 58
                if ($child instanceof HeadingPermalink) {
149 58
                    yield $child;
150
                }
151
            }
152
        }
153
    }
154
155 66
    private function getNormalizer(TableOfContents $toc): NormalizerStrategyInterface
156
    {
157 66
        switch ($this->normalizationStrategy) {
158 33
            case self::NORMALIZE_DISABLED:
159 4
                return new AsIsNormalizerStrategy($toc);
160 31
            case self::NORMALIZE_RELATIVE:
161 58
                return new RelativeNormalizerStrategy($toc);
162 2
            case self::NORMALIZE_FLAT:
163 4
                return new FlatNormalizerStrategy($toc);
164
            default:
165
                throw new InvalidConfigurationException(\sprintf('Invalid table of contents normalization strategy: "%s"', $this->normalizationStrategy));
166
        }
167
    }
168
}
169