markdown-notices.php 2.6 KB

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