46 lines
905B

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2014 Carsten Brandt
  4. * @license https://github.com/cebe/markdown/blob/master/LICENSE
  5. * @link https://github.com/cebe/markdown#readme
  6. */
  7. namespace cebe\markdown\inline;
  8. /**
  9. * Adds inline code elements
  10. */
  11. trait CodeTrait
  12. {
  13. /**
  14. * Parses an inline code span `` ` ``.
  15. * @marker `
  16. */
  17. protected function parseInlineCode($text)
  18. {
  19. if (preg_match('/^(``+)\s(.+?)\s\1/s', $text, $matches)) { // code with enclosed backtick
  20. return [
  21. [
  22. 'inlineCode',
  23. $matches[2],
  24. ],
  25. strlen($matches[0])
  26. ];
  27. } elseif (preg_match('/^`(.+?)`/s', $text, $matches)) {
  28. return [
  29. [
  30. 'inlineCode',
  31. $matches[1],
  32. ],
  33. strlen($matches[0])
  34. ];
  35. }
  36. return [['text', $text[0]], 1];
  37. }
  38. protected function renderInlineCode($block)
  39. {
  40. return '<code>' . htmlspecialchars($block[1], ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</code>';
  41. }
  42. }