monotone

monotone Mtn Source Tree

Root/contrib/ext_hooks_expand.pl

1#! /usr/bin/perl
2
3use warnings;
4use strict;
5use File::Basename;
6use File::Spec;
7
8my $here = dirname($0);
9my $there = File::Spec->catdir($here,File::Spec->updir());
10my $monotone_texi = File::Spec->catfile($there, "monotone.texi");
11my $ext_hooks_lua_in = File::Spec->catfile($here, "ext_hooks.lua.in");
12my $ext_hooks_lua = File::Spec->catfile($here, "ext_hooks.lua");
13
14# Format: $name => $arglist
15my %hooks = ();
16
17open INPUT, $monotone_texi ||
18 die "Couldn't open $monotone_texi for reading: $!\n";
19
20# First, read until we find the Hooks section
21while(<INPUT>) {
22 chomp;
23 last if (/^\s*\@section\s+Hooks\s*$/);
24}
25
26# Now, parse all the hooks docs until we find the Additional Lua Functions
27# section
28while(<INPUT>) {
29 chomp;
30 last if (/^\s*\@section\s+Additional\s+Lua\s+Functions\s*$/);
31 if (/^\s*\@item\s+(\w+)\s*\((\@var\{(\w+|\.\.\.)\}(,\s*\@var\{(\w+|\.\.\.)\})*)?\)\s*$/) {
32my $name = $1;
33my $arglist = "";
34if (defined $2) {
35 $arglist = join(", ",
36 map {
37my $x = $_;
38$x =~ s/^\s*\@var\{(\w+|\.\.\.)\}\s*$/$1/;
39$x;
40 } split(/,\s*/, $2));
41}
42$hooks{$name} = $arglist;
43 }
44}
45
46close INPUT;
47
48foreach my $n (sort keys %hooks) {
49 print $n," => \"",$hooks{$n},"\"\n";
50}
51
52open INPUT, $ext_hooks_lua_in ||
53 die "Couldn't open $ext_hooks_lua_in for reading: $!\n";
54open OUTPUT, ">$ext_hooks_lua" ||
55 die "Couldn't open $ext_hooks_lua for writing: $!\n";
56
57# Format : $name => [ list of strings ]
58my %expansions = ( HOOKS_LIST => [],
59 HOOKS_FUNCTIONS => []);
60
61foreach my $n (sort keys %hooks) {
62 push(@{$expansions{HOOKS_LIST}}, $n);
63 push(@{$expansions{HOOKS_FUNCTIONS}},
64"function $n ($hooks{$n})
65 local i
66 local fn
67 res_$n = nil
68 for i,fn in pairs(mtn_luahooks[\"$n\"]) do
69 res = fn($hooks{$n})
70 if res ~= nil then res_$n = res end
71 end
72 return res_$n
73end"
74 );
75}
76
77while(<INPUT>) {
78 chomp;
79 if (/^(.*)\@\@(\w+)\@\@(.*)$/) {
80my $prefix = $1;
81my $listname = $2;
82my $postfix = $3;
83if (defined $expansions{$listname}) {
84 foreach my $item (@{$expansions{$listname}}) {
85print OUTPUT $prefix,$item,$postfix,"\n";
86 }
87} else {
88 print OUTPUT $prefix,$listname,$postfix,"\n";
89}
90 } else {
91print OUTPUT $_,"\n";
92 }
93}
94
95close OUTPUT;
96close INPUT;

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status