-
Notifications
You must be signed in to change notification settings - Fork 3
/
syntax.php
134 lines (118 loc) · 4.86 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Plugin Search Form: Inserts a search form in any page
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Adolfo González Blázquez <code@infinicode.org>
*/
use dokuwiki\Extension\SyntaxPlugin;
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_searchform extends SyntaxPlugin {
/**
* Syntax Type
*
* Needs to return one of the mode types defined in $PARSER_MODES in parser.php
* @return string
*/
public function getType() {
return 'substition';
}
/**
* Sort order when overlapping syntax
* @return int
*/
public function getSort() {
return 138;
}
/**
* @param $mode
*/
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('\{searchform\b.*?\}', $mode, 'plugin_searchform');
}
/**
* Handler to prepare matched data for the rendering process
*
* @param string $match The text matched by the patterns
* @param int $state The lexer state for the match
* @param int $pos The character position of the matched text
* @param Doku_Handler $handler Reference to the Doku_Handler object
* @return array Return an array with all data you want to use in render
*/
public function handle($match, $state, $pos, Doku_Handler $handler) {
$match = trim(substr($match,11,-1)); //strip {searchform from start and } from end
list($key, $value) = array_pad(explode('=', $match, 2), 2, null);
$options = ['namespace' => null, 'excludens' => false];
if(isset($key) && ($key == 'ns' || $key == '-ns')) {
$options['namespace'] = cleanID($value);
$options['excludens'] = $key === '-ns';
}
return array($options, $state, $pos);
}
/**
* The actual output creation.
*
* @param string $format output format being rendered
* @param Doku_Renderer $renderer reference to the current renderer object
* @param array $data data created by handler()
* @return boolean rendered correctly?
*/
public function render($format, Doku_Renderer $renderer, $data) {
global $lang, $INFO, $ACT, $QUERY, $ID;
if($format == 'xhtml') {
list($options,,) = $data;
// don't print the search form if search action has been disabled
if(!actionOK('search')) return true;
$ns = $options['namespace'];
$notns = '';
if($ns === null) {
$ns = $INFO['namespace'];
}
if($options['excludens']) {
$notns = $ns;
$ns = '';
}
/** based on tpl_searchform() */
$autocomplete=true;
$ajax=true;
$searchForm = new dokuwiki\Form\Form([
'action' => wl(),
'method' => 'get',
'role' => 'search',
'class' => 'search',
], true);
$searchForm->addTagOpen('div')->addClass('no');
$searchForm->setHiddenField('do', 'search');
$searchForm->setHiddenField('id', $ID);
$searchForm->setHiddenField('ns', $ns)->addClass('searchform__ns');
$searchForm->setHiddenField('-ns', $notns)->addClass('searchform__notns');
$searchForm->addTextInput('q')
->addClass('edit searchform__qsearch_in')
->attrs([
'title' => '[F]',
'accesskey' => 'f',
'placeholder' => $lang['btn_search'],
'autocomplete' => $autocomplete ? 'on' : 'off',
])
->val($ACT === 'search' ? $QUERY : '')
->useInput(false);
$searchForm->addButton('', $lang['btn_search'])->attrs([
'type' => 'submit',
'title' => $lang['btn_search'],
]);
if ($ajax) {
$searchForm->addTagOpen('div')->addClass('ajax_qsearch JSpopup searchform__qsearch_out');
$searchForm->addTagClose('div');
}
$searchForm->addTagClose('div');
$renderer->doc .= '<div class="searchform__form">';
$renderer->doc .= $searchForm->toHTML('quicksearch');
$renderer->doc .= '</div>';
return true;
}
return false;
}
}