Created
December 2, 2012 13:02
-
-
Save KamilaBorowska/4188536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Bailador; | |
use URI::Escape; | |
use HTTP::Easy::PSGI; | |
sub git { | |
chdir 'content'; | |
my $command = ('git', @_).map: "'" ~ *.subst("'", qq{'"'"'}) ~ "'"; | |
my $result = qqx{$command}; | |
print $result; | |
chdir '..'; | |
$result; | |
} | |
Bailador::import; | |
get /^\/(<-[./]><-[/]>*)\/edit$/ => sub ($name is copy) { | |
$name = uri_unescape $name; | |
my $content = ""; | |
try { $content = slurp("content/$name") }; | |
template 'edit.tt', $name, $content; | |
} | |
post /^\/(<-[./]><-[/]>*)\/edit$/ => sub ($name is copy) { | |
$name = uri_unescape $name; | |
spurt "content/$name", request.params<content>.subst: /\n?$/, "\n"; | |
git 'add', '.'; | |
git 'commit', '-m', request.params<reason> || 'No reason specified'; | |
status 303; | |
header 'Location', "/$name"; | |
""; | |
} | |
get /^\/(<-[/]>*)[\/(<[0..9a..fA..F]>**5..*)]?$/ => sub ($name is copy, $revision) { | |
$name = uri_unescape($name) || 'Main page'; | |
unless "content/$name".IO.f { | |
status 404; | |
return template '404.tt', $name; | |
} | |
my $content = $revision ?? git 'show', "$revision:$name" !! slurp "content/$name"; | |
if $content ~~ /'<<<<<<< HEAD'/ { | |
status 303; | |
header 'Location', "/$name/edit"; | |
return ""; | |
} | |
if $revision { | |
git 'checkout', 'master'; | |
} | |
template 'index.tt', $name, $content; | |
} | |
get /^\/(<-[/]>*)\/log[\/(<[0..9a..fA..F]>**5..*)]?$/ => sub ($name is copy, $from is copy) { | |
$name = uri_unescape $name; | |
my @log = git('log', '--oneline', '--', $name).lines.map: {[.split: ' ', 2]}; | |
template 'log.tt', $name, $(@log), $from, $from ?? 'to revision' !! 'from revision'; | |
} | |
get /^\/(<-[/]>*)\/log\/(<[0..9a..fA..F]>**5..*)\/(<[0..9a..fA..F]>**5..*)$/ => sub ($name, $r1, $r2) { | |
content_type 'text/plain'; | |
git 'diff', "$r1..$r2", '--', uri_unescape $name; | |
} | |
get /^\/(<-[/]>*)\/revert\/(<[0..9a..fA..F]>**5..*)$/ => sub ($name, $revision) { | |
git 'revert', '-n', $revision; | |
git 'commit', '-m', "Reverted $revision."; | |
status 303; | |
header "Location", "/$name"; | |
} | |
baile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment