-
Notifications
You must be signed in to change notification settings - Fork 4
/
mojowka
executable file
·1203 lines (969 loc) · 31.8 KB
/
mojowka
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
=head1 NAME
mojowka
=head1 DESCRIPTION
mojowka - is a L<Mojolicious::Lite> based lightweight wiki.
=head1 AUTHOR
Alexander Sapozhnikov
L<http://shoorick.ru/>
L<< E<lt>[email protected]<gt> >>
2000-2015
=head1 LICENSE
This program is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
use utf8;
use Mojolicious::Lite;
use Locale::TextDomain qw( mojowka locale );
no warnings 'redefine';
use DBI;
use File::Basename 'basename';
use File::Path 'mkpath';
use Mojo::ByteStream 'b';
use Mojo::Log;
use Text::Textile;
our $VERSION=0.05;
my $log = Mojo::Log->new;
my $dbfile
= $ENV{'MOJOWKA_DATAFILE'}
|| 'data.sqlite';
#our $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", undef, undef);
our $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", undef, undef, {sqlite_unicode => 1});
our $txt = Text::Textile->new;
$txt->charset('utf-8');
# Image base URL
our $IMAGE_BASE
= $ENV{'MOJOWKA_IMAGE_BASE'}
|| '/images';
# Directory to save image files
# http://d.hatena.ne.jp/yukikimoto/20100212/1265989676
my $IMAGE_DIR = ${app->static->paths}[0] . $IMAGE_BASE;
unless (-d $IMAGE_DIR) {
mkpath $IMAGE_DIR or die "Cannot create directory: $IMAGE_DIR";
}
# i18n
plugin 'charset' => { 'charset' => 'utf-8' };
plugin 'textdomain' => {
'domain' => 'mojowka',
'search_dirs' => [ 'locale' ],
'available_languages' => [ 'en', 'ru' ],
'default_language' => 'en',
};
app->hook( 'before_routes' => sub {
my $self = shift;
$self->set_language( $self->session( 'language' ) || $self->detect_language );
});
# Routes
# any '/' => sub { shift->redirect_to( '/index' ) };
# For security reasons User can login only via POST
get '/login' => 'login';
post '/login' => sub {
my $self = shift;
my $login = $self->param('login') || '';
my $password = $self->param('password') || '';
$password = b($password)->md5_sum;
my $redirect
= $self->param('redirect')
# || $self->req->env('HTTP_REFERER')
|| '/';
if ( $login ) {
my $row = $dbh->selectrow_hashref(
q{SELECT id FROM user WHERE login=? AND password=?},
{},
$login, $password
);
if ( $row ) {
$self->session( 'login' => $login );
$self->flash( 'message' => __ 'Thanks for logging in' );
}
else {
$self->flash( 'message' => __ 'Wrong username or password' );
$redirect = '/login';
}
}
else {
$redirect = '/login';
} # else
$self->redirect_to( $redirect );
};
get '/logout' => sub {
my $self = shift;
$self->session( 'expires' => 1 );
my $redirect = $self->param('redirect') || '/';
$self->redirect_to( $redirect );
}; # => 'logout';
get '/set_language' => \&set_language;
get '/set_language/:language' => \&set_language;
post '/preview' => sub {
my $self = shift;
my $text = $self->param('article') || return;
$self->render( 'text' => markup( $text ) );
};
get '/sitemap' => sub {
my $self = shift;
my $rows = $dbh->selectcol_arrayref(
q{SELECT title FROM page ORDER BY LOWER(title)},
);
return undef unless $rows;
foreach my $row ( @$rows ) {
$row = b( $row )->decode('UTF-8')->to_string;
}
return $self->render(
'rows' => $rows,
);
};
get '/tags' => sub {
my $self = shift;
my $rows = $dbh->selectcol_arrayref(
q{SELECT title FROM tag ORDER BY LOWER(title)},
);
return undef unless $rows;
foreach my $row ( @$rows ) {
$row = b( $row )->decode('UTF-8')->to_string;
}
return $self->render(
'rows' => $rows,
);
};
get '/tags/:tag' => sub {
my $self = shift;
my $tag = $self->param('tag');
my $rows = $dbh->selectall_arrayref(
q{
SELECT filename,image.text
FROM image
LEFT JOIN image_tag
ON image_tag.image=image.id
LEFT JOIN tag
ON image_tag.tag=tag.id
WHERE LOWER(tag.title)=LOWER(?)
},
{ Slice => {} },
$tag,
);
return $self->render(
'tag' => $tag,
'base' => $IMAGE_BASE,
'rows' => $rows,
);
};
get '/search/:query' => \&search;
get '/search' => \&search;
# Authentication
under sub {
my $self = shift;
# Authenticated
return 1
if $self->session( 'login' );
# Not authenticated
$self->flash( 'message' => __ 'You are not logged in' );
$self->render(
'status' => 401,
);
return;
};
any '/edit/:id' => [ id => qr/\d+/ ] => \&edit_article;
any '/edit/:id' => [ id => 'new' ] => \&edit_article;
any '/create/(*title)' => \&edit_article;
post '/images/(*title)' => \&upload_image;
any '/delete/:id' => [ id => qr/\d+/ ] => \&delete_article;
get '/userlist' => \&get_userlist;
# Change password
get '/change_password/:id' => [ id => qr/\d+/ ] => 'change_password';
get '/change_password' => 'change_password';
post '/change_password' => \&change_password;
# Other requests
under sub { 1 };
get '/images/(*title)' => 'imagestitle';
get '/profile/:login' => \&get_user;
get '/(*title)' => \&get_article;
get '/' => \&get_article;
app->types->type( 'html' => 'text/html; charset=utf-8' );
app->secrets([ 'mojoWka' ]);
app->start( @ARGV ? @ARGV : 'daemon' );
END { my $rc = $dbh->disconnect }
=head1 FUNCTIONS
=head2 markup
Convert lightweight markup into HTML
=cut
sub markup {
local $_ = shift || return;
# [[internal links]]
s{
\[\[
(([^\]\|]+)\|)?
([^\]\|]+)
\]\]
}{
my $link = $2 || $3;
$link =~ tr/ /_/;
my $text = $3;
sprintf q{"%s":/%s}, $text, $link;
}gex;
# $log->debug("Textiling of $_");
return $txt->process( $_ );
} # sub markup
=head2 get_article
Get article by title
=cut
sub get_article {
my $self = shift;
my $title = $self->param('title') || '';
$title =~ s/\.html$//;
$title =~ tr/_/ /;
my $row = $dbh->selectrow_hashref(
q{SELECT * FROM page WHERE LOWER(title)=LOWER(?)},
{},
$title,
);
return $self->render(
'template' => 'not_found',
'status' => 404,
) unless $row;
$self->render(
'text' => markup( $row->{'text'} ),
'title' => $title,
'layout' => 'default',
'id' => $row->{'id'},
);
} # sub get_article
=head2 edit_article
Draw editing form for article found by its ID
and process this form.
=cut
sub edit_article {
my $self = shift;
my $id = $self->param('id') || 0; # zero, natural number or word 'new'. See above
$id = 0 if $id eq 'new'; # digits only
my $article_title = $self->param('title') || '';
$article_title =~ s/\.html$//;
$article_title =~ tr/_/ /;
my $article_content = $self->param('article');
if ( $self->req->method eq 'POST' ) {
# Validate
my @errors = ();
TRY: {
# is title non-unique?
if (
$dbh->selectrow_hashref(
q{SELECT id FROM page WHERE LOWER(title)=LOWER(?) AND id!=?},
{},
$article_title, $id,
)
) {
push ( @errors, __ 'Title must be unique' );
last TRY;
} # if
# is content empty?
unless ( $article_content ) {
push ( @errors, __ 'Page content can not be empty' );
last TRY;
}
} # TRY
if ( @errors ) {
return $self->render(
'errors' => [ @errors ],
'template' => 'edit_article',
'title' => b( __ 'Errors occured' )->decode( 'UTF-8' ),
'id' => $id,
'article_content' => $article_content,
'article_title' => b( $article_title )->decode( 'UTF-8' ),
);
} # if errors
# else OK
my $rows_affected = 0;
if ( $id ) { # edit
$rows_affected = $dbh->do(
q{UPDATE page SET title=?, text=? WHERE id=?},
{},
$article_title, $article_content, $id
)
} # if exists
else { # new page
$rows_affected = $dbh->do(
q{INSERT INTO page VALUES(NULL, ?, ?)},
{},
$article_title, $article_content,
)
} # else new
if ( $rows_affected ) {
$article_title =~ tr/ /_/;
$self->flash( 'message' => __ 'Changes was saved' );
$self->redirect_to( "/$article_title" );
}
# else NOT saved
push ( @errors, __ 'Can not save' );
return $self->render(
'errors' => [ @errors ],
'template' => 'edit_article',
'title' => b( __ 'Errors occured' )->decode( 'UTF-8' ),
'id' => $id,
'article_content' => $article_content,
'article_title' => b( $article_title )->decode( 'UTF-8' ),
);
} # if POST
if ( $id ) { # existing record
my $row = $dbh->selectrow_hashref(q{SELECT * FROM page WHERE id=?}, {}, $id);
if ( $row ) {
my $article_title = b( $row->{'title'} )->decode( 'UTF-8' );
return $self->render(
'template' => 'edit_article',
'title' => b( __ 'Editing' )->decode( 'UTF-8' ),
'id' => $id,
'article_content' => b( $row->{'text'} )->decode( 'UTF-8' ),
'article_title' => $article_title,
'old_title' => $article_title,
);
} # if $row
} # if $id
else { # new record
$article_title =~ tr/_/ /;
return $self->render(
'template' => 'edit_article',
'title' => b( __ 'New page' )->decode( 'UTF-8' ),
'id' => 0,
'article_content' => '',
'article_title' => b( $article_title )->decode( 'UTF-8' ),
);
} # else
} # sub edit_article
=head2 delete_article
Delete article specified by ID
=cut
sub delete_article {
my $self = shift;
my $id = $self->param('id') || return; # natural number only
my $rows_affected = $dbh->do(
q{DELETE FROM page WHERE id=?},
{},
$id,
);
if ( $rows_affected == 1 ) { # deleted without errors
$self->flash( 'message' => __ 'Page was deleted' );
$self->redirect_to( '/' );
} # if deleted
} # sub delete_article
=head2 get_user
Get user's profile
=cut
sub get_user {
my $self = shift;
my $login = $self->param('login') || return;
my $row = $dbh->selectrow_hashref(
q{SELECT text FROM user WHERE login=?},
{},
$login,
);
return $self->render(
'text' => markup( b( $row->{'text'} )->decode('UTF-8')->to_string ),
'title' => $login,
'layout' => 'default',
) if $row;
} # sub get_user
=head2 get_userlist
Get list of registered users
=cut
sub get_userlist {
my $self = shift;
my $login = $self->session( 'login' );
my $row = $dbh->selectrow_hashref(
q{SELECT 1 FROM user WHERE login=? AND is_admin=1},
{},
$login,
);
if ($row) {
my $rows = $dbh->selectall_arrayref(
q{SELECT login FROM user},
{ Slice => {} },
);
return $self->render(
'rows' => $rows,
'layout' => 'default',
'template' => 'userlist',
)
}
# else
$self->flash( 'message' => __ 'Permission denied' );
$self->redirect_to( '/login' );
} # sub get_userlist
=head2 search
Search pages by title and content
=cut
sub search {
my $self = shift;
my $title = __ 'Search';
my $query = $self->param('query') || '';
my $rows = [];
if ( $query ) {
my $row = $dbh->selectcol_arrayref(
q{SELECT title FROM page WHERE LOWER(title)=LOWER(?)},
{},
$query,
);
if ( @$row ) {
$self->flash( 'message' => __ 'Exact matching found' );
$query = b( shift @$row )->decode('UTF-8')->to_string;
$query =~ tr/ /_/;
$self->redirect_to( "/$query" );
return;
} # if row
$rows = $dbh->selectcol_arrayref(
q{SELECT title FROM page WHERE LOWER(title) LIKE LOWER(?) OR LOWER(text) LIKE LOWER(?) ORDER BY LOWER(title)},
{},
"%$query%", "%$query%",
);
$title = __nx
'Found {count} page',
'Found {count} pages',
scalar @$rows,
'count' => scalar @$rows;
foreach my $row ( @$rows ) {
$row = b( $row )->decode('UTF-8');
}
} # if query
return $self->render(
'template' => 'search',
'query' => $query,
'rows' => $rows,
'title' => b( $title )->decode('UTF-8'),
);
} # sub search
=head2 change_password
Change password of specified user
=cut
sub change_password {
my $self = shift;
my $login = $self->session( 'login' );
my $redirect = '/change_password'; # Default redirect - to form
my $old_password = $self->param( 'old_password' ) || '';
my $new_password = $self->param( 'new_password' ) || '';
my $repeat_new_password = $self->param( 'repeat_new_password' ) || '';
if ( $new_password ne $repeat_new_password ) {
$self->flash( 'message' => __ 'Passwords does not match' );
$self->redirect_to( $redirect );
return;
}
# else
# Check old password
my $row = $dbh->selectrow_hashref(
q{SELECT id FROM user WHERE login=? AND password=?},
{},
$login, b($old_password)->md5_sum
);
# Wrong old password
unless ( $row ) {
$self->flash( 'message' => __ 'Wrong old password' );
$self->redirect_to( $redirect );
return;
}
my $rows_affected = $dbh->do(
q{UPDATE user SET password=? WHERE id=?},
{},
b($new_password)->md5_sum, $row->{'id'},
);
if ( $rows_affected ) {
$self->flash( 'message' => __ 'Password was changed' );
$redirect = "/profile/$login";
}
else {
$self->flash( 'message' => __ 'Can not change password' );
}
$self->redirect_to( $redirect );
} # sub change_password
=head2 upload_image
Upload image file.
See L<http://d.hatena.ne.jp/yukikimoto/20100212/1265989676> for more details.
=cut
sub upload_image {
my $self = shift;
my $title = $self->param('title') || time;
$title =~ s/\.\w{3,4}$//i; # Trim extension when presented. Images have 3 or 4 letter extension
my $image = $self->req->upload('file');
# Nothing uploaded
return $self->render(
'template' => 'error',
'title' => b( __ 'Upload failed' )->decode( 'UTF-8' ),
'errors' => [ __ 'File is not specified.' ],
)
unless $image;
# Skip oversized
return $self->render(
'template' => 'error',
'title' => b( __ 'Upload failed' )->decode( 'UTF-8' ),
'errors' => [ __ 'Image size is too large.' ],
)
if $ENV{'MOJO_MAX_MESSAGE_SIZE'}
&& $ENV{'MOJO_MAX_MESSAGE_SIZE'} < $image->size;
# Check file type
my %exts = ( 'image/gif' => 'gif', 'image/jpeg' => 'jpg', 'image/png' => 'png' );
my $image_type = $image->headers->content_type;
# Extention
my $ext = $exts{$image_type}
or
# Content type is wrong
return $self->render(
template => 'error',
message => __ 'Upload failed. Content type is wrong.'
);
# Image file
my $image_file = "$IMAGE_DIR/$title.$ext";
# If file is exists, Retry creating filename
my $i = 0;
$image_file = "$IMAGE_DIR/$title." . $i++ . ".$ext"
while -f $image_file;
my $image_link = substr( $image_file, length $IMAGE_DIR );
# Save to file
$image->move_to($image_file);
$dbh->do(
q{INSERT INTO image (filename) VALUES(?)},
{},
$image_link,
);
$self->render(
'title' => b( __ 'Image uploaded' )->decode( 'UTF-8' ),
'base' => $IMAGE_BASE,
'link' => $image_link,
'template' => 'upload_image',
);
} # sub upload_image
=head2 set_language
Set preferred language.
Called as set_language/I<ln> or as set_language?language=I<ln>, where I<ln> is language code.
In version 0.0.3 available 2 languages: English and Russian.
=cut
sub set_language {
my $self = shift;
$self->session( 'language' => $self->param( 'language' ) );
$self->redirect_to( $self->req->headers->referrer || '/' );
} # sub set_language
__DATA__
@@ title.html.ep
% layout 'default';
%= $text
@@ userlist.html.ep
% layout 'default', 'title' => __ 'User list';
<ul>
% foreach my $row (@$rows) {
<li><a href="<%= url_for "/profile/$row->{'login'}" %>"><%= $row->{'login'} %></a></li>
% }
</ul>
@@ login.html.ep
% layout 'default';
% stash 'title' => __ 'Login';
<form method="post" action="<%= url_for '/login' %>">
<dl>
<dt><%= __ 'Username' %></dt><dd><input type="text" name="login"></dd>
<dt><%= __ 'Password' %></dt><dd><input type="password" name="password"></dd>
</dl>
<input type="submit" value="<%= __ 'Login' %>">
</form>
%# } # else
@@ change_password.html.ep
% layout 'default';
%= include 'jquery';
<script type="text/javascript">
function compare_passwords(){
if ( $('#new_password').val() == $('#repeat_new_password').val() ) {
$('#matching').html( '' );
return true;
}
$('#matching').html('<%= __ 'Passwords does not match' %>');
return false;
}
$(document).ready(function() {
// Check strength and matching
$('#new_password').change(function() {
compare_passwords();
// Strong password must be at least 8 characters long
// and must contain letters in other cases, digits and punctuation signs
var np = $('#new_password').val();
var strength = '<%= __ 'Strong password' %>';
if ( np.length == 0 ) {
strength = '<%= __ 'Empty password' %>';
}
else if ( np.length < 8 ) {
strength = '<%= __ 'Too short password' %>';
}
else if ( np == '<%= session 'login' %>' ) {
strength = '<%= __ 'Password and login are equal' %>';
}
else if ( ! np.match(/\d/) ) {
strength = '<%= __ 'Missing digits' %>';
}
else if ( ! np.match(/[a-z]/) ) {
strength = '<%= __ 'Missing small letters' %>';
}
else if ( ! np.match(/[A-Z]/) ) {
strength = '<%= __ 'Missing capital letters' %>';
}
$('#strength').html( strength );
});
// Only check matching when change last field
$('#repeat_new_password').change(function() {
compare_passwords();
});
// Prevent sending wrong data
$('#password_form').submit(function() {
return compare_passwords();
});
});
</script>
% stash 'title' => __ 'Change password';
<form method="post" id="password_form">
<dl>
<dt><%= __ 'Old Password' %></dt><dd><input type="password" name="old_password"></dd>
<dt><%= __ 'New Password' %></dt><dd><input type="password" name="new_password" id="new_password">
<em class="tip" id="strength"></em></dd>
<dt><%= __ 'Repeat new password' %></dt><dd><input type="password" name="repeat_new_password" id="repeat_new_password">
<em class="tip" id="matching"></em></dd>
</dl>
<input type="submit" value="<%= __ 'Change' %>">
</form>
%# } # else
@@ sitemap.html.ep
% layout 'default', 'title' => __ 'Sitemap';
<ul>
% for my $row ( @$rows ) {
% my $link = $row;
% $link =~ tr/ /_/;
% $row ||= __ 'Main page';
<li><a href="<%= url_for "/$link" %>"><%= $row %></a></li>
% }
</ul>
<h2><%= __ 'Special pages' %></h2>
<ul>
<li><a href="<%= url_for '/search' %>"><%= __ 'Search' %></a></li>
<li><a href="<%= url_for '/sitemap' %>"><%= __ 'Sitemap' %></a></li>
<li><a href="<%= url_for '/login' %>"><%= __ 'Login' %></a></li>
</ul>
@@ tags.html.ep
% layout 'default', 'title' => __ 'Tags';
<ul>
% for my $row ( @$rows ) {
% my $link = $row;
% $link =~ tr/ /_/;
<li><a href="<%= url_for "/tags/$link" %>"><%= $row %></a></li>
% }
</ul>
@@ tagstag.html.ep
% layout 'default', 'title' => __x '{tag} tagged images', 'tag' => $tag;
<ul class="gallery">
% for my $row ( @$rows ) {
<li><a href="<%= url_for( $base . '/' . $row->{'filename'} ) %>"><img src="<%= url_for ( $base . '/tn/' . $row->{'filename'} ) %>" alt="<%= $row->{'text'}%>"></a></li>
% }
</ul>
@@ search.html.ep
% layout 'default';
<form action="<%= url_for '/search' %>">
<input type="text" name="query" value="<%= $query %>">
<input type="submit" value="<%= __ 'Find' %>">
</dl>
</form>
<ol>
% for my $row ( @$rows ) {
% my $link = $row;
% $link =~ tr/ /_/;
% $row ||= __ 'Main page';
<li><a href="<%= url_for "/$link" %>"><%= $row %></a></li>
% }
</ol>
@@ upload_image.html.ep
% layout 'default';
%# my $base = param 'base';
<p><%= __x 'Image uploaded and saved as {link}', 'link' => url_for $link %>
<br><img src="<%= url_for "$base/$link" %>" alt="<%= __ 'Just uploaded' %>"></p>
<p><%= __ 'You can insert this image to pages with following code:' %></p>
<blockquote><code>!<%= url_for "$base/$link" %>!</code></blockquote>
@@ not_found.html.ep
% layout 'error';
% stash 'title' => __ 'Not Found - ERROR 404';
<h1><%= __ 'Not found' %></h1><p><%= __ 'Document you requested was not found.' %></p>
<ul>
% my $article_title = param 'title';
<li><a href="<%= url_for "/search/$article_title" %>"><%== __x 'Search for “{title}”', 'title' => $article_title %></a></li>
% if ( session 'login' ) {
<li><a href="<%= url_for "/create/$article_title" %>"><%== __x 'Create page “{title}”', 'title' => $article_title %></a></li>
% }
</ul>
@@ imagestitle.html.ep
% layout 'error';
% stash 'title' => 'Not Found - ERROR 404';
<h1><%= __ 'Not found' %></h1><p><%= __ 'Image you requested was not found.' %></p>
<ul>
% my $article_title = param 'title';
% my $base = param 'base';
<li><a href="<%= url_for "/search/$article_title" %>"><%== __x 'Search for “{title}”', 'title' => $article_title %></a></li>
% if ( session 'login' ) {
<li><form action="<%= url_for "$base/$article_title" %>" method="post" enctype ="multipart/form-data"><%= __ 'Choose an image' %>
<input type="file" name="file"> <%= __ 'and' %>
<input type="submit" value="<%= __ 'Upload' %>"></form></li>
% }
</ul>
@@ edit_article.html.ep
% layout 'default';
% use Mojo::ByteStream 'b';
%= include 'jquery';
<script type="text/javascript" src="<%= url_for '/jquery-ui-1.8.11.custom.min.js' %>"></script>
<script type="text/javascript" src="<%= url_for '/jquery.caret.1.02.min.js' %>"></script>
<script type="text/javascript">
function wrap(prefix, suffix) {
with ( $('#article') ) {
var inner = caret().text;
var start = caret().start;
var end = caret().end;
val( caret().replace( prefix + inner + suffix ) );
caret({ start: start+prefix.length, end:end+prefix.length });
}
return false;
}
$(document).ready(function() {
// Tie onclick action to all .ajax links
$('a#preview_link').click(function() {
$.ajax({
url: '<%= url_for '/preview' %>',
type: 'post',
data: { article: $('#article').val() },
dataType: 'html',
success: function(data, textStatus) {
$('div#preview_area').html(data);
$('div#preview_area').show();
},
error: function() {
alert('Can not preview');
}
});
return false;
});
$('#article').resizable({
handles: 's'
});
$('#btn_bold').click(function(){ wrap('*', '*') });
$('#btn_italic').click(function(){ wrap('_', '_') });
$('#btn_h1').click(function(){ wrap('\n\nh1. ', '\n\n') });
$('#btn_h2').click(function(){ wrap('\n\nh2. ', '\n\n') });
$('#btn_h3').click(function(){ wrap('\n\nh3. ', '\n\n') });
$('#btn_ul').click(function(){ wrap('\n\n* ', '\n\n') });
$('#btn_ol').click(function(){ wrap('\n\n# ', '\n\n') });
$('#article').keypress(function(e){
switch (e.which) {
case 2: wrap('*', '*'); break;
case 9: wrap('_', '_'); break;
}
});
});
</script>
<style type="text/css">
.ui-resizable { position: relative}
.ui-resizable-handle { position: absolute;font-size: 0.1px;z-index: 99999; display: block;}
.ui-resizable-disabled .ui-resizable-handle, .ui-resizable-autohide .ui-resizable-handle { display: none; }
.ui-resizable-s { cursor: s-resize; height: 20px; width: 100%; bottom: 11px; left: 0;background:url(<%= url_for '/grip.gif' %>) no-repeat right top;margin:0 -4px }
#toolbar a { margin: 0 2px; padding: 0 0.3em; border: 1px outset #999; text-decoration:none; color: #000 }
button { background:url(<%= url_for '/toolbar.gif' %>) no-repeat 0 50%;width:22px;height:22px;text-indent:-9999em;border:1px outset #999;-moz-border-radius:2px;border-radius:3px}
button:hover{border-color:#69c}
#btn_italic { background-position: -20px 50% }
#btn_h1 { background-position: -40px 50% }
#btn_h2 { background-position: -60px 50% }
#btn_h3 { background-position: -80px 50% }
#btn_ul { background-position: -100px 50% }
#btn_ol { background-position: -120px 50% }
</style>
% if ( my $errors = stash 'errors' ) {
<ul id="errors">
% for my $error ( @$errors ) {
<li><%= b( $error )->decode('UTF-8')->to_string %></li>
% }
</ul>
% } # if
<form method="post" action="<%= url_for "/edit/$id" %>">
<dl>
<dt><%= __ 'Title' %></dt>
<dd><input type="text" name="title" value="<%= $article_title %>"></dd>
<dt><%= __ 'Content' %></dt>
<dd><span id="toolbar">
<button type="button" id="btn_bold" title="<%= __ 'Bold (Ctrl+B)' %>"><%= __ 'Bold' %></button>
<button type="button" id="btn_italic" title="<%= __ 'Italic (Ctrl+I)' %>"><%= __ 'Italic' %></button>
<button type="button" id="btn_h1" title="<%= __ 'Header 1' %>"><%= __ 'Header 1' %></button>
<button type="button" id="btn_h2" title="<%= __ 'Header 2' %>"><%= __ 'Header 2' %></button>
<button type="button" id="btn_h3" title="<%= __ 'Header 3' %>"><%= __ 'Header 3' %></button>
<button type="button" id="btn_ul" title="<%= __ 'Unordered list item' %>"><%= __ 'Unordered list item' %></button>
<button type="button" id="btn_ol" title="<%= __ 'Ordered list item' %>"><%= __ 'Ordered list item' %></button>
</span>
<br><textarea name="article" id="article"><%= $article_content %></textarea>
<br><a href="http://search.cpan.org/perldoc?Text::Textile#SYNTAX"><%= __ 'textile syntax' %></a>
</dd>
</dl>
<input type="submit" value="<%= __ 'Save' %>">
<a href="#" id="preview_link"><%= __ 'Preview' %></a>
% if ( my $old_title = stash 'old_title' ) {
<a href="<%= url_for "/$old_title" %>"><%= __ 'Cancel' %></a>