1<?php
2
3if(!defined('DOKU_INC')) die();
4if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
5require_once(DOKU_PLUGIN.'syntax.php');
6include_once('headeratx.php');
7
8use dokuwiki\Parsing\Handler\Quote;
9
10class syntax_plugin_markdowku_blockquotes extends DokuWiki_Syntax_Plugin {
11
12    function getType()  { return 'container'; }
13    function getPType() { return 'block'; }
14    function getSort()  { return 219; }
15    function getAllowedTypes() {
16        return array('formatting', 'substition', 'disabled', 'protected',
17        'container');
18    }
19
20    function connectTo($mode) {
21        $this->Lexer->addEntryPattern(
22        // (?<=\n)[ \t]*>[ \t]?(?=(?:\n[ ]*[^>]|\Z))
23//            '\n[ \t]*>[ \t]?.+?\n(?:.+\n)*',
24//            '(?:\n|\A)[ \t]*>(?:[ >\t]*)?', //[ \t]?(?=[^\n]+?\n)',
25            '(?:\n|\A)[ \t]*>(?:[ >\t]*)?.*?(?=\n)', //[ \t]?(?=[^\n]+?\n)',
26            $mode,
27            'plugin_markdowku_blockquotes');
28
29        /* Setext headers need two lines */
30        $this->Lexer->addPattern(
31            '\n[ \t]*>(?:[ \t>]*>)?[ \t]?[^\n]+?[ \t]*\n[ \t]*>(?:[ \t>]*>)?[ \t]?=+[ \t]*',
32            'plugin_markdowku_blockquotes');
33
34        $this->Lexer->addPattern(
35            '\n[ \t]*>(?:[ \t>]*>)?[ \t]?[^\n]+?[ \t]*\n[ \t]*>(?:[ \t>]*>)?[ \t]?-+[ \t]*',
36            'plugin_markdowku_blockquotes');
37
38        $this->Lexer->addPattern(
39//            '\n[ \t]*>(?:[ \t>]*>)?[ \t]?', //[ \t]?(?=[^\n]+?\n)',
40            '\n[ \t]*>(?:[ \t>]*>)?[ \t]?.*?(?=\n)', //[ \t]?(?=[^\n]+?\n)',
41            'plugin_markdowku_blockquotes');
42    }
43
44    function postConnect() {
45        $this->Lexer->addExitPattern(
46            '(?:\n[^>]|\Z)',
47            'plugin_markdowku_blockquotes');
48    }
49
50    function handle($match, $state, $pos, Doku_Handler $handler) {
51        global $DOKU_PLUGINS;
52
53        preg_match('/^\n[ \t]*>(?:[ \t>]*>)?[ \t]?/', $match, $quotearg);
54        $quoteinarg = preg_replace('/^\n[ \t]*>(?:[ \t>]*>)?[ \t]?/', '', $match);
55
56        if ($state == DOKU_LEXER_ENTER) {
57            $ReWriter = new Doku_Handler_Markdown_Quote($handler->getCallWriter());
58            $handler->setCallWriter($ReWriter);
59            $handler->_addCall('quote_start', $quotearg, $pos);
60        } elseif ($state == DOKU_LEXER_EXIT) {
61            $handler->_addCall('quote_end', array(), $pos);
62            $handler->getCallWriter()->process();
63            $ReWriter = & $handler->getCallWriter();
64            $handler->setCallWriter($ReWriter->getCallWriter());
65        }
66
67        if ($quoteinarg == '') {
68            $handler->_addCall('quote_newline', $quotearg, $pos);
69        /* ATX headers (headeratx) */
70        } elseif (preg_match('/^\#{1,6}[ \t]*.+?[ \t]*\#*/', $quoteinarg)) {
71            $plugin =& plugin_load('syntax', 'markdowku_headeratx');
72            $plugin->handle($quoteinarg, $state, $pos, $handler);
73        /* Horizontal rulers (hr) */
74        } elseif (preg_match('/[ ]{0,2}(?:[ ]?_[ ]?){3,}[ \t]*/', $quoteinarg)
75                or preg_match('/[ ]{0,2}(?:[ ]?-[ ]?){3,}[ \t]*/', $quoteinarg)
76                or preg_match('/[ ]{0,2}(?:[ ]?\*[ ]?){3,}[ \t]*/', $quoteinarg)) {
77            $plugin =& plugin_load('syntax', 'markdowku_hr');
78            $plugin->handle($quoteinarg, $state, $pos, $handler);
79        /* Setext headers (headersetext) */
80        } elseif (preg_match('/^[^\n]+?[ \t]*\n[ \t]*>(?:[ \t>]*>)?[ \t]?=+[ \t]*/', $quoteinarg)
81                or preg_match('/^[^\n]+?[ \t]*\n[ \t]*>(?:[ \t>]*>)?[ \t]?-+[ \t]*/', $quoteinarg)) {
82            $quoteinarg = preg_replace('/(?<=\n)[ \t]*>(?:[ \t>]*>)?[ \t]?/', '', $quoteinarg);
83            $plugin =& plugin_load('syntax', 'markdowku_headersetext');
84            $plugin->handle($quoteinarg, $state, $pos, $handler);
85        } else {
86            $handler->_addCall('cdata', array($quoteinarg), $pos);
87        }
88
89        return true;
90    }
91
92    function render($mode, Doku_Renderer $renderer, $data) {
93        return true;
94    }
95}
96
97class Doku_Handler_Markdown_Quote extends Quote {
98    function getDepth($marker) {
99        $quoteLength = 0;
100        $position = 0;
101        $text = preg_replace('/^\n*/', '', $marker);
102        while (TRUE) {
103            if (preg_match('/^[ \t]/', substr($text, $position)) > 0) {
104                $position++;
105            } elseif (preg_match('/^>/', substr($text, $position)) > 0) {
106                $position++;
107                $quoteLength++;
108            } else {
109                break;
110            }
111        }
112        return $quoteLength;
113    }
114}
115//Setup VIM: ex: et ts=4 enc=utf-8 :
116