|
| 1 | +# -*-perl-*- |
| 2 | + |
| 3 | +package utils; |
| 4 | +require Exporter; |
| 5 | +@ISA = qw(Exporter); |
| 6 | +@EXPORT = qw(readfile writefile read_config $contents $stylesheet |
| 7 | + $styletype @chapter @resetnumber @format %lookup $chapno @relations |
| 8 | + @links @tonavbar $src_ext); |
| 9 | + |
| 10 | +# @EXPORT_OK = qw(); |
| 11 | + |
| 12 | + |
| 13 | +# use Text::ParseWords; |
| 14 | + |
| 15 | +$contents = ''; # File containing ToC |
| 16 | +$stylesheet = ''; # URL of style sheet |
| 17 | +$styletype = ''; # MIME type of style sheet |
| 18 | +@chapter = (); # Array of chapter files |
| 19 | +@resetnumber = (); # For each chapter: the chapter number - 1 |
| 20 | +@format = (); # For each chapter: list of number formats |
| 21 | +%lookup = (); # Reverse index: chapter name -> number |
| 22 | +$chapno = -1; # Number of the chapter under consideration |
| 23 | +@relations = (); # Additional links |
| 24 | +@links = (); # Additional links (paired with @relations) |
| 25 | +@tonavbar = (); # Whether the link goes into the navbar |
| 26 | +$src_ext = undef; # Extension of source files |
| 27 | + |
| 28 | +sub parsewords { |
| 29 | + my $line = $_[0]; |
| 30 | + my @words = (); |
| 31 | + while ($line ne '') { |
| 32 | + if ($line =~ /^\s+/) { |
| 33 | + # Skip whitespace |
| 34 | + } elsif ($line =~ /^\"((?:[^\"]|\\\")*)\"/) { |
| 35 | + push(@words, $1); |
| 36 | + } elsif ($line =~ /^\'((?:[^\']|\\\')*)\'/) { |
| 37 | + push(@words, $1); |
| 38 | + } elsif ($line =~ /^\S+/) { |
| 39 | + push(@words, $&); |
| 40 | + } else { |
| 41 | + die "Cannot happen\n"; |
| 42 | + } |
| 43 | + $line = $'; |
| 44 | + } |
| 45 | + return @words; |
| 46 | +} |
| 47 | + |
| 48 | +# Read info about book's structure from $_[0] |
| 49 | +sub read_config { |
| 50 | + open(IN, $_[0]) || die "$0: cannot open file $_[0]\n"; |
| 51 | + my $reset = 0; # If >= 0, start chapter numbering w/ this |
| 52 | + my @curformat = ("1","1.1","1.1.1","1.1.1.1","1.1.1.1.1","1.1.1.1.1.1"); |
| 53 | + while (<IN>) { |
| 54 | + chop; |
| 55 | + my @words = parsewords($_); |
| 56 | + if (!defined $words[0]) {next;} # Empty line |
| 57 | + for ($words[0]) { |
| 58 | + /\@contents$/o and do { # URL of ToC |
| 59 | + $contents = $words[1]; |
| 60 | + last; |
| 61 | + }; |
| 62 | + /\@stylesheet$/o and do { # URL (and type) of style sheet |
| 63 | + $stylesheet = $words[1]; |
| 64 | + if (defined $words[2]) {$styletype = $words[2];} |
| 65 | + last; |
| 66 | + }; |
| 67 | + /\@format$/o and do { # Set numbering style for next chapters |
| 68 | + @curformat = @words; |
| 69 | + $reset = 0; |
| 70 | + last; |
| 71 | + }; |
| 72 | + /\@restart$/o and do { # Reset the chapter number |
| 73 | + $reset = int($words[1]) - 1; |
| 74 | + next; |
| 75 | + }; |
| 76 | + /\@chapter$/o and do { # File with next chapter |
| 77 | + push(@chapter, ($words[1])); |
| 78 | + push(@format, ([@curformat])); |
| 79 | + push(@resetnumber, ($reset)); |
| 80 | + $reset++; |
| 81 | + $lookup{$words[1]} = $#chapter; |
| 82 | + last; |
| 83 | + }; |
| 84 | + /\@link$/o and do { # Additional <LINK> tags |
| 85 | + push(@relations, ($words[1])); |
| 86 | + push(@links, ($words[2])); |
| 87 | + push(@tonavbar, (defined $words[3] ? $words[3] : "")); |
| 88 | + last; |
| 89 | + }; |
| 90 | + /\@source-extension$/o and do { # Extension of sources |
| 91 | + $src_ext = $words[1]; |
| 92 | + last; |
| 93 | + }; |
| 94 | + /^\#/o and do { # Comment |
| 95 | + last; |
| 96 | + }; |
| 97 | + die "$0: syntax error in config file $_[0], line $.\n"; |
| 98 | + } |
| 99 | + } |
| 100 | + close(IN); |
| 101 | +} |
| 102 | + |
| 103 | +# Load file into memory |
| 104 | +sub readfile { |
| 105 | + open(INPUT, $_[0]) || die "$PROG: cannot open file $_[0]\n"; |
| 106 | + local $/ = undef; |
| 107 | + my $buf = <INPUT>; |
| 108 | + close(INPUT); |
| 109 | + return $buf; |
| 110 | +} |
| 111 | + |
| 112 | +# Create a file $_[0] containing $_[1] |
| 113 | +sub writefile { |
| 114 | + open(OUTPUT, ">$_[0]") || die "$PROG: cannot create file $_[0]\n"; |
| 115 | + print OUTPUT $_[1]; |
| 116 | + close(OUTPUT); |
| 117 | +} |
| 118 | + |
| 119 | +return 1; |
0 commit comments