summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: 0177f98a9263917d6554d880fd150d5f1fe44d12 (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. # A slightly modified version of the old loadindex function.
  85. sub oldloadindex {
  86. %oldrenderedfiles=%pagectime=();
  87. if (! $config{rebuild}) {
  88. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  89. %destsources=%renderedfiles=%pagecase=%pagestate=();
  90. }
  91. open (my $in, "<", "$config{wikistatedir}/index") || return;
  92. while (<$in>) {
  93. chomp;
  94. my %items;
  95. $items{link}=[];
  96. $items{dest}=[];
  97. foreach my $i (split(/ /, $_)) {
  98. my ($item, $val)=split(/=/, $i, 2);
  99. push @{$items{$item}}, decode_entities($val);
  100. }
  101. next unless exists $items{src}; # skip bad lines for now
  102. my $page=pagename($items{src}[0]);
  103. if (! $config{rebuild}) {
  104. $pagesources{$page}=$items{src}[0];
  105. $pagemtime{$page}=$items{mtime}[0];
  106. $oldlinks{$page}=[@{$items{link}}];
  107. $links{$page}=[@{$items{link}}];
  108. $depends{$page}=$items{depends}[0] if exists $items{depends};
  109. $destsources{$_}=$page foreach @{$items{dest}};
  110. $renderedfiles{$page}=[@{$items{dest}}];
  111. $pagecase{lc $page}=$page;
  112. foreach my $k (grep /_/, keys %items) {
  113. my ($id, $key)=split(/_/, $k, 2);
  114. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  115. }
  116. }
  117. $oldrenderedfiles{$page}=[@{$items{dest}}];
  118. $pagectime{$page}=$items{ctime}[0];
  119. }
  120. # saveindex relies on %hooks being populated, else it won't save
  121. # the page state owned by a given hook. But no plugins are loaded
  122. # by this program, so populate %hooks with all hook ids that
  123. # currently have page state.
  124. foreach my $page (keys %pagemtime) {
  125. foreach my $id (keys %{$pagestate{$page}}) {
  126. $hooks{_dummy}{$id}=1;
  127. }
  128. }
  129. return close($in);
  130. }