1
+ const md5 = require ( 'md5' ) ;
2
+
1
3
/**
2
4
* cleanup i18n fields.
3
5
* A valid i18n field is of the following
@@ -15,6 +17,49 @@ var processI18nField = function(field) {
15
17
}
16
18
}
17
19
20
+ /**
21
+ * Cleanup image fields.
22
+ * URIs must be transformed into URLs.
23
+ */
24
+ const URI_REGEX = / ^ ( c a r t o | o s m i c | o s m - i c o n s | h t t p s ? | r e s ) : \/ \/ ( .* ) $ / ;
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
+
18
63
/**
19
64
* Clean values and ensure they are all converted into an i18nalized map
20
65
*/
@@ -48,30 +93,42 @@ var cleanValues = function(values) {
48
93
* - all tag values fields are declared in a key/value form
49
94
*/
50
95
var postProcessCleanup = function ( parsed ) {
96
+ if ( typeof parsed . image != 'undefined' ) {
97
+ parsed . image = processImageField ( parsed . image ) ;
98
+ }
51
99
for ( var i in parsed . groups ) {
52
100
var group = parsed . groups [ i ] ;
53
101
processI18nField ( group . name ) ;
54
102
if ( typeof group . description != 'undefined' ) {
55
103
processI18nField ( group . description ) ;
56
104
}
105
+ if ( typeof group . icon != 'undefined' ) {
106
+ group . icon = processImageField ( group . icon ) ;
107
+ }
57
108
console . log ( "-----------------------------------------------------" )
58
109
console . log ( "Processing group " + group . name . default ) ;
59
110
console . log ( "-----------------------------------------------------" )
60
111
for ( var j in group . items ) {
61
112
var item = group . items [ j ] ;
62
113
processI18nField ( item . label ) ;
63
114
processI18nField ( item . description ) ;
115
+ if ( typeof item . icon != 'undefined' ) {
116
+ item . icon = processImageField ( item . icon ) ;
117
+ }
64
118
console . log ( "Processing item " + item . label . default ) ;
65
119
for ( var k in item . tags ) {
66
120
var tag = item . tags [ k ] ;
67
121
if ( typeof tag . values != 'undefined' ) {
68
122
// Tag has values
69
123
cleanValues ( tag . values ) ;
70
124
}
125
+ if ( typeof tag . images != 'undefined' ) {
126
+ tag . images = processImageField ( tag . images ) ;
127
+ }
71
128
}
72
129
}
73
130
}
74
- }
131
+ } ;
75
132
76
133
module . exports = {
77
134
"postProcessCleanup" : postProcessCleanup
0 commit comments