File: bloggery.cgi

package info (click to toggle)
libweb-simple-perl 0.033-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 380 kB
  • sloc: perl: 1,622; makefile: 7
file content (165 lines) | stat: -rwxr-xr-x 3,090 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/perl

use FindBin;
use lib $FindBin::Bin.'/code';
use Web::Simple 'Bloggery';

package Bloggery::PostList;

use File::stat;

sub from_dir {
  my ($class, $dir) = @_;
  bless ({ dir => $dir }, $class);
}

sub all {
  my ($self) = @_;
  map { Bloggery::Post->from_file($_) }
    sort { stat($a)->mtime <=> stat($b)->mtime }
    grep { !/\.summary\.html$/ }
      glob($self->{dir}.'/*.html');
}

sub post {
  my ($self, $name) = @_;
  my $file = $self->{dir}."/${name}.html";
  return unless $file && -f $file;
  return Bloggery::Post->from_file($file);
}

sub map {
  my ($self, $code) = @_;
  map $code->($_), $self->all;
}

package Bloggery::Post;

sub from_file {
  my ($class, $file) = @_;
  bless({ file => $file }, $class);
}

sub name {
  my $name = shift->{file};
  $name =~ s/.*\///;
  $name =~ s/\.html$//;
  $name;
}

sub title {
  my $title = shift->name;
  $title =~ s/-/ /g;
  $title;
}

sub html {
  \do { local (@ARGV, $/) = shift->{file}; <> };
}

sub summary_html {
  my $file = shift->{file};
  $file =~ s/\.html$/.summary.html/;
  return \'<p>No summary</p>' unless -f $file;
  \do { local (@ARGV, $/) = $file; <> };
}

package Bloggery;

has post_list => (is => 'lazy');

sub default_config {
  (
    title => 'Bloggery',
    posts_dir => $FindBin::Bin.'/posts',
  );
}

sub _build_post_list {
  my ($self) = @_;
  Bloggery::PostList->from_dir(
    $self->config->{posts_dir}
  );
}

sub post {
  my ($self, $post) = @_;
  $self->post_list->post($post);
}

sub dispatch_request {
  my $self = shift;
  sub (GET + /) {
    redispatch_to '/index.html'
  },
  sub (.html) {
    response_filter { $self->render_html(@_) }
  },
  sub (GET + /index) {
    $self->post_list
  },
  sub (GET + /*) {
    $self->post($_[1])
  },
  sub (GET) {
    [ 404, [ 'Content-type', 'text/plain' ], [ 'Not found' ] ]
  },
  sub {
    [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
  },
};

sub render_html {
  my ($self, $data) = @_;
  use HTML::Tags;
  return $data if ref($data) eq 'ARRAY';
  return [
    200,
    [ 'Content-type', 'text/html' ],
    [
      HTML::Tags::to_html_string(
        <html>,
          <head>,
            <title>, $self->title_for($data), </title>,
          </head>,
          <body>,
            <h1>, $self->title_for($data), </h1>,
            <div id="main">,
              $self->main_html_for($data),
            </div>,
          </body>,
        </html>
      )
    ]
  ];
}

sub title_for {
  my ($self, $data) = @_;
  if ($data->isa('Bloggery::Post')) {
    return $data->title;
  }
  return $self->config->{title};
}

sub main_html_for {
  my ($self, $data) = @_;
  use HTML::Tags;
  if ($data->isa('Bloggery::Post')) {
    $data->html
  } elsif ($data->isa('Bloggery::PostList')) {
    <ul>,
      $data->map(sub {
        my $path = $_->name.'.html';
        <li>,
          <h4>, <a href="$path">, $_->title, </a>, </h4>,
          <span class="summary">, $_->summary_html, </span>,
        </li>;
      }),
    </ul>;
  } else {
    <h2>, "Don't know how to render $data", </h2>;
  }
}

Bloggery->run_if_script;