mermaid.pl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #! /usr/bin/perl
  2. # Copyright (C) 2017 Alex Schroeder <alex@gnu.org>
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. AddModuleDescription('mermaid.pl', 'Mermaid for Diagrams');
  17. our ($bol, @MyRules, $MermaidCss, $MermaidJs, $HtmlHeaders, %Page);
  18. $MermaidCss = '/mermaid/mermaid.css';
  19. $MermaidJs = '/mermaid/mermaid.min.js';
  20. # When browsing a page containing mermaid markup, load the mermaid Javascript and CSS
  21. *MermaidOldBrowsePage = *BrowsePage;
  22. *BrowsePage = *MermaidNewBrowsePage;
  23. sub MermaidNewBrowsePage {
  24. my ($id) = @_;
  25. OpenPage($id);
  26. # Uses <mermaid> to render graphs
  27. if ($Page{text} =~ /<mermaid>/
  28. and $HtmlHeaders !~ /mermaid/) {
  29. $HtmlHeaders .= qq{
  30. <link type="text/css" rel="stylesheet" href="$MermaidCss" />
  31. <script type="text/javascript" src="$MermaidJs"></script>
  32. };
  33. }
  34. return MermaidOldBrowsePage(@_);
  35. }
  36. # When previewing an edit containing mermaid markup, load the mermaid Javascript
  37. # and CSS
  38. *MermaidOldDoEdit = *DoEdit;
  39. *DoEdit = *MermaidNewDoEdit;
  40. sub MermaidNewDoEdit {
  41. # Uses <mermaid> to render graphs
  42. if (GetParam('text') =~ /&lt;mermaid&gt;/
  43. and $HtmlHeaders !~ /mermaid/) {
  44. $HtmlHeaders = q{
  45. <link rel="stylesheet" href="/mermaid/mermaid.css" />
  46. <script type="text/javascript" src="/mermaid/mermaid.min.js"></script>
  47. };
  48. }
  49. return MermaidOldDoEdit(@_);
  50. }
  51. # And a formatting rule, of course.
  52. push(@MyRules, \&MermaidRule);
  53. sub MermaidRule {
  54. if ($bol && m/\G\&lt;mermaid\&gt;\n(.+?)\n\&lt;\/mermaid\&gt;/cgs) {
  55. return CloseHtmlEnvironments()
  56. . '<div class="mermaid">' . UrlDecode($1) . '</div>';
  57. }
  58. return undef;
  59. }