1<?php
2
3use ComboStrap\PluginUtility;
4
5if (!defined('DOKU_INC')) die();
6
7/**
8 * Add the heading numbering snippet
9 *
10 * Page on DokuWiki
11 * https://www.dokuwiki.org/tips:numbered_headings
12 */
13class action_plugin_combo_outlinenumbering extends DokuWiki_Action_Plugin
14{
15
16    const SNIPPET_ID = "outline-numbering";
17
18    const CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL2 = "outlineNumberingCounterStyleLevel2";
19    const CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL3 = "outlineNumberingCounterStyleLevel3";
20    const CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL4 = "outlineNumberingCounterStyleLevel4";
21    const CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL5 = "outlineNumberingCounterStyleLevel5";
22    const CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL6 = "outlineNumberingCounterStyleLevel6";
23    const CONF_OUTLINE_NUMBERING_COUNTER_SEPARATOR = "outlineNumberingCounterSeparator";
24    const CONF_OUTLINE_NUMBERING_PREFIX = "outlineNumberingPrefix";
25    const CONF_OUTLINE_NUMBERING_SUFFIX = "outlineNumberingSuffix";
26    const CANONICAL = "outline";
27    const CONF_OUTLINE_NUMBERING_ENABLE = "outlineNumberingEnable";
28
29
30    public function register(Doku_Event_Handler $controller)
31    {
32        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, '_outline_numbering', array());
33    }
34
35    /**
36     * As seen on
37     * https://drafts.csswg.org/css-counter-styles-3/#predefined-counters
38     */
39    const CONF_COUNTER_STYLES_CHOICES = [
40        'arabic-indic',
41        'bengali',
42        'cambodian/khmer',
43        'cjk-decimal',
44        'decimal',
45        'decimal-leading-zero',
46        'devanagari',
47        'georgian',
48        'gujarati',
49        'gurmukhi',
50        'hebrew',
51        'hiragana',
52        'hiragana-iroha',
53        'kannada',
54        'katakana',
55        'katakana-iroha',
56        'lao',
57        'lower-alpha',
58        'lower-armenian',
59        'lower-greek',
60        'lower-roman',
61        'malayalam',
62        'mongolian',
63        'myanmar',
64        'oriya',
65        'persian',
66        'tamil',
67        'telugu',
68        'thai',
69        'tibetan',
70        'upper-alpha',
71        'upper-armenian',
72        'upper-roman'
73    ];
74
75    /**
76     *
77     * @param $event
78     */
79    function _outline_numbering($event)
80    {
81
82        $enable = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_ENABLE, 0);
83        if ($enable) {
84
85            global $ACT;
86            if ($ACT == "preview") {
87                $mainContainerSelector = ".pad";
88            } else {
89                $mainContainerSelector = "main";
90            }
91            $level2CounterStyle = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL2, "decimal");
92            $level3CounterStyle = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL3, "decimal");
93            $level4CounterStyle = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL4, "decimal");
94            $level5CounterStyle = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL5, "decimal");
95            $level6CounterStyle = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_COUNTER_STYLE_LEVEL6, "decimal");
96            $counterSeparator = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_COUNTER_SEPARATOR, ".");
97            $prefix = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_PREFIX, "");
98            $suffix = PluginUtility::getConfValue(self::CONF_OUTLINE_NUMBERING_SUFFIX, " - ");
99
100            /**
101             * The level counter on the toc are based
102             * on the https://www.dokuwiki.org/config:toptoclevel
103             * configuration
104             * if toptoclevel = 2, then level1 = h2 and not h1
105             */
106            global $conf;
107            $topTocLevel = $conf['toptoclevel'];
108            $tocLevel2 = 2;
109            $tocLevel3 = 3;
110            $tocLevel4 = 4;
111            $tocLevel5 = 5;
112            $tocLevel6 = 6;
113            if ($topTocLevel == "2") {
114                $tocLevel2 -= 1;
115                $tocLevel3 -= 1;
116                $tocLevel4 -= 1;
117                $tocLevel5 -= 1;
118                $tocLevel6 -= 1;
119            }
120
121            $numberingCss = <<<EOF
122$mainContainerSelector > h2 { counter-increment: h2; }
123$mainContainerSelector > h3 { counter-increment: h3; }
124$mainContainerSelector > h4 { counter-increment: h4; }
125$mainContainerSelector > h5 { counter-increment: h5; }
126$mainContainerSelector > h6 { counter-increment: h6; }
127$mainContainerSelector > h2::before { content: "$prefix" counter(h2, $level2CounterStyle) "$suffix\A"; }
128$mainContainerSelector > h3::before { content: "$prefix" counter(h2, $level2CounterStyle) "$counterSeparator" counter(h3,$level3CounterStyle) "$suffix\A"; }
129$mainContainerSelector > h4::before { content: "$prefix" counter(h2, $level2CounterStyle) "$counterSeparator" counter(h3,$level3CounterStyle) "$counterSeparator" counter(h4,$level4CounterStyle) "$suffix\A"; }
130$mainContainerSelector > h5::before { content: "$prefix" counter(h2, $level2CounterStyle) "$counterSeparator" counter(h3,$level3CounterStyle) "$counterSeparator" counter(h4,$level4CounterStyle) "$counterSeparator" counter(h5,$level5CounterStyle) "$suffix\A"; }
131$mainContainerSelector > h6::before { content: "$prefix" counter(h2, $level2CounterStyle) "$counterSeparator" counter(h3,$level3CounterStyle) "$counterSeparator" counter(h4,$level4CounterStyle) "$counterSeparator" counter(h5,$level5CounterStyle) "$counterSeparator" counter(h6,$level6CounterStyle) "$suffix\A"; }
132#dw__toc .level{$tocLevel2} { counter-increment: toc2; }
133#dw__toc .level{$tocLevel3} { counter-increment: toc3; }
134#dw__toc .level{$tocLevel4} { counter-increment: toc4; }
135#dw__toc .level{$tocLevel5} { counter-increment: toc5; }
136#dw__toc .level{$tocLevel6} { counter-increment: toc6; }
137#dw__toc .level{$tocLevel2} a::before { content: "$prefix" counter(toc2, $level2CounterStyle) "$suffix\A"; }
138#dw__toc .level{$tocLevel3} a::before { content: "$prefix" counter(toc2, $level2CounterStyle) "$counterSeparator" counter(toc3,$level3CounterStyle) "$suffix\A"; }
139#dw__toc .level{$tocLevel4} a::before { content: "$prefix" counter(toc2, $level2CounterStyle) "$counterSeparator" counter(toc3,$level3CounterStyle) "$counterSeparator" counter(toc4,$level4CounterStyle) "$suffix\A"; }
140#dw__toc .level{$tocLevel5} a::before { content: "$prefix" counter(toc2, $level2CounterStyle) "$counterSeparator" counter(toc3,$level3CounterStyle) "$counterSeparator" counter(toc4,$level4CounterStyle) "$counterSeparator" counter(toc5,$level5CounterStyle) "$suffix\A"; }
141#dw__toc .level{$tocLevel6} a::before { content: "$prefix" counter(toc2, $level2CounterStyle) "$counterSeparator" counter(toc3,$level3CounterStyle) "$counterSeparator" counter(toc4,$level4CounterStyle) "$counterSeparator" counter(toc5,$level5CounterStyle) "$counterSeparator" counter(toc6,$level6CounterStyle) "$suffix\A"; }
142EOF;
143
144
145            PluginUtility::getSnippetManager()->upsertCssSnippetForRequest(self::SNIPPET_ID, $numberingCss);
146        }
147    }
148
149
150}
151