1<?php
2/**
3 * Copyright (c) 2020. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16use dokuwiki\Extension\SyntaxPlugin;
17
18class RenderUtility
19{
20
21    /**
22     * @param $content
23     * @param bool $strip
24     * @return string|null
25     */
26    public static function renderText2XhtmlAndStripPEventually($content, $strip=true)
27    {
28        $instructions = self::getInstructionsAndStripPEventually($content,$strip);
29        return p_render('xhtml', $instructions, $info);
30    }
31
32    /**
33     * @param $pageContent - the text (not the id)
34     * @param bool $stripOpenAndEnd - to avoid the p element in test rendering
35     * @return array
36     */
37    public static function getInstructionsAndStripPEventually($pageContent, $stripOpenAndEnd = true)
38    {
39
40        $instructions = p_get_instructions($pageContent);
41
42        if ($stripOpenAndEnd) {
43
44            /**
45             * Delete the p added by {@link Block::process()}
46             * if the plugin of the {@link SyntaxPlugin::getPType() normal} and not in a block
47             *
48             * p_open = document_start in renderer
49             */
50            if ($instructions[1][0] == 'p_open') {
51                unset($instructions[1]);
52
53                /**
54                 * The last p position is not fix
55                 * We may have other calls due for instance
56                 * of {@link \action_plugin_combo_syntaxanalytics}
57                 */
58                $n = 1;
59                while (($lastPBlockPosition = (sizeof($instructions) - $n)) >= 0) {
60
61                    /**
62                     * p_open = document_end in renderer
63                     */
64                    if ($instructions[$lastPBlockPosition][0] == 'p_close') {
65                        unset($instructions[$lastPBlockPosition]);
66                        break;
67                    } else {
68                        $n = $n + 1;
69                    }
70                }
71            }
72
73        }
74
75        return $instructions;
76    }
77
78    /**
79     * @param $pageId
80     * @return string|null
81     */
82    public
83    static function renderId2Xhtml($pageId)
84    {
85        $file = wikiFN($pageId);
86        if (file_exists($file)) {
87            $content = file_get_contents($file);
88            return self::renderText2XhtmlAndStripPEventually($content);
89        } else {
90            return false;
91        }
92    }
93
94    public
95    static function renderId2Json($pageId)
96    {
97        return Analytics::processAndGetDataAsJson($pageId);
98    }
99}
100