]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
user_content: simplify internal API and use v5.12
authorEric Wong <e@80x24.org>
Thu, 26 Sep 2024 00:55:01 +0000 (00:55 +0000)
committerEric Wong <e@80x24.org>
Sat, 28 Sep 2024 20:39:01 +0000 (20:39 +0000)
We use {env} and {ibx} everywhere so there's no point in
unpacking args.  There's no odd unicode_strings problems
here, either, so we can use v5.12 and autodie to reduce
`or die' checks.

lib/PublicInbox/UserContent.pm
lib/PublicInbox/WWW.pm
lib/PublicInbox/WwwText.pm

index f28610f7f9b25833943f790c8e81bb13e7d9e0aa..9dacfa0bef6f9dee350a89e7cd6e2ab9ba795251 100644 (file)
@@ -1,11 +1,11 @@
-# Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # Self-updating module containing a sample CSS for client-side
 # customization by users of public-inbox.  Used by Makefile.PL
 package PublicInbox::UserContent;
-use strict;
-use warnings;
+use v5.12;
+use autodie qw(close open seek);
 
 # this sub is updated automatically:
 sub CSS () {
@@ -71,9 +71,9 @@ _
 # end of auto-updated sub
 
 # return a sample CSS
-sub sample ($$) {
-       my ($ibx, $env) = @_;
-       my $url_prefix = $ibx->base_url($env);
+sub sample ($) {
+       my ($ctx) = @_;
+       my $url_prefix = $ctx->{ibx}->base_url($ctx->{env});
        my $preamble = <<"";
 /*
  * Firefox users: this goes in \$PROFILE_FOLDER/chrome/userContent.css
@@ -87,15 +87,16 @@ sub sample ($$) {
  */
 \@-moz-document url-prefix($url_prefix) { /* moz-only */
 
-       $preamble . CSS() . "\n} /* moz-only */\n";
+       $preamble . CSS . "\n} /* moz-only */\n";
 }
 
 # Auto-update this file based on the contents of a CSS file:
 # usage: perl -I lib __FILE__ contrib/css/216dark.css
 # (See Makefile.PL)
 if (scalar(@ARGV) == 1 && -r __FILE__) {
-       open my $ro, '<', $ARGV[0] or die $!;
-       my $css = do { local $/; <$ro> } or die $!;
+       require PublicInbox::IO;
+       open my $ro, '<', $ARGV[0];
+       my $css = PublicInbox::IO::read_all($ro);
 
        # indent one level:
        $css =~ s/^([ \t]*\S)/\t$1/smg;
@@ -104,13 +105,13 @@ if (scalar(@ARGV) == 1 && -r __FILE__) {
        $css =~ s/;/ !important;/sg;
        $css =~ s/(\w) \}/$1 !important }/msg;
 
-       open my $rw, '+<', __FILE__ or die $!;
-       my $out = do { local $/; <$rw> } or die $!;
+       open my $rw, '+<', __FILE__;
+       my $out = PublicInbox::IO::read_all($rw);
        $css =~ s/; /;\n\t\t/g;
        $out =~ s/^sub CSS.*^_\n\}/sub CSS () {\n\t<<'_'\n${css}_\n}/sm;
        seek $rw, 0, 0;
-       print $rw $out or die $!;
-       close $rw or die $!;
+       print $rw $out;
+       close $rw;
 }
 
 1;
index 1bc2966d3e893b82d3907e32d0cb52b139c5ba15..32d0410c630da43333cd88243154c1de3b0f0fe0 100644 (file)
@@ -681,8 +681,7 @@ sub get_css ($$$) {
                stylesheets_prepare($self, defined($inbox) ? '' : '+/');
        my $css = $css_map->{$key};
        if (!defined($css) && defined($inbox) && $key eq 'userContent') {
-               my $env = $ctx->{env};
-               $css = PublicInbox::UserContent::sample($ctx->{ibx}, $env);
+               $css = PublicInbox::UserContent::sample($ctx);
        }
        defined $css or return r404();
        my $h = [ 'Content-Length', length($css), 'Content-Type', 'text/css' ];
index 20b2213658858d85ffe91fad3b13ffb3d12e15ef..79fe46aab4185f67e146a9a859bd48ac8cd318cd 100644 (file)
@@ -77,9 +77,7 @@ sub get_text {
 
 sub _colors_help ($$) {
        my ($ctx, $txt) = @_;
-       my $ibx = $ctx->{ibx};
-       my $env = $ctx->{env};
-       my $base_url = $ibx->base_url($env);
+       my $base_url = $ctx->{ibx}->base_url($ctx->{env});
        $$txt .= "color customization for $base_url\n";
        $$txt .= <<EOF;
 
@@ -96,7 +94,7 @@ CSS sample
 ----------
 ```css
 EOF
-       $$txt .= PublicInbox::UserContent::sample($ibx, $env) . "```\n";
+       $$txt .= PublicInbox::UserContent::sample($ctx) . "```\n";
 }
 
 # git-config section names are quoted in the config file, so escape them