summaryrefslogtreecommitdiff
path: root/ikiwiki-prefix-directives
blob: f14fe339fdffde3095e3fb1acd7107aa088aa6f2 (plain)
  1. #!/usr/bin/perl -i
  2. use warnings;
  3. use strict;
  4. undef $/; # process whole files at once
  5. my $regex = qr{
  6. (\\?) # 1: escape?
  7. \[\[(!?) # directive open; 2: optional prefix
  8. ([-\w]+) # 3: command
  9. ( # 4: the parameters (including initial whitespace)
  10. \s+
  11. (?:
  12. (?:[-\w]+=)? # named parameter key?
  13. (?:
  14. """.*?""" # triple-quoted value
  15. |
  16. "[^"]+" # single-quoted value
  17. |
  18. [^\s\]]+ # unquoted value
  19. )
  20. \s* # whitespace or end
  21. # of directive
  22. )
  23. *) # 0 or more parameters
  24. \]\] # directive closed
  25. }sx;
  26. sub handle_directive {
  27. my $escape = shift;
  28. my $prefix = shift;
  29. my $directive = shift;
  30. my $args = shift;
  31. if (length $escape) {
  32. return "${escape}[[${prefix}${directive}${args}]]"
  33. }
  34. if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
  35. $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  36. }
  37. return "[[!${directive}${args}]]"
  38. }
  39. while (<>) {
  40. s{$regex}{handle_directive($1, $2, $3, $4)}eg;
  41. print;
  42. }