xref: /plugin/commonmark/action.php (revision 4c91f333b19006451b0fec4e612ae0ab106ca979)
1ad615f37SSungbin Jeon<?php
273971cfeSSungbin Jeon/*
373971cfeSSungbin Jeon * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
473971cfeSSungbin Jeon *
573971cfeSSungbin Jeon * (c) Sungbin Jeon <clockoon@gmail.com>
673971cfeSSungbin Jeon *
773971cfeSSungbin Jeon * Original code based on the followings:
873971cfeSSungbin Jeon * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
973971cfeSSungbin Jeon * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
1073971cfeSSungbin Jeon *
1173971cfeSSungbin Jeon * For the full copyright and license information, please view the LICENSE
1273971cfeSSungbin Jeon * file that was distributed with this source code.
13ad615f37SSungbin Jeon */
148ec9a8f2SSungbin Jeon
158ec9a8f2SSungbin Jeon require_once __DIR__.'/src/bootstrap.php';
168ec9a8f2SSungbin Jeon
178ec9a8f2SSungbin Jeon use Dokuwiki\Plugin\Commonmark\Commonmark;
1880734199SSungbin Jeon use Nette\Utils\Strings;
19ad615f37SSungbin Jeon
20ad615f37SSungbin Jeon if(!defined('DOKU_INC')) die();
21ad615f37SSungbin Jeon
22ad615f37SSungbin Jeon class action_plugin_commonmark extends DokuWiki_Action_Plugin {
2380734199SSungbin Jeon    // array for heading positions
2480734199SSungbin Jeon    // [hid] => {[depth], [startline], [endline]}
2580734199SSungbin Jeon    public $headingInfo = [];
2680734199SSungbin Jeon    // positions of newline
2780734199SSungbin Jeon    public $linePosition = [];
2880734199SSungbin Jeon    // flag for checking first run only
2980734199SSungbin Jeon    public $firstRun = true;
3080734199SSungbin Jeon
31ad615f37SSungbin Jeon    /**
32ad615f37SSungbin Jeon     * pass text to Commonmark parser before DW parser
33ad615f37SSungbin Jeon     */
34ad615f37SSungbin Jeon    public function register(Doku_Event_Handler $controller) {
35ad615f37SSungbin Jeon        $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'BEFORE', $this,
36ad615f37SSungbin Jeon                                    '_commonmarkparse');
3780734199SSungbin Jeon        $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this,
3880734199SSungbin Jeon                                    '_editbutton');
3980734199SSungbin Jeon    }
4080734199SSungbin Jeon
4180734199SSungbin Jeon    public function _editbutton(Doku_Event $event, $param) {
42*4c91f333SSungbin Jeon        echo(print_r($this->headingInfo));
43*4c91f333SSungbin Jeon        echo(print_r($this->linePosition));
4480734199SSungbin Jeon        global $conf;
4580734199SSungbin Jeon
4680734199SSungbin Jeon        // get hid
4780734199SSungbin Jeon        $hid = $event->data['hid'];
4880734199SSungbin Jeon        // fetch range on original md
4980734199SSungbin Jeon        // check hid match
5080734199SSungbin Jeon        $keys = array_keys($this->headingInfo);
51*4c91f333SSungbin Jeon        if (in_array($hid,$keys)) {
5280734199SSungbin Jeon            // get max section editing level config
5380734199SSungbin Jeon            $maxsec = $conf['maxseclevel'];
5480734199SSungbin Jeon            // set start position
5580734199SSungbin Jeon            // first, check whether first heading
5680734199SSungbin Jeon            if ($hid == $keys[0]) {
5780734199SSungbin Jeon                $start = 1;
5880734199SSungbin Jeon            } else {
5980734199SSungbin Jeon                $lineStart = $this->headingInfo[$hid]['startline'];
6080734199SSungbin Jeon                // since CommonMark library finds heading marks, we have to declare
6180734199SSungbin Jeon                $start = $this->linePosition[$lineStart];
6280734199SSungbin Jeon            }
6380734199SSungbin Jeon            // find end key & location; proceed while max level or less arrived
6480734199SSungbin Jeon            $endlevel = 52;
6580734199SSungbin Jeon            $index = array_search($hid,$keys);
6680734199SSungbin Jeon            $end = 0; // 0 means end of document
6780734199SSungbin Jeon            $stop = false;
6880734199SSungbin Jeon            while($stop == false) {
6980734199SSungbin Jeon                if (isset($keys[$index+1])) { // check for non-last element
7080734199SSungbin Jeon                    $endlevel = $this->headingInfo[$keys[$index+1]]['level'];
7180734199SSungbin Jeon                    $lineEnd = $this->headingInfo[$keys[$index+1]]['startline'] - 1; // go one line up
7280734199SSungbin Jeon                    $end = $this->linePosition[$lineEnd];
7380734199SSungbin Jeon                    if($maxsec<=$endlevel) { $stop = true; }
7480734199SSungbin Jeon                } else {
7580734199SSungbin Jeon                    $end = 0;
7680734199SSungbin Jeon                    $stop = true;
7780734199SSungbin Jeon                }
7880734199SSungbin Jeon                $index = $index + 1;
7980734199SSungbin Jeon            }
8080734199SSungbin Jeon            if($end == 0) {
8180734199SSungbin Jeon                $event->data['range'] = (string)$start.'-';
8280734199SSungbin Jeon            } else {
8380734199SSungbin Jeon                $event->data['range'] = (string)$start.'-'.$end;
8480734199SSungbin Jeon            }
8580734199SSungbin Jeon
8680734199SSungbin Jeon        }
8780734199SSungbin Jeon        // example: $event->data['range'] = '1-2';
88ad615f37SSungbin Jeon    }
89ad615f37SSungbin Jeon
90ad615f37SSungbin Jeon    public function _commonmarkparse(Doku_Event $event, $param) {
9180734199SSungbin Jeon        $markdown = $event->data;
92532432a2SSungbin Jeon        // check force_commonmark option; if 1, ignore doctype
93532432a2SSungbin Jeon        if ($this->getConf('force_commonmark')) {
947569cca4SSungbin Jeon            $markdown = ltrim($markdown);
9580734199SSungbin Jeon            $result = Commonmark::RendtoDW($markdown, $this->getConf('frontmatter_tag'));
96ad615f37SSungbin Jeon        }
9780734199SSungbin Jeon        elseif (preg_match('/\A<!DOCTYPE markdown>/',$markdown)) {
9880734199SSungbin Jeon            $markdown = preg_replace('/\A<!DOCTYPE markdown>\n/','',$markdown);
9980734199SSungbin Jeon            $markdown = ltrim($markdown);
10080734199SSungbin Jeon            $result = Commonmark::RendtoDW($markdown, $this->getConf('frontmatter_tag'));
10180734199SSungbin Jeon            $event->data = $result['text'];
10280734199SSungbin Jeon        }
10380734199SSungbin Jeon        $event->data = $result['text'];
10480734199SSungbin Jeon        if ($this->firstRun == true) {
10580734199SSungbin Jeon            // get position of each line
10680734199SSungbin Jeon            $lastPos = 0;
107*4c91f333SSungbin Jeon            $this->linePosition[] = $lastPos;
108*4c91f333SSungbin Jeon            while(($lastPos = strpos($markdown,PHP_EOL,$lastPos)) !== false){
109*4c91f333SSungbin Jeon                $this->linePosition[] = $lastPos;
110*4c91f333SSungbin Jeon                $lastPos = $lastPos + strlen(PHP_EOL);
11180734199SSungbin Jeon            }
11280734199SSungbin Jeon            $this->headingInfo = $this->CleanHeadingInfo($result['heading']);
11380734199SSungbin Jeon            $this->firstRun = false;
11480734199SSungbin Jeon        }
11580734199SSungbin Jeon    }
11680734199SSungbin Jeon
11780734199SSungbin Jeon    public function CleanHeadingInfo(array $input): array {
11880734199SSungbin Jeon        $keys = array_keys($input);
11980734199SSungbin Jeon        foreach($keys as $key) {
120*4c91f333SSungbin Jeon            $check = false;
121*4c91f333SSungbin Jeon            $new_key = sectionId($key, $check);
12280734199SSungbin Jeon            if($new_key != $key) {
12380734199SSungbin Jeon                $input[$new_key] = $input[$key];
12480734199SSungbin Jeon                unset($input[$key]);
12580734199SSungbin Jeon            }
12680734199SSungbin Jeon        }
127*4c91f333SSungbin Jeon        uasort($input, fn($a, $b) => $a['startline'] <=> $b['startline']);
12880734199SSungbin Jeon        return $input;
129ad615f37SSungbin Jeon    }
130ad615f37SSungbin Jeon}
131