1
0

Added Parsedown + ParsedownExtra + Prism

This commit is contained in:
2014-07-10 22:32:47 +02:00
parent e17154d28c
commit 4ca578cfde
14 changed files with 2169 additions and 122 deletions

View File

@@ -136,12 +136,10 @@ class ProgramHelper {
public static function getDescriptionMarkdownTab($path)
{
$md = new CMarkdown;
$content = file_get_contents($path);
$result = '<div class="markdownOwner"><div><p>';
$result .= $md->transform($content);
$result .= ParsedownHelper::parse($content);
$result .= '</p></div></div>';
return $result;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,371 @@
<?php
#
#
# Parsedown Extra
# https://github.com/erusev/parsedown-extra
#
# (c) Emanuil Rusev
# http://erusev.com
#
# For the full license information, view the LICENSE file that was distributed
# with this source code.
#
#
class ParsedownExtra extends Parsedown
{
#
# ~
function __construct()
{
$this->BlockTypes[':'] []= 'DefinitionList';
$this->DefinitionTypes['*'] []= 'Abbreviation';
# identify footnote definitions before reference definitions
array_unshift($this->DefinitionTypes['['], 'Footnote');
# identify footnote markers before before links
array_unshift($this->SpanTypes['['], 'FootnoteMarker');
}
#
# ~
function text($text)
{
$markup = parent::text($text);
# merge consecutive dl elements
$markup = preg_replace('/<\/dl>\s+<dl>\s+/', '', $markup);
# add footnotes
if (isset($this->Definitions['Footnote']))
{
$Element = $this->buildFootnoteElement();
$markup .= "\n" . $this->element($Element);
}
return $markup;
}
#
# Blocks
#
#
# Atx
protected function identifyAtx($Line)
{
$Block = parent::identifyAtx($Line);
if (preg_match('/[ ]*'.$this->attributesPattern.'[ ]*$/', $Block['element']['text'], $matches, PREG_OFFSET_CAPTURE))
{
$attributeString = $matches[1][0];
$Block['element']['attributes'] = $this->parseAttributes($attributeString);
$Block['element']['text'] = substr($Block['element']['text'], 0, $matches[0][1]);
}
return $Block;
}
#
# Definition List
protected function identifyDefinitionList($Line, $Block)
{
if (isset($Block['type']))
{
return;
}
$Element = array(
'name' => 'dl',
'handler' => 'elements',
'text' => array(),
);
$terms = explode("\n", $Block['element']['text']);
foreach ($terms as $term)
{
$Element['text'] []= array(
'name' => 'dt',
'handler' => 'line',
'text' => $term,
);
}
$Element['text'] []= array(
'name' => 'dd',
'handler' => 'line',
'text' => ltrim($Line['text'], ' :'),
);
$Block['element'] = $Element;
return $Block;
}
protected function addToDefinitionList($Line, array $Block)
{
if ($Line['text'][0] === ':')
{
$Block['element']['text'] []= array(
'name' => 'dd',
'handler' => 'line',
'text' => ltrim($Line['text'], ' :'),
);
return $Block;
}
if ( ! isset($Block['interrupted']))
{
$Element = array_pop($Block['element']['text']);
$Element['text'] .= "\n" . chop($Line['text']);
$Block['element']['text'] []= $Element;
return $Block;
}
}
#
# Setext
protected function identifySetext($Line, array $Block = null)
{
$Block = parent::identifySetext($Line, $Block);
if (preg_match('/[ ]*'.$this->attributesPattern.'[ ]*$/', $Block['element']['text'], $matches, PREG_OFFSET_CAPTURE))
{
$attributeString = $matches[1][0];
$Block['element']['attributes'] = $this->parseAttributes($attributeString);
$Block['element']['text'] = substr($Block['element']['text'], 0, $matches[0][1]);
}
return $Block;
}
#
# Definitions
#
#
# Abbreviation
protected function identifyAbbreviation($Line)
{
if (preg_match('/^\*\[(.+?)\]:[ ]*(.+?)[ ]*$/', $Line['text'], $matches))
{
$Abbreviation = array(
'id' => $matches[1],
'data' => $matches[2],
);
return $Abbreviation;
}
}
#
# Footnote
protected function identifyFootnote($Line)
{
if (preg_match('/^\[\^(.+?)\]:[ ]?(.+)$/', $Line['text'], $matches))
{
$Footnote = array(
'id' => $matches[1],
'data' => array(
'text' => $matches[2],
'count' => null,
'number' => null,
),
);
return $Footnote;
}
}
#
# Spans
#
#
# Footnote Marker
protected function identifyFootnoteMarker($Excerpt)
{
if (preg_match('/^\[\^(.+?)\]/', $Excerpt['text'], $matches))
{
$name = $matches[1];
if ( ! isset($this->Definitions['Footnote'][$name]))
{
return;
}
$this->Definitions['Footnote'][$name]['count'] ++;
if ( ! isset($this->Definitions['Footnote'][$name]['number']))
{
$this->Definitions['Footnote'][$name]['number'] = ++ $this->footnoteCount; # » &
}
$Element = array(
'name' => 'sup',
'attributes' => array('id' => 'fnref'.$this->Definitions['Footnote'][$name]['count'].':'.$name),
'handler' => 'element',
'text' => array(
'name' => 'a',
'attributes' => array('href' => '#fn:'.$name, 'class' => 'footnote-ref'),
'text' => $this->Definitions['Footnote'][$name]['number'],
),
);
return array(
'extent' => strlen($matches[0]),
'element' => $Element,
);
}
}
private $footnoteCount = 0;
#
# Link
protected function identifyLink($Excerpt)
{
$Span = parent::identifyLink($Excerpt);
$remainder = substr($Excerpt['text'], $Span['extent']);
if (preg_match('/^[ ]*'.$this->attributesPattern.'/', $remainder, $matches))
{
$Span['element']['attributes'] += $this->parseAttributes($matches[1]);
$Span['extent'] += strlen($matches[0]);
}
return $Span;
}
#
# ~
protected function readPlainText($text)
{
$text = parent::readPlainText($text);
if (isset($this->Definitions['Abbreviation']))
{
foreach ($this->Definitions['Abbreviation'] as $abbreviation => $phrase)
{
$text = str_replace($abbreviation, '<abbr title="'.$phrase.'">'.$abbreviation.'</abbr>', $text);
}
}
return $text;
}
#
# ~
#
protected function buildFootnoteElement()
{
$Element = array(
'name' => 'div',
'attributes' => array('class' => 'footnotes'),
'handler' => 'elements',
'text' => array(
array(
'name' => 'hr',
),
array(
'name' => 'ol',
'handler' => 'elements',
'text' => array(),
),
),
);
usort($this->Definitions['Footnote'], function($A, $B) {
return $A['number'] - $B['number'];
});
foreach ($this->Definitions['Footnote'] as $name => $Data)
{
if ( ! isset($Data['number']))
{
continue;
}
$text = $Data['text'];
foreach (range(1, $Data['count']) as $number)
{
$text .= '&#160;<a href="#fnref'.$number.':'.$name.'" rev="footnote" class="footnote-backref">&#8617;</a>';
}
$Element['text'][1]['text'] []= array(
'name' => 'li',
'attributes' => array('id' => 'fn:'.$name),
'handler' => 'elements',
'text' => array(
array(
'name' => 'p',
'text' => $text,
),
),
);
}
return $Element;
}
#
# Private
#
private function parseAttributes($attributeString)
{
$Data = array();
$attributes = preg_split('/[ ]+/', $attributeString, - 1, PREG_SPLIT_NO_EMPTY);
foreach ($attributes as $attribute)
{
if ($attribute[0] === '#')
{
$Data['id'] = substr($attribute, 1);
}
else # "."
{
$classes []= substr($attribute, 1);
}
}
if (isset($classes))
{
$Data['class'] = implode(' ', $classes);
}
return $Data;
}
private $attributesPattern = '{((?:[#.][-\w]+[ ]*)+)}';
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Class ParsedownHelper
*/
class ParsedownHelper {
/**
* @param string $raw_text
* @return string
*/
public static function parse($raw_text)
{
$Instance = new ParsedownExtra();
return $Instance->text($raw_text);
}
}

View File

@@ -17,11 +17,7 @@
<p>
<?php
$this->beginWidget('CMarkdown');
echo $this->content;
$this->endWidget();
echo ParsedownHelper::parse($this->content);
?>
</p>

View File

@@ -33,6 +33,7 @@ return ArrayX::merge(
'application.components.*',
'application.components.widgets.*',
'application.components.extendedGitGraph.*',
'application.components.parsedown.*',
'bootstrap.components.*',
'bootstrap.behaviors.*',
'bootstrap.helpers.*',

View File

@@ -21,6 +21,7 @@
<?php Yii::app()->bootstrap->register(); ?>
<link rel="stylesheet" type="text/css" href="/css/styles.css"/>
<link rel="stylesheet" type="text/css" href="/css/prism.css"/>
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
@@ -98,6 +99,7 @@
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/javascript/scripts.js"></script>
<script src="/javascript/prism.js"></script>
<?php
foreach ($this->js_scripts as $script) {

View File

@@ -1,7 +1,3 @@
<?php
$this->beginWidget('CMarkdown', array('purifyOutput'=>true));
echo $content;
$this->endWidget();
echo ParsedownHelper::parse($content);

View File

@@ -36,7 +36,7 @@ $this->selectedNav = 'about';
<div class="gitbox-footer">
<div class="gitbox-footer-box">
<span class="gitbox-footer-box-header">Last Update</span>
<span class="gitbox-footer-box-number"><?php echo $egh['creation']->diff(new DateTime())->format('%d') + 1; ?> day<?php echo (($egh['creation']->diff(new DateTime())->format('d') == 1) ? '' : 's')?> ago</span>
<span class="gitbox-footer-box-number"><?php echo $egh['creation']->diff(new DateTime())->format('%d') + 1; ?> day<?php echo (($egh['creation']->diff(new DateTime())->format('%d') == 0) ? '' : 's')?> ago</span>
<span class="gitbox-footer-box-footer"><?php echo $egh['creation']->format('M d Y'); ?></span>
</div>