summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: 0e2bd4e22020b909de8bf3098f563e3d7772f672 (plain)
  1. #!/usr/bin/perl -i
  2. use warnings;
  3. use strict;
  4. use IkiWiki;
  5. use HTML::Entities;
  6. my $regex = qr{
  7. (\\?) # 1: escape?
  8. \[\[(!?) # directive open; 2: optional prefix
  9. ([-\w]+) # 3: command
  10. ( # 4: the parameters (including initial whitespace)
  11. \s+
  12. (?:
  13. (?:[-\w]+=)? # named parameter key?
  14. (?:
  15. """.*?""" # triple-quoted value
  16. |
  17. "[^"]+" # single-quoted value
  18. |
  19. [^\s\]]+ # unquoted value
  20. )
  21. \s* # whitespace or end
  22. # of directive
  23. )
  24. *) # 0 or more parameters
  25. \]\] # directive closed
  26. }sx;
  27. sub handle_directive {
  28. my $escape = shift;
  29. my $prefix = shift;
  30. my $directive = shift;
  31. my $args = shift;
  32. if (length $escape) {
  33. return "${escape}[[${prefix}${directive}${args}]]"
  34. }
  35. if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
  36. $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  37. }
  38. return "[[!${directive}${args}]]"
  39. }
  40. sub prefix_directives {
  41. $/=undef; # process whole files at once
  42. while (<>) {
  43. s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  44. print;
  45. }
  46. }
  47. sub indexdb {
  48. $config{wikistatedir}=shift()."/.ikiwiki";
  49. if (! defined $config{wikistatedir}) {
  50. usage();
  51. }
  52. if (! IkiWiki::oldloadindex()) {
  53. die "failed to load index\n";
  54. }
  55. if (! IkiWiki::saveindex()) {
  56. die "failed to save indexdb\n"
  57. }
  58. if (! IkiWiki::loadindex()) {
  59. die "transition failed, cannot load new indexdb\n";
  60. }
  61. if (! unlink("$config{wikistatedir}/index")) {
  62. die "unlink failed: $!\n";
  63. }
  64. }
  65. sub usage {
  66. print STDERR "Usage: ikiwiki-transition type ...\n";
  67. print STDERR "Currently supported transition subcommands:\n";
  68. print STDERR " prefix_directives file\n";
  69. print STDERR " indexdb srcdir\n";
  70. exit 1;
  71. }
  72. usage() unless @ARGV;
  73. my $mode=shift;
  74. if ($mode eq 'prefix_directives') {
  75. prefix_directives(@ARGV);
  76. }
  77. elsif ($mode eq 'indexdb') {
  78. indexdb(@ARGV);
  79. }
  80. else {
  81. usage();
  82. }
  83. package IkiWiki;
  84. sub oldloadindex {
  85. %oldrenderedfiles=%pagectime=();
  86. if (! $config{rebuild}) {
  87. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  88. %destsources=%renderedfiles=%pagecase=%pagestate=();
  89. }
  90. open (my $in, "<", "$config{wikistatedir}/index") || return;
  91. while (<$in>) {
  92. chomp;
  93. my %items;
  94. $items{link}=[];
  95. $items{dest}=[];
  96. foreach my $i (split(/ /, $_)) {
  97. my ($item, $val)=split(/=/, $i, 2);
  98. push @{$items{$item}}, decode_entities($val);
  99. }
  100. next unless exists $items{src}; # skip bad lines for now
  101. my $page=pagename($items{src}[0]);
  102. if (! $config{rebuild}) {
  103. $pagesources{$page}=$items{src}[0];
  104. $pagemtime{$page}=$items{mtime}[0];
  105. $oldlinks{$page}=[@{$items{link}}];
  106. $links{$page}=[@{$items{link}}];
  107. $depends{$page}=$items{depends}[0] if exists $items{depends};
  108. $destsources{$_}=$page foreach @{$items{dest}};
  109. $renderedfiles{$page}=[@{$items{dest}}];
  110. $pagecase{lc $page}=$page;
  111. foreach my $k (grep /_/, keys %items) {
  112. my ($id, $key)=split(/_/, $k, 2);
  113. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  114. }
  115. }
  116. $oldrenderedfiles{$page}=[@{$items{dest}}];
  117. $pagectime{$page}=$items{ctime}[0];
  118. }
  119. return close($in);
  120. }