Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ghi committed Jan 12, 2016
1 parent 70268cb commit 4af1955
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 38 deletions.
2 changes: 1 addition & 1 deletion plugin.info.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
base subnumberlist
author Szymon Olewniczak
email dokuwiki@imz.re
date 2016-01-09
date 2016-01-12
name subnumberlist
desc Change numbering style in ordered list.
url http://www.dokuwiki.org/plugin:subnumberlist
129 changes: 92 additions & 37 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,66 +21,109 @@ function getSort() { return 1; }
function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }

function connectTo($mode) {
$this->Lexer->addEntryPattern('[ \t]*\n {2,}[\-]',$mode,'plugin_subnumberlist');
$this->Lexer->addEntryPattern('[ \t]*\n\t{1,}[\-]',$mode,'plugin_subnumberlist');
$this->Lexer->addEntryPattern('[ \t]*\n {2,}[\-\*]',$mode,'plugin_subnumberlist');
$this->Lexer->addEntryPattern('[ \t]*\n\t{1,}[\-\*]',$mode,'plugin_subnumberlist');

$this->Lexer->addPattern('\n {2,}[\-]','plugin_subnumberlist');
$this->Lexer->addPattern('\n\t{1,}[\-]','plugin_subnumberlist');
$this->Lexer->addPattern('\n {2,}[\-\*]','plugin_subnumberlist');
$this->Lexer->addPattern('\n\t{1,}[\-\*]','plugin_subnumberlist');

}

function postConnect() {
$this->Lexer->addExitPattern('\n','plugin_subnumberlist');
}

/*map real level(nr of spaces) into logical level*/
private $levels_map = array();
//ident
private $levels = array();
private $max_levels = 100;
private $start_level = 0;
function init_levels() {
$levels = array();
for ($i = 0; $i < $this->max_levels; $i++)
$this->levels[$i] = 0;
}
//types of lists;
private $types = array();

function interpretSyntax($match) {
function interpretSyntax($match, &$type) {
if ( substr($match,-1) == '*' ) {
$type = 'u';
} else {
$type = 'o';
}
// Is the +1 needed? It used to be count(explode(...))
// but I don't think the number is seen outside this handler
return substr_count(str_replace("\t",' ',$match), ' ') + 1;
}



function gen_numbers($level) {
$r = '';
for ($i = $this->start_level; $i < $level; $i++)
$r .= "&nbsp;&nbsp;&nbsp;&nbsp;";
for ($i = $this->start_level; $i <= $level; $i++)
for ($i = 0; $i <= $level; $i++)
$r .= $this->levels[$i].'.';
return $r;
}


private $previous_level;
function handle($match, $state, $pos, &$handler)
{
switch ($state) {
case DOKU_LEXER_ENTER:
//list_open
$this->init_levels();
$level = $this->interpretSyntax($match);
$this->start_level = $level;
$this->levels[$level]++;
$nr = $this->gen_numbers($level);
return array('BEGIN_LIST', array('BEGIN_ELEMENT', $nr));
$level = $this->interpretSyntax($match, $type);

$this->levels_map[$level] = 0;
$this->previous_level = 0;
$this->levels[0] = 1;
$this->types[0] = $type;

$nr = $this->gen_numbers(0);
return array(array('BEGIN_LIST', $type), array('BEGIN_ELEMENT', $nr, $type));
break;
case DOKU_LEXER_EXIT:
//list_close
return array('END_ELEMENT', 'END_LIST');
break;
case DOKU_LEXER_MATCHED:
//list_item
$level = $this->interpretSyntax($match);
$this->levels[$level]++;
$nr = $this->gen_numbers($level);
return array('END_ELEMENT', array('BEGIN_ELEMENT', $nr));
$level = $this->interpretSyntax($match, $type);
if (!isset($this->levels[$mlevel]))
$this->levels[$mlevel] = 0;
//schodzimy w dół
if (!isset($this->levels_map[$level]) || $this->levels_map[$level] > $this->previous_level) {
$this->previous_level++;
$this->levels_map[$level] = $this->previous_level;
$this->levels[$this->previous_level] = 1;
$this->types[$this->previous_level] = $type;
$nr = $this->gen_numbers($this->previous_level);
$r = array(array('BEGIN_LIST', $type), array('BEGIN_ELEMENT', $nr, $type));
//idziemy w górę
} else if ($this->levels_map[$level] < $this->previous_level) {
$return_level = $this->levels_map[$level];
$prev_lvl = $this->previous_level;

$r = array();
for ($i = $prev_lvl; $i > $return_level; $i--) {
$r[] = 'END_ELEMENT';
//not last element
$r[] = array('END_LIST', $this->types[$i]);

if ($i != $return_level-1) {
$l = array_search($i, $this->levels_map);
unset($this->levels_map[$l]);
}
}
$r[] = 'END_ELEMENT';

$this->levels[$return_level]++;
$nr = $this->gen_numbers($return_level);
$r[] = array('BEGIN_ELEMENT', $nr, $this->types[$return_level]);

$this->previous_level = $return_level;
//zostajemy na tym samym poziomie
} else {
$mlevel = $this->levels_map[$level];
$this->levels[$mlevel]++;
$nr = $this->gen_numbers($mlevel);

$r = array('END_ELEMENT', array('BEGIN_ELEMENT', $nr, $this->types[$mlevel]));
}
return $r;

break;
case DOKU_LEXER_UNMATCHED:
return array(array('CDATA', $match));
Expand All @@ -91,28 +134,40 @@ function handle($match, $state, $pos, &$handler)

function render($mode, &$renderer, $data) {
if($mode == 'xhtml'){
foreach ($data as $action)
foreach ($data as $action) {
if (is_array($action))
switch($action[0]) {
case 'BEGIN_ELEMENT':
$renderer->doc .= '<li>'.$action[1];
if ($action[2] == 'o')
$renderer->doc .= '<li>'.$action[1];
else
$renderer->doc .= '<li>';
$renderer->doc .= '<div class="li" style="display:inline">';
break;
case 'BEGIN_LIST':
if ($action[1] == 'o')
$renderer->doc .= "\n".'<ol style="list-style-type:none; margin-left:-1em;">'."\n";
else
$renderer->doc .= "\n".'<ul>'."\n";
break;
case 'END_LIST':
if ($action[1] == 'o')
$renderer->doc .= '</ol>'."\n";
else
$renderer->doc .= '</ul>'."\n";
break;
case 'CDATA':
$renderer->doc .= $action[1];
break;
}
else
switch($action) {
case 'BEGIN_LIST':
$renderer->doc .= '<ol style="list-style-type:none; margin-left:-1em;">';
break;
case 'END_LIST':
$renderer->doc .= '</ol>';
break;
case 'END_ELEMENT':
$renderer->doc .= '</li>';
$renderer->doc .= '</div>';
$renderer->doc .= '</li>'."\n";
break;
}
}

}
return false;
Expand Down

0 comments on commit 4af1955

Please sign in to comment.