diff --git a/HexGrid.pm b/HexGrid.pm index 77bf7c9..e021190 100644 --- a/HexGrid.pm +++ b/HexGrid.pm @@ -16,6 +16,8 @@ use Data::Dumper; use feature "signatures"; no warnings "experimental::signatures"; +my $DEBUG = 0; + has regions => (is => 'rw', default => sub { {} }); has images => (is => 'rw', default => sub{ {} }); @@ -26,6 +28,7 @@ has defaults => (is => 'rw', default => sub { {} }); has make_popups => (is => 'rw', default => 1, alias => 'popups'); has popup_class => (is => 'rw', default => 'pin-popup'); has hidden_popups => (is => 'rw', default => 1, alias => 'popups_are_hidden'); +has embed_images => (is => 'rw', default => 1); sub tile_width($this) { 2 * $this->{sideLength} } sub tile_height($this) { sqrt(3) * $this->{sideLength} } @@ -48,7 +51,8 @@ sub add_image($this, $name, $source) source => $source, id => "${name}_img", width => 1, - height => 1 + height => 1, + fetch => $this->embed_images ); } diff --git a/HexGrid/Image.pm b/HexGrid/Image.pm index f655ba4..a0e7ac4 100644 --- a/HexGrid/Image.pm +++ b/HexGrid/Image.pm @@ -4,10 +4,15 @@ use v5.30; use Moo; use MooX::Aliases; +use LWP::UserAgent; +use MIME::Base64; use feature "signatures"; no warnings "experimental::signatures"; +my $DEBUG = 0; + +my $USER_AGENT; has source => (is => 'rw', required => 1, alias => [qw(src url source_url)]); has fetch => (is => 'rw', default => 0); @@ -18,10 +23,22 @@ has height => (is => 'rw', required => 1); sub render($this, $container) { # hard coded into external URL mode - my $image_element = $container->image(id => $this->{id}, href => $this->{source}); + my $image_element = $container->image(id => $this->{id}); + $image_element->{href} = $this->{fetch} ? $this->_fetch_base64() : $this->{source}; $image_element->{width} = $this->{width} if defined($this->{width}); $image_element->{height} = $this->{height} if defined($this->{height}); return $image_element; } +sub _fetch_base64($this) +{ + $USER_AGENT // ($USER_AGENT = LWP::UserAgent->new(timeout => 10)); + say STDERR "Fetching $this->{source}"; + my $response = $USER_AGENT->get($this->{source}); + my $content_type = $response->headers->content_type; + my $encoded_content = encode_base64($response->content); + return "data:$content_type;base64, $encoded_content"; +} + +sub DEBUG { $DEBUG = 1; } 1; diff --git a/wiki-map.pl b/wiki-map.pl index 03f4833..275c847 100644 --- a/wiki-map.pl +++ b/wiki-map.pl @@ -14,7 +14,7 @@ use Data::Dumper; use feature "signatures"; no warnings "experimental::signatures"; -# The below regex is a whitespace forgiving version of /^(-?\d+),(-?\d+)/, an int pair +# This regex is a whitespace forgiving version of /^(-?\d+),(-?\d+)/, an int pair my $coords_regex = qr/^\s*(-?\s*\d+)\s*,\s*(-?\s*\d+)\s*$/; @@ -27,6 +27,7 @@ my $site_template_name = "MapSite"; my $border_width = 1; my $border_colour = 'black'; my $show_coords = 0; +my $embed_images = 1; my $html_document = 1; my $outfile = '-'; @@ -43,6 +44,7 @@ GetOptions 'border-width|bw=f' => \$border_width, 'border-colour|border-color|bc=s' => \$border_colour, 'show-coords|coords!' => \$show_coords, + 'embed-images!' => \$embed_images, 'html-document!' => \$html_document, 'outfile=s' => \$outfile, @@ -51,10 +53,11 @@ GetOptions $api_url // croak "Base API URL is required! Use --api-url to set"; -my $grid = HexGrid->new(defaults => { +my $grid = HexGrid->new(embed_images => $embed_images, defaults => { style => { 'stroke-width' => $border_width, stroke => $border_colour }, - show_coords => $show_coords }); + show_coords => $show_coords}); my %region_grids; +my %region_grids_by_subregion; my $mw = MediaWiki::API->new(); $mw->{config}->{api_url} = $api_url; @@ -91,7 +94,8 @@ foreach my $page (values %{$region_query_results->{query}{pages}}) show_coords => $show_coords }, height => 300, - width => 300 + width => 300, + embed_images => $embed_images ); $region_grids{$page->{title}}->add_region($region); } @@ -126,6 +130,7 @@ foreach my $page (values %{$subregion_query_results->{query}{pages}}) { my $region_name = $parsed_template->{positional_params}[0]; $region_grids{$region_name}->add_region($subregion); + $region_grids_by_subregion{$subregion->{name}} = $region_grids{$region_name}; } push @tile_pages, "$page->{title}/Tiles"; @@ -165,8 +170,6 @@ foreach my $page (values %{$location_query_results->{query}{pages}}) if($regiondir) { $region_grids{$region_name}->add_region($location); - my $region_name = $parsed_template->{positional_params}[1]; - $region_grids{$region_name}->add_region($location); $region_grids{$location->{name}} = HexGrid->new ( @@ -191,6 +194,8 @@ foreach my $page (values %{$location_query_results->{query}{pages}}) +HexGrid::Image::DEBUG(); + say STDERR "Getting Background image pages"; my $background_query_results = $mw->api({ action => 'query', prop => 'imageinfo', @@ -198,6 +203,7 @@ my $background_query_results = $mw->api({ action => 'query', iiprop => 'url' }) || carp $mw->{error}->{code} . ': ' . $mw->{error}->{details}; +# say STDERR Dumper(\%region_grids); foreach my $page (values %{$background_query_results->{query}{pages}}) { if($page->{imageinfo}) @@ -207,6 +213,18 @@ foreach my $page (values %{$background_query_results->{query}{pages}}) foreach my $region (@{$background_pages{$page->{title}}}) { $region->{defaults}{image} = HexGrid::to_id($page->{title}); + if($regiondir) + { + # say STDERR $region->{name}; + if(exists $region_grids{$region->{name}}) + { + $region_grids{$region->{name}}->add_image(HexGrid::to_id($page->{title}), $page->{imageinfo}[0]{url}); + } + elsif(exists $region_grids_by_subregion{$region->{name}}) + { + $region_grids_by_subregion{$region->{name}}->add_image(HexGrid::to_id($page->{title}), $page->{imageinfo}[0]{url}); + } + } } } }