Skip to content

Commit

Permalink
[jan] Add 'html' hint to Simplemarkup filter to indicate the input fo…
Browse files Browse the repository at this point in the history
…rmat.

Also fix a few more edge cases with full line matching.
  • Loading branch information
yunosh committed Feb 12, 2015
1 parent b86cf58 commit 3288964
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
36 changes: 23 additions & 13 deletions framework/Text_Filter/lib/Horde/Text/Filter/Simplemarkup.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,36 @@ class Horde_Text_Filter_Simplemarkup extends Horde_Text_Filter_Base
*/
public function getPatterns()
{
$startOfLine = '((?:^|<br(?:\s*/)?>)(?:\s|&nbsp;)*)';
$endOfLine = '((?:\s|&nbsp;)*(?:$|<br|\.))';
$startOfWord = '(^|\s|&nbsp;|<br(?:\s*/)?>)';
$endOfWord = '($|\s|&nbsp;|<br|\.)';
if (!isset($this->_params['html'])) {
$linebreak = '\n|<br(?:\s*/)?>';
$whitespace = '\s|&nbsp;';
} elseif ($this->_params['html']) {
$linebreak = '<br(?:\s*/)?>';
$whitespace = '&nbsp;';
} else {
$linebreak = '\n';
$whitespace = '\s';
}
$startOfLine = '((?:^|' . $linebreak . ')(?:' . $whitespace . ')*)';
$endOfLine = '(?=(?:' . $whitespace . ')*(?:$|\.|' . $linebreak . '))';
$startOfWord = '(^|' . $whitespace . '|' . $linebreak . ')';
$endOfWord = '(?=$|\.|' . $whitespace . '|' . $linebreak . ')';

return array('regexp' => array(
// Bold.
'!' . $startOfLine . '(\*[^*]+\*)' . $endOfLine .
'|' . $startOfWord . '(\*[^*\s]+\*)' . $endOfWord . '!i'
=> '$1$4<strong>$2$5</strong>$3$6',
'#' . $startOfLine . '(\*(?:[^*](?!$|' . $linebreak . '))+\*)' . $endOfLine .
'|' . $startOfWord . '(\*[^*\s]+\*)' . $endOfWord . '#i'
=> '$1$3<strong>$2$4</strong>',

// Underline.
'!' . $startOfLine . '(_[^_]+_)' . $endOfLine .
'|' . $startOfWord . '(_[^_\s]+_)' . $endOfWord . '!i'
=> '$1$4<u>$2$5</u>$3$6',
'#' . $startOfLine . '(_(?:[^*](?!$|' . $linebreak . '))+_)' . $endOfLine .
'|' . $startOfWord . '(_[^_\s]+_)' . $endOfWord . '#i'
=> '$1$3<u>$2$4</u>',

// Italic.
'!' . $startOfLine . '(/[^/]+/)' . $endOfLine .
'|' . $startOfWord . '(/[^/\s]+/)' . $endOfWord . '!i'
=> '$1$4<em>$2$5</em>$3$6',
'#' . $startOfLine . '(/(?:[^*](?!$|' . $linebreak . '))+/)' . $endOfLine .
'|' . $startOfWord . '(/[^/\s]+/)' . $endOfWord . '#i'
=> '$1$3<em>$2$4</em>',
));
}

Expand Down
2 changes: 2 additions & 0 deletions framework/Text_Filter/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [jan] Add &apos;html&apos; hint to Simplemarkup filter to indicate the input format.
* [jan] Match simple markup spanning a complete line too.
</notes>
<contents>
Expand Down Expand Up @@ -1038,6 +1039,7 @@ Initial release as a PEAR package
<date>2015-01-29</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [jan] Add &apos;html&apos; hint to Simplemarkup filter to indicate the input format.
* [jan] Match simple markup spanning a complete line too.
</notes>
</release>
Expand Down
62 changes: 47 additions & 15 deletions framework/Text_Filter/test/Horde/Text/Filter/SimplemarkupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class Horde_Text_Filter_SimplemarkupTest extends PHPUnit_Framework_TestCase
/**
* @dataProvider markupExamples
*/
public function testSimplemarkup($markup, $html)
public function testSimplemarkup($markup, $output, $html)
{
$this->assertEquals(
$html,
Horde_Text_Filter::filter($markup, 'simplemarkup')
$output,
Horde_Text_Filter::filter($markup, 'simplemarkup', array('html' => $html))
);
}

Expand All @@ -41,69 +41,101 @@ public function markupExamples()
// Simple examples.
array(
'some *bold* text',
'some <strong>*bold*</strong> text'
'some <strong>*bold*</strong> text',
false,
),
array(
'some _underlined_ text',
'some <u>_underlined_</u> text'
'some <u>_underlined_</u> text',
false,
),
array(
'some /italic/ text',
'some <em>/italic/</em> text'
'some <em>/italic/</em> text',
false,
),

// Edge cases.
array(
'*bold* at start',
'<strong>*bold*</strong> at start'
'<strong>*bold*</strong> at start',
false,
),
array(
'at end *bold*',
'at end <strong>*bold*</strong>'
'at end <strong>*bold*</strong>',
false,
),
array(
'full stop *bold*.',
'full stop <strong>*bold*</strong>.'
'full stop <strong>*bold*</strong>.',
false,
),
array(
'some&nbsp;*bold*&nbsp;text',
'some&nbsp;<strong>*bold*</strong>&nbsp;text'
'some&nbsp;<strong>*bold*</strong>&nbsp;text',
true,
),
array(
'some<br>*bold*<br />text more<br />*bold*<br>text',
'some<br><strong>*bold*</strong><br />text more<br /><strong>*bold*</strong><br>text'
'some<br><strong>*bold*</strong><br />text more<br /><strong>*bold*</strong><br>text',
true,
),

// Whole phrase matching.
array(
'*some bold text*',
'<strong>*some bold text*</strong>'
'<strong>*some bold text*</strong>',
false,
),
array(
' *some bold text* ',
' <strong>*some bold text*</strong> '
' <strong>*some bold text*</strong> ',
false,
),
array(
'&nbsp;*some bold&nbsp;text*&nbsp;',
'&nbsp;<strong>*some bold&nbsp;text*</strong>&nbsp;'
'&nbsp;<strong>*some bold&nbsp;text*</strong>&nbsp;',
true,
),
array(
'<br>*some bold text*<br />',
'<br><strong>*some bold text*</strong><br />'
'<br><strong>*some bold text*</strong><br />',
true,
),

// No matching.
array(
'some *bold**bold* text',
'some *bold**bold* text',
false,
),
array(
'some *bold*bold* text',
'some *bold*bold* text',
false,
),
array(
'some bold*bold text',
'some bold*bold text',
false,
),

// More edge cases.
array(
"* some bullet point\n* ...\n",
"* some bullet point\n* ...\n",
false,
),
array(
"* some bullet point<br>* ...<br>",
"* some bullet point<br>* ...<br>",
true,
),
array(
'some *bold* *text*.',
'some <strong>*bold*</strong> <strong>*text*</strong>.',
false,
),
);
}
Expand Down

0 comments on commit 3288964

Please sign in to comment.