'; } function markupHeading($markup, $matches) { $level = max(1, 5 - mb_strlen($matches[1])); return sprintf('%s', $level, $markup->format($matches[2]), $level); } function markupText($markup, $matches, $contents) { static $tag = [ '**' => 'b', '%%' => 'i', '__' => 'u', '##' => 'tt', '++' => 'big', '--' => 'small', '>>' => 'sup', '<<' => 'sub', '{-' => 'strike' ]; $key = $matches[0]; return sprintf('<%s>%s', $tag[$key], $contents, $tag[$key]); } function markupSimple($markup, $matches) { static $tag = [ '*' => 'b', '%' => 'i', '_' => 'u', '#' => 'tt', '+' => 'big', '-' => 'small', '>' => 'sup', '<' => 'sub' ]; $key = $matches[0][0]; $text = str_replace($key, ' ', $matches[1]); return sprintf('<%s>%s', $tag[$key], $markup->quote($text), $tag[$key]); } function markupImage($markup, $matches) { if (mb_strlen($matches[1]) > 1) { $title = $markup->format(mb_substr($matches[1], 1)); } else { $title = ''; } return sprintf('', $markup->quote($matches[2]), $title); } function markupTable($markup, $matches) { $rows = explode("\n", rtrim($matches[0])); $result = ''; foreach ($rows as $row) { $cells = explode('|', trim(trim($row), '|')); $result .= ''; foreach ($cells as $cell) { $result .= ''; } $result .= ''; } $result .= '
'; $result .= $markup->format($cell); $result .= '
'; return $result; } function markupList($markup, $matches) { $rows = explode("\n", rtrim($matches[0])); $indent = 0; foreach ($rows as $row) { list($level, $text) = explode(' ', $row, 2); $level = mb_strlen($level); if ($indent < $level) { for (; $indent < $level; ++$indent) { $type = $row[$indent] == '=' ? 'ol' : 'ul'; $result .= sprintf('<%s>
  • ', $type); $types[] = $type; } } else { for (; $indent > $level; --$indent) { $result .= sprintf('
  • ', array_pop($types)); } $result .= '
  • '; } $result .= $markup->format($text); } for (; $indent > 0; --$indent) { $result .= sprintf('
  • ', array_pop($types)); } return $result; } function markupIndent($markup, $matches) { $text = preg_replace('/^ /m', '', $matches[0]); return sprintf('

    %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
    %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 { private function getMarkup(): TextFormat { $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'); return $markup; } public function testLine() { $input = "Test\n--\nTest"; $expected = "Test\n
    \nTest"; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testHeading() { $input = '!!%%Überschrift%%'; $expected = '

    Überschrift

    '; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testBoldItalics() { $input = '**some %%code%%**'; $expected = 'some code'; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testBigSmall() { $input = '++some --code--++'; $expected = 'some code'; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testSimpleBoldItalics() { $input = '*bold*text* %some%italics%'; $expected = 'bold text some italics'; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testMissingClose() { $input = '**missing %%close'; $expected = $input; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testCloseBeforeOpen() { $input = 'there is -}no markup{- here'; $expected = $input; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testIncorrectNesting() { $input = '** test %% test ** test %%'; $expected = '** test test ** test '; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testImage() { $input = '[img=Stud.IP-Logo]http://www.studip.de/logo.png'; $expected = ''; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testTable() { $input = "|Name|Matrikelnummer|Studiengang|\n|Max Mustermann|55555|Mathe Diplom|\n"; $expected = '' .'' .'' .'' .'' .'' .'' .'' .'' .'' .'' .'
    NameMatrikelnummerStudiengang
    Max Mustermann55555Mathe Diplom
    '; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testList() { $input = "- Einführung\n- Hauptteil\n-= Argument 1\n-= Argument 2\n- Schluss\n"; $expected = ''; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testIndent() { $input = " Ebene 1\n Ebene 2\n Ebene 2\n Ebene 1\n"; $expected = '

    ' ."Ebene 1\n" .'

    ' ."Ebene 2\n" ."Ebene 2\n" .'

    ' ."Ebene 1\n" .'

    '; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testNop() { $input = '[nop]**A**[quote]B[/quote]{-C-}[/nop]'; $expected = '**A**[quote]B[/quote]{-C-}'; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testPre() { $input = '[pre]**A**{-C-}[/pre]'; $expected = '
    AC
    '; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testQuote() { $input = '[quote=_Anonymous_]some text[/quote]'; $expected = '
    ' .'Anonymous hat geschrieben:
    some text' .'
    '; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testLink() { $input = '[Testlink]https://www.studip.de/'; $expected = 'Testlink'; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testMail() { $input = '[Mail]some.user@example.com'; $expected = 'Mail'; $this->assertEquals($expected, $this->getMarkup()->format($input)); } public function testSum() { $input = '(:sum(3\\4):)'; $expected = '7'; $this->assertEquals($expected, $this->getMarkup()->format($input)); } }