| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
pamphletangler - Extract code bits from LaTeX pamphlet files. |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
=head1 SYNOPSIS |
| 10 |
|
| 11 |
B<pamphletangler> [OPTION...] I<FILE> I<CHUNKNAME> |
| 12 |
|
| 13 |
=cut |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
The clojure pamphlet system is a system based on the clojure literate |
| 18 |
system. In the clojure's pamphlet system you have your main LaTeX file, which |
| 19 |
can be compiled regularly. This file contains documentation and source code |
| 20 |
(just like in other forms of literate programming). This code snippets are |
| 21 |
wrapped in the 'chunk' environment, hence they can be recognized by the tangler |
| 22 |
in order to extract them. Chunks can be included inside each other by the |
| 23 |
'getchunk' command (which will be typesetted acordingly). Finally, you run your |
| 24 |
LaTeX file through the tangler and get your desired chunk of code. |
| 25 |
|
| 26 |
=cut |
| 27 |
|
| 28 |
=head1 OPTIONS |
| 29 |
|
| 30 |
=over 29 |
| 31 |
|
| 32 |
=item -L, --line=linetext |
| 33 |
|
| 34 |
If set, this text will be inserted after jumps of lines, changing the string |
| 35 |
inside "changetext" for the line number. This is used so that error report |
| 36 |
refers to pamphlet line number instead of output line number |
| 37 |
|
| 38 |
=item -C, --change=changetext |
| 39 |
|
| 40 |
Defaults to "{}" this is the text that will be replaced with the line number if |
| 41 |
the linetext option is specified |
| 42 |
|
| 43 |
=item -?, --help |
| 44 |
|
| 45 |
Give this help list |
| 46 |
|
| 47 |
=item --usage |
| 48 |
|
| 49 |
Give a short usage message |
| 50 |
|
| 51 |
=item --man |
| 52 |
|
| 53 |
Print manual page |
| 54 |
|
| 55 |
=item -V, --version |
| 56 |
|
| 57 |
Print program version |
| 58 |
|
| 59 |
=back |
| 60 |
|
| 61 |
=head1 LICENSE |
| 62 |
|
| 63 |
Copyright (C) 2019 Ernesto Lanchares Sanchez. |
| 64 |
|
| 65 |
The clojure-pamphlet tagler is free software; you can redistribute |
| 66 |
it and/or modify it under the terms of the GNU Lesser General |
| 67 |
Public License as published by the Free Software Foundation; |
| 68 |
either version 3 of the License, or (at your option) any later |
| 69 |
version. |
| 70 |
|
| 71 |
The clojure-pamphlet tangler is distributed in the hope that it |
| 72 |
will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 73 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 74 |
See the GNU Lesser General Public License for more details. |
| 75 |
|
| 76 |
You should have received a copy of the GNU Lesser General Public |
| 77 |
License along with this program; if not, see |
| 78 |
L<http://www.gnu.org/licenses/>. |
| 79 |
|
| 80 |
|
| 81 |
=head1 AUTHOR |
| 82 |
|
| 83 |
Ernesto Lanchares <[email protected]> |
| 84 |
|
| 85 |
=head1 BUGS |
| 86 |
|
| 87 |
Report bugs to [email protected] |
| 88 |
|
| 89 |
=cut |
| 90 |
|
| 91 |
use strict; |
| 92 |
use warnings; |
| 93 |
use Getopt::Long; |
| 94 |
use Pod::Usage qw(pod2usage); |
| 95 |
use vars qw/$VERSION/; |
| 96 |
|
| 97 |
BEGIN { $VERSION = '1.3' } |
| 98 |
|
| 99 |
my $linetext = ""; |
| 100 |
my $changetext = "{}"; |
| 101 |
my $filename; |
| 102 |
my $chunkname; |
| 103 |
|
| 104 |
OPTS: { |
| 105 |
GetOptions( |
| 106 |
'usage' => sub { pod2usage( -verbose => 99, -sections => [ qw(SYNOPSIS) ]); }, |
| 107 |
'help|?' => sub { pod2usage( -verbose => 99, -sections => [ qw(SYNOPSIS DESCRIPTION OPTIONS BUGS) ]); }, |
| 108 |
'man' => sub { pod2usage( -verbose => 2) }, |
| 109 |
'version|V' => sub { print "$VERSION\n"; exit; }, |
| 110 |
'line|L=s' => \$linetext, |
| 111 |
'change|C=s' => \$changetext |
| 112 |
); |
| 113 |
|
| 114 |
pod2usage( -message => "[ERROR]: Invalid number of arguments." ) |
| 115 |
if scalar(@ARGV) != 2; |
| 116 |
$filename = shift; |
| 117 |
$chunkname = shift; |
| 118 |
} |
| 119 |
|
| 120 |
open(my $file, "<", $filename) or die "Can't open $filename: $!"; |
| 121 |
|
| 122 |
my $file_contents = do { local $/; <$file> }; |
| 123 |
my %codeblocks; |
| 124 |
|
| 125 |
while ($file_contents =~ /\\begin\{chunk\}\{([\w.]+)\}[\s\h\t]*\n?(.*?)\n?[\s\t\h]*\\end\{chunk\}/sg) { |
| 126 |
$codeblocks{$1} = { source => $2, printing => undef }; |
| 127 |
} |
| 128 |
|
| 129 |
open($file, "<", $filename) or die "Can't open $filename: $!"; |
| 130 |
my $index = 1; |
| 131 |
while (my $line = <$file>) { |
| 132 |
while ($line =~ /\\begin\{chunk\}\{([\w.]+)\}/g) { |
| 133 |
$codeblocks{$1}{line} = $index+1; |
| 134 |
} |
| 135 |
$index++; |
| 136 |
} |
| 137 |
|
| 138 |
sub printchunk { |
| 139 |
my ($chunk, $indent) = @_; |
| 140 |
|
| 141 |
die "Chunk $chunk not found." if (!exists($codeblocks{$chunk})); |
| 142 |
|
| 143 |
die "Cyclic reference found: $chunk" if $codeblocks{$chunk}{printing}; |
| 144 |
$codeblocks{$chunk}{printing} = 1; |
| 145 |
|
| 146 |
my $localines = 1; |
| 147 |
print $linetext =~ s/$changetext/$codeblocks{$chunk}{line}/gr, "\n"; |
| 148 |
foreach my $line (split(/\n/, $codeblocks{$chunk}{source})) { |
| 149 |
if ($line =~ /([\s\t\h]*)\\getchunk\{([\w.]+)\}/) { |
| 150 |
eval { |
| 151 |
printchunk($2, $1); #$1 contains indent and $2 contains chunkname |
| 152 |
print $linetext =~ s/$changetext/@{[$codeblocks{$chunk}{line} + $localines]}/gr, "\n"; |
| 153 |
1; |
| 154 |
} or do { |
| 155 |
if (rindex($@, "Cyclic reference found:", 0) == 0) { |
| 156 |
$@ =~ /(.+?)( at .*)/s; |
| 157 |
die "$1 <- $chunk"; |
| 158 |
} |
| 159 |
die $@; |
| 160 |
} |
| 161 |
} else { |
| 162 |
print $indent, $line, "\n"; |
| 163 |
} |
| 164 |
$localines++; |
| 165 |
} |
| 166 |
|
| 167 |
$codeblocks{$chunk}{printing} = undef; |
| 168 |
return 0; |
| 169 |
} |
| 170 |
printchunk($chunkname, ""); |