Browse Source

Restored original functionality within Dancer

Alois Mahdal (@azzgoat) 9 years ago
parent
commit
0a2dbdef36
6 changed files with 39 additions and 21 deletions
  1. 4
    0
      environments/development.yml
  2. 25
    16
      lib/SugarTrail.pm
  3. 5
    5
      lib/SugarTrail/Template/Slave.pm
  4. 2
    0
      views/layouts/html.tt
  5. 1
    0
      views/layouts/plain.tt
  6. 2
    0
      views/st.tt

+ 4
- 0
environments/development.yml View File

@@ -25,3 +25,7 @@ show_errors: 1
25 25
 # DO NOT EVER USE THIS FEATURE IN PRODUCTION 
26 26
 # OR TINY KITTENS SHALL DIE WITH LOTS OF SUFFERING
27 27
 auto_reload: 0
28
+
29
+# sugar-trail repo root
30
+repo_root:  /srv/strail/strepo
31
+repo_vcs:   git

+ 25
- 16
lib/SugarTrail.pm View File

@@ -3,33 +3,42 @@ use Dancer ':syntax';
3 3
 
4 4
 use SugarTrail::Repo;
5 5
 use helper;
6
-use URL::Encode qw/ url_decode /;
7 6
 
8 7
 our $VERSION = '0.1';
9 8
 
10
-my $repo_root = '/srv/strail/strepo';
11
-my $repo_vcs  = 'git';
9
+my $r = SugarTrail::Repo->new(
10
+    root    => config->{repo_root},
11
+    vcs     => config->{repo_vcs}
12
+);
12 13
 
13
-my $repo = SugarTrail::Repo->new( root => $repo_root, vcs=> $repo_vcs );
14 14
 
15
+get qr|^/st/([^\?]*)(.*)$| => sub {
15 16
 
17
+    my $p = params();
16 18
 
17
-get qr{(/[^\?]*)} => sub {
19
+    &helper::dmup (ENV => \%ENV);
20
+    &helper::dmup (params => scalar params);
21
+    &helper::dmup (config => config);
18 22
 
19
-    my ($stm) = split '\?', $ENV{REQUEST_URI}, 2;
20
-    $stm = url_decode($stm);
23
+    my ($stpath, $query) = splat;
24
+    my $format = params->{__format} // 'plain';
25
+    header('Content-Type' => ( 
26
+            $format eq 'html' ? 'text/html' : 'text/plain'
27
+    ));
21 28
 
22
-    my $m = $repo->load_stm( stm => $stm . '.stm' );
23
-    unless ($m) {
24
-        return template 'not_found';
25
-    }
29
+    my $m = $r->load_stm( stm => "/$stpath.stm" )
30
+        or return template 'not_found';
31
+    my $s = $m->generate_slave(scalar params);
26 32
 
27
-    my $params = params();
28
-    my $s = $m->generate_slave($params);
29
-
30
-    &helper::dmup(params=>$params);
33
+    template 'st', {
34
+        st_headers =>   $s->head(),
35
+        content =>      $s->body(),
36
+        warnings =>     $s->warnings()
37
+    }, { layout => $format };
38
+};
31 39
 
32
-    return '<pre>' . $s->source . '</pre>';
40
+get '/' => sub {
41
+    template 'index';
33 42
 };
34 43
 
35 44
 true;

+ 5
- 5
lib/SugarTrail/Template/Slave.pm View File

@@ -17,21 +17,21 @@ sub new {
17 17
 
18 18
 sub source {
19 19
     my $self = shift;
20
-    return sprintf "%s\n\n%s%s", $self->head, $self->warnings, $self->body; 
20
+    return sprintf "%s\n%s%s", $self->head, $self->warnings, $self->body;
21 21
 }
22 22
 
23 23
 sub head {
24 24
     my $self = shift;
25 25
     my @head;
26
-    push @head, sprintf "%s: %s", $_, $self->{meta}->{$_} foreach keys %{$self->{meta}};
27
-    return join "\n", @head;
26
+    push @head, sprintf "%s: %s\n", $_, $self->{meta}->{$_} foreach keys %{$self->{meta}};
27
+    return join "", @head;
28 28
 }
29 29
 
30 30
 sub body {
31 31
     my $self = shift;
32 32
     my @source;
33
-    push @source, $_->{line} foreach @{$self->{steps}};
34
-    return join "\n", @source;
33
+    push @source, $_->{line} . "\n" foreach @{$self->{steps}};
34
+    return join "", @source;
35 35
 }
36 36
 
37 37
 sub warnings {

+ 2
- 0
views/layouts/html.tt View File

@@ -0,0 +1,2 @@
1
+<p style="color:#0a0">This is a green html but nevermind...</p>
2
+<pre><% content %></pre>

+ 1
- 0
views/layouts/plain.tt View File

@@ -0,0 +1 @@
1
+<% content %>

+ 2
- 0
views/st.tt View File

@@ -0,0 +1,2 @@
1
+<% st_headers %>
2
+<% warnings %><% content %>