Debian Codegraph @

vendor/wikimedia/parsoid/tools/cite_css/generateCiteLanguageCSS.php

raw
1 <?php
2
3 /* phpcs:disable MediaWiki.Commenting.FunctionComment.MissingDocumentationPublic */
4
5 function wfGetCSS( string $selector, array $rules ) {
6 return "$selector {\n\t" . implode( "\n\t", $rules ) . "\n}\n";
7 }
8
9 function wfLoadJson( string $file ): array {
10 return json_decode( file_get_contents( $file ), true );
11 }
12
13 // Generate language-specific CSS files
14
15 /*
16 * Extracted from https://www.w3.org/TR/predefined-counter-styles/
17 * Intersected with https://www.w3.org/International/i18n-tests/results/predefined-counter-styles
18 * where all major browsers have support with a green cell.
19 */
20 $w3cCounterTypeMaps = [
21 /* numeric - no suffixes or prefixes */
22 "0123456789" => "decimal",
23 "٠١٢٣٤٥٦٧٨٩" => "arabic-indic",
24 "০১২৩৪৫৬৭৮৯" => "bengali",
25 "០១២៣៤៥៦៧៨៩" => "cambodian",
26 "०१२३४५६७८९" => "devanagari",
27 "૦૧૨૩૪૫૬૭૮૯" => "gujarati",
28 "੦੧੨੩੪੫੬੭੮੯" => "gurmukhi",
29 "೦೧೨೩೪೫೬೭೮೯" => "kannada",
30 "០១២៣៤៥៦៧៨៩" => "khmer",
31 "໐໑໒໓໔໕໖໗໘໙" => "lao",
32 "൦൧൨൩൪൫൬൭൮൯" => "malayalam",
33 "၀၁၂၃၄၅၆၇၈၉" => "myanmar", /* w3c page says suffix, but testing shows no suffix! */
34 "᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙" => "mongolian",
35 "୦୧୨୩୪୫୬୭୮୯" => "oriya",
36 "۰۱۲۳۴۵۶۷۸۹" => "persian",
37 "௦௧௨௩௪௫௬௭௮௯" => "tamil",
38 "౦౧౨౩౪౫౬౭౮౯" => "telugu",
39 "๐๑๒๓๔๕๖๗๘๙" => "thai",
40 "༠༡༢༣༤༥༦༧༨༩" => "tibetan",
41 ];
42 # Language fallback chain
43 $langFallbacks = wfLoadJson( __DIR__ . "/language.fallbacks.json" );
44 $localizedDigits = wfLoadJson( __DIR__ . "/localized.digits.json" );
45 $localizedSeps = wfLoadJson( __DIR__ . "/localized.separators.json" );
46 $rtl = wfLoadJson( __DIR__ . "/rtl.langs.json" );
47 $rtl['en'] = false; // to ensure fallbacks work properly
48 $allLangs = array_merge( array_keys( $localizedDigits ), array_keys( $localizedSeps ) );
49
50 // Process fallback chains
51 foreach ( $allLangs as $lang ) {
52 if ( $lang !== 'en' ) {
53 // last fallback is 'en'
54 $len = count( $langFallbacks[$lang] ?? [] );
55 if ( $len === 0 || ( $langFallbacks[$lang][$len - 1] !== 'en' ) ) {
56 $langFallbacks[$lang][] = 'en';
57 }
58 foreach ( $langFallbacks[$lang] ?? [] as $fbLang ) {
59 if ( !isset( $localizedDigits[$lang] ) && isset( $localizedDigits[$fbLang] ) ) {
60 $localizedDigits[$lang] = $localizedDigits[$fbLang];
61 }
62 if ( !isset( $localizedSeps[$lang] ) && isset( $localizedSeps[$fbLang] ) ) {
63 $localizedSeps[$lang] = $localizedSeps[$fbLang];
64 }
65 if ( !isset( $rtl[$lang] ) && isset( $rtl[$fbLang] ) ) {
66 $rtl[$lang] = $rtl[$fbLang];
67 }
68 }
69 }
70 }
71
72 foreach ( $allLangs as $lang ) {
73 $out = [];
74
75 if ( $rtl[$lang] ) { // guaranteed to be set because of 'en' fallback
76 $out[] = "/* @noflip */\n" . wfGetCSS(
77 ".mw-cite-dir-ltr",
78 [ "direction: ltr;", "text-align: left;" ]
79 );
80 $out[] = "/* @noflip */\n" . wfGetCSS(
81 ".mw-cite-dir-rtl",
82 [ "direction: rtl;", "text-align: right;" ]
83 );
84 }
85
86 $digits = $localizedDigits[$lang] ?? null;
87
88 if ( $digits === null ) {
89 $counterType = 'decimal';
90 } else {
91 $str = implode( $digits );
92 $counterType = $w3cCounterTypeMaps[$str] ?? null;
93 if ( !$counterType ) {
94 $counterType = "$lang-counter";
95 $cssSel = "@counter-style $counterType";
96 $cssRules = [];
97 $cssRules[] = "system: numeric;";
98 $cssRules[] = "symbols: " . implode(
99 array_map(
100 static function ( $digit ) {
101 return "'" . $digit . "'";
102 },
103 $digits ),
104 ' ' ) . ';';
105 $out[] = wfGetCSS( $cssSel, $cssRules );
106 }
107 $out[] = wfGetCSS(
108 ".mw-ref > a::after",
109 [ "content: '[' counter( mw-Ref, $counterType ) ']';" ]
110 );
111 $out[] = wfGetCSS(
112 ".mw-ref > a[ data-mw-group ]::after",
113 [ "content: '[' attr( data-mw-group ) ' ' counter( mw-Ref, $counterType ) ']';" ]
114 );
115 }
116
117 $separator = $localizedSeps[$lang] ?? '.';
118 if ( $counterType !== 'decimal' || $separator !== '.' ) {
119 $out[] = wfGetCSS(
120 "span[ rel='mw:referencedBy' ] > a::before",
121 [ "content: counter( mw-references, $counterType )" .
122 " '$separator' counter( mw-ref-linkback, $counterType );" ]
123 );
124 }
125 file_put_contents( "./tools/cite_css/ext.cite.style.$lang.css", implode( $out, "\n" ) );
126 }