Skip to content

Commit 8b00039

Browse files
fix: remove hardcoded slider range mappings
1 parent 71ddebd commit 8b00039

File tree

1 file changed

+44
-69
lines changed

1 file changed

+44
-69
lines changed

packages/docs-builder/src/parse.ts

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { BlockId } from './block'
88
import type { Command } from './command'
99
import type { Context } from './context'
1010
import { readTextFile } from './fs'
11-
import type { LangCode, MarkdownPage } from './types'
11+
import type { MarkdownPage } from './types'
1212

1313
class ProcessState {
1414
/** The current heading level (0 == none, 1 == '#', 2 == '##', etc). */
@@ -765,7 +765,7 @@ function markdownFromTokens(context: Context, tokens: marked.Token[], level = 0)
765765
// language-specific format. This is very specific to the En-ROADS
766766
// User Guide and should be made pluggable.
767767
const rawCellText = markdownFromTokens(context, cell.tokens)
768-
const cellText = convertSliderRange(rawCellText, context.lang)
768+
const cellText = convertSliderRange(context, rawCellText)
769769
md += `| ${cellText} `
770770
}
771771
md += '|\n'
@@ -845,9 +845,9 @@ function resolveReferenceStyleLinks(context: Context, mdText: string, links: Lin
845845
*
846846
* XXX: This is very specific to the En-ROADS User Guide and should be made pluggable.
847847
*/
848-
function convertSliderRange(cellText: string, lang: LangCode): string {
848+
function convertSliderRange(context: Context, cellText: string): string {
849849
function num(s: string): string {
850-
switch (lang) {
850+
switch (context.lang) {
851851
case 'cs':
852852
case 'de':
853853
case 'it':
@@ -860,90 +860,65 @@ function convertSliderRange(cellText: string, lang: LangCode): string {
860860
}
861861

862862
// Try matching dollar ranges, e.g., **+$0.01 to -$0.01**
863-
// TODO: Inject values into template string like `$${sign1}${value1} to $${sign2}${value2}`
864863
let m = cellText.match(/(\*?\*?)([+-]?)\$(\d+\.?\d{0,2})\s+to\s+([+-]?)\$(\d+\.?\d{0,2})(\*?\*?)/)
865864
if (m) {
866-
switch (lang) {
867-
case 'cs':
868-
return `${m[1]}${m[2]}${num(m[3])} $ až ${m[4]}${num(m[5])} $${m[6]}`
869-
case 'de':
870-
return `${m[1]}${m[2]}${num(m[3])} $ bis ${m[4]}${num(m[5])} $${m[6]}`
871-
case 'es':
872-
return `${m[1]}del ${m[2]}$${num(m[3])} al ${m[4]}$${num(m[5])}${m[6]}`
873-
case 'it':
874-
return `${m[1]}da ${m[2]}$${num(m[3])} a ${m[4]}$${num(m[5])}${m[6]}`
875-
case 'nb':
876-
return `${m[1]}${m[2]}$ ${num(m[3])} til ${m[4]}$ ${num(m[5])}${m[6]}`
877-
case 'pt':
878-
return `${m[1]}${m[2]}$ ${num(m[3])} a ${m[4]}$ ${num(m[5])}${m[6]}`
879-
case 'en':
880-
default:
881-
return cellText
865+
let s = context.getTranslatedBlockText('slider_range_dollars')
866+
if (s) {
867+
// Inject values into template string like `$${sign1}${value1} to $${sign2}${value2}`
868+
s = s.replace('${sign1}', m[2])
869+
s = s.replace('${value1}', num(m[3]))
870+
s = s.replace('${sign2}', m[4])
871+
s = s.replace('${value2}', num(m[5]))
872+
return `${m[1]}${s}${m[6]}`
873+
} else {
874+
// If no template string, return the original English text
875+
return cellText
882876
}
883877
}
884878

885879
// Try matching percent ranges, e.g., **+10% to -20%**
886-
// TODO: Inject values into template string like `${sign1}${value1}% to ${sign2}${value2}%`
887880
m = cellText.match(/(\*?\*?)([+-]?)(\d+\.?\d{0,2})%\s+to\s+([+-]?)(\d+\.?\d{0,2})%(\*?\*?)/)
888881
if (m) {
889-
switch (lang) {
890-
case 'cs':
891-
return `${m[1]}${m[2]}${num(m[3])} % až ${m[4]}${num(m[5])} %${m[6]}`
892-
case 'de':
893-
return `${m[1]}${m[2]}${num(m[3])} % bis ${m[4]}${num(m[5])} %${m[6]}`
894-
case 'es':
895-
return `${m[1]}del ${m[2]}${num(m[3])}% al ${m[4]}${num(m[5])}%${m[6]}`
896-
case 'it':
897-
return `${m[1]}da ${m[2]}${num(m[3])}% a ${m[4]}${num(m[5])}%${m[6]}`
898-
case 'nb':
899-
return `${m[1]}${m[2]}${num(m[3])} % til ${m[4]}${num(m[5])} %${m[6]}`
900-
case 'pt':
901-
return `${m[1]}${m[2]}${num(m[3])}% a ${m[4]}${num(m[5])}%${m[6]}`
902-
case 'en':
903-
default:
904-
return cellText
882+
let s = context.getTranslatedBlockText('slider_range_percents')
883+
if (s) {
884+
// Inject values into template string like `${sign1}${value1}% to ${sign2}${value2}%`
885+
s = s.replace('${sign1}', m[2])
886+
s = s.replace('${value1}', num(m[3]))
887+
s = s.replace('${sign2}', m[4])
888+
s = s.replace('${value2}', num(m[5]))
889+
return `${m[1]}${s}${m[6]}`
890+
} else {
891+
// If no template string, return the original English text
892+
return cellText
905893
}
906894
}
907895

908896
// Try matching population ranges, e.g., **10.5 to 11.4 billion**
909-
// TODO: Inject values into template string like `${value1} to ${value2} billion`
910897
m = cellText.match(/(\*?\*?)(\d+\.?\d{0,2})\s+to\s+(\d+\.?\d{0,2}) billion(\*?\*?)/)
911898
if (m) {
912-
switch (lang) {
913-
case 'cs':
914-
return `${m[1]}${num(m[2])}${num(m[3])} miliardy${m[4]}`
915-
case 'de':
916-
return `${m[1]}${num(m[2])} bis ${num(m[3])} Milliarden${m[4]}`
917-
case 'es':
918-
return `${m[1]}${num(m[2])} a ${num(m[3])} mil millones${m[4]}`
919-
case 'it':
920-
return `${m[1]}da ${num(m[2])} a ${num(m[3])} miliardi${m[4]}`
921-
case 'nb':
922-
return `${m[1]}${num(m[2])} til ${num(m[3])} milliarder${m[4]}`
923-
case 'pt':
924-
return `${m[1]}${num(m[2])} a ${num(m[3])} bilhões${m[4]}`
925-
case 'en':
926-
default:
927-
return cellText
899+
let s = context.getTranslatedBlockText('slider_range_billions')
900+
if (s) {
901+
// Inject values into template string like `${value1} to ${value2} billion`
902+
s = s.replace('${value1}', num(m[2]))
903+
s = s.replace('${value2}', num(m[3]))
904+
return `${m[1]}${s}${m[4]}`
905+
} else {
906+
// If no template string, return the original English text
907+
return cellText
928908
}
929909
}
930910

931911
// Try matching plain percentage, e.g. 2.6%
932-
// TODO: Inject values into template string like `${value}%`
933-
m = cellText.match(/(\*?\*?)([+-]?)(\d+\.?\d{0,2})%(\*?\*?)/)
912+
m = cellText.match(/^(\*?\*?)([+-]?)(\d+\.?\d{0,2})%(\*?\*?)$/)
934913
if (m) {
935-
switch (lang) {
936-
case 'de':
937-
case 'nb':
938-
return `${m[1]}${m[2]}${num(m[3])} %${m[4]}`
939-
case 'cs':
940-
case 'es':
941-
case 'it':
942-
case 'pt':
943-
return `${m[1]}${m[2]}${num(m[3])}%${m[4]}`
944-
case 'en':
945-
default:
946-
return cellText
914+
let s = context.getTranslatedBlockText('slider_value_percent')
915+
if (s) {
916+
// Inject values into template string like `${value}%`
917+
s = s.replace('${value}', num(m[2]))
918+
return `${m[1]}${s}${m[3]}`
919+
} else {
920+
// If no template string, return the original English text
921+
return cellText
947922
}
948923
}
949924

0 commit comments

Comments
 (0)