Changeset 366

Show
Ignore:
Timestamp:
05/14/08 14:53:29 (2 months ago)
Author:
ingy
Message:
CPAN v0.10
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/ingy/Vroom/Makefile.PL

    r364 r366  
    44all_from    'lib/Vroom.pm'; 
    55 
    6 require 'IO::All'; 
    7 require 'YAML::XS'; 
    8 require 'Class::Field'; 
     6requires 'IO::All'; 
     7requires 'YAML::XS'; 
     8requires 'Class::Field'; 
     9 
     10install_script 'vroom'; 
    911 
    1012WriteAll; 
  • trunk/src/ingy/Vroom/README

    r364 r366  
    33 
    44SYNOPSIS 
    5         > vim vroom         # Write Some Slides 
    6         > vroom vroom       # Compile Your Slides 
    7         > cd slides         # Go To Your Slides 
    8         > vim *             # Show Your Slides 
     5        > vim slides.vroom  # Write Some Slides 
     6        > vroom --vroom     # Show Your Slides 
    97 
    108DESCRIPTION 
     
    2826 
    2927    Since Vim is an editor, you can change your slides during the show. 
     28 
     29COMMAND USAGE 
     30    Vroom has a few command line options: 
     31 
     32    vroom 
     33        Just running vroom will compiles 'slides.vroom' into slide files. 
     34 
     35    vroom --vroom 
     36        Compile and start vim show. 
     37 
     38    vroom --clean 
     39        Clean up all the compiled output files. 
     40 
     41INPUT FORMAT 
     42    Here is an example slides.vroom: 
     43 
     44        ---- config 
     45        title: My Spiffy Slideshow 
     46        height: 84 
     47        width: 20 
     48        ---- center 
     49        My Presentation 
     50 
     51        by Ingy 
     52        ---- 
     53        == Stuff I care about: 
     54 
     55        * Foo 
     56        +* Bar 
     57        +* Baz 
     58        ---- perl 
     59        use Vroom; 
     60 
     61        print "Hello World"; 
     62        ---- center 
     63        THE END 
    3064 
    3165KEY MAPPINGS 
  • trunk/src/ingy/Vroom/lib/Vroom.pm

    r364 r366  
    33use strict; 
    44use warnings; 
     5 
     6# use XXX; 
     7# use diagnostics; 
     8 
     9our $VERSION = '0.10'; 
    510 
    611use IO::All; 
     
    1015use Carp; 
    1116 
    12 field input => ''; 
     17field input => 'slides.vroom'; 
     18field stream => ''; 
    1319field ext => ''; 
    14 field height => 20; 
    15 field width => 80; 
     20field clean => 0; 
     21field vroom => 0; 
     22field digits => 0; 
     23field config => { 
     24    title => 'Unnamed Presentation', 
     25    height => 24, 
     26    width => 80, 
     27    list_indent => 10, 
     28}; 
    1629 
    1730sub new { 
     
    2235    my $self = shift; 
    2336 
     37    local @ARGV = @_; 
     38 
    2439    $self->getOptions; 
     40 
    2541    $self->cleanUp; 
     42    return if $self->clean; 
     43 
     44    $self->makeAll; 
     45 
     46    $self->startUp if $self->vroom; 
    2647} 
    2748 
    2849sub getOptions { 
    2950    my $self = shift; 
    30     my $height = $self->height; 
    31     my $width = $self->width; 
    3251    GetOptions( 
    33         "height=i" => \$height, 
    34         "width=i"  => \$width, 
    35     ); 
    36     $self->height($height); 
    37     $self->width($width); 
     52        "clean" => \$self->{clean}, 
     53        "input=s"  => \$self->{input}, 
     54        "vroom"  => \$self->{vroom}, 
     55    ) or die $self->usage; 
     56 
     57    do { delete $self->{$_} unless defined $self->{$_} } 
     58        for qw(clean input vroom); 
     59 
     60    my @files = <*.vroom>; 
     61 
     62    $self->input($files[0]) 
     63        if @files; 
    3864} 
    3965 
    4066sub cleanUp { 
    41     my $self = shift; 
    42     if (-e $self->dir) { 
    43         $_->unlink for io($self->dir)->All_Files; 
    44     } 
     67    unlink(glob "0*"); 
     68    unlink(".vimrc"); 
     69
     70 
     71sub makeAll { 
     72    my $self = shift; 
     73    $self->getInput; 
     74    $self->buildSlides; 
     75    $self->writeVimrc; 
    4576} 
    4677 
    4778sub getInput { 
    4879    my $self = shift; 
    49     my $input = io('-')->all || io('vroom')->all 
    50         or croak "No input provided. Make a file called 'vroom'"; 
    51     $self->input($input); 
     80    my $stream = io($self->input)->all 
     81        or croak "No input provided. Make a file called 'slides.vroom'"; 
     82    $self->stream($stream); 
    5283} 
    5384 
    5485sub buildSlides { 
    5586    my $self = shift; 
    56     my @raw_slides = grep length, split /^----\n/m, $self->input; 
    57     $self->digits = int(log(@raw_slides)/log(10)) + 2; 
     87    my @split = grep length, split /^(----\ *.*)\n/m, $self->stream; 
     88    push @split, '----' if $split[0] =~ /\n/; 
     89    my (@raw_configs, @raw_slides); 
     90    while (@split) { 
     91        my ($config, $slide) = splice(@split, 0, 2); 
     92        $config =~ s/^----\s*(.*?)\s*$/$1/; 
     93        push @raw_configs, $config; 
     94        push @raw_slides, $slide; 
     95    } 
     96    $self->{digits} = int(log(@raw_slides)/log(10)) + 2; 
    5897 
    5998    my $number = 1; 
    6099 
    61100    for my $raw_slide (@raw_slides) { 
    62         $self->ext(''); 
    63         if ($raw_slide =~ s/^!(.*)\n//) { 
    64             $raw_slide = setOptions($raw_slide, $1) 
    65                 or next; 
    66         } 
    67         $raw_slide = padVertical($raw_slide); 
     101        my $config = $self->parseSlideConfig(shift @raw_configs); 
     102        next if $config->{skip}; 
     103 
     104        $raw_slide = $self->applyOptions($raw_slide, $config) 
     105            or next;         
     106 
     107        $raw_slide = $self->padVertical($raw_slide); 
     108 
    68109        my @slides; 
    69110        my $slide = ''; 
     
    77118        my $suffix = 'a'; 
    78119        for (my $i = 1; $i <= @slides; $i++) { 
    79             my $slide = padFullScreen($slides[$i - 1]); 
     120            my $slide = $self->padFullScreen($slides[$i - 1]); 
    80121            $slide =~ s{^\ *==\ *(.*?)\ *$} 
    81                        {' ' x (($self->width - length($1)) / 2) . $1}gem; 
    82             io("$base_name$suffix" . $self->ext)->print($slide); 
    83             $suffix++; 
     122                       {' ' x (($self->config->{width} - length($1)) / 2) . $1}gem; 
     123            my $suf = $suffix++; 
     124            $suf = $suf eq 'a' 
     125                ? '' 
     126                : $i == @slides 
     127                    ? 'z' 
     128                    : $suf; 
     129            io("$base_name$suf" . $self->ext)->print($slide); 
    84130        } 
    85131    } 
    86132} 
    87133 
     134sub formatNumber { 
     135    my $self = shift; 
     136    my $number = shift; 
     137    my $digits = $self->digits; 
     138    return sprintf "%0${digits}d", $number; 
     139} 
     140 
     141sub parseSlideConfig { 
     142    my $self = shift; 
     143    my $string = shift; 
     144    my $config = {}; 
     145    for my $option (split /\s*,\s*/, $string) { 
     146        $config->{$1} = 1 
     147            if $option =~ /^(config|skip|center|perl|yaml)$/; 
     148        $config->{indent} = $1 
     149            if $option =~ /i(\d+)/; 
     150    } 
     151    return $config; 
     152} 
     153 
     154sub applyOptions { 
     155    my $self = shift; 
     156    my ($slide, $config) = @_; 
     157 
     158    $config = { 
     159        %{$self->config}, 
     160        %$config, 
     161    }; 
     162 
     163    $self->ext(""); 
     164    if ($config->{config}) { 
     165        $config = { 
     166            %{$self->config}, 
     167            %{(YAML::XS::Load($slide))}, 
     168        }; 
     169        $self->config($config); 
     170        return ''; 
     171    } 
     172    if ($config->{center}) { 
     173        $slide =~ s{^(\+?)\ *(.*?)\ *$} 
     174                   {$1 . ' ' x (($self->config->{width} - length($2)) / 2) . $2}gem; 
     175        $slide =~ s{^\s*$}{}gm; 
     176    } 
     177    if (defined $config->{indent}) { 
     178        my $indent = $config->{indent}; 
     179        $slide =~ s{^(\+?)}{$1 . ' ' x $indent}gem; 
     180    } 
     181    elsif ($slide =~ /^\+?\*/m) { 
     182        my $indent = $config->{list_indent}; 
     183        $slide =~ s{^(\+?)}{$1 . ' ' x $indent}gem; 
     184    } 
     185    if ($config->{perl}) { 
     186        $self->ext(".pl"); 
     187    } 
     188    if ($config->{yaml}) { 
     189        $self->ext(".yaml"); 
     190    } 
     191    return $slide; 
     192} 
     193 
     194sub padVertical { 
     195    my $self = shift; 
     196    my $slide = shift; 
     197    $slide =~ s/\A\s*\n//; 
     198    $slide =~ s/\n\s*\z//; 
     199    my @lines = split /\n/, $slide; 
     200    my $lines = @lines; 
     201    my $before = int(($self->config->{height} - $lines) / 2) - 1; 
     202    return "\n" x $before . $slide; 
     203} 
     204 
     205sub padFullScreen { 
     206    my $self = shift; 
     207    my $slide = shift; 
     208    chomp $slide; 
     209    my @lines = split /\n/, $slide; 
     210    my $lines = @lines; 
     211    my $after = $self->config->{height} - $lines + 1; 
     212    return $slide . "\n" x $after; 
     213} 
     214 
    88215sub writeVimrc { 
    89216    my $self = shift; 
     217    my $title = "%f         " . $self->config->{title}; 
     218    $title =~ s/\s/_/g; 
    90219    io(".vimrc")->print(<<"..."); 
    91220map <SPACE> :n<CR>:<CR>gg 
     
    94223map Q :q!<CR> 
    95224set laststatus=2 
    96 set statusline=$self->{title} 
     225set statusline=$title 
    97226... 
    98227} 
    99228 
    100 sub writeMakefile { 
    101     my $self = shift; 
    102     io("Makefile")->print(<<'...'); 
    103 trouble love tracks: 
    104         vim [a-z]* 
    105 ... 
    106 
    107  
    108 sub printFinished { 
    109     print "Your slide show is ready... vroom vroom!!!\n"; 
    110 
    111  
    112 sub setOptions { 
    113     my $self = shift; 
    114     my ($slide, $options) = @_; 
    115     my @options = split /,/, $options; 
    116     my $config = {}; 
    117     for my $option (@options) { 
    118         if ($option eq 'config') { 
    119             $config = { %$config, %{(YAML::XS::Load($slide))} }; 
    120             return ''; 
    121         } 
    122         if ($option =~ /^c(enter)?$/) { 
    123             $slide =~ s{^(\+?)\ *(.*?)\ *$} 
    124                        {$1 . ' ' x ((78 - length($2)) / 2) . $2}gem; 
    125             $slide =~ s{^\s*$}{}gm; 
    126         } 
    127         if ($option =~ /^i(\d+)$/) { 
    128             my $indent = $1; 
    129             $slide =~ s{^(\+?)}{$1 . ' ' x $indent}gem; 
    130         } 
    131         if ($option =~ /^(pl|js|rb|yaml)$/) { 
    132             $self->ext(".$1"); 
    133         } 
    134     } 
    135     return $slide; 
    136 
    137  
    138 sub padVertical { 
    139     my $self = shift; 
    140     my $slide = shift; 
    141     $slide =~ s/\A\s*\n//; 
    142     $slide =~ s/\n\s*\z//; 
    143     my @lines = split /\n/, $slide; 
    144     my $lines = @lines; 
    145     my $before = int(($self->height - $lines) / 2) - 1; 
    146     return "\n" x $before . $slide; 
    147 
    148  
    149 sub padFullScreen { 
    150     my $self = shift; 
    151     my $slide = shift; 
    152     chomp $slide; 
    153     my @lines = split /\n/, $slide; 
    154     my $lines = @lines; 
    155     my $after = $self->height - $lines + 1; 
    156     return $slide . "\n" x $after; 
     229sub startUp { 
     230    exec "vim 0*"; 
    157231} 
    158232 
     
    163237=head1 SYNOPSIS 
    164238 
    165     > vim vroom         # Write Some Slides 
     239    > vim slides.vroom  # Write Some Slides 
    166240    > vroom --vroom     # Show Your Slides 
    167241 
     
    190264=head1 COMMAND USAGE 
    191265 
    192  
     266Vroom has a few command line options: 
     267 
     268=over 
     269 
     270=item vroom 
     271 
     272Just running vroom will compiles 'slides.vroom' into slide files. 
     273 
     274=item vroom --vroom 
     275 
     276Compile and start vim show. 
     277 
     278=item vroom --clean 
     279 
     280Clean up all the compiled  output files. 
     281 
     282=back 
    193283 
    194284=head1 INPUT FORMAT 
    195285 
    196  
     286Here is an example slides.vroom: 
     287 
     288    ---- config 
     289    title: My Spiffy Slideshow 
     290    height: 84 
     291    width: 20 
     292    ---- center 
     293    My Presentation 
     294 
     295    by Ingy 
     296    ---- 
     297    == Stuff I care about: 
     298 
     299    * Foo 
     300    +* Bar 
     301    +* Baz 
     302    ---- perl 
     303    use Vroom; 
     304 
     305    print "Hello World"; 
     306    ---- center 
     307    THE END 
    197308 
    198309=head1 KEY MAPPINGS 
  • trunk/src/ingy/Vroom/vroom

    r364 r366  
    22 
    33use Vroom; 
    4 Vroom->new->run
     4Vroom->new->run(@ARGV)