aboutsummaryrefslogtreecommitdiff
path: root/bin/mkdocs-prep.pl
blob: efa71fbc320dc27f76257dc1645b670ad8cae344 (plain)
  1. #!/usr/bin/perl
  2. use v5.18; # needed for \h (horizontal whitespace) in regexes
  3. use strict;
  4. use warnings;
  5. use autodie;
  6. use List::Util qw(pairs);
  7. use Config::Tiny;
  8. use Path::Tiny;
  9. use Text::Hogan::Compiler;
  10. =head1 NAME
  11. mkdocs-prep - mkdocs preprocessor expanding inline hints and mustache tokens
  12. =head1 VERSION
  13. Version 0.02
  14. =head1 SYNOPSIS
  15. =head1 DESCRIPTION
  16. B<mkdocs-prep> prepares a markdown document for B<mkdocs> processing.
  17. The document is converted using inline hint section to a mustache template
  18. and resolved using an external set of single-word tokens.
  19. A document section named "Special strings" is parsed for token definitions
  20. where each line lists a single-word token, a colon, and default string.
  21. The section is then stripped,
  22. and all occurences of default strings are replaced with tokens
  23. wrapped with double curly brackets.
  24. Template tokens are gathered from external config file F<site.mk>,
  25. where each line lists a token, an equals sign, and replacement string.
  26. Each token in the document,
  27. wrapped with double curly brackets {{like_this}},
  28. is replaced (including brackets) with corresponding string.
  29. =cut
  30. my ( $infile, $outfile ) = @ARGV;
  31. my $config = Config::Tiny->new;
  32. $config = Config::Tiny->read( 'site.mk', 'utf8' );
  33. my $content = path($infile)->slurp_utf8;
  34. $content =~ s/^#+\s*Special strings\s*\n((?:\n|[^#\n][^\n]*\n)*)//m;
  35. my $section = $1 || '';
  36. my %defaults;
  37. while ( $section =~ /^(\w+)\h*:\h*(\w+(?:\h+\w+)*)/mg ) {
  38. my $token = $1;
  39. my $string = $2;
  40. $content =~ s/\Q$string\E/{{$token}}/g;
  41. };
  42. my $compiler = Text::Hogan::Compiler->new;
  43. my $template = $compiler->compile($content);
  44. path($outfile)->spew_utf8( $template->render( $config->{_} ) );
  45. 1;