yellow/system/core/core-markdownextra.php

112 lines
3.6 KiB
PHP
Raw Normal View History

2013-04-07 18:04:09 +00:00
<?php
// Copyright (c) 2013 Datenstrom, http://datenstrom.se
2013-04-07 18:04:09 +00:00
// This file may be used and distributed under the terms of the public license.
2013-12-01 11:59:07 +00:00
// Markdown extra parser core plugin
class YellowMarkdownExtra
2013-04-07 18:04:09 +00:00
{
2013-12-01 11:59:07 +00:00
const Version = "0.2.1";
2013-08-28 10:01:46 +00:00
var $yellow; //access to API
var $textHtml; //generated text (HTML format)
2013-04-14 22:41:04 +00:00
// Initialise plugin
2013-12-01 11:59:07 +00:00
function onLoad($yellow)
2013-04-07 18:04:09 +00:00
{
2013-08-28 10:01:46 +00:00
$this->yellow = $yellow;
2013-04-07 18:04:09 +00:00
}
2013-04-14 22:41:04 +00:00
// Parse text
2013-04-07 18:04:09 +00:00
function parse($text)
{
2013-12-01 11:59:07 +00:00
$markdown = new YellowMarkdownExtraParser($this->yellow);
2013-08-28 10:01:46 +00:00
return $this->textHtml = $markdown->transform($text);
2013-04-07 18:04:09 +00:00
}
}
2013-04-14 22:41:04 +00:00
2013-08-28 10:01:46 +00:00
require_once("markdown.php");
2013-12-01 11:59:07 +00:00
class YellowMarkdownExtraParser extends MarkdownExtra_Parser
2013-04-07 18:04:09 +00:00
{
2013-04-14 22:41:04 +00:00
var $yellow; //access to API
2013-04-07 18:04:09 +00:00
2013-04-14 22:41:04 +00:00
function __construct($yellow)
{
$this->yellow = $yellow;
parent::__construct();
}
// Transform text
function transform($text)
{
$text = preg_replace("/@pageRead/i", $this->yellow->page->get("pageRead"), $text);
2013-07-16 16:56:27 +00:00
$text = preg_replace("/@pageEdit/i", $this->yellow->page->get("pageEdit"), $text);
2013-07-11 20:33:28 +00:00
$text = preg_replace("/@pageError/i", $this->yellow->page->get("pageError"), $text);
2013-04-14 22:41:04 +00:00
return parent::transform($text);
}
2013-08-28 10:01:46 +00:00
// Handle links
function doAutoLinks($text)
{
$text = preg_replace_callback("/<(\w+:[^\'\">\s]+)>/", array(&$this, "_doAutoLinks_url_callback"), $text);
$text = preg_replace_callback("/<(\w+@[\w\-\.]+)>/", array(&$this, "_doAutoLinks_email_callback"), $text);
$text = preg_replace_callback("/\[(\w+)\s+(.*?)\]/", array(&$this, "_doAutoLinks_shortcut_callback"), $text);
return $text;
}
// Handle shortcuts
function _doAutoLinks_shortcut_callback($matches)
{
$text = preg_replace("/\s+/s", " ", $matches[2]);
$output = $this->yellow->page->parseType($matches[1], $text, true);
if(is_null($output)) $output = $matches[0];
return $this->hashBlock($output);
2013-08-28 10:01:46 +00:00
}
// Handle fenced code blocks
function _doFencedCodeBlocks_callback($matches)
{
$text = $matches[4];
$output = $this->yellow->page->parseType($matches[2], $text, false);
if(is_null($output))
{
$attr = $this->doExtraAttributes("pre", $dummy =& $matches[3]);
$output = "<pre$attr><code>".htmlspecialchars($text, ENT_NOQUOTES)."</code></pre>";
}
return "\n\n".$this->hashBlock($output)."\n\n";
}
// Handle inline links
function _doAnchors_inline_callback($matches)
{
$url = $matches[3]=="" ? $matches[4] : $matches[3];
$text = $matches[2];
$title = $matches[7];
$attr = $this->doExtraAttributes("a", $dummy =& $matches[8]);
$output = "<a href=\"".$this->encodeAttribute($url)."\"";
if(!empty($title)) $output .= " title=\"".$this->encodeAttribute($title)."\"";
$output .= $attr;
$output .= ">".$this->runSpanGamut($text)."</a>";
return $this->hashPart($output);
}
2013-04-14 22:41:04 +00:00
// Handle inline images
2013-04-07 18:04:09 +00:00
function _doImages_inline_callback($matches)
2013-04-14 22:41:04 +00:00
{
2013-04-07 18:04:09 +00:00
$path = $matches[3]=="" ? $matches[4] : $matches[3];
$src = $this->yellow->config->get("serverBase").$this->yellow->config->get("imageLocation").$path;
2013-05-23 06:24:50 +00:00
list($width, $height) = $this->yellow->toolbox->detectImageDimensions($this->yellow->config->get("imageDir").$path);
2013-04-07 18:04:09 +00:00
$alt = $matches[2];
2013-04-14 22:41:04 +00:00
$title = $matches[7];
$attr = $this->doExtraAttributes("img", $dummy =& $matches[8]);
2013-08-28 10:01:46 +00:00
$output = "<img src=\"".$this->encodeAttribute($src)."\"";
if($width && $height) $output .= " width=\"$width\" height=\"$height\"";
if(!empty($alt)) $output .= " alt=\"".$this->encodeAttribute($alt)."\"";
if(!empty($title)) $output .= " title=\"".$this->encodeAttribute($title)."\"";
$output .= $attr;
$output .= $this->empty_element_suffix;
return $this->hashPart($output);
2013-04-07 18:04:09 +00:00
}
}
2013-12-01 11:59:07 +00:00
$yellow->registerPlugin("markdownextra", "YellowMarkdownExtra", YellowMarkdownExtra::Version);
2013-04-07 18:04:09 +00:00
?>