Skip to content

Commit

Permalink
Switch to cebe\markdown parser (server-side)
Browse files Browse the repository at this point in the history
  • Loading branch information
svivian committed Jun 13, 2016
1 parent c4fb568 commit 3028e06
Show file tree
Hide file tree
Showing 23 changed files with 2,172 additions and 1,555 deletions.
21 changes: 21 additions & 0 deletions MyMarkdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class MyMarkdown extends \cebe\markdown\Markdown
{
/**
* Handle new lines
*/
protected function renderText($text)
{
return strtr($text[1], [" \n" => "<br>\n", "\n" => "<br>\n"]);
}

/**
* Update headings to run from 2..6 to avoid multiple H1s on the page
*/
protected function renderHeadline($block)
{
$tag = 'h' . min($block['level'] + 1, 6);
return "<$tag>" . $this->renderAbsy($block['content']) . "</$tag>\n";
}
}
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Markdown Editor plugin for Question2Answer
=================================================

Expand All @@ -7,7 +6,6 @@ This is an editor plugin for popular open source Q&A platform, [Question2Answer]
The plugin uses modified versions of the PageDown scripts (released by Stack Overflow) for the editor and live preview respectively.



Installation
-------------------------------------------------

Expand All @@ -23,7 +21,6 @@ In Admin > Plugins, you can set three options:
- "Use syntax highlighting" - Integrates [highlight.js](http://softwaremaniacs.org/soft/highlight/en/) for code blocks (including while writing posts). All common programming languages are supported, but you can add more using the [customized download here](http://softwaremaniacs.org/soft/highlight/en/download/). Save the file and overwrite `pagedown/highlight.min.js`. If you ticked the box for CSS above, copy the CSS from `pagedown/highlightjs.css` to the bottom of your theme's current stylesheet.



Extra bits
-------------------------------------------------

Expand All @@ -34,10 +31,9 @@ Extra bits
(Make sure to change `qa_` above to your installation's table prefix if it is different.)



Pay What You Like
-------------------------------------------------

Most of my code is released under the open source GPLv3 license, and provided with a 'Pay What You Like' approach. Feel free to download and modify the plugins/themes to suit your needs, and I hope you value them enough to make a small donation of a few dollars or more.
Most of my code is released under the open source GPLv3 license, and provided with a 'Pay What You Like' approach. Feel free to download and modify the plugins/themes to suit your needs, and I hope you value them enough to donate a few dollars.

### [Donate here](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4R5SHBNM3UDLU)
114 changes: 114 additions & 0 deletions cebe-markdown/GithubMarkdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/

namespace cebe\markdown;

/**
* Markdown parser for github flavored markdown.
*
* @author Carsten Brandt <[email protected]>
*/
class GithubMarkdown extends Markdown
{
// include block element parsing using traits
use block\TableTrait;
use block\FencedCodeTrait;

// include inline element parsing using traits
use inline\StrikeoutTrait;
use inline\UrlLinkTrait;

/**
* @var boolean whether to interpret newlines as `<br />`-tags.
* This feature is useful for comments where newlines are often meant to be real new lines.
*/
public $enableNewlines = false;

/**
* @inheritDoc
*/
protected $escapeCharacters = [
// from Markdown
'\\', // backslash
'`', // backtick
'*', // asterisk
'_', // underscore
'{', '}', // curly braces
'[', ']', // square brackets
'(', ')', // parentheses
'#', // hash mark
'+', // plus sign
'-', // minus sign (hyphen)
'.', // dot
'!', // exclamation mark
'<', '>',
// added by GithubMarkdown
':', // colon
'|', // pipe
];



/**
* Consume lines for a paragraph
*
* Allow headlines, lists and code to break paragraphs
*/
protected function consumeParagraph($lines, $current)
{
// consume until newline
$content = [];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
if ($line === ''
|| ltrim($line) === ''
|| !ctype_alpha($line[0]) && (
$this->identifyQuote($line, $lines, $i) ||
$this->identifyFencedCode($line, $lines, $i) ||
$this->identifyUl($line, $lines, $i) ||
$this->identifyOl($line, $lines, $i) ||
$this->identifyHr($line, $lines, $i)
)
|| $this->identifyHeadline($line, $lines, $i))
{
break;
} elseif ($this->identifyCode($line, $lines, $i)) {
// possible beginning of a code block
// but check for continued inline HTML
// e.g. <img src="file.jpg"
// alt="some alt aligned with src attribute" title="some text" />
if (preg_match('~<\w+([^>]+)$~s', implode("\n", $content))) {
$content[] = $line;
} else {
break;
}
} else {
$content[] = $line;
}
}
$block = [
'paragraph',
'content' => $this->parseInline(implode("\n", $content)),
];
return [$block, --$i];
}

/**
* @inheritdocs
*
* Parses a newline indicated by two spaces on the end of a markdown line.
*/
protected function renderText($text)
{
if ($this->enableNewlines) {
$br = $this->html5 ? "<br>\n" : "<br />\n";
return strtr($text[1], [" \n" => $br, "\n" => $br]);
} else {
return parent::renderText($text);
}
}
}
128 changes: 128 additions & 0 deletions cebe-markdown/Markdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/

namespace cebe\markdown;

/**
* Markdown parser for the [initial markdown spec](http://daringfireball.net/projects/markdown/syntax).
*
* @author Carsten Brandt <[email protected]>
*/
class Markdown extends Parser
{
// include block element parsing using traits
use block\CodeTrait;
use block\HeadlineTrait;
use block\HtmlTrait {
parseInlineHtml as private;
}
use block\ListTrait {
// Check Ul List before headline
identifyUl as protected identifyBUl;
consumeUl as protected consumeBUl;
}
use block\QuoteTrait;
use block\RuleTrait {
// Check Hr before checking lists
identifyHr as protected identifyAHr;
consumeHr as protected consumeAHr;
}

// include inline element parsing using traits
use inline\CodeTrait;
use inline\EmphStrongTrait;
use inline\LinkTrait;

/**
* @var boolean whether to format markup according to HTML5 spec.
* Defaults to `false` which means that markup is formatted as HTML4.
*/
public $html5 = false;

/**
* @var array these are "escapeable" characters. When using one of these prefixed with a
* backslash, the character will be outputted without the backslash and is not interpreted
* as markdown.
*/
protected $escapeCharacters = [
'\\', // backslash
'`', // backtick
'*', // asterisk
'_', // underscore
'{', '}', // curly braces
'[', ']', // square brackets
'(', ')', // parentheses
'#', // hash mark
'+', // plus sign
'-', // minus sign (hyphen)
'.', // dot
'!', // exclamation mark
'<', '>',
];


/**
* @inheritDoc
*/
protected function prepare()
{
// reset references
$this->references = [];
}

/**
* Consume lines for a paragraph
*
* Allow headlines and code to break paragraphs
*/
protected function consumeParagraph($lines, $current)
{
// consume until newline
$content = [];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];

// a list may break a paragraph when it is inside of a list
if (isset($this->context[1]) && $this->context[1] === 'list' && !ctype_alpha($line[0]) && (
$this->identifyUl($line, $lines, $i) || $this->identifyOl($line, $lines, $i))) {
break;
}

if ($line === '' || ltrim($line) === '' || $this->identifyHeadline($line, $lines, $i)) {
break;
} elseif ($line[0] === "\t" || $line[0] === " " && strncmp($line, ' ', 4) === 0) {
// possible beginning of a code block
// but check for continued inline HTML
// e.g. <img src="file.jpg"
// alt="some alt aligned with src attribute" title="some text" />
if (preg_match('~<\w+([^>]+)$~s', implode("\n", $content))) {
$content[] = $line;
} else {
break;
}
} else {
$content[] = $line;
}
}
$block = [
'paragraph',
'content' => $this->parseInline(implode("\n", $content)),
];
return [$block, --$i];
}


/**
* @inheritdocs
*
* Parses a newline indicated by two spaces on the end of a markdown line.
*/
protected function renderText($text)
{
return str_replace(" \n", $this->html5 ? "<br>\n" : "<br />\n", $text[1]);
}
}
Loading

0 comments on commit 3028e06

Please sign in to comment.