Skip to content

Commit f0fbd16

Browse files
committed
Add theme & translation system + use Etherpad instead of Etherpad::API
1 parent 69337a3 commit f0fbd16

21 files changed

+193
-16
lines changed

cpanfile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
requires "Mojolicious";
22
requires "Mojolicious::Plugin::I18N";
33
requires "Mojo::Pg";
4-
requires "Minion";
5-
requires "Etherpad::API";
6-
requires "Term::ProgressBar";
4+
requires 'Minion', '== 4.06';
5+
requires 'Etherpad';
6+
requires 'Term::ProgressBar';
7+
requires 'IO::Socket::SSL';
8+
requires 'Locale::Maketext';
9+
requires 'Locale::Maketext::Extract';
10+
requires 'Mojolicious::Plugin::DebugDumperHelper';

lib/Mounter.pm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Mounter;
2+
use Mojo::Base 'Mojolicious';
3+
use FindBin qw($Bin);
4+
use File::Spec qw(catfile);
5+
6+
# This method will run once at server start
7+
sub startup {
8+
my $self = shift;
9+
10+
push @{$self->commands->namespaces}, 'Padro::Command';
11+
12+
my $config = $self->plugin('Config' =>
13+
{
14+
file => File::Spec->catfile($Bin, '..' ,'padro.conf'),
15+
default => {
16+
prefix => '/',
17+
theme => 'default',
18+
}
19+
}
20+
);
21+
22+
# Themes handling
23+
shift @{$self->static->paths};
24+
if ($config->{theme} ne 'default') {
25+
my $theme = $self->home->rel_dir('themes/'.$config->{theme});
26+
push @{$self->static->paths}, $theme.'/public' if -d $theme.'/public';
27+
}
28+
push @{$self->static->paths}, $self->home->rel_dir('themes/default/public');
29+
30+
$self->plugin('Mount' => {$config->{prefix} => File::Spec->catfile($Bin, '..', 'script', 'application')});
31+
}
32+
33+
1;

lib/Padro.pm

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package Padro;
22
use Mojo::Base 'Mojolicious';
33
use Mojo::Pg;
44
use Mojo::Pg::Migrations;
5-
use Etherpad::API;
5+
use Etherpad;
66

77
# This method will run once at server start
88
sub startup {
@@ -24,36 +24,52 @@ sub startup {
2424
$addr .= $self->config->{minion}->{user};
2525
$addr .= ':'.$self->config->{minion}->{pwd};
2626
$addr .= '@'.$self->config->{minion}->{host};
27+
$addr .= ':'.$self->config->{minion}->{port} if defined $self->config->{minion}->{port};
2728
$addr .= '/'.$self->config->{minion}->{database};
2829
$self->plugin('Minion' => {Pg => $addr});
2930

31+
# Themes handling
32+
shift @{$self->renderer->paths};
33+
shift @{$self->static->paths};
34+
if ($config->{theme} ne 'default') {
35+
my $theme = $self->home->rel_dir('themes/'.$config->{theme});
36+
push @{$self->renderer->paths}, $theme.'/templates' if -d $theme.'/templates';
37+
push @{$self->static->paths}, $theme.'/public' if -d $theme.'/public';
38+
}
39+
push @{$self->renderer->paths}, $self->home->rel_dir('themes/default/templates');
40+
push @{$self->static->paths}, $self->home->rel_dir('themes/default/public');
41+
42+
# Internationalization
43+
my $lib = $self->home->rel_dir('themes/'.$config->{theme}.'/lib');
44+
eval qq(use lib "$lib");
45+
$self->plugin('I18N');
46+
47+
# Debug
48+
$self->plugin('DebugDumperHelper');
49+
3050
# Add new MIME type
3151
$self->types->type(txt => 'text/plain; charset=utf-8');
3252

3353
# Helpers
34-
$self->helper(
35-
debug => sub {
36-
my $c = shift;
37-
$c->app->log->debug($c->dumper(\@_));
38-
}
39-
);
40-
4154
$self->helper(
4255
pg => sub {
4356
my $c = shift;
4457
my $addr = 'postgresql://';
45-
$addr .= $c->config->{db}->{user};
46-
$addr .= ':'.$c->config->{db}->{pwd};
47-
$addr .= '@'.$c->config->{db}->{host};
58+
$addr .= $c->config->{db}->{host};
59+
$addr .= ':'.$c->config->{db}->{port} if defined $c->config->{db}->{port};
4860
$addr .= '/'.$c->config->{db}->{database};
4961
state $pg = Mojo::Pg->new($addr);
62+
$pg->password($c->config->{db}->{pwd});
63+
$pg->username($c->config->{db}->{user});
64+
return $pg;
5065
}
5166
);
5267

5368
$self->helper(
5469
ep => sub {
5570
my $c = shift;
56-
state $ep = Etherpad::API->new($c->config->{ep});
71+
state $ep = Etherpad->new($c->config->{ep});
72+
return $ep;
5773
}
5874
);
5975

@@ -163,6 +179,9 @@ sub startup {
163179
}
164180
);
165181

182+
# Check Etherpad connection
183+
$self->app->ep->check_token();
184+
166185
# Database migration
167186
my $migrations = Mojo::Pg::Migrations->new(pg => $self->pg);
168187
if ($self->mode eq 'development') {

lib/Padro/Command/theme.pm

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
2+
package Padro::Command::theme;
3+
use Mojo::Base 'Mojolicious::Commands';
4+
use FindBin qw($Bin);
5+
use File::Spec qw(catfile cat dir);
6+
use File::Path qw(make_path);
7+
8+
has description => 'Create new theme skeleton.';
9+
has usage => sub { shift->extract_usage };
10+
has message => sub { shift->extract_usage . "\nCreate new theme skeleton:\n" };
11+
has namespaces => sub { ['Padro::Command::theme'] };
12+
13+
sub run {
14+
my $c = shift;
15+
my $name = shift;
16+
17+
unless (defined $name) {
18+
say $c->extract_usage;
19+
exit 1;
20+
}
21+
22+
my $home = File::Spec->catdir($Bin, '..', 'themes', $name);
23+
24+
unless (-d $home) {
25+
26+
# Create skeleton
27+
mkdir $home;
28+
mkdir File::Spec->catdir($home, 'public');
29+
make_path(File::Spec->catdir($home, 'templates', 'layouts'));
30+
make_path(File::Spec->catdir($home, 'lib', 'Padro', 'I18N'));
31+
32+
my $i18n = <<EOF;
33+
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
34+
package Padro::I18N;
35+
36+
use base 'Locale::Maketext';
37+
use File::Basename qw/dirname/;
38+
use Locale::Maketext::Lexicon {
39+
_auto => 1,
40+
_decode => 1,
41+
_style => 'gettext',
42+
'*' => [
43+
Gettext => dirname(__FILE__) . '/I18N/*.po',
44+
Gettext => dirname(__FILE__) . '/../../../default/lib/Padro/I18N/*.po',
45+
]
46+
};
47+
48+
1;
49+
EOF
50+
51+
open my $f, '>', File::Spec->catfile($home, 'lib', 'Padro', 'I18N.pm') or die "Unable to open $home/lib/Padro/I18N.pm: $!";
52+
print $f $i18n;
53+
close $f;
54+
55+
my $makefile = <<EOF;
56+
EN=lib/Padro/I18N/en.po
57+
FR=lib/Padro/I18N/fr.po
58+
SEDOPTS=-e "s\@SOME DESCRIPTIVE TITLE\@Padro language file\@" \\
59+
-e "s\@YEAR THE PACKAGE'S COPYRIGHT HOLDER\@2015 Luc Didry\@" \\
60+
-e "s\@CHARSET\@utf8\@" \\
61+
-e "s\@the PACKAGE package\@the Padro package\@" \\
62+
-e '/^\\#\\. (/{N;/\\n\\#\\. (/{N;/\\n.*\\.\\.\\/default\\//{s/\\#\\..*\\n.*\\#\\./\\#. (/g}}}' \\
63+
-e '/^\\#\\. (/{N;/\\n.*\\.\\.\\/default\\//{s/\\n/ /}}'
64+
SEDOPTS2=-e '/^\\#.*\\.\\.\\/default\\//,+3d'
65+
XGETTEXT=carton exec ../../local/bin/xgettext.pl
66+
CARTON=carton exec
67+
68+
locales:
69+
\$(XGETTEXT) -D templates -D ../default/templates -o \$(EN) 2>/dev/null
70+
\$(XGETTEXT) -D templates -D ../default/templates -o \$(FR) 2>/dev/null
71+
sed \$(SEDOPTS) -i \$(EN)
72+
sed \$(SEDOPTS2) -i \$(EN)
73+
sed \$(SEDOPTS) -i \$(FR)
74+
sed \$(SEDOPTS2) -i \$(FR)
75+
EOF
76+
77+
open $f, '>', File::Spec->catfile($home, 'Makefile') or die "Unable to open $home/Makefile: $!";
78+
print $f $makefile;
79+
close $f;
80+
} else {
81+
say "$name theme already exists. Aborting.";
82+
exit 1;
83+
}
84+
}
85+
86+
=encoding utf8
87+
88+
=head1 NAME
89+
90+
Padro::Command::theme - Create new theme skeleton.
91+
92+
=head1 SYNOPSIS
93+
94+
Usage: script/padro theme THEME_NAME
95+
96+
Your new theme will be available in the themes directory.
97+
=cut
98+
99+
1;

script/application

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use lib 'lib';
7+
8+
# Start command line interface for application
9+
require Mojolicious::Commands;
10+
Mojolicious::Commands->start_app('Padro');

script/padro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ BEGIN { unshift @INC, "$FindBin::Bin/../lib" }
88

99
# Start command line interface for application
1010
require Mojolicious::Commands;
11-
Mojolicious::Commands->start_app('Padro');
11+
Mojolicious::Commands->start_app('Mounter');

themes/default/lib/Padro/I18N.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Padro::I18N;
2+
3+
use base 'Locale::Maketext';
4+
use File::Basename qw/dirname/;
5+
use Locale::Maketext::Lexicon {
6+
_auto => 1,
7+
_decode => 1,
8+
_style => 'gettext',
9+
'*' => [Gettext => dirname(__FILE__) . '/I18N/*.po']
10+
};
11+
12+
1;
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)