@@ -7,14 +7,17 @@ import {isHidden} from 'is-hidden'
7
7
import { iso6393 } from 'iso-639-3'
8
8
import { speakers } from 'speakers'
9
9
import { unified } from 'unified'
10
+ import rehypeParse from 'rehype-parse'
10
11
import gfm from 'remark-gfm'
11
12
import stringify from 'remark-stringify'
12
13
import { u } from 'unist-builder'
14
+ import { selectAll } from 'hast-util-select'
15
+ import { toText } from 'hast-util-to-text'
13
16
import format from 'format'
14
17
import author from 'parse-author'
15
18
import human from 'human-format'
16
19
import alphaSort from 'alpha-sort'
17
- import udhr from 'udhr'
20
+ import { udhr } from 'udhr'
18
21
import allTrigrams from 'trigrams'
19
22
import unicode from 'unicode-12.1.0'
20
23
import customFixtures from './custom-fixtures.js'
@@ -24,8 +27,6 @@ import exclude from './udhr-exclude.js'
24
27
const ascending = alphaSort ( )
25
28
26
29
const trigrams = allTrigrams . min ( )
27
- const information = udhr . information ( )
28
- const declarations = udhr . json ( )
29
30
const scripts = unicode . Script
30
31
31
32
const require = createRequire ( import . meta. url )
@@ -175,19 +176,32 @@ function generate(basename) {
175
176
fixtures = { }
176
177
177
178
support . forEach ( function ( language ) {
178
- var udhrKey = language . udhr
179
+ var udhrKey = language . udhr || language . iso6393
179
180
var fixture
180
181
181
182
if ( udhrKey in customFixtures ) {
182
183
fixture = customFixtures [ udhrKey ]
183
- } else if ( udhrKey in declarations ) {
184
- if (
185
- declarations [ udhrKey ] . preamble &&
186
- declarations [ udhrKey ] . preamble . para
187
- ) {
188
- fixture = declarations [ udhrKey ] . preamble . para
189
- } else if ( declarations [ udhrKey ] . note && declarations [ udhrKey ] . note [ 0 ] ) {
190
- fixture = declarations [ udhrKey ] . note [ 0 ] . para
184
+ } else if ( udhrKey ) {
185
+ const info = udhr . find ( ( d ) => d . code === udhrKey )
186
+
187
+ if ( info ) {
188
+ const declaration = String (
189
+ fs . readFileSync (
190
+ path . join ( 'node_modules' , 'udhr' , 'declaration' , udhrKey + '.html' )
191
+ )
192
+ )
193
+ const tree = unified ( ) . use ( rehypeParse ) . parse ( declaration )
194
+
195
+ fixture =
196
+ selectAll ( 'header p' , tree )
197
+ . map ( ( d ) => toText ( d ) )
198
+ . join ( '\n' ) ||
199
+ selectAll (
200
+ 'body > :matches(h1, h2, h3, h4, h5, h6), header :matches(h1, h2, h3, h4, h5, h6)' ,
201
+ tree
202
+ )
203
+ . map ( ( d ) => toText ( d ) )
204
+ . join ( '\n' )
191
205
}
192
206
}
193
207
@@ -326,30 +340,22 @@ function count(list) {
326
340
return map
327
341
}
328
342
329
- /* Get all values at `key` properties in `object`. */
330
- function all ( object , key ) {
331
- var results = [ ]
332
- var property
333
- var value
334
-
335
- for ( property in object ) {
336
- value = object [ property ]
343
+ /* Get which scripts are used for a given UDHR code. */
344
+ function scriptInformation ( code ) {
345
+ const info = code ? udhr . find ( ( d ) => d . code === code ) : undefined
346
+ let paragraphs = ''
337
347
338
- if ( property === key ) {
339
- results . push ( value )
340
- } else if ( typeof value === 'object' ) {
341
- results = results . concat ( all ( value , key ) )
342
- }
348
+ if ( info ) {
349
+ const declaration = fs . readFileSync (
350
+ path . join ( 'node_modules' , 'udhr' , 'declaration' , code + '.html' )
351
+ )
352
+ const tree = unified ( ) . use ( rehypeParse ) . parse ( declaration )
353
+ paragraphs = selectAll ( 'article p' , tree )
354
+ . map ( ( d ) => toText ( d ) )
355
+ . join ( '\n' )
343
356
}
344
357
345
- return results
346
- }
347
-
348
- /* Get which scripts are used for a given UDHR code. */
349
- function scriptInformation ( code ) {
350
- var declaration = declarations [ code ]
351
- var content = all ( declaration , 'para' ) . join ( '' )
352
- var length = content . length
358
+ var length = paragraphs . length
353
359
var scriptInformation = { }
354
360
355
361
Object . keys ( expressions ) . forEach ( function ( script ) {
@@ -360,7 +366,7 @@ function scriptInformation(code) {
360
366
return
361
367
}
362
368
363
- count = content . match ( expressions [ script ] )
369
+ count = paragraphs . match ( expressions [ script ] )
364
370
count = ( count ? count . length : 0 ) / length
365
371
count = Math . round ( count * 100 ) / 100
366
372
@@ -520,11 +526,9 @@ function getUDHRKeysfromISO(iso) {
520
526
return overrides [ iso ]
521
527
}
522
528
523
- Object . keys ( information ) . forEach ( function ( code ) {
524
- var info = information [ code ]
525
-
526
- if ( info . ISO === iso || info . code === iso ) {
527
- udhrs . push ( code )
529
+ udhr . forEach ( function ( info ) {
530
+ if ( info . iso6393 === iso || info . code === iso ) {
531
+ udhrs . push ( info . code )
528
532
}
529
533
} )
530
534
0 commit comments