EmphStrongTrait.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 emphasizes and strong elements
  10. */
  11. trait EmphStrongTrait
  12. {
  13. /**
  14. * Parses empathized and strong elements.
  15. * @marker _
  16. * @marker *
  17. */
  18. protected function parseEmphStrong($text)
  19. {
  20. $marker = $text[0];
  21. if (!isset($text[1])) {
  22. return [['text', $text[0]], 1];
  23. }
  24. if ($marker == $text[1]) { // strong
  25. if ($marker == '*' && preg_match('/^[*]{2}((?:[^*]|[*][^*]*[*])+?)[*]{2}(?![*]{2})/s', $text, $matches) ||
  26. $marker == '_' && preg_match('/^__((?:[^_]|_[^_]*_)+?)__(?!__)/us', $text, $matches)) {
  27. return [
  28. [
  29. 'strong',
  30. $this->parseInline($matches[1]),
  31. ],
  32. strlen($matches[0])
  33. ];
  34. }
  35. } else { // emph
  36. if ($marker == '*' && preg_match('/^[*]((?:[^*]|[*][*][^*]+?[*][*])+?)[*](?![*][^*])/s', $text, $matches) ||
  37. $marker == '_' && preg_match('/^_((?:[^_]|__[^_]*__)+?)_(?!_[^_])\b/us', $text, $matches)) {
  38. return [
  39. [
  40. 'emph',
  41. $this->parseInline($matches[1]),
  42. ],
  43. strlen($matches[0])
  44. ];
  45. }
  46. }
  47. return [['text', $text[0]], 1];
  48. }
  49. protected function renderStrong($block)
  50. {
  51. return '<strong>' . $this->renderAbsy($block[1]) . '</strong>';
  52. }
  53. protected function renderEmph($block)
  54. {
  55. return '<em>' . $this->renderAbsy($block[1]) . '</em>';
  56. }
  57. }