markdown-converter.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #! /usr/bin/perl
  2. # Copyright (C) 2018 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. use utf8;
  17. AddModuleDescription('markdown-converter.pl', 'Markdown Convert');
  18. our (%Action, @MyAdminCode, $q, $OpenPageName, %Page, @MyRules);
  19. push(@MyAdminCode, \&MarkdownConvertMenu);
  20. sub MarkdownConvertMenu {
  21. my ($id, $menuref, $restref) = @_;
  22. my $name = $id;
  23. $name =~ s/_/ /g;
  24. if ($id) {
  25. push(@$menuref, ScriptLink('action=convert;id=' . $id, Ts('Help convert %s to Markdown', $name), 'convert'));
  26. }
  27. push(@$menuref, ScriptLink('action=conversion-candidates', Ts('List all non-Markdown pages'), 'convert'));
  28. }
  29. $Action{convert} = \&MarkdownConvert;
  30. # some text that doesn't start and end with a space, or just one non-space
  31. sub MarkdownConvertString {
  32. my $c = shift;
  33. return qr"([^\\$c \n][^\\$c\n]*[^\\$c \n]|[^\\$c \n])";
  34. }
  35. sub MarkdownConvert {
  36. my $id = GetParam('id', '');
  37. ValidIdOrDie($id);
  38. print GetHeader('', Ts('Converting %s', $id), '');
  39. $_ = GetPageContent($id);
  40. s/^\{\{\{((?:.*\n)+?)\}\}\}$/```$1```/gm;
  41. s/\{\{\{(.+?)\}\}\}/`$1`/gm;
  42. my $s = MarkdownConvertString('*');
  43. s/\*$s\*/**$1**/g;
  44. # avoid URL schemas like http://example.org
  45. $s = MarkdownConvertString('/');
  46. s#(?<!:/)/$s/#*$1*#g;
  47. s#(?<!:)//$s//#*$1*#g;
  48. s/^# /1. /gm;
  49. s/##(.*?)##/`$1`/g;
  50. s/^(=+) *(.*?) ?=*$/'#' x length($1) . ' ' . $2/gme;
  51. s!\[(https?://\S+) (.*?)\]![$2]($1)!g;
  52. return DoEdit($id, "#MARKDOWN\n" . $_, 1); # preview
  53. }
  54. $Action{'conversion-candidates'} = \&MarkdownConversionCandidates;
  55. sub MarkdownConversionCandidates {
  56. # from Search
  57. print GetHeader('', Ts('Candidates for Conversion to Markdown'));
  58. print $q->start_div({-class=>'content'});
  59. print $q->start_ol();
  60. # from SearchTitleAndBody
  61. my $regex = qr'^(?!#MARKDOWN)';
  62. foreach my $id (Filtered($regex, AllPagesList())) {
  63. my $name = NormalToFree($id);
  64. my ($text) = PageIsUploadedFile($id); # set to mime-type if this is an uploaded file
  65. local ($OpenPageName, %Page); # this is local!
  66. if (not $text) { # not uploaded file, therefore allow searching of page body
  67. OpenPage($id); # this opens a page twice if it is not uploaded, but that's ok
  68. if ($Page{text} =~ /$regex/) {
  69. my $action = 'action=convert;id=' . UrlEncode($id);
  70. my $name = NormalToFree($id);
  71. print $q->li(GetPageLink($id, $name) . ' – '
  72. . ScriptLink($action, Ts('Help convert %s to Markdown', $name)));
  73. }
  74. }
  75. }
  76. print $q->end_ol();
  77. print $q->end_div();
  78. PrintFooter();
  79. }
  80. push(@MyRules, \&MarkdownConvertRule);
  81. sub MarkdownConvertRule {
  82. if (pos == 0 and /\G#MARKDOWN\n/gc) {
  83. return '';
  84. }
  85. return;
  86. }