git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/24] Documentation: Add variable-substitution script

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 26, 2010, 21:49 UTC
Message-ID
<AANLkTinM8w4MiwdNmiUzLDRvR-NqSxNn0I1avQDt9QtR@mail.gmail.com>
In-Reply-To
<m3wrsikkol.fsf@localhost.localdomain>
On Mon, Jul 26, 2010 at 21:17, Jakub Narebski <jnareb@gmail.com> wrote:

Well caught. Those were on me, here's the fixed up version (in my branch, which Thomas can pull in to his):

#!/usr/bin/perl use 5.006002; use strict; use warnings; use Getopt::Long;

Getopt::Long::Configure qw/ pass_through /;
my $rc = GetOptions(
	"varlist=s" => \my $varlist,
	"input=s"	=> \my $input,
	"output=s"	=> \my $output,
);
if (!$rc or (!-r $varlist or !-r $input)) {
	print "$0 --varlist=<varlist> --input=<in> --output=<out>\n";
	exit 1;
}

my $vars = read_varlist($varlist); substitute_variables($vars, $input, $output); exit 0;

sub read_varlist {
	my ($file) = @_;
	open my $fh, "<", $file or die "Can't open $file: $!";
	my %vars;
	my ($v, $last_v);
	my $in_block = 0;
	while (<$fh>) {
		if (/^(\S+)::/) {
			$v = lc $1;
			$in_block = 0;
			push @{$vars{$v}}, $_;
		} elsif (/^$/ && !$in_block) {
			if (defined $last_v && !$#{$vars{$last_v}}) {
				$vars{$last_v} = $vars{$v};
			}
			$last_v = $v;
		} elsif (defined $v) {
			push @{$vars{$v}}, $_;
			$in_block = !$in_block if /^--$/;
		}
	}
	close $fh or die "Closing $file failed: $!";
	return \%vars
}
sub substitute_variables {
	my ($varlist, $in, $out) = @_;
	open my $infh, "<", $in or die "Can't open $in: $!";
	open my $outfh, ">", $out or die "Can't open $out: $!";
	while (<$infh>) {
		if (/^\@\@CONFIG\((\S+)\)\@\@$/) {
			my $v = lc $1;
			die "Key $v not documented" unless exists $varlist->{$v};
			print $outfh @{$varlist->{$v}};
			print $outfh "\n";
		} else {
			print $outfh $_;
		}
	}
	close $infh or die "Closing $in failed: $!";
	close $outfh or die "Closing $out failed: $!";
	return;
}
Previous: Jakub NarebskiNext: Thomas Rast
Message 6 of 14 in “Documentation: refactor config variable descriptions”
  1. 0/24 Documentation: refactor config variable descriptionsThomas Rast, Jul 26, 2010
  2. 2/24 Documentation: Add variable-substitution scriptThomas Rast, Jul 26, 2010
  3. Jonathan NiederJul 26, 2010
  4. Ævar Arnfjörð BjarmasonJul 26, 2010
  5. Jakub NarebskiJul 26, 2010
  6. Ævar Arnfjörð BjarmasonJul 26, 2010
  7. 24/24 Documentation: include configuration options in manpagesThomas Rast, Jul 26, 2010
  8. Jonathan NiederJul 26, 2010
  9. Ævar Arnfjörð BjarmasonJul 26, 2010
  10. Ævar Arnfjörð BjarmasonJul 26, 2010
  11. Jonathan NiederJul 26, 2010
  12. Ævar Arnfjörð BjarmasonJul 26, 2010
  13. Sverre RabbelierJul 27, 2010
  14. Junio C HamanoJul 28, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.