1<?php
2
3
4use ComboStrap\Html;
5use ComboStrap\PluginUtility;
6
7
8require_once(__DIR__ . '/../vendor/autoload.php');
9
10/**
11 * Class syntax_plugin_combo_metadata
12 * Add the metadata box
13 */
14class syntax_plugin_combo_metadata extends DokuWiki_Syntax_Plugin
15{
16    /**
17     * A regular expression to filter the output
18     */
19    public const EXCLUDE_ATTRIBUTE = "exclude";
20    /**
21     * The default attributes
22     */
23    public const CONF_METADATA_DEFAULT_ATTRIBUTES = "metadataViewerDefaultAttributes";
24    public const TITLE_ATTRIBUTE = "title";
25    /**
26     * The HTML tag
27     */
28    public const TAG = "metadata";
29    /**
30     * The HTML id of the box (for testing purpose)
31     */
32    public const META_MESSAGE_BOX_ID = "metadata-viewer";
33
34    /**
35     *
36     * @param syntax_plugin_combo_metadata $plugin - the calling dokuwiki plugin
37     * @param $inlineAttributes - the inline attribute of a component if any
38     * @return string - an HTML box of the array
39     */
40    public static function getHtmlMetadataBox($plugin, $inlineAttributes = array()): string
41    {
42
43        // Attributes processing
44        $defaultStringAttributes = $plugin->getConf(self::CONF_METADATA_DEFAULT_ATTRIBUTES);
45        $defaultAttributes = PluginUtility::parseAttributes($defaultStringAttributes);
46        $attributes = PluginUtility::mergeAttributes($inlineAttributes, $defaultAttributes);
47
48        // Building the box
49        $content = '<div id="' . self::META_MESSAGE_BOX_ID . '" class="alert alert-success " role="note">';
50        if (array_key_exists(self::TITLE_ATTRIBUTE, $attributes)) {
51            $content .= '<h2 class="alert-heading" ">' . $attributes[self::TITLE_ATTRIBUTE] . '</h2>';
52        }
53        global $ID;
54        $metadata = p_read_metadata($ID);
55        $metas = $metadata['persistent'];
56
57
58        if (array_key_exists(self::EXCLUDE_ATTRIBUTE, $attributes)) {
59            $filter = $attributes[self::EXCLUDE_ATTRIBUTE];
60            \ComboStrap\ArrayUtility::filterArrayByKey($metas, $filter);
61        }
62        if (!array_key_exists("canonical", $metas)) {
63            $metas["canonical"] = PluginUtility::getDocumentationHyperLink("canonical", "No Canonical");
64        }
65
66        $content .= \ComboStrap\ArrayUtility::formatAsHtmlList($metas);
67
68
69        $referenceStyle = array(
70            "font-size" => "95%",
71            "clear" => "both",
72            "bottom" => "10px",
73            "right" => "15px",
74            "position" => "absolute",
75            "font-style" => "italic"
76        );
77
78        $content .= '<div style="' . Html::array2InlineStyle($referenceStyle) . '">' . $plugin->getLang('message_come_from') . PluginUtility::getDocumentationHyperLink("metadata:viewer", "ComboStrap Metadata Viewer") . '</div>';
79        $content .= '</div>';
80        return $content;
81
82    }
83
84    /**
85     * Syntax Type.
86     *
87     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
88     * @see DokuWiki_Syntax_Plugin::getType()
89     */
90    function getType()
91    {
92        return 'substition';
93    }
94
95    /**
96     * How Dokuwiki will add P element
97     *
98     * * 'normal' - The plugin can be used inside paragraphs
99     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
100     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
101     *
102     * @see DokuWiki_Syntax_Plugin::getPType()
103     */
104    function getPType()
105    {
106        return 'block';
107    }
108
109    /**
110     * @return array
111     * Allow which kind of plugin inside
112     *
113     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
114     * because we manage self the content and we call self the parser
115     *
116     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
117     */
118    function getAllowedTypes()
119    {
120        return array();
121    }
122
123    function getSort()
124    {
125        return 201;
126    }
127
128
129    function connectTo($mode)
130    {
131
132
133        $pattern = PluginUtility::getEmptyTagPattern(self::TAG);
134        $this->Lexer->addSpecialPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
135
136
137    }
138
139    /**
140     *
141     * The handle function goal is to parse the matched syntax through the pattern function
142     * and to return the result for use in the renderer
143     * This result is always cached until the page is modified.
144     * @param string $match
145     * @param int $state
146     * @param int $pos
147     * @param Doku_Handler $handler
148     * @return array|bool
149     * @see DokuWiki_Syntax_Plugin::handle()
150     *
151     */
152    function handle($match, $state, $pos, Doku_Handler $handler)
153    {
154        /**
155         * There is only one state call ie DOKU_LEXER_SPECIAL
156         * because of the connect to
157         */
158
159        return PluginUtility::getTagAttributes($match);
160
161    }
162
163    /**
164     * Render the output
165     * @param string $format
166     * @param Doku_Renderer $renderer
167     * @param array $data - what the function handle() return'ed
168     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
169     * @see DokuWiki_Syntax_Plugin::render()
170     *
171     *
172     */
173    function render($format, Doku_Renderer $renderer, $data)
174    {
175        if ($format == 'xhtml') {
176
177            /** @var Doku_Renderer_xhtml $renderer */
178
179            $renderer->doc .= self::getHtmlMetadataBox($this, $data);
180            return true;
181
182        }
183
184        // unsupported $mode
185        return false;
186    }
187
188
189}
190
191