use
5.006001;
our
$VERSION
=
'0.12'
;
field
input
=>
'slides.vroom'
;
field
stream
=>
''
;
field
ext
=>
''
;
field
clean
=> 0;
field
start
=> 0;
field
digits
=> 0;
field
config
=> {
title
=>
'Untitled Presentation'
,
height
=> 24,
width
=> 80,
list_indent
=> 10,
skip
=> 0,
};
sub
new {
return
bless
{},
shift
;
}
sub
vroom {
my
$self
=
ref
(
$_
[0]) ?
shift
: (
shift
)->new;
$self
->getOptions;
$self
->cleanUp;
return
if
$self
->clean;
$self
->makeAll;
$self
->startUp
if
$self
->start;
}
sub
getOptions {
my
$self
=
shift
;
GetOptions(
"clean"
=> \
$self
->{clean},
"input=s"
=> \
$self
->{input},
"vroom"
=> \
$self
->{start},
) or
die
$self
->usage;
do
{
delete
$self
->{
$_
}
unless
defined
$self
->{
$_
} }
for
qw(clean input vroom)
;
}
sub
cleanUp {
unlink
(
glob
"0*"
);
unlink
(
".vimrc"
);
}
sub
makeAll {
my
$self
=
shift
;
$self
->getInput;
$self
->buildSlides;
$self
->writeVimrc;
}
sub
getInput {
my
$self
=
shift
;
my
$stream
= io(
$self
->input)->all
or croak
"No input provided. Make a file called 'slides.vroom'"
;
$self
->stream(
$stream
);
}
sub
buildSlides {
my
$self
=
shift
;
my
@split
=
grep
length
,
split
/^(----\ *.*)\n/m,
$self
->stream;
push
@split
,
'----'
if
$split
[0] =~ /\n/;
my
(
@raw_configs
,
@raw_slides
);
while
(
@split
) {
my
(
$config
,
$slide
) =
splice
(
@split
, 0, 2);
$config
=~ s/^----\s*(.*?)\s*$/$1/;
push
@raw_configs
,
$config
;
push
@raw_slides
,
$slide
;
}
$self
->{digits} =
int
(
log
(
@raw_slides
)/
log
(10)) + 2;
my
$number
= 0;
for
my
$raw_slide
(
@raw_slides
) {
my
$config
=
$self
->parseSlideConfig(
shift
@raw_configs
);
next
if
$config
->{skip};
$raw_slide
=
$self
->applyOptions(
$raw_slide
,
$config
)
or
next
;
$number
++;
if
(
$self
->config->{skip}) {
$self
->config->{skip}--;
next
;
}
$raw_slide
=
$self
->padVertical(
$raw_slide
);
my
@slides
;
my
$slide
=
''
;
for
(
split
/^\+/m,
$raw_slide
) {
$slide
.=
$_
;
push
@slides
,
$slide
;
}
my
$base_name
=
$self
->formatNumber(
$number
);
my
$suffix
=
'a'
;
for
(
my
$i
= 1;
$i
<=
@slides
;
$i
++) {
my
$slide
=
$self
->padFullScreen(
$slides
[
$i
- 1]);
$slide
=~ s{^\ *==\ *(.*?)\ *$}
{
' '
x ((
$self
->config->{width} -
length
($1)) / 2) . $1}gem;
my
$suf
=
$suffix
++;
$suf
=
$suf
eq
'a'
?
''
:
$i
==
@slides
?
'z'
:
$suf
;
io(
"$base_name$suf"
.
$self
->ext)->
print
(
$slide
);
}
}
}
sub
formatNumber {
my
$self
=
shift
;
my
$number
=
shift
;
my
$digits
=
$self
->digits;
return
sprintf
"%0${digits}d"
,
$number
;
}
sub
parseSlideConfig {
my
$self
=
shift
;
my
$string
=
shift
;
my
$config
= {};
for
my
$option
(
split
/\s*,\s*/,
$string
) {
$config
->{$1} = 1
if
$option
=~ /^(config|skip|center|perl|yaml|make)$/;
$config
->{indent} = $1
if
$option
=~ /i(\d+)/;
}
return
$config
;
}
sub
applyOptions {
my
$self
=
shift
;
my
(
$slide
,
$config
) =
@_
;
$config
= {
%{
$self
->config},
%$config
,
};
if
(
$config
->{config}) {
$config
= {
%{
$self
->config},
%{(YAML::XS::Load(
$slide
))},
};
$self
->config(
$config
);
return
''
;
}
if
(
$config
->{center}) {
$slide
=~ s{^(\+?)\ *(.*?)\ *$}
{$1 .
' '
x ((
$self
->config->{width} -
length
($2)) / 2) . $2}gem;
$slide
=~ s{^\s*$}{}gm;
}
if
(
defined
$config
->{indent}) {
my
$indent
=
$config
->{indent};
$slide
=~ s{^(\+?)}{$1 .
' '
x
$indent
}gem;
}
elsif
(
$slide
=~ /^\+?\*/m) {
my
$indent
=
$config
->{list_indent};
$slide
=~ s{^(\+?)}{$1 .
' '
x
$indent
}gem;
}
my
$ext
=
$config
->{perl} ?
".pl"
:
$config
->{python} ?
".py"
:
$config
->{ruby} ?
".rb"
:
$config
->{shell} ?
".sh"
:
$config
->{yaml} ?
".yaml"
:
$config
->{make} ?
".mk"
:
""
;
$self
->ext(
$ext
);
return
$slide
;
}
sub
padVertical {
my
$self
=
shift
;
my
$slide
=
shift
;
$slide
=~ s/\A\s*\n//;
$slide
=~ s/\n\s*\z//;
my
@lines
=
split
/\n/,
$slide
;
my
$lines
=
@lines
;
my
$before
=
int
((
$self
->config->{height} -
$lines
) / 2) - 1;
return
"\n"
x
$before
.
$slide
;
}
sub
padFullScreen {
my
$self
=
shift
;
my
$slide
=
shift
;
chomp
$slide
;
my
@lines
=
split
/\n/,
$slide
;
my
$lines
=
@lines
;
my
$after
=
$self
->config->{height} -
$lines
+ 1;
return
$slide
.
"\n"
x
$after
;
}
sub
writeVimrc {
my
$self
=
shift
;
my
$title
=
"%f "
.
$self
->config->{title};
$title
=~ s/\s/_/g;
io(
".vimrc"
)->
print
(
<<"...");
map <SPACE> :n<CR>:<CR>gg
map <BACKSPACE> :N<CR>:<CR>gg
map R :!perl %<CR>
map Q :q!<CR>
map O :!open <cWORD><CR>
map E :e <cWORD><CR>
set laststatus=2
set statusline=$title
...
}
sub
startUp {
exec
"vim 0*"
;
}