Skip to content

Commit

Permalink
Synced to 92f61dc
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Dec 10, 2016
1 parent 80e218d commit 0e65a12
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 38 deletions.
15 changes: 14 additions & 1 deletion src/Plugins/FancyPants/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class Configurator extends ConfiguratorBase
{
protected $attrName = 'char';
protected $disabledPasses = array();
protected $tagName = 'FP';
protected function setUp()
{
Expand All @@ -20,11 +21,23 @@ protected function setUp()
$tag->template
= '<xsl:value-of select="@' . \htmlspecialchars($this->attrName) . '"/>';
}
public function disablePass($passName)
{
$this->disabledPasses[] = $passName;
}
public function enablePass($passName)
{
foreach (\array_keys($this->disabledPasses, $passName, \true) as $k)
unset($this->disabledPasses[$k]);
}
public function asConfig()
{
return array(
$config = array(
'attrName' => $this->attrName,
'tagName' => $this->tagName
);
foreach ($this->disabledPasses as $passName)
$config['disable' . $passName] = \true;
return $config;
}
}
80 changes: 69 additions & 11 deletions src/Plugins/FancyPants/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,30 @@ var attrName = config.attrName,
hasDoubleQuote = (text.indexOf('"') >= 0),
tagName = config.tagName;

parseSingleQuotes();
parseSymbolsAfterDigits();
parseSingleQuotePairs();
parseDoubleQuotePairs();
parseDashesAndEllipses();
parseSymbolsInParentheses();
parseNotEqualSign();
parseGuillemets();
if (!config.disableQuotes)
{
parseSingleQuotes();
parseSingleQuotePairs();
parseDoubleQuotePairs();
}
if (!config.disableGuillemets)
{
parseGuillemets();
}
if (!config.disableMathSymbols)
{
parseNotEqualSign();
parseSymbolsAfterDigits();
parseFractions();
}
if (!config.disablePunctuation)
{
parseDashesAndEllipses();
}
if (!config.disableSymbols)
{
parseSymbolsInParentheses();
}

/**
* Add a fancy replacement tag
Expand Down Expand Up @@ -67,6 +83,46 @@ function parseDoubleQuotePairs()
}
}

/**
* Parse vulgar fractions
*/
function parseFractions()
{
if (text.indexOf('/') < 0)
{
return;
}

/** @const */
var map = {
'0/3' : "\u2189",
'1/10' : "\u2152",
'1/2' : "\u00BD",
'1/3' : "\u2153",
'1/4' : "\u00BC",
'1/5' : "\u2155",
'1/6' : "\u2159",
'1/7' : "\u2150",
'1/8' : "\u215B",
'1/9' : "\u2151",
'2/3' : "\u2154",
'2/5' : "\u2156",
'3/4' : "\u00BE",
'3/5' : "\u2157",
'3/8' : "\u215C",
'4/5' : "\u2158",
'5/6' : "\u215A",
'5/8' : "\u215D",
'7/8' : "\u215E"
};

var m, regexp = /\b(?:0\/3|1\/(?:[2-9]|10)|2\/[35]|3\/[458]|4\/5|5\/[68]|7\/8)\b/g;
while (m = regexp.exec(text))
{
addTag(+m['index'], m[0].length, map[m[0]]);
}
}

/**
* Parse guillemets-style quotation marks
*/
Expand All @@ -89,18 +145,20 @@ function parseGuillemets()

/**
* Parse the not equal sign
*
* Supports != and =/=
*/
function parseNotEqualSign()
{
if (text.indexOf('!=') < 0)
if (text.indexOf('!=') < 0 && text.indexOf('=/=') < 0)
{
return;
}

var m, regexp = /\b !=(?= \b)/g;
var m, regexp = /\b (?:!|=\/)=(?= \b)/g;
while (m = regexp.exec(text))
{
addTag(+m['index'] + 1, 2, "\u2260");
addTag(+m['index'] + 1, m[0].length - 1, "\u2260");
}
}

Expand Down
62 changes: 51 additions & 11 deletions src/Plugins/FancyPants/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ public function parse($text, array $matches)
$this->text = $text;
$this->hasSingleQuote = (\strpos($text, "'") !== \false);
$this->hasDoubleQuote = (\strpos($text, '"') !== \false);
$this->parseSingleQuotes();
$this->parseSymbolsAfterDigits();
$this->parseSingleQuotePairs();
$this->parseDoubleQuotePairs();
$this->parseDashesAndEllipses();
$this->parseSymbolsInParentheses();
$this->parseNotEqualSign();
$this->parseGuillemets();
if (empty($this->config['disableQuotes']))
{
$this->parseSingleQuotes();
$this->parseSingleQuotePairs();
$this->parseDoubleQuotePairs();
}
if (empty($this->config['disableGuillemets']))
$this->parseGuillemets();
if (empty($this->config['disableMathSymbols']))
{
$this->parseNotEqualSign();
$this->parseSymbolsAfterDigits();
$this->parseFractions();
}
if (empty($this->config['disablePunctuation']))
$this->parseDashesAndEllipses();
if (empty($this->config['disableSymbols']))
$this->parseSymbolsInParentheses();
unset($this->text);
}
protected function addTag($tagPos, $tagLen, $chr, $prio = 0)
Expand Down Expand Up @@ -56,6 +66,36 @@ protected function parseDoubleQuotePairs()
"\xE2\x80\x9D"
);
}
protected function parseFractions()
{
if (\strpos($this->text, '/') === \false)
return;
$map = array(
'1/4' => "\xC2\xBC",
'1/2' => "\xC2\xBD",
'3/4' => "\xC2\xBE",
'1/7' => "\xE2\x85\x90",
'1/9' => "\xE2\x85\x91",
'1/10' => "\xE2\x85\x92",
'1/3' => "\xE2\x85\x93",
'2/3' => "\xE2\x85\x94",
'1/5' => "\xE2\x85\x95",
'2/5' => "\xE2\x85\x96",
'3/5' => "\xE2\x85\x97",
'4/5' => "\xE2\x85\x98",
'1/6' => "\xE2\x85\x99",
'5/6' => "\xE2\x85\x9A",
'1/8' => "\xE2\x85\x9B",
'3/8' => "\xE2\x85\x9C",
'5/8' => "\xE2\x85\x9D",
'7/8' => "\xE2\x85\x9E",
'0/3' => "\xE2\x86\x89"
);
$regexp = '/\\b(?:0\\/3|1\\/(?:[2-9]|10)|2\\/[35]|3\\/[458]|4\\/5|5\\/[68]|7\\/8)\\b/S';
\preg_match_all($regexp, $this->text, $matches, \PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $m)
$this->addTag($m[1], \strlen($m[0]), $map[$m[0]]);
}
protected function parseGuillemets()
{
if (\strpos($this->text, '<<') === \false)
Expand All @@ -71,12 +111,12 @@ protected function parseGuillemets()
}
protected function parseNotEqualSign()
{
if (\strpos($this->text, '!=') === \false)
if (\strpos($this->text, '!=') === \false && \strpos($this->text, '=/=') === \false)
return;
$regexp = '/\\b !=(?= \\b)/';
$regexp = '/\\b (?:!|=\\/)=(?= \\b)/';
\preg_match_all($regexp, $this->text, $matches, \PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $m)
$this->addTag($m[1] + 1, 2, "\xE2\x89\xA0");
$this->addTag($m[1] + 1, \strlen($m[0]) - 1, "\xE2\x89\xA0");
}
protected function parseQuotePairs($regexp, $leftQuote, $rightQuote)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Litedown/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function addInlineCodeTags(left, right)
function addLinkTag(startTagPos, endTagPos, endTagLen, linkInfo)
{
// Give the link a slightly worse priority if this is a implicit reference and a slightly
// better priority if it's an explicit reference or an inline link or to give it precedence
// better priority if it's an explicit reference or an inline link or to give it precedence
// over possible BBCodes such as [b](https://en.wikipedia.org/wiki/B)
var priority = (endTagLen === 1) ? 1 : -1;

Expand Down
12 changes: 6 additions & 6 deletions src/Plugins/PipeTables/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function captureTables()
*/
function createBodyTags(startPos, endPos)
{
addTagPair('TBODY', startPos, 0, endPos, 0, -3);
addTagPair('TBODY', startPos, 0, endPos, 0, -103);
}

/**
Expand All @@ -162,11 +162,11 @@ function createCellTags(tagName, startPos, endPos, align)
var tag;
if (startPos === endPos)
{
tag = addSelfClosingTag(tagName, startPos, 0, -1);
tag = addSelfClosingTag(tagName, startPos, 0, -101);
}
else
{
tag = addTagPair(tagName, startPos, 0, endPos, 0, -1);
tag = addTagPair(tagName, startPos, 0, endPos, 0, -101);
}
if (align)
{
Expand All @@ -182,7 +182,7 @@ function createCellTags(tagName, startPos, endPos, align)
*/
function createHeadTags(startPos, endPos)
{
addTagPair('THEAD', startPos, 0, endPos, 0, -3);
addTagPair('THEAD', startPos, 0, endPos, 0, -103);
}

/**
Expand All @@ -204,7 +204,7 @@ function createIgnoreTag(pos, len)
*/
function createRowTags(startPos, endPos)
{
addTagPair('TR', startPos, 0, endPos, 0, -2);
addTagPair('TR', startPos, 0, endPos, 0, -102);
}

/**
Expand All @@ -225,7 +225,7 @@ function createSeparatorTag(row)
*/
function createTableTags(startPos, endPos)
{
tableTag = addTagPair('TABLE', startPos, 0, endPos, 0, -4);
tableTag = addTagPair('TABLE', startPos, 0, endPos, 0, -104);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Plugins/PipeTables/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,36 +105,36 @@ protected function captureTables()
}
protected function createBodyTags($startPos, $endPos)
{
$this->parser->addTagPair('TBODY', $startPos, 0, $endPos, 0, -3);
$this->parser->addTagPair('TBODY', $startPos, 0, $endPos, 0, -103);
}
protected function createCellTags($tagName, $startPos, $endPos, $align)
{
if ($startPos === $endPos)
$tag = $this->parser->addSelfClosingTag($tagName, $startPos, 0, -1);
$tag = $this->parser->addSelfClosingTag($tagName, $startPos, 0, -101);
else
$tag = $this->parser->addTagPair($tagName, $startPos, 0, $endPos, 0, -1);
$tag = $this->parser->addTagPair($tagName, $startPos, 0, $endPos, 0, -101);
if ($align)
$tag->setAttribute('align', $align);
}
protected function createHeadTags($startPos, $endPos)
{
$this->parser->addTagPair('THEAD', $startPos, 0, $endPos, 0, -3);
$this->parser->addTagPair('THEAD', $startPos, 0, $endPos, 0, -103);
}
protected function createIgnoreTag($pos, $len)
{
$this->tableTag->cascadeInvalidationTo($this->parser->addIgnoreTag($pos, $len, 1000));
}
protected function createRowTags($startPos, $endPos)
{
$this->parser->addTagPair('TR', $startPos, 0, $endPos, 0, -2);
$this->parser->addTagPair('TR', $startPos, 0, $endPos, 0, -102);
}
protected function createSeparatorTag(array $row)
{
$this->createIgnoreTag($row['pos'] - 1, 1 + \strlen($row['line']));
}
protected function createTableTags($startPos, $endPos)
{
$this->tableTag = $this->parser->addTagPair('TABLE', $startPos, 0, $endPos, 0, -4);
$this->tableTag = $this->parser->addTagPair('TABLE', $startPos, 0, $endPos, 0, -104);
}
protected function endTable()
{
Expand Down
21 changes: 19 additions & 2 deletions src/Renderers/XSLT.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public function __wakeup()
}
public function setParameter($paramName, $paramValue)
{
if (\strpos($paramValue, '"') !== \false
&& \strpos($paramValue, "'") !== \false)
if (\strpos($paramValue, '"') !== \false && \strpos($paramValue, "'") !== \false)
$paramValue = \str_replace('"', "\xEF\xBC\x82", $paramValue);
else
$paramValue = (string) $paramValue;
Expand All @@ -61,6 +60,8 @@ protected function renderRichText($xml)
$output = \str_replace('</embed>', '', $output);
if (\substr($output, -1) === "\n")
$output = \substr($output, 0, -1);
if (\strpos($output, "='") !== \false)
$output = $this->normalizeAttributes($output);
return $output;
}
protected function load()
Expand All @@ -72,4 +73,20 @@ protected function load()
$this->proc->importStylesheet($xsl);
}
}
protected function normalizeAttribute(array $m)
{
if ($m[0][0] === '"')
return $m[0];
return '"' . \str_replace('"', '&quot;', \substr($m[0], 1, -1)) . '"';
}
protected function normalizeAttributes($html)
{
return \preg_replace_callback('(<\\S++ [^>]++>)', array($this, 'normalizeElement'), $html);
}
protected function normalizeElement(array $m)
{
if (\strpos($m[0], "='") === \false)
return $m[0];
return \preg_replace_callback('((?:"[^"]*"|\'[^\']*\'))S', array($this, 'normalizeAttribute'), $m[0]);
}
}

0 comments on commit 0e65a12

Please sign in to comment.