From 00b60dc9f6e8b241d441339af80c2640328d2666 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Mon, 10 Jul 2023 13:02:00 -0500 Subject: [PATCH] Make subregion parsing an option --- wiki-tile.pl | 106 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 32 deletions(-) diff --git a/wiki-tile.pl b/wiki-tile.pl index f174b87..c18622a 100644 --- a/wiki-tile.pl +++ b/wiki-tile.pl @@ -1,4 +1,4 @@ -use v5.30; +use v5.36; use HexGrid; use HexGrid::Pin; @@ -24,13 +24,23 @@ my $border_width = 1; my $border_colour = 'black'; my $show_coords = 0; -GetOptions( - 'border-width=f' => \$border_width, - 'border-colour|border-color=s' => \$border_colour, - 'show-coords|coords!' => \$show_coords, +my $interactive = 1; +my $do_subregions = 1; + +GetOptions +( 'api-url=s' => \$api_url, 'region-template-name=s' => \$region_template_name, - 'site-template-name=s' => \$site_template_name + 'subregion-template-name=s' => \$subregion_template_name, + 'location-template-name=s' => \$location_template_name, + 'site-template-name=s' => \$site_template_name, + + 'border-width|bw=f' => \$border_width, + 'border-colour|border-color|bc=s' => \$border_colour, + 'show-coords|coords!' => \$show_coords, + + 'interactive|s!' => \$interactive, + 'subregions!|sub' => \$do_subregions ); my $grid = HexGrid->new(defaults => { @@ -57,13 +67,24 @@ my $region_query_results = $mw->api my (@tile_pages, %background_pages); foreach my $page (values %{$region_query_results->{query}{pages}}) { + next if $page->{title} =~ /^Category:/; my $region = $grid->make_region($page->{title}); my $parsed_template = MWTemplate::Parse($page->{revisions}[0]{'*'}, $region_template_name); $region->{defaults}{colour} = $parsed_template->{named_params}{colour}; - $region_grids{$page->{title}} = HexGrid->new(defaults => { - style => { 'stroke-width' => $border_width, stroke => $border_colour }, - show_coords => $show_coords }); - $region_grids{$page->{title}}->add_region($region); + if($do_subregions) + { + $region_grids{$page->{title}} = HexGrid->new + ( + defaults => + { + style => { 'stroke-width' => $border_width, stroke => $border_colour }, + show_coords => $show_coords + }, + height => 300, + width => 300 + ); + $region_grids{$page->{title}}->add_region($region); + } push @tile_pages, "$page->{title}/Tiles"; push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $region; @@ -85,11 +106,15 @@ my $subregion_query_results = $mw->api foreach my $page (values %{$subregion_query_results->{query}{pages}}) { + next if $page->{title} =~ /^Category:/; my $parsed_template = MWTemplate::Parse($page->{revisions}[0]{'*'}, $subregion_template_name); my $subregion = $grid->make_region($page->{title}); $subregion->{defaults}{colour} = $parsed_template->{named_params}{colour}; - my $region_name = $parsed_template->{positional_params}[0]; - $region_grids{$region_name}->add_region($subregion); + if($do_subregions) + { + my $region_name = $parsed_template->{positional_params}[0]; + $region_grids{$region_name}->add_region($subregion); + } push @tile_pages, "$page->{title}/Tiles"; push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $subregion; @@ -106,10 +131,8 @@ my $background_query_results = $mw->api({ action => 'query', foreach my $page (values %{$background_query_results->{query}{pages}}) { - # say STDERR Dumper($page); if($page->{imageinfo}) { - # say STDERR Dumper($page->{title}); foreach my $region (@{$background_pages{$page->{title}}}) { $region->{defaults}{image} = $page->{imageinfo}[0]{url}; @@ -158,36 +181,51 @@ my $location_query_results = $mw->api foreach my $page (values %{$location_query_results->{query}{pages}}) { + next if $page->{title} =~ /^Category:/; my $parsed_template = MWTemplate::Parse($page->{revisions}[0]{'*'}, $location_template_name); my $location = $grid->make_region($page->{title}); - $region_grids{$location->{name}} = HexGrid->new(defaults => { - style => { 'stroke-width' => $border_width, stroke => $border_colour }, - show_coords => $show_coords }); - my $location_with_context = $region_grids{$location->{name}}->make_region($location->{name}); - - $location_with_context->{defaults}{colour} = $location->{defaults}{colour} = $parsed_template->{named_params}{colour}; + my $location_with_context; + if($do_subregions) + { + $region_grids{$location->{name}} = HexGrid->new + ( + defaults => + { + style => { 'stroke-width' => $border_width, stroke => $border_colour }, + show_coords => $show_coords + }, + height => 300, + width => 300 + ); + $location_with_context = $region_grids{$location->{name}}->make_region($location->{name}); + + $location_with_context->{defaults}{colour} = $location->{defaults}{colour} = $parsed_template->{named_params}{colour}; + } # The below regex is a whitespace forgiving version of /^(-?\d+),(-?\d+)/, an int pair if($parsed_template->{positional_params}[0] =~ /^\s*(-?\s*\d+)\s*,\s*(-?\s*\d+)\s*$/) { $location->make_tile_at($1,$2); - $location_with_context->make_tile_at($1,$2); - foreach my $coords (split /;/, $parsed_template->{named_params}{context_tiles}) + if($do_subregions) { - # The below regex is a whitespace forgiving version of /^(-?\d+),(-?\d+)/, an int pair - do { carp "Skipping bad spec: $coords"; next; } - unless $coords =~ /^\s*(-?\s*\d+)\s*,\s*(-?\s*\d+)\s*$/; - # say STDERR Dumper($grid->get_tile_at($1, $2)); - # say STDERR Dumper($grid->{regions}{$parsed_template->{positional_params}[1]}); - $location_with_context->add_tile($grid->get_tile_at($1, $2)); + $location_with_context->make_tile_at($1,$2); + foreach my $coords (split /;/, $parsed_template->{named_params}{context_tiles}) + { + # The below regex is a whitespace forgiving version of /^(-?\d+),(-?\d+)/, an int pair + do { carp "Skipping bad spec: $coords"; next; } + unless $coords =~ /^\s*(-?\s*\d+)\s*,\s*(-?\s*\d+)\s*$/; + $location_with_context->add_tile($grid->get_tile_at($1, $2)); + } } } - my $region_name = $parsed_template->{positional_params}[1]; - $region_grids{$region_name}->add_region($location); + if($do_subregions) + { + my $region_name = $parsed_template->{positional_params}[1]; + $region_grids{$region_name}->add_region($location); + } - # push @tile_pages, "$page->{title}/Tiles"; push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $location; } @@ -208,6 +246,7 @@ my $site_query_results = $mw->api # say STDERR "Sites found: " . join(" ,", map { $_->{title} } @$site_pages); foreach my $site_page_ref (values %{$site_query_results->{query}{pages}}) { + next if $site_page_ref->{title} =~ /^Category:/; my $site_name = $site_page_ref->{title}; say STDERR "Processing Site $site_name"; my $site_url = $site_page_ref->{canonicalurl}; @@ -251,7 +290,10 @@ sub wrap_in_html($grid) EOS $html_builder .= "\n" . $grid->render; - $html_builder .= "\n" . $_->render for values %region_grids; + if ($do_subregions) + { + $html_builder .= "\n" . $_->render for values %region_grids; + } $html_builder .= "\n\n"; return $html_builder; }