xref: /plugin/combo/syntax/contentlistitem.php (revision 85e82846b0a214bc35e62864fa49d9cad0723d0e)
1<?php
2
3
4use ComboStrap\PluginUtility;
5use ComboStrap\TagAttributes;
6
7
8/**
9 * Class syntax_plugin_combo_list
10 * Implementation of a list
11 *
12 * This component is not not public
13 *
14 */
15class syntax_plugin_combo_contentlistitem extends DokuWiki_Syntax_Plugin
16{
17
18    const DOKU_TAG = "contentlistitem";
19    const MARKI_TAG = "content-list-item";
20    const ALL_TAGS = array(self::MARKI_TAG, "list-item", "li");
21
22
23
24    /**
25     * Syntax Type.
26     *
27     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
28     * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types
29     * @see DokuWiki_Syntax_Plugin::getType()
30     */
31    function getType()
32    {
33        return 'container';
34    }
35
36    /**
37     * How Dokuwiki will add P element
38     *
39     *  * 'normal' - The plugin can be used inside paragraphs (inline or inside)
40     *  * 'block'  - Open paragraphs need to be closed before plugin output (box) - block should not be inside paragraphs
41     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
42     *
43     * @see DokuWiki_Syntax_Plugin::getPType()
44     * @see https://www.dokuwiki.org/devel:syntax_plugins#ptype
45     */
46    function getPType()
47    {
48        /**
49         * No paragraph inside, this is a layout
50         */
51        return 'block';
52    }
53
54    public function accepts($mode)
55    {
56        return syntax_plugin_combo_preformatted::disablePreformatted($mode);
57    }
58
59
60    /**
61     * @return array
62     * Allow which kind of plugin inside
63     *
64     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
65     * because we manage self the content and we call self the parser
66     *
67     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
68     */
69    function getAllowedTypes()
70    {
71        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
72    }
73
74    /**
75     * @see Doku_Parser_Mode::getSort()
76     * the mode with the lowest sort number will win out
77     * Higher than {@link syntax_plugin_combo_contentlist}
78     * but less than {@link syntax_plugin_combo_preformatted}
79     */
80    function getSort()
81    {
82        return 18;
83    }
84
85
86    function connectTo($mode)
87    {
88
89        /**
90         * This is now know as `row`
91         * This is the old tags
92         */
93        foreach (self::ALL_TAGS as $tag) {
94            $pattern = PluginUtility::getContainerTagPattern($tag);
95            $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
96        }
97
98
99    }
100
101    public function postConnect()
102    {
103        foreach (self::ALL_TAGS as $tag) {
104            $this->Lexer->addExitPattern('</' . $tag . '>', PluginUtility::getModeFromTag($this->getPluginComponent()));
105        }
106
107    }
108
109
110    /**
111     *
112     * The handle function goal is to parse the matched syntax through the pattern function
113     * and to return the result for use in the renderer
114     * This result is always cached until the page is modified.
115     * @param string $match
116     * @param int $state
117     * @param int $pos - byte position in the original source file
118     * @param Doku_Handler $handler
119     * @return array|bool
120     * @see DokuWiki_Syntax_Plugin::handle()
121     *
122     */
123    function handle($match, $state, $pos, Doku_Handler $handler)
124    {
125
126        switch ($state) {
127
128            case DOKU_LEXER_ENTER :
129
130                $attributes = TagAttributes::createFromTagMatch($match);
131                $tag = PluginUtility::getTag($match);
132                return array(
133                    PluginUtility::STATE => $state,
134                    PluginUtility::ATTRIBUTES => $attributes->toCallStackArray(),
135                    PluginUtility::PAYLOAD=>$tag
136                );
137
138            case DOKU_LEXER_UNMATCHED :
139                return PluginUtility::handleAndReturnUnmatchedData(self::DOKU_TAG, $match, $handler);
140
141            case DOKU_LEXER_EXIT :
142
143                return array(
144                    PluginUtility::STATE => $state
145                );
146
147
148        }
149        return array();
150
151    }
152
153    /**
154     * Render the output
155     * @param string $format
156     * @param Doku_Renderer $renderer
157     * @param array $data - what the function handle() return'ed
158     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
159     * @see DokuWiki_Syntax_Plugin::render()
160     *
161     *
162     */
163    function render($format, Doku_Renderer $renderer, $data)
164    {
165
166        /**
167         * The normal flow is that the `row` in a content
168         * list are transformed to `content-list-item` in the {@link DOKU_LEXER_EXIT} state
169         * of {@link syntax_plugin_combo_contentlist::handle()}
170         *
171         */
172        if ($format == 'xhtml') {
173
174            /** @var Doku_Renderer_xhtml $renderer */
175            $state = $data[PluginUtility::STATE];
176            switch ($state) {
177                case DOKU_LEXER_ENTER :
178                    $tagAttributes = TagAttributes::createFromCallStackArray($data[PluginUtility::ATTRIBUTES], self::MARKI_TAG);
179                    $tagAttributes->addClassName("list-group-item");
180                    $renderer->doc .= $tagAttributes->toHtmlEnterTag("li");
181                    break;
182                case DOKU_LEXER_EXIT :
183                    $renderer->doc .= "</li>" . DOKU_LF;
184                    break;
185                case DOKU_LEXER_UNMATCHED :
186                    $render = PluginUtility::renderUnmatched($data);
187                    if (!empty($render)) {
188                        $renderer->doc .= "<span>" . $render . '</span>';
189                    }
190                    break;
191            }
192            return true;
193        }
194
195        // unsupported $mode
196        return false;
197    }
198
199
200}
201
202