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