1<?php
2/**
3 * FAQ plugin. FAQ styled headers.
4 *
5 * Syntax:
6 *    ?????? FAQ level 1 ??????
7 *    ????? FAQ level 2 ?????
8 *    ???? FAQ level 3 ????
9 *    ??? FAQ level 4 ???
10 *    ?? FAQ level 5 ??
11 *
12 * FAQs are just headers with a different class in them.
13 *
14 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
15 * @author     Dion Nicolaas <dion@nicolaas.net>
16 */
17
18// must be run within Dokuwiki
19if(!defined('DOKU_INC')) die();
20
21if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
22require_once(DOKU_PLUGIN.'syntax.php');
23
24class syntax_plugin_faq extends DokuWiki_Syntax_Plugin {
25
26    function getInfo() {
27        return array(
28                'author' => 'Dion Nicolaas',
29                'email'  => 'dion@nicolaas.net',
30                'date'   => '2008-10-01',
31                'name'   => 'FAQ plugin',
32                'desc'   => 'Easy markup for Frequently Asked Questions',
33                'url'    => 'http://www.dokuwiki.org/plugin:faq',
34                );
35    }
36
37    function getType() { return 'container'; }
38    function getPType() { return 'block'; }
39    function getSort() { return 49; }
40
41    function getAllowedTypes() {
42        return array('formatting', 'substition', 'disabled', 'protected');
43    }
44
45    function preConnect() {
46        $this->Lexer->addSpecialPattern(
47                '(?m)^[ \t]*\?+[^\n]+\?+[ \t]*$',
48                'base',
49                'plugin_faq'
50                );
51    }
52
53    function handle($match, $state, $pos, &$handler) {
54        global $conf;
55
56        // get level and title
57        $title = trim($match);
58        $level = 7 - strspn($title, '?');
59        if ($level < 1) $level = 1;
60        elseif ($level > 5) $level = 5;
61        $title = trim($title, '?');
62        $title = trim($title);
63
64        // Repeat the normal handling, except for the default rendering
65        if ($handler->status['section']) $handler->_addCall('section_close', array(), $pos);
66
67        //$handler->_addCall('header', array($title, $level, $pos), $pos);
68
69        $handler->_addCall('section_open', array($level), $pos);
70        $handler->status['section'] = true;
71        return array($level, $title);
72    }
73
74    function render($mode, &$renderer, $data) {
75        // Repeat the rendering of normal headers, but with a span class=faq
76        // in between
77        list($level, $text) = $data;
78        $hid = $renderer->_headerToLink($text,true);
79
80        //only add items within configured levels
81        $renderer->toc_additem($hid, $text, $level);
82
83        // write the header
84        $renderer->doc .= DOKU_LF.'<h'.$level.' class="faq"><a name="'.$hid.'" id="'.$hid.'"><span class="faq">';
85        $renderer->doc .= $text;
86        $renderer->doc .= "</span></a></h$level>".DOKU_LF;
87    }
88}
89// vim:ts=4:sw=4:et:enc=utf-8:
90