markdown-notices.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. use RocketTheme\Toolbox\Event\Event;
  5. class MarkdownNoticesPlugin extends Plugin
  6. {
  7. protected $base_classes;
  8. protected $level_classes;
  9. /**
  10. * @return array
  11. */
  12. public static function getSubscribedEvents()
  13. {
  14. return [
  15. 'onMarkdownInitialized' => ['onMarkdownInitialized', 0],
  16. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
  17. ];
  18. }
  19. public function onMarkdownInitialized(Event $event)
  20. {
  21. $markdown = $event['markdown'];
  22. $markdown->addBlockType('!', 'Notices', true, false);
  23. $markdown->blockNotices = function($Line) {
  24. $this->level_classes = $this->config->get('plugins.markdown-notices.level_classes');
  25. $this->base_classes = $this->config->get('plugins.markdown-notices.base_classes');
  26. if (preg_match('/^(!{1,'.count($this->level_classes).'})[ ]+(.*)/', $Line['text'], $matches))
  27. {
  28. $level = strlen($matches[1]) - 1;
  29. // if we have more levels than we support
  30. if ($level > count($this->level_classes)-1)
  31. {
  32. return;
  33. }
  34. $text = $matches[2];
  35. $base_classes = (empty($this->base_classes)) ? '' : str_replace(',', ' ', $this->base_classes) . ' ';
  36. $Block = [
  37. 'element' => [
  38. 'name' => 'div',
  39. 'handler' => 'lines',
  40. 'attributes' => [
  41. 'class' => $base_classes . $this->level_classes[$level],
  42. ],
  43. 'text' => (array) $text,
  44. ],
  45. ];
  46. return $Block;
  47. }
  48. };
  49. $markdown->blockNoticesContinue = function($Line, array $Block) {
  50. if (isset($Block['interrupted']))
  51. {
  52. return;
  53. }
  54. if ($Line['text'][0] === '!' and preg_match('/^(!{1,'.count($this->level_classes).'})(.*)/', $Line['text'], $matches))
  55. {
  56. $Block['element']['text'] []= ltrim($matches[2]);
  57. return $Block;
  58. }
  59. };
  60. }
  61. public function onTwigSiteVariables()
  62. {
  63. if ($this->config->get('plugins.markdown-notices.built_in_css')) {
  64. $this->grav['assets']
  65. ->add('plugin://markdown-notices/assets/notices.css');
  66. }
  67. }
  68. }