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