1<?php
2/**
3 * DokuWiki Plugin markdownextra (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once DOKU_PLUGIN.'action.php';
13
14class action_plugin_markdownextra extends DokuWiki_Action_Plugin {
15
16   function register(Doku_Event_Handler $controller) {
17      $controller->register_hook('PARSER_WIKITEXT_PREPROCESS',
18'BEFORE', $this, 'handle_parser_wikitext_preprocess');
19      $controller->register_hook('TPL_METAHEADER_OUTPUT',
20'BEFORE', $this, 'handle_meltdown_metadata');
21   }
22
23   function handle_parser_wikitext_preprocess(&$event, $param) {
24       global $ACT;
25       global $ID;
26       global $TEXT;
27       // Check if file is a .md page:
28       if(substr($ID,-3) != '.md') return true;
29       // Check for default view (in this case there is only 1 parsed text)
30       // or check that the text parsed is the text being edited
31       // (see: http://www.dokuwiki.org/devel:environment#text):
32       if($ACT != 'show' && $event->data != $TEXT) return true;
33
34       if ($this->getConf('frontmatter')){
35           if (preg_match('/^---\s*\n(.*?\n?)^---\s*$\n?(.+)/sm',$event->data, $match)){
36               $event->data = sprintf("%s<markdown>\n%s\n</markdown>", $match[1], $match[2]);
37           }else{
38               $event->data = "<markdown>\n".$event->data."\n</markdown>";
39           }
40       }else{
41           $event->data = "<markdown>\n".$event->data."\n</markdown>";
42       }
43   }
44
45   function handle_meltdown_metadata(&$event, $param) {
46       global $ACT;
47       global $ID;
48       // Check if file is a .md page and if we are editing a page:
49       if (substr($ID,-3) != '.md' || $ACT != 'edit') return;
50
51       if ($this->getConf('markdowneditor') == 'meltdown') {
52           $meltdownBase = DOKU_BASE.'lib/plugins/markdownextra/lib/meltdown/';
53           $meltdownTweaksBase = DOKU_BASE.'lib/plugins/markdownextra/lib/meltdown-tweaks/';
54           // Add Meltdown css and script files, as well as our custom css and js tweaks:
55           $event->data['link'][] = array(
56                'rel'     => 'stylesheet',
57                'type'    => 'text/css',
58                'href'    => $meltdownBase.'css/meltdown.css');
59           $event->data['link'][] = array(
60                'rel'     => 'stylesheet',
61                'type'    => 'text/css',
62                'href'    => $meltdownTweaksBase.'meltdown-tweaks.css');
63           $event->data['script'][] = array(
64                'type'    => 'text/javascript',
65                '_data'   => '',
66                'src'     => $meltdownBase.'js/jquery.meltdown.js');
67           $event->data['script'][] = array(
68                'type'    => 'text/javascript',
69                '_data'   => '',
70                'src'     => $meltdownBase.'js/lib/js-markdown-extra.js');
71           $event->data['script'][] = array(
72                'type'    => 'text/javascript',
73                '_data'   => '',
74                'src'     => $meltdownBase.'js/lib/rangyinputs-jquery.min.js');
75           $event->data['script'][] = array(
76                'type'    => 'text/javascript',
77                '_data'   => '',
78                'src'     => $meltdownTweaksBase.'meltdown-tweaks.js');
79        }
80   }
81}