1<?php
2/*
3 * Ordered lists:
4 *  1. ...
5 *  2. ...
6 */
7
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'syntax.php');
11
12use dokuwiki\Parsing\Handler\Lists;
13
14class syntax_plugin_markdowku_olists extends DokuWiki_Syntax_Plugin {
15    function getType()  { return 'container'; }
16    function getPType() { return 'block'; }
17    function getSort()  { return 9; }
18    function getAllowedTypes() {
19        return array('formatting', 'substition', 'paragraphs', 'baseonly');
20    }
21
22    function connectTo($mode) {
23        $this->Lexer->addEntryPattern(
24            '\n\n[ ]{0,3}\d+\.[ \t]',
25            $mode,
26            'plugin_markdowku_olists');
27
28        $this->Lexer->addPattern(
29            '\n^[ \t]*\d+\.[ \t]',
30            'plugin_markdowku_olists');
31    }
32
33    function postConnect() {
34        $this->Lexer->addExitPattern(
35            '(?:\Z|\n{1,}(?=\n\S)(?!\n[ \t]*\d+\.[ \t]))',
36            'plugin_markdowku_olists');
37    }
38
39    function handle($match, $state, $pos, Doku_Handler $handler) {
40        switch ($state) {
41            case DOKU_LEXER_ENTER:
42                $ReWriter = new Doku_Handler_Markdown_Ordered_List($handler->getCallWriter());
43                $handler->setCallWriter($ReWriter);
44                $handler->_addCall('list_open', array($match), $pos);
45                break;
46            case DOKU_LEXER_MATCHED:
47                $handler->_addCall('list_item', array($match), $pos);
48                break;
49            case DOKU_LEXER_UNMATCHED:
50                $handler->_addCall('cdata', array($match), $pos);
51                break;
52            case DOKU_LEXER_EXIT:
53                $handler->_addCall('list_close', array(), $pos);
54                $handler->getCallWriter()->process();
55                $ReWriter = & $handler->getCallWriter();
56                $handler->setCallWriter($ReWriter->getCallWriter());
57                break;
58        }
59        return true;
60    }
61
62    function render($mode, Doku_Renderer $renderer, $data) {
63        return true;
64    }
65}
66
67class Doku_Handler_Markdown_Ordered_List extends Lists {
68    private $depth = array(0, 4);
69
70    function interpretSyntax($match, &$type) {
71        $type="o";
72        $listlevel = 1;
73        $real_position = 0;
74        $logical_position = 0;
75        $text = preg_replace('/^\n*/', '', $match);
76
77        while (TRUE) {
78            if (preg_match('/^[ ]{'.$this->depth[$listlevel].'}/', substr($text, $real_position)) > 0) {
79                $real_position += $this->depth[$listlevel];
80                $logical_position += $this->depth[$listlevel];
81                $listlevel += 1;
82                continue;
83            }
84            if (preg_match('/^\t/', substr($text, $real_position)) > 0) {
85                $real_position += 1;
86                $logical_position += 4;
87                $listlevel += 1;
88                continue;
89            }
90            if (preg_match('/^[ ]{0,3}\d+\.[ \t]/', substr($text, $real_position)) > 0) {
91                $this->depth[$listlevel] = strlen(substr($text, $real_position)) - 1;
92            }
93            break;
94        }
95        return $listlevel;
96    }
97}
98//Setup VIM: ex: et ts=4 enc=utf-8 :
99