moj-2024/wyk/helpers/fix-intro.pl

62 lines
1.0 KiB
Perl
Executable File

#!/usr/bin/perl
use strict;
binmode(STDERR, ':utf8');
my $intro_file = 'helpers/intro';
my $intro_template = 'helpers/intro-template';
my $notebook_file = $ARGV[0];
my $number;
if (($number) = ($notebook_file =~ m{^(\d+)_})) {
} else {
die "????";
}
my $org_file = $notebook_file;
$org_file =~ s{\.ipynb}{\.org};
my $title = undef;
open(my $org_fh, '<', $org_file);
binmode($org_fh, ':utf8');
LOOP:
while (my $line=<$org_fh>) {
chomp $line;
if ($line =~ m{^\* (.*)}) {
$title = $1;
last LOOP;
} elsif ($line =~ /\S/) {
last LOOP;
}
}
if (not defined($title)) {
die '???? TITLE';
}
print STDERR "NUMBER: $number\n";
print STDERR "TITLE: $title\n";
my $intro = '';
open(my $intro_ih, '<', $intro_template);
binmode($intro_ih, ':utf8');
while(my $line=<$intro_ih>) {
$line =~ s{NUMBER}{$number};
$line =~ s{TITLE}{$title};
$intro .= $line;
}
close($intro_ih);
open(my $intro_oh, '>', $intro_file);
binmode($intro_oh, ':utf8');
print $intro_oh $intro;
close($intro_oh);