xref: /plugin/commonmark/action.php (revision 4384789b6aba8741023ae6f3d27a03b005a08e84)
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;
18ad615f37SSungbin Jeon
19ad615f37SSungbin Jeon if(!defined('DOKU_INC')) die();
20ad615f37SSungbin Jeon
21ad615f37SSungbin Jeon class action_plugin_commonmark extends DokuWiki_Action_Plugin {
22ad615f37SSungbin Jeon    /**
23ad615f37SSungbin Jeon     * pass text to Commonmark parser before DW parser
24ad615f37SSungbin Jeon     */
25ad615f37SSungbin Jeon    public function register(Doku_Event_Handler $controller) {
26ad615f37SSungbin Jeon        $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'BEFORE', $this,
27ad615f37SSungbin Jeon                                   '_commonmarkparse');
28ad615f37SSungbin Jeon    }
29ad615f37SSungbin Jeon
30ad615f37SSungbin Jeon    public function _commonmarkparse(Doku_Event $event, $param) {
31532432a2SSungbin Jeon        // check force_commonmark option; if 1, ignore doctype
32532432a2SSungbin Jeon        if ($this->getConf('force_commonmark')) {
33*4384789bSSungbin Jeon            $markdown = <<<MD
34*4384789bSSungbin Jeon            $event->data
35*4384789bSSungbin Jeon            MD;
36*4384789bSSungbin Jeon            $event->data = Commonmark::RendtoDW($markdown);
37532432a2SSungbin Jeon        }
38532432a2SSungbin Jeon        elseif (preg_match('/\A<!DOCTYPE markdown>/',$event->data)) {
39*4384789bSSungbin Jeon            $markdown = preg_replace('/\A<!DOCTYPE markdown>\n/','',$event->data);
40*4384789bSSungbin Jeon            $markdown = <<<MD
41*4384789bSSungbin Jeon            $markdown
42*4384789bSSungbin Jeon            MD;
43*4384789bSSungbin Jeon            $event->data = Commonmark::RendtoDW($markdown);
44ad615f37SSungbin Jeon        }
45ad615f37SSungbin Jeon
46ad615f37SSungbin Jeon    }
47ad615f37SSungbin Jeon}
48