#!/usr/bin/perl -w use strict; $|=1; die unless @ARGV == 1; my ($file) = @ARGV; die if $file =~ m{^-}; open STDIN, "<", $file or die "$file $!"; open L, "expand -t4 |" or die $!; our (@l) = ; $?=0; $!=0; close L or die "$? $!"; unshift @l, ''; my $block_start = 0; my $heading; foreach my $i (0..$#l) { $_ = $l[$i]; if (m{^\`\`\`}) { if ($block_start) { $block_start = 0; } else { $block_start = $i + 1; } } if (!$block_start && m{^\#+ (\S.*)\n}) { $heading = $1; } if (defined($block_start) && m{^(total +)(\d+)\s}) { my ($lhs, $exp) = ($1, $2); my $got = 0; foreach my $j ($block_start .. ($i-1)) { $_ = $l[$j]; my $earlier = length($lhs) - 1; next if m{^\#}; next unless length > $earlier+1; die "$file:$j: ($i): $_ ?" unless m{^.{$earlier} (\S+)\s}; my $here = $1; next unless $here =~ m{^[-+]?\d+$}; $got += $here; } my $delta = $exp - $got; print "$file: checking total: $heading\n"; if ($delta) { print STDERR "$file:$i: expected $exp but got $got, spend $delta\n"; } next; } }