Markdown update (headers with id)

This commit is contained in:
markseu 2014-03-21 10:12:56 +01:00
parent 4f7541ad6f
commit 716772f0bb
2 changed files with 47 additions and 8 deletions

View file

@ -5,7 +5,7 @@
// Markdown extra core plugin
class YellowMarkdownExtra
{
const Version = "0.2.8";
const Version = "0.2.9";
var $yellow; //access to API
// Initialise plugin
@ -25,11 +25,13 @@ class YellowMarkdownExtra
// Markdown extra parser
class YellowMarkdownExtraParser extends MarkdownExtraParser
{
var $yellow; //access to API
var $yellow; //access to API
var $idAttributes; //id attributes
function __construct($yellow)
{
$this->yellow = $yellow;
$this->idAttributes = array();
parent::__construct();
}
@ -44,6 +46,19 @@ class YellowMarkdownExtraParser extends MarkdownExtraParser
"<a$1href=\"$location$2\"$3>", $this->transform($text));
}
// Return unique id attribute
function getIdAttribute($text)
{
$text = $this->yellow->toolbox->normaliseName($text, false, true);
$text = trim(preg_replace("/-+/", "-", $text), "-");
if(is_null($this->idAttributes[$text]))
{
$this->idAttributes[$text] = $text;
$attr = " id=\"$text\"";
}
return $attr;
}
// Handle links
function doAutoLinks($text)
{
@ -75,6 +90,29 @@ class YellowMarkdownExtraParser extends MarkdownExtraParser
return "\n\n".$this->hashBlock($output)."\n\n";
}
// Handle headers, text style
function _doHeaders_callback_setext($matches)
{
if ($matches[3] == '-' && preg_match('{^- }', $matches[1])) return $matches[0];
$text = $matches[1];
$level = $matches[3]{0} == '=' ? 1 : 2;
$attr = $this->doExtraAttributes("h$level", $dummy =& $matches[2]);
if(empty($attr) && $level==2) $attr = $this->getIdAttribute($text);
$output = "<h$level$attr>".$this->runSpanGamut($text)."</h$level>";
return "\n".$this->hashBlock($output)."\n\n";
}
// Handle headers, atx style
function _doHeaders_callback_atx($matches)
{
$text = $matches[2];
$level = strlen($matches[1]);
$attr = $this->doExtraAttributes("h$level", $dummy =& $matches[3]);
if(empty($attr) && $level==2) $attr = $this->getIdAttribute($text);
$output = "<h$level$attr>".$this->runSpanGamut($text)."</h$level>";
return "\n".$this->hashBlock($output)."\n\n";
}
// Handle inline links
function _doAnchors_inline_callback($matches)
{

View file

@ -5,9 +5,9 @@
// Yellow main class
class Yellow
{
const Version = "0.2.12";
var $page; //current page data
var $pages; //current page tree from file system
const Version = "0.2.13";
var $page; //current page
var $pages; //pages from file system
var $config; //configuration
var $text; //text strings
var $toolbox; //toolbox with helpers
@ -330,7 +330,7 @@ class Yellow
}
}
// Yellow page data
// Yellow page
class YellowPage
{
var $yellow; //access to API
@ -831,7 +831,7 @@ class YellowPageCollection extends ArrayObject
}
}
// Yellow page tree from file system
// Yellow pages from file system
class YellowPages
{
var $yellow; //access to API
@ -1486,10 +1486,11 @@ class YellowToolbox
}
// Normalise directory/file name
function normaliseName($text, $removeExtension = false)
function normaliseName($text, $removeExtension = false, $lowerCase = false)
{
if(preg_match("/^[\d\-\_\.]+(.*)$/", $text, $matches)) $text = $matches[1];
if($removeExtension) $text = ($pos = strrposu($text, '.')) ? substru($text, 0, $pos) : $text;
if($lowerCase) $text = strtoloweru($text);
return preg_replace("/[^\pL\d\-\_\.]/u", "-", $text);
}