| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 1 | package Kalamar::Plugin::Plugins; |
| 2 | use Mojo::Base 'Mojolicious::Plugin'; |
| Akron | baae980 | 2021-02-27 18:32:15 +0100 | [diff] [blame] | 3 | use Mojo::JSON 'decode_json'; |
| 4 | use Mojo::File 'path'; |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 5 | |
| 6 | # Register the plugin |
| 7 | sub 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 | |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 15 | my @json_array = (); |
| 16 | |
| 17 | # Legacy support |
| 18 | $param->{default_file} //= $param->{default_plugins}; |
| 19 | |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 20 | # There are default plugins to be registered |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 21 | if ($param->{default_file}) { |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 22 | |
| 23 | # Read default plugins file |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 24 | my $default = path($param->{default_file}); |
| Akron | baae980 | 2021-02-27 18:32:15 +0100 | [diff] [blame] | 25 | |
| 26 | # Use correct working directory |
| 27 | $default = $app->home->child($default) unless $default->is_abs; |
| 28 | |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 29 | @json_array = @{ decode_json $default->slurp }; |
| 30 | }; |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 31 | |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 32 | # 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 | } |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 37 | |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 38 | else { |
| 39 | push @json_array, @{$param->{default}}; |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 40 | }; |
| 41 | }; |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 42 | |
| 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 | }; |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | |
| 75 | 1; |
| 76 | |
| 77 | |
| 78 | __END__ |
| 79 | |
| 80 | =pod |
| 81 | |
| 82 | =encoding utf8 |
| 83 | |
| 84 | =head1 NAME |
| 85 | |
| 86 | Kalamar::Plugin::Plugins - Register plugins in the user interface |
| 87 | |
| 88 | =head1 DESCRIPTION |
| 89 | |
| 90 | L<Kalamar::Plugin::Plugins> is an interface to register embedded plugins |
| 91 | in the Kalamar user interface. |
| 92 | |
| 93 | B<WARNING! This is early software and not ready to use!> |
| 94 | |
| 95 | =head1 CONFIGURATION |
| 96 | |
| 97 | L<Kalamar::Plugin::Plugins> supports the following parameter for the |
| 98 | C<Kalamar-Plugins> configuration section in the Kalamar configuration: |
| 99 | |
| 100 | =over 2 |
| 101 | |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 102 | =item B<default_file> |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 103 | |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 104 | Path for default plugins (mandatory to all users) to register in the |
| 105 | frontend. |
| 106 | |
| 107 | =item B<default> |
| 108 | |
| 109 | Array of default plugins (mandatory to all users) to register in the |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 110 | frontend. |
| 111 | |
| 112 | =back |
| 113 | |
| 114 | =head2 COPYRIGHT AND LICENSE |
| 115 | |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 116 | Copyright (C) 2021-2025, L<IDS Mannheim|http://www.ids-mannheim.de/> |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 117 | Author: L<Nils Diewald|http://nils-diewald.de/> |
| 118 | |
| 119 | Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/> |
| 120 | Corpus Analysis Platform at the |
| 121 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 122 | member of the |
| 123 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de> |
| 124 | and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project, |
| 125 | funded by the |
| 126 | L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>. |
| 127 | |
| 128 | Kalamar is free software published under the |
| 129 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>. |
| 130 | |
| 131 | =cut |