undo-usemod.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #! /usr/bin/perl -w
  2. # Copyright (C) 2015 Alex Schroeder <alex@gnu.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use utf8;
  17. use strict;
  18. use warnings;
  19. undef $/; # slurp
  20. my %index = ();
  21. my $verbose = '';
  22. sub write_file {
  23. my ($file, $data) = @_;
  24. return unless $data;
  25. open(my $fh, '>:utf8', $file) or die "Cannot write $file: $!";
  26. print $fh $data;
  27. close($fh);
  28. }
  29. sub replacement_block {
  30. my ($block, $pos, @no_go) = @_;
  31. while (@no_go) {
  32. my $first = shift @no_go;
  33. print "Is $pos between " . $first->[0] . " and " . $first->[1] . "?\n" if $verbose;
  34. return $block if $pos >= $first->[0] and $pos <= $first->[1];
  35. }
  36. return "[quote]\n" . join("\n", split(/ \n :+ \h? /x, $block)) . "[/quote]\n";
  37. }
  38. sub replacement {
  39. my ($block, $tag, $pos, @no_go) = @_;
  40. while (@no_go) {
  41. my $first = shift @no_go;
  42. print "Is $pos between " . $first->[0] . " and " . $first->[1] . "?\n" if $verbose;
  43. return $block if $pos >= $first->[0] and $pos <= $first->[1];
  44. }
  45. return $tag . $block . $tag;
  46. }
  47. sub translate_file {
  48. my ($data) = @_;
  49. my @no_go = ();
  50. while ($data =~ /( <nowiki>.*?<\/nowiki>
  51. | <code>.*?<\/code>
  52. | ^ <pre> (.*\n)+ <\/pre>
  53. | ^ {{{ (.*\n)+ }}} )/gmx) {
  54. push @no_go, [pos($data) - length $1, pos($data)];
  55. print "no go from " . $no_go[-1]->[0] . ".." . $no_go[-1]->[1] . " for $1\n" if $verbose;
  56. }
  57. # The problem is that these replacements don't adjust @no_go! Perhaps it is good enough?
  58. my $subs = '';
  59. $subs = $subs || $data =~ s/ ''' (.*?) ''' /replacement($1, '**', pos($data), @no_go)/gxe;
  60. $subs = $subs || $data =~ s/ '' (.*?) '' /replacement($1, '\/\/', pos($data), @no_go)/gxe;
  61. $subs = $data =~ s/ ^ :+ \h? ( .* \n (?: .+ \n ) * ) /replacement_block($1, pos($data), @no_go)/gmxe;
  62. return $data if $subs;
  63. }
  64. sub read_file {
  65. my $file = shift;
  66. open(my $fh, '<:utf8', $file) or die "Cannot read $file: $!";
  67. my $data = <$fh>;
  68. close($fh);
  69. return $data;
  70. }
  71. sub main {
  72. my ($dir) = @_;
  73. mkdir($dir . '-new') or die "Cannot create $dir-new: $!";
  74. print "Indexing files\n";
  75. foreach my $file (glob("$dir/.* $dir/*")) {
  76. next unless $file =~ /$dir\/(.+)/;
  77. my $id = $1;
  78. next if $id eq ".";
  79. next if $id eq "..";
  80. $index{$id} = 1;
  81. }
  82. print "Converting files\n";
  83. foreach my $id (sort keys %index) {
  84. # this is where you debug a particular page
  85. # $verbose = $id eq '2014-12-18_Emacs_Wiki_Migration';
  86. write_file("$dir-new/$id", translate_file(read_file("$dir/$id")));
  87. }
  88. }
  89. use Getopt::Long;
  90. my $dir = 'raw';
  91. my $help = '';
  92. GetOptions ("dir=s" => \$dir,
  93. "help" => \$help);
  94. if ($help) {
  95. print qq{
  96. Usage: $0 [--dir=DIR]
  97. You need to use the raw.pl script to create a directory full of raw
  98. wiki text files.
  99. --dir=DIR is where the raw wiki text files are. Default: raw. The
  100. converted files will be stored in DIR-new, ie. in raw-new by
  101. default.
  102. Example: $0 --dir=~/alexschroeder/raw
  103. }
  104. } else {
  105. main ($dir);
  106. }