From c2d12f79e78eb4ea909cac6106880c0f41e8c5fd Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 20 Jan 2019 09:29:08 +0000 Subject: $INBOX/_/text/color/ and sample user-side CSS Since we now support more CSS classes for coloring, give this feature more visibility. --- lib/PublicInbox/UserContent.pm | 78 ++++++++++++++++++++++++++++++++++++++++++ lib/PublicInbox/WWW.pm | 17 ++++++--- lib/PublicInbox/WwwStream.pm | 2 ++ lib/PublicInbox/WwwText.pm | 35 +++++++++++++++++++ 4 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 lib/PublicInbox/UserContent.pm (limited to 'lib') diff --git a/lib/PublicInbox/UserContent.pm b/lib/PublicInbox/UserContent.pm new file mode 100644 index 00000000..b34ebf91 --- /dev/null +++ b/lib/PublicInbox/UserContent.pm @@ -0,0 +1,78 @@ +# Copyright (C) 2019 all contributors +# License: AGPL-3.0+ + +# 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; + +# this sub is updated automatically: +sub CSS () { + <<'_' + /* + * Dark color scheme using 216 web-safe colors, inspired + * somewhat by the default color scheme in mutt. + * It reduces eyestrain for me, and energy usage for all: + * https://en.wikipedia.org/wiki/Light-on-dark_color_scheme + */ + * { background:#000; color:#ccc } + + /* + * Underlined links add visual noise which make them hard-to-read. + * Use colors to make them stand out, instead. + */ + a { color:#69f; text-decoration:none } + a:visited { color:#96f } + + /* quoted text gets a different color */ + *.q { color:#09f } + + /* + * these may be used with cgit, too + * (cgit uses
, public-inbox uses ) + */ + *.add { color:#0ff } + *.del { color:#f0f } + *.head { color:#fff } + *.hunk { color:#c93 } +_ +} +# end of auto-updated sub + +# return a sample CSS +sub sample ($$) { + my ($ibx, $env) = @_; + my $url_prefix = $ibx->base_url($env); + my $preamble = <<""; +/* + * Firefox users: this goes in \$PROFILE_FOLDER/chrome/userContent.css + * where \$PROFILE_FOLDER is platform-specific + * + * cf. http://kb.mozillazine.org/UserContent.css + * http://kb.mozillazine.org/Profile_folder_-_Firefox + * + * Users of dillo can remove the entire lines with "moz-only" + * in them and place the resulting file in ~/.dillo/style.css + */ +\@-moz-document url-prefix($url_prefix) { /* moz-only */ + + $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__) { + use autodie; + open my $ro, '<', $ARGV[0]; + my $css = do { local $/; <$ro> }; + $css =~ s/^([ \t]*\S)/\t$1/smg; + open my $rw, '+<', __FILE__; + my $out = do { local $/; <$rw> }; + $out =~ s/^sub CSS.*^_\n\}/sub CSS () {\n\t<<'_'\n${css}_\n}/sm; + seek $rw, 0, 0; + print $rw $out; +} + +1; diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm index 863da85a..406802a9 100644 --- a/lib/PublicInbox/WWW.pm +++ b/lib/PublicInbox/WWW.pm @@ -20,6 +20,7 @@ use URI::Escape qw(uri_unescape); use PublicInbox::MID qw(mid_escape); require PublicInbox::Git; use PublicInbox::GitHTTPBackend; +use PublicInbox::UserContent; # TODO: consider a routing tree now that we have more endpoints: our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!; @@ -120,7 +121,7 @@ sub call { } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) { get_text($ctx, $1, $2); } elsif ($path_info =~ m!$INBOX_RE/([\w\-\.]+)\.css\z!o) { - get_css($self, $2); + get_css($ctx, $1, $2); } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) { get_vcs_object($ctx, $1, $2); } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/([\w\.\-]+)\z!o) { @@ -577,10 +578,18 @@ sub style { # CSS is configured globally for all inboxes, but we access them on # a per-inbox basis. This allows administrators to setup per-inbox # static routes to intercept the request before it hits PSGI -sub get_css ($$) { - my ($self, $key) = @_; +sub get_css ($$$) { + my ($ctx, $inbox, $key) = @_; + my $r404 = invalid_inbox($ctx, $inbox); + return $r404 if $r404; + my $self = $ctx->{www}; my $css_map = $self->{-css_map} || stylesheets_prepare($self, ''); - defined(my $css = $css_map->{$key}) or return r404(); + my $css = $css_map->{$key}; + if (!defined($css) && $key eq 'userContent') { + my $env = $ctx->{env}; + $css = PublicInbox::UserContent::sample($ctx->{-inbox}, $env); + } + defined $css or return r404(); my $h = [ 'Content-Length', bytes::length($css), 'Content-Type', 'text/css' ]; PublicInbox::GitHTTPBackend::cache_one_year($h); diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm index c3aeb6b0..8ae35c73 100644 --- a/lib/PublicInbox/WwwStream.pm +++ b/lib/PublicInbox/WwwStream.pm @@ -38,10 +38,12 @@ sub _html_top ($) { my $title = $ctx->{-title_html} || $desc; my $upfx = $ctx->{-upfx} || ''; my $help = $upfx.'_/text/help'; + my $color = $upfx.'_/text/color'; my $atom = $ctx->{-atom} || $upfx.'new.atom'; my $tip = $ctx->{-html_tip} || ''; my $top = "$desc"; my $links = "help / ". + "color / ". "Atom feed"; if ($obj->search) { my $q_val = $ctx->{-q_value_html}; diff --git a/lib/PublicInbox/WwwText.pm b/lib/PublicInbox/WwwText.pm index b5874cf6..d3413ad7 100644 --- a/lib/PublicInbox/WwwText.pm +++ b/lib/PublicInbox/WwwText.pm @@ -88,9 +88,44 @@ sub _srch_prefix ($$) { 1; } +sub _colors_help ($$) { + my ($ctx, $txt) = @_; + my $ibx = $ctx->{-inbox}; + my $base_url = $ibx->base_url($ctx->{env}); + $$txt .= "color customization for $base_url\n"; + $$txt .= <{-inbox}; -- cgit v1.2.3-24-ge0c7