Last active
January 4, 2016 15:08
-
-
Save jlgrall/8638333 to your computer and use it in GitHub Desktop.
Add basic Meltdown support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* DokuWiki Plugin markdownextra (Action Component) | |
* | |
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html | |
* @author Andreas Gohr <[email protected]> | |
*/ | |
// must be run within Dokuwiki | |
if (!defined('DOKU_INC')) die(); | |
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); | |
require_once DOKU_PLUGIN.'action.php'; | |
class action_plugin_markdownextra extends DokuWiki_Action_Plugin { | |
function register(&$controller) { | |
$controller->register_hook('PARSER_WIKITEXT_PREPROCESS', | |
'BEFORE', $this, 'handle_parser_wikitext_preprocess'); | |
$controller->register_hook('TPL_METAHEADER_OUTPUT', | |
'BEFORE', $this, 'handle_meltdown_metadata'); | |
} | |
function handle_parser_wikitext_preprocess(&$event, $param) { | |
global $ACT; | |
global $ID; | |
global $TEXT; | |
// Check if file is a .md page: | |
if(substr($ID,-3) != '.md') return true; | |
// Check for default view (in this case there is only 1 parsed text) | |
// or check that the text parsed is the text being edited | |
// (see: http://www.dokuwiki.org/devel:environment#text) | |
if($ACT != 'show' && $event->data != $TEXT) return true; | |
if ($this->getConf('frontmatter')){ | |
if (preg_match('/^---\s*\n(.*?\n?)^---\s*$\n?(.+)/sm',$event->data, $match)){ | |
$event->data = sprintf("%s<markdown>\n%s\n</markdown>", $match[1], $match[2]); | |
}else{ | |
$event->data = "<markdown>\n".$event->data."\n</markdown>"; | |
} | |
}else{ | |
$event->data = "<markdown>\n".$event->data."\n</markdown>"; | |
} | |
} | |
function handle_meltdown_metadata(&$event, $param) { | |
global $ACT; | |
global $ID; | |
// Check if file is a .md page: | |
if(substr($ID,-3) != '.md' || $ACT != 'edit') return; | |
$event->data['link'][] = array( | |
"rel" => "stylesheet", | |
"type" => "text/css", | |
'href' => 'http://iphands.github.com/Meltdown/dupe/css/meltdown.css'); | |
$event->data['script'][] = array( | |
'type' => 'text/javascript', | |
'_data' => '', | |
'src' => 'http://iphands.github.com/Meltdown/dupe/js/jquery.meltdown.js'); | |
$event->data['script'][] = array( | |
'type' => 'text/javascript', | |
'_data' => '', | |
'src' => 'http://iphands.github.com/Meltdown/dupe/js/lib/js-markdown-extra.js'); | |
$event->data['script'][] = array( | |
'type' => 'text/javascript', | |
'_data' => '', | |
'src' => 'http://iphands.github.com/Meltdown/dupe/js/lib/textinputs_jquery.js'); | |
$event->data['style'][] = array('type' => 'text/css', | |
'_data' => '.meltdown_preview { resize: vertical; }'. | |
'.meltdown_wrap textarea { margin-bottom: 5px !important; }'); | |
$event->data['script'][] = array( | |
'type' => 'text/javascript', | |
'_data' => 'jQuery(document).ready(function($) {'. | |
'$("#tool__bar").hide().after($("<div\>", {text: "[Show original toolbar]", click: function() {$(this).remove();$("#tool__bar").show();}}));'. | |
'$("#wiki__text").meltdown();'. | |
'$(".meltdown_wrap").append($(".meltdown_preview-wrap"));'. | |
'$(".meltdown_control-preview").click();'. | |
'});'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment