xref: /plugin/commonmark/action.php (revision 50c6b1ddef623a1cad3c79c6336583faeca76ef4)
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 $image_pattern = "";
16 require_once __DIR__.'/src/bootstrap.php';
17 if (file_exists( __DIR__.'/user.php')) {
18    require_once __DIR__.'/user.php';
19 }
20
21 use Dokuwiki\Plugin\Commonmark\Commonmark;
22
23 if(!defined('DOKU_INC')) die();
24
25 class action_plugin_commonmark extends DokuWiki_Action_Plugin {
26    /**
27     * pass text to Commonmark parser before DW parser
28     */
29    public function register(Doku_Event_Handler $controller) {
30        $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'BEFORE', $this,
31                                   '_commonmarkparse');
32    }
33
34    public function _commonmarkparse(Doku_Event $event, $param) {
35        // check force_commonmark option; if 1, ignore doctype
36        if ($this->getConf('force_commonmark')) {
37            $markdown = ltrim($event->data);
38            $event->data = Commonmark::RendtoDW($markdown, $this->getConf('frontmatter_tag'), $GLOBALS['image_pattern']);
39        }
40        elseif (preg_match('/\A<!DOCTYPE markdown>/',$event->data)) {
41            $markdown = preg_replace('/\A<!DOCTYPE markdown>\n/','',$event->data);
42            $markdown = ltrim($markdown);
43            $event->data = Commonmark::RendtoDW($markdown, $this->getConf('frontmatter_tag'), $GLOBALS['image_pattern']);
44        }
45    }
46}
47