yellow/system/core/core_markdown.php

66 lines
1.8 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.
// Markdown parser core plugin
class Yellow_Markdown
{
2013-06-02 20:58:52 +00:00
const Version = "0.1.3";
2013-04-14 22:41:04 +00:00
var $markdown; //markdown parser
var $html; //generated HTML
// Initialise plugin
2013-04-07 18:04:09 +00:00
function initPlugin($yellow)
{
$this->markdown = new Yellow_MarkdownExtraParser($yellow);
}
2013-04-14 22:41:04 +00:00
// Parse text
2013-04-07 18:04:09 +00:00
function parse($text)
{
return $this->html = $this->markdown->transform($text);
}
}
2013-04-14 22:41:04 +00:00
2013-04-07 18:04:09 +00:00
require("markdown.php");
class Yellow_MarkdownExtraParser extends MarkdownExtra_Parser
{
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)
{
$baseLocation = $this->yellow->config->get("baseLocation");
$text = preg_replace("/@baseLocation/i", $baseLocation, $text);
return parent::transform($text);
}
// Handle 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];
2013-05-23 06:24:50 +00:00
$src = $this->yellow->config->get("baseLocation").$this->yellow->config->get("imageLocation").$path;
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];
2013-06-02 20:58:52 +00:00
$attr = $this->doExtraAttributes("img", $dummy =& $matches[8]);
2013-04-14 22:41:04 +00:00
2013-04-07 18:04:09 +00:00
$result = "<img src=\"".$this->encodeAttribute($src)."\"";
2013-04-14 22:41:04 +00:00
if($width && $height) $result .= " width=\"$width\" height=\"$height\"";
2013-06-02 20:58:52 +00:00
if(!empty($alt)) $result .= " alt=\"".$this->encodeAttribute($alt)."\"";
if(!empty($title)) $result .= " title=\"".$this->encodeAttribute($title)."\"";
$result .= $attr;
2013-04-07 18:04:09 +00:00
$result .= $this->empty_element_suffix;
2013-04-14 22:41:04 +00:00
2013-04-07 18:04:09 +00:00
return $this->hashPart($result);
}
}
2013-04-14 22:41:04 +00:00
$yellow->registerPlugin("markdown", "Yellow_Markdown", Yellow_Markdown::Version);
2013-04-07 18:04:09 +00:00
?>