';
}
function markupHeading($markup, $matches)
{
$level = max(1, 5 - mb_strlen($matches[1]));
return sprintf('', $markup->quote($matches[2]), $title);
}
function markupTable($markup, $matches)
{
$rows = explode("\n", rtrim($matches[0]));
$result = '
| '; $result .= $markup->format($cell); $result .= ' | '; } $result .= '
%s
', $markup->format($text)); } function markupNop($markup, $matches) { return $markup->quote($matches[1]); } function markupPre($markup, $matches, $contents) { return sprintf('%s', $contents); } function markupCode($markup, $matches) { return highlight_string($matches[1], true); } function markupQuote($markup, $matches, $contents) { if (mb_strlen($matches[1]) > 1) { $title = sprintf(_('%s hat geschrieben:'), $markup->format(mb_substr($matches[1], 1))); } else { $title = _('Zitat:'); } return sprintf('
%s', $title, $contents); } function markupLink($markup, $matches) { if (mb_strlen($matches[1]) > 1) { $text = $markup->format(mb_substr($matches[1], 1, -1)); } else { $text = $markup->quote($matches[2]); } return sprintf('%s', $markup->quote($matches[2]), $text); } function markupMail($markup, $matches) { if (mb_strlen($matches[1]) > 1) { $text = $markup->format(mb_substr($matches[1], 1, -1)); } else { $text = $markup->quote($matches[2]); } return sprintf('%s', $markup->quote($matches[2]), $text); } function markupSum($markup, $matches) { return $matches[1] + $matches[2]; } class TextFormatTest extends \Codeception\Test\Unit { public function setUp(): void { $markup = new TextFormat(); $markup->addMarkup('line', '^--+$', NULL, 'markupLine'); $markup->addMarkup('heading', '^(!{1,4})([^\n]+)', NULL, 'markupHeading'); $markup->addMarkup('bold', '\*\*', '\*\*', 'markupText'); $markup->addMarkup('italics', '%%', '%%', 'markupText'); $markup->addMarkup('underline', '__', '__', 'markupText'); $markup->addMarkup('verb', '##', '##', 'markupText'); $markup->addMarkup('big', '\+\+', '\+\+', 'markupText'); $markup->addMarkup('small', '--', '--', 'markupText'); $markup->addMarkup('super', '>>', '>>', 'markupText'); $markup->addMarkup('sub', '<<', '<<', 'markupText'); $markup->addMarkup('strike', '\{-', '-\}', 'markupText'); $markup->addMarkup('simple_bold', '(?<=\s|^)\*(\S+)\*(?=\s|$)', NULL, 'markupSimple'); $markup->addMarkup('simple_italics', '(?<=\s|^)%(\S+)%(?=\s|$)', NULL, 'markupSimple'); $markup->addMarkup('simple_underline', '(?<=\s|^)_(\S+)_(?=\s|$)', NULL, 'markupSimple'); $markup->addMarkup('simple_verb', '(?<=\s|^)#(\S+)#(?=\s|$)', NULL, 'markupSimple'); $markup->addMarkup('simple_big', '(?<=\s|^)\+(\S+)\+(?=\s|$)', NULL, 'markupSimple'); $markup->addMarkup('simple_small', '(?<=\s|^)-(\S+)-(?=\s|$)', NULL, 'markupSimple'); $markup->addMarkup('simple_super', '(?<=\s|^)>(\S+)>(?=\s|$)', NULL, 'markupSimple'); $markup->addMarkup('simple_sub', '(?<=\s|^)<(\S+)<(?=\s|$)', NULL, 'markupSimple'); $markup->addMarkup('image', '\[img(=.*?)?\](\S+)', NULL, 'markupImage'); $markup->addMarkup('table', '(^\|[^\n]*\|[^\n]*\n)+', NULL, 'markupTable'); $markup->addMarkup('list', '(^[=-]+ [^\n]+\n)+', NULL, 'markupList'); $markup->addMarkup('indent', '(^ [^\n]+\n)+', NULL, 'markupIndent'); $markup->addMarkup('nop', '\[nop\](.*?)\[\/nop\]', NULL, 'markupNop'); $markup->addMarkup('pre', '\[pre\]', '\[\/pre\]', 'markupPre'); $markup->addMarkup('code', '\[code\](.*?)\[\/code\]', NULL, 'markupCode'); $markup->addMarkup('quote', '\[quote(=.*?)?\]', '\[\/quote\]', 'markupQuote'); $markup->addMarkup('link', '(\[.*?\])?\b(https?:\/\/\S+)', NULL, 'markupLink'); $markup->addMarkup('mail', '(\[.*?\])?\b([\w!#%+.-]+@[[:alnum:].-]+)', NULL, 'markupMail'); $markup->addMarkup('sum', '\(:sum\((\d+)\\\\(\d+)\):\)', NULL, 'markupSum'); $this->markup = $markup; } public function testLine() { $input = "Test\n--\nTest"; $expected = "Test\n
%s
';
$this->assertEquals($expected, $this->markup->format($input));
}
public function testTable()
{
$input = "|Name|Matrikelnummer|Studiengang|\n|Max Mustermann|55555|Mathe Diplom|\n";
$expected = '| Name | ' .'Matrikelnummer | ' .'Studiengang | ' .'
| Max Mustermann | ' .'55555 | ' .'Mathe Diplom | ' .'
' ."Ebene 1\n" .'
' ."Ebene 2\n" ."Ebene 2\n" .'
' ."Ebene 1\n" .''; $this->assertEquals($expected, $this->markup->format($input)); } public function testNop() { $input = '[nop]**A**[quote]B[/quote]{-C-}[/nop]'; $expected = '**A**[quote]B[/quote]{-C-}'; $this->assertEquals($expected, $this->markup->format($input)); } public function testPre() { $input = '[pre]**A**{-C-}[/pre]'; $expected = 'A'; $this->assertEquals($expected, $this->markup->format($input)); } public function testQuote() { $input = '[quote=_Anonymous_]some text[/quote]'; $expected = 'C
' .'Anonymous hat geschrieben:'; $this->assertEquals($expected, $this->markup->format($input)); } public function testLink() { $input = '[Testlink]https://www.studip.de/'; $expected = 'Testlink'; $this->assertEquals($expected, $this->markup->format($input)); } public function testMail() { $input = '[Mail]some.user@example.com'; $expected = 'Mail'; $this->assertEquals($expected, $this->markup->format($input)); } public function testSum() { $input = '(:sum(3\\4):)'; $expected = '7'; $this->assertEquals($expected, $this->markup->format($input)); } }
some text' .'