-
Notifications
You must be signed in to change notification settings - Fork 1
/
splitAsciidoc.pl
executable file
·109 lines (94 loc) · 2.71 KB
/
splitAsciidoc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl -w
use strict;
use FileHandle;
our $SOURCEFILE = $ARGV[0];
print 'Source: ' . $SOURCEFILE . "\n";
our $SUFFIX = '.asciidoc';
our $BASENAME = $SOURCEFILE;
$BASENAME =~ s/${SUFFIX}//;
print 'Base: ' . $BASENAME . "\n";
our $PREFACE = $BASENAME . '-Preface' . $SUFFIX;
our $AFTERWORD = $BASENAME . '-Afterword' . $SUFFIX;
our $APPENDIX = $BASENAME . '-Appendix' . $SUFFIX;
print 'Filename: ' . $PREFACE . "\n";
open( INPUT, "<${SOURCEFILE}" )
or die "Unable to read sourcefile: ${SOURCEFILE}\n";
# Read past the start of the preface
my $OUTPUTFH = FileHandle->new( $PREFACE, 'w' );
while( my $line = <INPUT> ) {
print $OUTPUTFH $line;
if( $line =~ /^== Preface/ ) {
last;
}
}
# Now create a loop that outputs each line, starting a new file after each chapter break
my $iterator = 0;
my $skipnext2nd = 0;
my $line;
while( $line = <INPUT> ) {
if( $line =~ /^\[\[[A-Z][\w\-]+\]\]/ ) {
# Now see if the next line is the start of a new chapter
my $nextline = <INPUT>;
if( $nextline =~ /^= / ) {
# Change file handles
$OUTPUTFH = &nextFile( $OUTPUTFH, ++$iterator );
$skipnext2nd = 1;
}
elsif( $nextline =~ /^== / ) {
if( $skipnext2nd ) {
$skipnext2nd = 0;
}
else {
# Change file handles
$OUTPUTFH = &nextFile( $OUTPUTFH, ++$iterator );
}
}
# Either way print out both lines and proceed
print $OUTPUTFH $line . $nextline;
next;
}
elsif( $line =~ /^\[preface\]$/ ) {
# Now see if the next line is the afterword
my $nextline = <INPUT>;
if( $nextline =~ /^\[role="afterword"\]$/ ) {
# get out of loop
$line .= $nextline;
last;
}
}
print $OUTPUTFH $line;
}
$OUTPUTFH->close();
print 'Filename: ' . $AFTERWORD . "\n";
$OUTPUTFH = FileHandle->new( $AFTERWORD, 'w' );
print $OUTPUTFH $line;
while( my $line = <INPUT> ) {
if( $line =~ /^\[appendix\]/i ) {
last;
}
print $OUTPUTFH $line;
}
$OUTPUTFH->close();
print 'Filename: ' . $APPENDIX . "\n";
$OUTPUTFH = FileHandle->new( $APPENDIX, 'w' );
print $OUTPUTFH "[appendix]\n";
while( my $line = <INPUT> ) {
print $OUTPUTFH $line;
}
close( INPUT )
or die;
exit 0;
sub nextFile() {
use vars qw( $BASENAME $SUFFIX );
my $fh = shift;
my $iterator = shift;
if( ref( $fh ) eq 'FileHandle' ) {
$fh->close();
}
my $chapternum = sprintf( '%02i', $iterator );
my $filename = $BASENAME . '-Chapter' . $chapternum . $SUFFIX;
print 'Filename: ' . $filename . "\n";
$fh = FileHandle->new( $filename, 'w' )
|| die;
return $fh;
}