1<?php
2
3/*
4 * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
5 *
6 * (c) Sungbin Jeon <clockoon@gmail.com>
7 *
8 * Original code based on the followings:
9 * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
10 * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
11 *
12 * For the full copyright and license information, please view the LICENSE
13 * file that was distributed with this source code.
14 */
15
16declare(strict_types=1);
17
18namespace DokuWiki\Plugin\Commonmark\Extension;
19
20use League\CommonMark\Environment\EnvironmentBuilderInterface;
21use League\CommonMark\Event\DocumentParsedEvent;
22use League\CommonMark\Extension\ConfigurableExtensionInterface;
23use League\CommonMark\Extension\Footnote\Event\AnonymousFootnotesListener;
24use League\CommonMark\Extension\Footnote\Event\FixOrphanedFootnotesAndRefsListener;
25use League\CommonMark\Extension\Footnote\Event\GatherFootnotesListener;
26use League\CommonMark\Extension\Footnote\Event\NumberFootnotesListener;
27use League\CommonMark\Extension\Footnote\Node\Footnote;
28use League\CommonMark\Extension\Footnote\Node\FootnoteBackref;
29use League\CommonMark\Extension\Footnote\Node\FootnoteContainer;
30use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
31use League\CommonMark\Extension\Footnote\Parser\AnonymousFootnoteRefParser;
32use League\CommonMark\Extension\Footnote\Parser\FootnoteStartParser;
33use League\CommonMark\Extension\Footnote\Parser\FootnoteRefParser;
34use Dokuwiki\Plugin\Commonmark\Extension\Renderer\Inline\FootnoteBackrefRenderer;
35use Dokuwiki\Plugin\Commonmark\Extension\Renderer\Block\FootnoteContainerRenderer;
36use Dokuwiki\Plugin\Commonmark\Extension\Renderer\Inline\FootnoteRefRenderer;
37use Dokuwiki\Plugin\Commonmark\Extension\Renderer\Block\FootnoteRenderer;
38use League\Config\ConfigurationBuilderInterface;
39use Nette\Schema\Expect;
40
41final class FootnotetoDokuwikiExtension implements ConfigurableExtensionInterface
42{
43    public function configureSchema(ConfigurationBuilderInterface $builder): void
44    {
45        $builder->addSchema('footnote', Expect::structure([
46            'backref_class' => Expect::string('footnote-backref'),
47            'backref_symbol' => Expect::string('↩'),
48            'container_add_hr' => Expect::bool(true),
49            'container_class' => Expect::string('footnotes'),
50            'ref_class' => Expect::string('footnote-ref'),
51            'ref_id_prefix' => Expect::string('fnref:'),
52            'footnote_class' => Expect::string('footnote'),
53            'footnote_id_prefix' => Expect::string('fn:'),
54        ]));
55    }
56
57    public function register(EnvironmentBuilderInterface $environment): void
58    {
59        $environment->addBlockStartParser(new FootnoteStartParser(), 51);
60        $environment->addInlineParser(new AnonymousFootnoteRefParser(), 35);
61        $environment->addInlineParser(new FootnoteRefParser(), 51);
62
63        $environment->addRenderer(FootnoteContainer::class, new FootnoteContainerRenderer());
64        $environment->addRenderer(Footnote::class, new FootnoteRenderer());
65        $environment->addRenderer(FootnoteBackref::class, new FootnoteBackrefRenderer());
66        $environment->addRenderer(FootnoteRef::class, new FootnoteRefRenderer());
67
68        $environment->addEventListener(DocumentParsedEvent::class, [new AnonymousFootnotesListener(), 'onDocumentParsed']);
69        $environment->addEventListener(DocumentParsedEvent::class, [new FixOrphanedFootnotesAndRefsListener(), 'onDocumentParsed'], 30);
70        $environment->addEventListener(DocumentParsedEvent::class, [new NumberFootnotesListener(), 'onDocumentParsed']);
71        $environment->addEventListener(DocumentParsedEvent::class, [new GatherFootnotesListener(), 'onDocumentParsed']);
72    }
73}
74