summaryrefslogtreecommitdiff
path: root/ikiwiki-transition
blob: 1fd23cec55edcd56e2e854d23948ba71f713305c (plain)
  1. #!/usr/bin/perl -i
  2. use warnings;
  3. use strict;
  4. my $regex = qr{
  5. (\\?) # 1: escape?
  6. \[\[(!?) # directive open; 2: optional prefix
  7. ([-\w]+) # 3: command
  8. ( # 4: the parameters (including initial whitespace)
  9. \s+
  10. (?:
  11. (?:[-\w]+=)? # named parameter key?
  12. (?:
  13. """.*?""" # triple-quoted value
  14. |
  15. "[^"]+" # single-quoted value
  16. |
  17. [^\s\]]+ # unquoted value
  18. )
  19. \s* # whitespace or end
  20. # of directive
  21. )
  22. *) # 0 or more parameters
  23. \]\] # directive closed
  24. }sx;
  25. sub handle_directive {
  26. my $escape = shift;
  27. my $prefix = shift;
  28. my $directive = shift;
  29. my $args = shift;
  30. if (length $escape) {
  31. return "${escape}[[${prefix}${directive}${args}]]"
  32. }
  33. if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
  34. $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  35. }
  36. return "[[!${directive}${args}]]"
  37. }
  38. sub prefix_directives {
  39. $/=undef; # process whole files at once
  40. while (<>) {
  41. s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  42. print;
  43. }
  44. }
  45. sub usage {
  46. print STDERR "Usage: ikiwiki-transition type file ...\n";
  47. print STDERR "Currently supported transition types:\n";
  48. print STDERR " prefix_directives\n";
  49. exit 1;
  50. }
  51. usage() unless @ARGV;
  52. my $mode=shift;
  53. if ($mode eq 'prefix_directives') {
  54. prefix_directives(@ARGV);
  55. }
  56. else {
  57. usage();
  58. }