blob: a2a81d32aa14acab90d33708daa404904baef8af [file] [log] [blame]
Akronccd9d7d2020-09-22 09:51:19 +02001package Kalamar::Plugin::Plugins;
2use Mojo::Base 'Mojolicious::Plugin';
Akronbaae9802021-02-27 18:32:15 +01003use Mojo::JSON 'decode_json';
4use Mojo::File 'path';
Akronccd9d7d2020-09-22 09:51:19 +02005
6# Register the plugin
7sub register {
8 my ($plugin, $app, $param) = @_;
9
10 # Load parameter from config file
11 if (my $config_param = $app->config('Kalamar-Plugins')) {
12 $param = { %$param, %$config_param };
13 };
14
Akronf412d442025-10-01 10:52:39 +020015 my @json_array = ();
16
17 # Legacy support
18 $param->{default_file} //= $param->{default_plugins};
19
Akronccd9d7d2020-09-22 09:51:19 +020020 # There are default plugins to be registered
Akronf412d442025-10-01 10:52:39 +020021 if ($param->{default_file}) {
Akronccd9d7d2020-09-22 09:51:19 +020022
23 # Read default plugins file
Akronf412d442025-10-01 10:52:39 +020024 my $default = path($param->{default_file});
Akronbaae9802021-02-27 18:32:15 +010025
26 # Use correct working directory
27 $default = $app->home->child($default) unless $default->is_abs;
28
Akronf412d442025-10-01 10:52:39 +020029 @json_array = @{ decode_json $default->slurp };
30 };
Akronccd9d7d2020-09-22 09:51:19 +020031
Akronf412d442025-10-01 10:52:39 +020032 # There are default plugins to be registered
33 if ($param->{default}) {
34 if (ref $param->{default} ne 'ARRAY') {
35 push @json_array, $param->{default};
36 }
Akronccd9d7d2020-09-22 09:51:19 +020037
Akronf412d442025-10-01 10:52:39 +020038 else {
39 push @json_array, @{$param->{default}};
Akronccd9d7d2020-09-22 09:51:19 +020040 };
41 };
Akronf412d442025-10-01 10:52:39 +020042
43 @json_array = grep { $_ ne 'PLUGIN_STUB' } @json_array;
44
45 # If any scripts are defined
46 if (@json_array > 0) {
47
48 # TODO:
49 # Add user registered plugins as a path
50
51 # TODO:
52 # Add sources to CORS.
53
54 # Add default plugins, if exist
55 $app->routes->get('/settings/plugin/list.json')->to(
56 cb => sub {
57 my $c = shift;
58 $c->res->headers->cache_control('no-cache');
59 $c->render(
60 json => \@json_array
61 );
62 }
63 )->name('plugin_list');
64
65 $app->content_block(
66 scripts => {
67 inline => q!<span id="kalamar-plugins" ! .
68 q!data-plugins="<%== url_for 'plugin_list' %>"></span>!
69 }
70 );
71 };
Akronccd9d7d2020-09-22 09:51:19 +020072};
73
74
751;
76
77
78__END__
79
80=pod
81
82=encoding utf8
83
84=head1 NAME
85
86Kalamar::Plugin::Plugins - Register plugins in the user interface
87
88=head1 DESCRIPTION
89
90L<Kalamar::Plugin::Plugins> is an interface to register embedded plugins
91in the Kalamar user interface.
92
93B<WARNING! This is early software and not ready to use!>
94
95=head1 CONFIGURATION
96
97L<Kalamar::Plugin::Plugins> supports the following parameter for the
98C<Kalamar-Plugins> configuration section in the Kalamar configuration:
99
100=over 2
101
Akronf412d442025-10-01 10:52:39 +0200102=item B<default_file>
Akronccd9d7d2020-09-22 09:51:19 +0200103
Akronf412d442025-10-01 10:52:39 +0200104Path for default plugins (mandatory to all users) to register in the
105frontend.
106
107=item B<default>
108
109Array of default plugins (mandatory to all users) to register in the
Akronccd9d7d2020-09-22 09:51:19 +0200110frontend.
111
112=back
113
114=head2 COPYRIGHT AND LICENSE
115
Akronf412d442025-10-01 10:52:39 +0200116Copyright (C) 2021-2025, L<IDS Mannheim|http://www.ids-mannheim.de/>
Akronccd9d7d2020-09-22 09:51:19 +0200117Author: L<Nils Diewald|http://nils-diewald.de/>
118
119Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/>
120Corpus Analysis Platform at the
121L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
122member of the
123L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de>
124and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project,
125funded by the
126L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>.
127
128Kalamar is free software published under the
129L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>.
130
131=cut