1<?php
2/*
3 * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
4 *
5 * (c) Sungbin Jeon <clockoon@gmail.com>
6 *
7 * Original code based on the followings:
8 * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
9 * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
10 *
11 * For the full copyright and license information, please view the LICENSE
12 * file that was distributed with this source code.
13 */
14
15 require_once __DIR__.'/src/bootstrap.php';
16
17 use Dokuwiki\Plugin\Commonmark\Commonmark;
18
19 if(!defined('DOKU_INC')) die();
20
21 class action_plugin_commonmark extends DokuWiki_Action_Plugin {
22    /**
23     * pass text to Commonmark parser before DW parser
24     */
25    public function register(Doku_Event_Handler $controller) {
26        $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'BEFORE', $this,
27                                   '_commonmarkparse');
28    }
29
30    public function _commonmarkparse(Doku_Event $event, $param) {
31        // check force_commonmark option; if 1, ignore doctype
32        if ($this->getConf('force_commonmark')) {
33            $markdown = ltrim($event->data);
34            $event->data = Commonmark::RendtoDW($markdown, $this->getConf('frontmatter_tag'));
35        }
36        elseif (preg_match('/\A<!DOCTYPE markdown>/',$event->data)) {
37            $markdown = preg_replace('/\A<!DOCTYPE markdown>\n/','',$event->data);
38            $markdown = ltrim($markdown);
39            $event->data = Commonmark::RendtoDW($markdown, $this->getConf('frontmatter_tag'));
40        }
41    }
42}
43