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