add Textile markup (implements #94)

This commit is contained in:
oliver 2015-05-12 16:37:03 +02:00
parent 4b192b4e14
commit e9d2b96571
5 changed files with 4303 additions and 7 deletions

View file

@ -57,14 +57,18 @@
*/
public function t($string)
{
// Remove chars
$string = htmlspecialchars($string, ENT_NOQUOTES);
// Use Textile
\hhu\z\lib\Textile::load();
$textileParser = new \Netcarver\Textile\Parser();
$string = $textileParser->textileThis($string);
// Important text
// TODO Deprecated
$string = str_replace('[strong]', '<strong>', $string);
$string = str_replace('[/strong]', '</strong>', $string);
// Create tables
// TODO Deprecated
$string = preg_replace('/(\[table\])\s+/u', '$1', $string);
$string = preg_replace('/\s*(\[tr\])\s*/u', '$1', $string);
$string = preg_replace('%\s+(\[/table\])%u', '$1', $string);
@ -78,10 +82,6 @@
$string = str_replace('[td]', '<td>', $string);
$string = str_replace('[/td]', '</td>', $string);
// Create links
$string = preg_replace('!(^|\s)"([^"]+)":(https?://[^\s]+)(\s|$)!i', '$1<a href="$3">$2</a>$4', $string);
$string = preg_replace('!(^|\s)(https?://[^\s]+)(\s|$)!i', '$1<a href="$2">$2</a>$3', $string);
// Handle Seminarymedia
$seminarymedia = array();
preg_match_all('/\[seminarymedia:(\d+)\]/iu', $string, $matches); //, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
@ -111,7 +111,8 @@
// Return processed string
return nl2br($string);
//return nl2br($string);
return $string;
}

View file

@ -0,0 +1,99 @@
<?php
/**
* Textile - A Humane Web Text Generator.
*
* @link https://github.com/textile/php-textile
*/
namespace Netcarver\Textile;
/*
* Copyright (c) 2013, Netcarver https://github.com/netcarver
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name Textile nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Simple data storage.
*
* This class to allows storing assignments in an internal
* data array.
*
* <code>
* use Netcarver\Textile\DataBag;
* $plant = new DataBag(array('key' => 'value'));
* $plant->flower('rose')->color('red');
* </code>
*/
class DataBag
{
/**
* The data array stored in the bag.
*
* @var array
*/
protected $data;
/**
* Constructor.
*
* @param array|null $data The initial data array stored in the bag
*/
public function __construct(array $data = null)
{
$this->data = (array) $data;
}
/**
* Adds a value to the bag.
*
* Empty values are rejected, unless the
* second argument is set TRUE.
*
* <code>
* use Netcarver\Textile\DataBag;
* $plant = new DataBag(array('key' => 'value'));
* $plant->flower('rose')->color('red')->emptyValue(false, true);
* </code>
*
* @param string $name The name
* @param array $params Arguments
* @return DataBag
*/
public function __call($name, array $params)
{
if (!empty($params[1]) || !empty($params[0])) {
$this->data[$name] = $params[0];
}
return $this;
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,118 @@
<?php
/**
* Textile - A Humane Web Text Generator.
*
* @link https://github.com/textile/php-textile
*/
namespace Netcarver\Textile;
/*
* Copyright (c) 2013, Netcarver https://github.com/netcarver
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name Textile nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Renders HTML elements.
*
* This class can be used to HTML elements. It
* does not sanitise attribute values, but can be
* used to construct tags with nice object oriented
* syntax.
*
* <code>
* use Netcarver\Textile\Tag;
* $img = new Tag('img');
* echo (string) $img->class('big blue')->src('images/elephant.jpg');
* </code>
*/
class Tag extends DataBag
{
/**
* The name of the tag.
*
* @var string
*/
protected $tag;
/**
* Whether the tag is self-closing.
*
* @var bool
*/
protected $selfclose;
/**
* Constructor.
*
* @param string $name The tag name
* @param array $attributes An array of attributes
* @param bool $selfclosing Whether the tag is self-closing
*/
public function __construct($name, array $attributes = null, $selfclosing = true)
{
parent::__construct($attributes);
$this->tag = $name;
$this->selfclose = $selfclosing;
}
/**
* Returns the tag as HTML.
*
* <code>
* $img = new Tag('img');
* $img->src('images/example.jpg')->alt('Example image');
* echo (string) $img;
* </code>
*
* @return string A HTML element
*/
public function __toString()
{
$attributes = '';
if ($this->data) {
ksort($this->data);
foreach ($this->data as $name => $value) {
$attributes .= " $name=\"$value\"";
}
}
if ($this->tag) {
return '<' . $this->tag . $attributes . (($this->selfclose) ? " />" : '>');
}
return $attributes;
}
}

36
app/lib/Textile.inc Normal file
View file

@ -0,0 +1,36 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\lib;
/**
* Class to ensure that Compatibility library below is loaded.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class Textile
{
/**
* Call this function to load necessary files.
*/
public static function load()
{
$path = implode(DS, array('Netcarver', 'Textile'));
require_once($path.DS.'DataBag.php');
require_once($path.DS.'Parser.php');
require_once($path.DS.'Tag.php');
}
}
?>