1<?php
2
3use ComboStrap\Tag\AdTag;
4use ComboStrap\FsWikiUtility;
5use ComboStrap\HeadingTag;
6use ComboStrap\Xml\XhtmlUtility;
7use ComboStrap\PluginUtility;
8use ComboStrap\Tag\TableTag;
9use ComboStrap\Toc;
10
11
12require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
13
14
15/**
16 * Class renderer_plugin_combo_renderer
17 * The last two parts ie `combo_renderer` is the id for dokuwiki
18 * The last part should also be equal to the name
19 */
20class  renderer_plugin_combo_renderer extends Doku_Renderer_xhtml
21{
22    const COMBO_RENDERER_NAME = 'combo_renderer';
23
24    /**
25     * @var array that hold the position of the parent
26     */
27    protected $nodeParentPosition = [];
28
29    /**
30     * @var array that hold the current position of an header for a level
31     * $headerNum[level]=position
32     */
33    protected $header = [];
34
35    /**
36     * @var array that will contains the whole doc but by section
37     */
38    protected $sections = [];
39
40    /**
41     * @var int the section number
42     */
43    protected $sectionNumber = 0;
44
45    /**
46     * @var string variable that permits to carry the header text of a previous section
47     */
48    protected $previousSectionTextHeader = '';
49
50
51    /**
52     * @var int variable that permits to carry the position of a previous section
53     */
54    protected $previousNodePosition = 0;
55
56    /**
57     * @var int variable that permits to carry the position of a previous section
58     */
59    protected $previousNodeLevel = 0;
60
61    /**
62     * @var int variable that permits to carry the number of words
63     */
64    protected $lineCounter = 0;
65
66
67    function getFormat()
68    {
69        return 'xhtml';
70    }
71
72    /*
73     * Function that enable to list the plugin in the options for config:renderer_xhtml
74     * http://www.dokuwiki.org/config:renderer_xhtml
75     * setting in its Configuration Manager.
76     */
77    public function canRender($format)
78    {
79        return ($format == 'xhtml');
80    }
81
82
83    /**
84     * Render a heading
85     *
86     * The rendering of the heading is done through the parent
87     * The function just:
88     *   - save the rendering between each header in the class variable $this->sections
89     * This variblae is used in the function document_end to recreate the whole doc.
90     *   - add the numbering to the header text
91     *
92     * @param string $text the text to display
93     * @param int $level header level
94     * @param int $pos byte position in the original source
95     */
96    function header($text, $level, $pos, $returnonly = false)
97    {
98
99
100        // We are going from 2 to 3
101        // The parent is 2
102        if ($level > $this->previousNodeLevel) {
103            $nodePosition = 1;
104            // Keep the position of the parent
105            $this->nodeParentPosition[$this->previousNodeLevel] = $this->previousNodePosition;
106        } elseif
107            // We are going from 3 to 2
108            // The parent is 1
109        ($level < $this->previousNodeLevel
110        ) {
111            $nodePosition = $this->nodeParentPosition[$level] + 1;
112        } else {
113            $nodePosition = $this->previousNodePosition + 1;
114        }
115
116        // Grab the doc from the previous section
117        $this->sections[$this->sectionNumber] = array(
118            'level' => $this->previousNodeLevel,
119            'position' => $this->previousNodePosition,
120            'content' => $this->doc,
121            'text' => $this->previousSectionTextHeader);
122
123        // And reset it
124        $this->doc = '';
125        // Set the looping variable
126        $this->sectionNumber = $this->sectionNumber + 1;
127        $this->previousNodeLevel = $level;
128        $this->previousNodePosition = $nodePosition;
129        $this->previousSectionTextHeader = $text;
130
131
132        /**
133         * Rendering is done by the parent
134         * And should be the last one
135         * Because we delete the heading
136         * with {@link HeadingTag::reduceToFirstOpeningTagAndReturnAttributes()}
137         * in order to be able to add the toc and section
138         *
139         */
140        parent::header($text, $level, $pos);
141
142
143    }
144
145
146    function document_end()
147    {
148
149        global $ID;
150        // The id of the page (not of the sidebar)
151        $id = $ID;
152        $isSidebar = FsWikiUtility::isSideBar();
153
154
155        // Pump the last doc
156        $this->sections[$this->sectionNumber] = array('level' => $this->previousNodeLevel, 'position' => $this->previousNodePosition, 'content' => $this->doc, 'text' => $this->previousSectionTextHeader);
157
158        // Recreate the doc
159        $this->doc = '';
160        $rollingLineCount = 0;
161        $currentLineCountSinceLastAd = 0;
162        foreach ($this->sections as $sectionNumber => $section) {
163
164            $sectionContent = $section['content'];
165
166            # Split by element line
167            # element p, h, br, tr, li, pre (one line for pre)
168            $sectionLineCount = XhtmlUtility::countLines($sectionContent);
169            $currentLineCountSinceLastAd += $sectionLineCount;
170            $rollingLineCount += $sectionLineCount;
171
172            // The content
173            if ($this->getConf('ShowCount') == 1) {
174                $this->doc .= "<p>Section " . $sectionNumber . ": (" . $sectionLineCount . "|" . $currentLineCountSinceLastAd . "|" . $rollingLineCount . ")</p>";
175            }
176            $this->doc .= $sectionContent;
177
178
179        }
180
181        parent::document_end();
182
183    }
184
185
186}
187