Skip to content

Commit 05f9785

Browse files
LeoFrachetLoïc Ortola
LeoFrachet
authored and
Loïc Ortola
committed
Replace URIs by URLs
1 parent 98027c2 commit 05f9785

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/post-processor.js

+58-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const md5 = require('md5');
2+
13
/**
24
* cleanup i18n fields.
35
* A valid i18n field is of the following
@@ -15,6 +17,49 @@ var processI18nField = function(field) {
1517
}
1618
}
1719

20+
/**
21+
* Cleanup image fields.
22+
* URIs must be transformed into URLs.
23+
*/
24+
const URI_REGEX = /^(carto|osmic|osm-icons|https?|res):\/\/(.*)$/;
25+
const processImageField = function(field) {
26+
function cleanUri(uri) {
27+
const prefix = uri.replace(URI_REGEX, '$1');
28+
const imageName = uri.replace(URI_REGEX, '$2');
29+
30+
switch (prefix) {
31+
case 'carto':
32+
return `https://raw.githubusercontent.com/gravitystorm/openstreetmap-carto/master/symbols/${imageName}.svg`;
33+
case 'osmic':
34+
return `https://raw.githubusercontent.com/gmgeo/osmic/master/${imageName}.svg`;
35+
case 'osm-icons':
36+
/* The directory names are from the first two characters of the md5 hash of the final filename.
37+
* Source: https://www.mediawiki.org/wiki/Manual:Image_administration#Data_storage */
38+
const hash = md5(imageName.replace(/ /g, '_') + '.svg');
39+
return `http://osm-icons.org/images/${hash[0]}/${hash.substring(0,2)}/${imageName}.svg`;
40+
case 'res':
41+
return `file://${imageName}`;
42+
case 'http':
43+
case 'https':
44+
return uri;
45+
default:
46+
console.log('Unsupported prefix: ' + prefix);
47+
return;
48+
}
49+
}
50+
51+
if (typeof field === 'string') {
52+
return cleanUri(field);
53+
} else if (typeof field === 'object') {
54+
for (const language of Object.keys(field)) {
55+
field[language] = cleanUri(field[language]);
56+
}
57+
return field;
58+
} else {
59+
console.log('Unknown field type for an image: ' + field);
60+
}
61+
};
62+
1863
/**
1964
* Clean values and ensure they are all converted into an i18nalized map
2065
*/
@@ -48,30 +93,42 @@ var cleanValues = function(values) {
4893
* - all tag values fields are declared in a key/value form
4994
*/
5095
var postProcessCleanup = function(parsed) {
96+
if (typeof parsed.image != 'undefined') {
97+
parsed.image = processImageField(parsed.image);
98+
}
5199
for (var i in parsed.groups) {
52100
var group = parsed.groups[i];
53101
processI18nField(group.name);
54102
if (typeof group.description != 'undefined') {
55103
processI18nField(group.description);
56104
}
105+
if (typeof group.icon != 'undefined') {
106+
group.icon = processImageField(group.icon);
107+
}
57108
console.log("-----------------------------------------------------")
58109
console.log("Processing group " + group.name.default);
59110
console.log("-----------------------------------------------------")
60111
for (var j in group.items) {
61112
var item = group.items[j];
62113
processI18nField(item.label);
63114
processI18nField(item.description);
115+
if (typeof item.icon != 'undefined') {
116+
item.icon = processImageField(item.icon);
117+
}
64118
console.log("Processing item " + item.label.default);
65119
for (var k in item.tags) {
66120
var tag = item.tags[k];
67121
if (typeof tag.values != 'undefined') {
68122
// Tag has values
69123
cleanValues(tag.values);
70124
}
125+
if (typeof tag.images != 'undefined') {
126+
tag.images = processImageField(tag.images);
127+
}
71128
}
72129
}
73130
}
74-
}
131+
};
75132

76133
module.exports = {
77134
"postProcessCleanup": postProcessCleanup

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"fs-extra": "^5.0.0",
88
"js-yaml": "^3.6.1",
99
"jsonschema": "^1.1.0",
10+
"md5": "^2.2.1",
1011
"rmdir-recursive": "0.0.1",
1112
"xml2js": "^0.4.17"
1213
},

0 commit comments

Comments
 (0)