Skip to content

Commit

Permalink
Merge pull request #2 from micgro42/syntaxEnhance
Browse files Browse the repository at this point in the history
Add syntax option for listtype, score and hide non-existing pages
  • Loading branch information
splitbrain committed Feb 19, 2015
2 parents acb256d + c6cc068 commit 59b8244
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
54 changes: 54 additions & 0 deletions _test/date.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function test_vanilla_syntax_parsing() {
1 => array(
'lang' => '',
'startdate' => '',
'tag' => 'ul',
'score' => 'false',
)
),
2 => DOKU_LEXER_SPECIAL,
Expand All @@ -34,6 +36,52 @@ function test_vanilla_syntax_parsing() {
$this->assertEquals($expected_response, $parser_response);
}

function test_ol_syntax_parsing() {
$parser_response = p_get_instructions('{{rating|tag=ol}}')[2];
$expected_response = array(
0 => 'plugin',
1 => array(
0 => 'rating',
1 => array(
0 => DOKU_LEXER_SPECIAL,
1 => array(
'lang' => '',
'startdate' => '',
'tag' => 'ol',
'score' => 'false',
)
),
2 => DOKU_LEXER_SPECIAL,
3 => '{{rating|tag=ol}}',
),
2 => 1,
);
$this->assertEquals($expected_response, $parser_response);
}

function test_score_syntax_parsing() {
$parser_response = p_get_instructions('{{rating|score=true}}')[2];
$expected_response = array(
0 => 'plugin',
1 => array(
0 => 'rating',
1 => array(
0 => DOKU_LEXER_SPECIAL,
1 => array(
'lang' => '',
'startdate' => '',
'tag' => 'ul',
'score' => 'true',
)
),
2 => DOKU_LEXER_SPECIAL,
3 => '{{rating|score=true}}',
),
2 => 1,
);
$this->assertEquals($expected_response, $parser_response);
}

function test_date_syntax_parsing() {
$parser_response = p_get_instructions('{{rating|startdate=2015-02-17}}')[2];
$expected_response = array(
Expand All @@ -45,6 +93,8 @@ function test_date_syntax_parsing() {
1 => array(
'lang' => '',
'startdate' => '2015-02-17',
'tag' => 'ul',
'score' => 'false',
)
),
2 => DOKU_LEXER_SPECIAL,
Expand All @@ -66,6 +116,8 @@ function test_lang_syntax_parsing() {
1 => array(
'lang' => 'en',
'startdate' => '',
'tag' => 'ul',
'score' => 'false',
)
),
2 => DOKU_LEXER_SPECIAL,
Expand All @@ -87,6 +139,8 @@ function test_datelang_syntax_parsing() {
1 => array(
'lang' => 'en',
'startdate' => '2015-02-17',
'tag' => 'ul',
'score' => 'false',
)
),
2 => DOKU_LEXER_SPECIAL,
Expand Down
25 changes: 21 additions & 4 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function connectTo($mode) {
*/
function handle($match, $state, $pos, Doku_Handler $handler) {
if ($state==DOKU_LEXER_SPECIAL) {
$options = array('lang' => null, 'startdate' => null );
$options = array('lang' => null, 'startdate' => null, 'tag' => 'ul', 'score' => 'false' );
$match = rtrim($match,'\}');
$match = substr($match,8);
if ($match != '') {
Expand All @@ -56,6 +56,11 @@ function handle($match, $state, $pos, Doku_Handler $handler) {

/**
* Create output
*
* @param string $format Renderer mode (supported modes: xhtml)
* @param Doku_Renderer $renderer The renderer
* @param array $data The data from the handler() function
* @return bool If rendering was successful.
*/
function render($format, Doku_Renderer $renderer, $data) {
if($format == 'metadata') return false;
Expand All @@ -64,22 +69,34 @@ function render($format, Doku_Renderer $renderer, $data) {
$hlp = plugin_load('helper', 'rating');
$list = $hlp->best($data[1]['lang'],$data[1]['startdate'], 20);

$renderer->listo_open();
if($data[1]['tag'] == 'ol') {
$renderer->listo_open();
} else {
$renderer->listu_open();
}

$num_items=0;
foreach($list as $item) {
if (auth_aclcheck($item['page'],'',null) < AUTH_READ) continue;
if (!page_exists($item['page'])) continue;
$num_items = $num_items +1;
$renderer->listitem_open(1);
if (strpos($item['page'],':') === false) {
$item['page'] = ':' . $item['page'];
}
$renderer->internallink($item['page']);
$renderer->cdata(' (' . $item['val'] . ')');
if ($data[1]['score'] === 'true') $renderer->cdata(' (' . $item['val'] . ')');

$renderer->listitem_close();
if ($num_items >= 10) break;
}
$renderer->listo_close();

if($data[1]['tag'] == 'ol') {
$renderer->listo_close();
} else {
$renderer->listu_close();
}
return true;
}

}

0 comments on commit 59b8244

Please sign in to comment.