| 1 | package App::Pinpp; | ||
| 2 | use v5.010; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use autodie; | ||
| 6 | use File::Spec; | ||
| 7 | use Getopt::Std; | ||
| 8 | use File::Temp qw(tempfile); | ||
| 9 | |||
| 10 | |||
| 11 | # ABSTRACT: pinpp - The Pinpoint Pre-processor | ||
| 12 | |||
| 13 | our $VERSION = '0.03'; # VERSION: Generated by DZP::OurPkg:Version | ||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | sub run { | ||
| 18 | my $class = shift; | ||
| 19 | |||
| 20 | # Load our arguments into ARGV, so we can use getopts. | ||
| 21 | local @ARGV = @_; | ||
| 22 | |||
| 23 | my %opts = ( | ||
| 24 | o => '', # Output to PDF | ||
| 25 | I => 'topics' # Includes directory | ||
| 26 | ); | ||
| 27 | |||
| 28 | getopts("o:i:",\%opts); | ||
| 29 | |||
| 30 | my ($file) = @ARGV; | ||
| 31 | |||
| 32 | $file or die "Usage: $0 file.pinpp"; | ||
| 33 | |||
| 34 | # Okay code, you have just one job: | ||
| 35 | # @include <foo.pin> -> includes $INCLUDE_DIR/foo.pin | ||
| 36 | # NB: Everything after the @include on a line will be removed. | ||
| 37 | |||
| 38 | my $content = $class->_slurp($file); | ||
| 39 | |||
| 40 | # Remove // comments | ||
| 41 | |||
| 42 | $content =~ s{^//[^\n]*\n}{}gsm; | ||
| 43 | |||
| 44 | # Process includes | ||
| 45 | |||
| 46 | $content =~ s{^\@include\s+<([^>]+)>[^\n]*\n}{$class->include($opts{I},$1)}gmse; | ||
| 47 | |||
| 48 | # Remove blank slides | ||
| 49 | |||
| 50 | $content =~ s{^--\n--}{--}gsm; | ||
| 51 | |||
| 52 | # If a slide has no styling at all, and consists only of indented text, | ||
| 53 | # reformat it as code. | ||
| 54 | |||
| 55 | $content =~ s{ | ||
| 56 | ^--[ \t]*\n # Start of slide | ||
| 57 | (?: | ||
| 58 | [ \t]+[^\n]*\n # Lines starting with spaces/tabs | ||
| 59 | | # or... | ||
| 60 | \n # blank lines. They're cool, too. | ||
| 61 | )+ # And we can have a bunch of either. | ||
| 62 | (?=^--) # End of slide look-ahead. | ||
| 63 | }{_codeify(${^MATCH})}gsmxpe; # USE ALL THE FLAGS! | ||
| 64 | |||
| 65 | if (my $outputfile = $opts{o}) { | ||
| 66 | |||
| 67 | # Iff we're producing a PDF, then remove speaker comments | ||
| 68 | $content =~ s{^#[^\n]*\n}{}gsm; | ||
| 69 | |||
| 70 | my ($tmp_fh, $tmp_filename) = tempfile( DIR => '.' ); | ||
| 71 | |||
| 72 | say {$tmp_fh} $content; | ||
| 73 | close($tmp_fh); | ||
| 74 | |||
| 75 | system("pinpoint", "--output=$opts{o}", $tmp_filename); | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | # Otherwise just display our text | ||
| 79 | say $content; | ||
| 80 | } | ||
| 81 | |||
| 82 | # And we're done! | ||
| 83 | |||
| 84 | # Success! | ||
| 85 | return 0; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | sub include { | ||
| 90 | my ($class, $dir, $include) = @_; | ||
| 91 | return $class->_slurp(File::Spec->catdir($dir, $include))."--\n"; | ||
| 92 | } | ||
| 93 | |||
| 94 | # Slurps in a file. Not as fancy as File::Slurp, but saves us a | ||
| 95 | # dependency. | ||
| 96 | sub _slurp { | ||
| 97 | my ($class, $file) = @_; | ||
| 98 | |||
| 99 | local $/; | ||
| 100 | |||
| 101 | open(my $fh, '<', $file); | ||
| 102 | |||
| 103 | return <$fh>; | ||
| 104 | } | ||
| 105 | |||
| 106 | # Trims out start/end of slide. | ||
| 107 | sub _trim_slide { | ||
| 108 | my ($slide) = @_; | ||
| 109 | |||
| 110 | $slide =~ s{^--[ \t]*\n}{}; | ||
| 111 | $slide =~ s{--[ \t]*\n?$}{}; | ||
| 112 | |||
| 113 | return $slide; | ||
| 114 | } | ||
| 115 | |||
| 116 | # Turns a slide into a code slide. | ||
| 117 | # Does not return trailing '--' | ||
| 118 | sub _codeify { | ||
| 119 | my ($slide) = @_; | ||
| 120 | |||
| 121 | $slide = _trim_slide($slide); | ||
| 122 | |||
| 123 | my @lines = split(/\n/,$slide); | ||
| 124 | |||
| 125 | # Initial count. | ||
| 126 | my $min_spaces = _count_leading_spaces($lines[0]); | ||
| 127 | |||
| 128 | # Find the minimum number of spaces on any line. | ||
| 129 | # Double-checks the first line. Meh. | ||
| 130 | foreach my $line (@lines) { | ||
| 131 | my $spaces = _count_leading_spaces($line); | ||
| 132 | |||
| 133 | next if $spaces == 0; # Blank lines don't count. | ||
| 134 | |||
| 135 | if ($spaces < $min_spaces) { | ||
| 136 | $min_spaces = $spaces; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | # String of actual leading spaces. | ||
| 141 | my $space_leader = ' ' x $min_spaces; | ||
| 142 | |||
| 143 | # Trim off leading spaces. | ||
| 144 | $slide =~ s{^$space_leader}{}smg; | ||
| 145 | |||
| 146 | # Return our slide. | ||
| 147 | return "-- [font=monospace 50px][text-align=left]\n$slide"; | ||
| 148 | } | ||
| 149 | |||
| 150 | # Returns number of leading spaces on a line. | ||
| 151 | # TODO: Handle tabs | ||
| 152 | sub _count_leading_spaces { | ||
| 153 | my ($line) = @_; | ||
| 154 | |||
| 155 | $line =~ m{^(?<spaces>[ ]*)}; | ||
| 156 | |||
| 157 | return length($+{spaces}); | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | |||
| 162 | __END__ |