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