- Timestamp:
- 2020-08-03T20:39:45+02:00 (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Tag2Link.java
r16835 r16836 9 9 import java.net.MalformedURLException; 10 10 import java.net.URL; 11 import java.util.Arrays; 11 12 import java.util.Collections; 13 import java.util.List; 12 14 import java.util.Map; 13 15 import java.util.function.UnaryOperator; … … 15 17 import java.util.regex.Pattern; 16 18 import java.util.stream.Collectors; 17 import java.util.stream.Stream;18 19 19 20 import javax.json.Json; … … 23 24 24 25 import org.openstreetmap.josm.data.osm.OsmUtils; 26 import org.openstreetmap.josm.data.preferences.CachingProperty; 25 27 import org.openstreetmap.josm.data.preferences.ListProperty; 26 28 import org.openstreetmap.josm.io.CachedFile; … … 59 61 Collections.singletonList("resource://META-INF/resources/webjars/tag2link/2020.8.3/index.json")); 60 62 63 static final CachingProperty<List<String>> PREF_SEARCH_ENGINES = new ListProperty("tag2link.search", 64 Arrays.asList("https://duckduckgo.com/?q=$1", "https://www.google.com/search?q=$1")).cached(); 65 61 66 private Tag2Link() { 62 67 // private constructor for utility class … … 139 144 // Search 140 145 if (key.matches("^(.+[:_])?name([:_]" + languagePattern + ")?$")) { 141 linkConsumer.acceptLink(tr("Search on DuckDuckGo"), "https://duckduckgo.com/?q=" + value); 146 PREF_SEARCH_ENGINES.get().forEach(url -> 147 linkConsumer.acceptLink(tr("Search on {0}", getHost(url, url)), url.replace("$1", Utils.encodeUrl(value)))); 142 148 } 143 149 … … 192 198 193 199 private static String getLinkName(String url, String fallback) { 200 return tr("Open {0}", getHost(url, fallback)); 201 } 202 203 private static String getHost(String url, String fallback) { 194 204 try { 195 return tr("Open {0}", new URL(url).getHost());205 return new URL(url).getHost().replaceFirst("^www\\.", ""); 196 206 } catch (MalformedURLException e) { 197 return tr("Open {0}", fallback);207 return fallback; 198 208 } 199 209 } -
trunk/test/unit/org/openstreetmap/josm/tools/Tag2LinkTest.java
r16834 r16836 48 48 */ 49 49 @Test 50 public void testName() { 51 Tag2Link.getLinksForTag("name", "foobar", this::addLink); 52 checkLinks("Search on duckduckgo.com // https://duckduckgo.com/?q=foobar", 53 "Search on google.com // https://www.google.com/search?q=foobar"); 54 } 55 56 /** 57 * Unit test of function {@link Tag2Link#getLinksForTag}. 58 */ 59 @Test 50 60 public void testWebsite() { 51 61 Tag2Link.getLinksForTag("website", "http://www.openstreetmap.org/", this::addLink); 52 checkLinks("Open www.openstreetmap.org // http://www.openstreetmap.org/");62 checkLinks("Open openstreetmap.org // http://www.openstreetmap.org/"); 53 63 links.clear(); 54 64 Tag2Link.getLinksForTag("website", "https://www.openstreetmap.org/", this::addLink); 55 checkLinks("Open www.openstreetmap.org // https://www.openstreetmap.org/");65 checkLinks("Open openstreetmap.org // https://www.openstreetmap.org/"); 56 66 links.clear(); 57 67 Tag2Link.getLinksForTag("website", "www.openstreetmap.org", this::addLink); 58 checkLinks("Open www.openstreetmap.org // http://www.openstreetmap.org");68 checkLinks("Open openstreetmap.org // http://www.openstreetmap.org"); 59 69 } 60 70
Note:
See TracChangeset
for help on using the changeset viewer.