diff options
author | Christopher Baines <mail@cbaines.net> | 2015-02-28 15:08:57 +0000 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2015-02-28 15:08:57 +0000 |
commit | 43b44dbbda102ff0e7f00693c3a80c7af10f2a7e (patch) | |
tree | a195078cebcd080db72972c430157a237df4d0fd /IkiWiki/Plugin | |
parent | 0b10b34c7fdbfc6baa573cddad991443b4065118 (diff) | |
download | ikiwiki-osm-icon-fixes.tar ikiwiki-osm-icon-fixes.tar.gz |
Icon related fixes in the OSM pluginosm-icon-fixes
- Use the icon parameter to the waypoint directive
- Fix the KML output to handle different icons for waypoints with the same tag.
This might cause breakage in wiki's using Ikiwiki if they were depending on
the existing style format
Diffstat (limited to 'IkiWiki/Plugin')
-rw-r--r-- | IkiWiki/Plugin/osm.pm | 74 |
1 files changed, 51 insertions, 23 deletions
diff --git a/IkiWiki/Plugin/osm.pm b/IkiWiki/Plugin/osm.pm index 604c15c50..edc20c9e0 100644 --- a/IkiWiki/Plugin/osm.pm +++ b/IkiWiki/Plugin/osm.pm @@ -177,9 +177,10 @@ sub process_waypoint { my $name = scrub($params{'name'} || $p, $page, $dest); # sanitized here my $desc = scrub($params{'desc'} || '', $page, $dest); # sanitized here my $zoom = scrub($params{'zoom'} // $config{'osm_default_zoom'} // 15, $page, $dest); # sanitized below - my $icon = $config{'osm_default_icon'} || "ikiwiki/images/osm.png"; # sanitized: we trust $config + my $icon = $params{'icon'}; # not sanitized? my $map = scrub($params{'map'} || 'map', $page, $dest); # sanitized here my $alt = $config{'osm_alt'} ? "alt=\"$config{'osm_alt'}\"" : ''; # sanitized: we trust $config + my $tag = $params{'tag'}; if ($zoom !~ /^\d\d?$/ || $zoom < 2 || $zoom > 18) { error("Bad zoom"); } @@ -189,20 +190,28 @@ sub process_waypoint { error("Must specify lat and lon"); } - my $tag = $params{'tag'}; - foreach my $t (keys %{$typedlinks{$page}{'tag'}}) { - if ($icon = get_tag_icon($t)) { - $tag = $t; - last; - } - $t =~ s!/$config{'tagbase'}/!!; - if ($icon = get_tag_icon($t)) { - $tag = $t; - last; + unless ($icon) { + foreach my $t (keys %{$typedlinks{$page}{'tag'}}) { + if ($icon = get_tag_icon($t)) { + $tag = $t; + last; + } + $t =~ s!/$config{'tagbase'}/!!; + if ($icon = get_tag_icon($t)) { + $tag = $t; + last; + } } } + + # Use the default icon, if none has been given + $icon = $icon || $config{'osm_default_icon'} || "ikiwiki/images/osm.png"; + $icon = urlto($icon, $dest, 1); $icon =~ s!/*$!!; # hack - urlto shouldn't be appending a slash in the first place + + add_depends($params{page}, $icon); + $tag = '' unless $tag; register_rendered_files($map, $page, $dest); $pagestate{$page}{'osm'}{$map}{'waypoints'}{$name} = { @@ -242,8 +251,10 @@ sub process_waypoint { sub get_tag_icon($) { my $tag = shift; # look for an icon attached to the tag - my $attached = $tag . '/' . $config{'osm_tag_default_icon'}; - if (srcfile($attached)) { + my $attached = $tag . '/' . ($config{'osm_tag_default_icon'} || "icon.png"); + # Try and find the icon, second argument indicates to return undef if it cannot be found + if (srcfile($attached, 1)) { + return $attached; } else { @@ -379,14 +390,28 @@ sub writekml($;$) { $writer->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2"); $writer->startTag("Document"); - # first pass: get the icons - my %tags_map = (); # keep track of tags seen + # Each waypoint has an icon, this is set in process_waypoint, as the + # kml output has styles, there must be one for each icon, such that the + # placemarks using that icon can have that style. + + # Used to generate unique names for each style + my $iconNum = 0; + # Maps the name of the style (id) to the icon + # This is used to determine if a new style is needed + my %iconStyle = (); + # Maps each waypoint to the style, used below + my %waypointStyle = (); + + # first pass: compute the styles foreach my $name (keys %{$waypoints{$map}}) { my %options = %{$waypoints{$map}{$name}}; - if (!$tags_map{$options{tag}}) { - debug("found new style " . $options{tag}); - $tags_map{$options{tag}} = (); - $writer->startTag("Style", id => $options{tag}); + + # Find the style by the tag + my $style = $iconStyle{$options{icon}}; + unless ($style) { + $style = $style = "_icon" . ($iconNum++); + + $writer->startTag("Style", id => $style); $writer->startTag("IconStyle"); $writer->startTag("Icon"); $writer->startTag("href"); @@ -395,10 +420,13 @@ sub writekml($;$) { $writer->endTag(); $writer->endTag(); $writer->endTag(); - } - $tags_map{$options{tag}}{$name} = \%options; + + # Add this style to the hash + $iconStyle{$options{'icon'}} = $style; + } + $waypointStyle{$name} = $style; } - + foreach my $name (keys %{$waypoints{$map}}) { my %options = %{$waypoints{$map}{$name}}; $writer->startTag("Placemark"); @@ -406,7 +434,7 @@ sub writekml($;$) { $writer->characters($name); $writer->endTag(); $writer->startTag("styleUrl"); - $writer->characters('#' . $options{tag}); + $writer->characters('#' . $waypointStyle{$name}); $writer->endTag(); #$writer->emptyTag('atom:link', href => $options{href}); # to make it easier for us as the atom:link parameter is |