This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-table-from-output.js
executable file
·200 lines (179 loc) · 6.35 KB
/
build-table-from-output.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env node
const fs = require('fs')
// This converts one structure definition or code system into a latex booktabs table
const help = 'Missing arguments. Pass the input FHIR resource json file with -f {filename} and the target tex file output path with -o {path}. Optionally, you can choose the target implementation guide path with -i.'
let args = process.argv
if (args.length < 3) {
console.error(help)
return
}
let target;
let file;
let igFile = './fhir-profile/fsh-generated/resources/ImplementationGuide-com.alextherapeutics.fhir.nicotine.json'
const available = ['-h', '-f', '-o', '-i']
args.shift() // remove the first node args
args.shift()
while (args.length > 0) {
const operator = args.shift()
if (!available.includes(operator)) {
console.error(help)
return
}
if (operator === '-h') {
console.info(help)
return
}
const arg = args.shift()
if (operator === '-o') {
target = arg
} else if (operator === '-f') {
file = arg
} else if (operator === '-i') {
igFile = arg
} else {
console.error('Internal error. Shouldnt arrive here')
return
}
}
if (!target || !file) {
console.error('You must specify an output path and file target. See -h for help')
return
}
console.info(`Using the FHIR file ${file} to generate a LaTeX booktabs table to ${target}...`)
const json = JSON.parse(fs.readFileSync(file))
const igJson = JSON.parse(fs.readFileSync(igFile))
const result = json.resourceType === 'CodeSystem'
? buildTableFromCodeSystem(json, igJson)
: buildTableFromStructureDefinition(json, igJson)
fs.writeFileSync(target, result)
console.info('Done')
// ---------------------------------------------------------------------------------------------------------
// functions
function escape(line) {
return line.replace(/\_/g, '\\_')
}
function buildTableFromCodeSystem(json, igJson) {
const name = json.name
const bookTabsHeader =
`
\\begin{table}[]\\centering
\\begin{tabular}{@{}ll@{}}
\\toprule
\\multicolumn{1}{c}{code} & \\multicolumn{1}{c}{definition} \\\\ \\midrule
`
const structureHeader = `
\\textbf{${boxed(name)}} & \\textbf{${boxed(json.description)}} \\\\ \\midrule
`
const rows = json.concept.map(item => `${boxed(escape(item.code))} & ${boxed(item.definition)} \\\\`)
const elements = rows.join('\n').concat('\\bottomrule')
const bookTabsEnd = `
\\end{tabular}
\\caption{The ${name} code system.}
\\label{tab:${name}}
\\end{table}
`
return bookTabsHeader + structureHeader + elements + bookTabsEnd
}
function buildTableFromStructureDefinition(json, igJson) {
const uri = json.url
const name = json.name
const type = json.type
const kind = json.kind
const caption = kind === 'resource'
? `The differential for the ${name} profile when compared to the base ${type} resource.`
: `The differential for the ${name} extension.`
const bookTabsHeader =
`
\\begin{table}[]\\centering
\\begin{tabular}{@{}lll@{}}
\\toprule
\\multicolumn{1}{c}{Name} & \\multicolumn{1}{c}{card.} & \\multicolumn{1}{c}{Type} \\\\ \\midrule
`
const structureHeader = `
\\textbf{${name}} & \\textbf{-} & \\textbf{${type}} \\\\ \\midrule
`
const jsonDifferential = json.differential.element
jsonDifferential.shift() // first element is the parent
const jsonSnapshot = json.snapshot.element
const tabRows = jsonDifferential.map(item => buildTabRow(item, jsonSnapshot, type))
const elements = tabRows.join('\n').concat('\\bottomrule')
const bookTabsEnd = `
\\end{tabular}
\\caption{${caption}}
\\label{tab:${name}}
\\end{table}
`
return bookTabsHeader + structureHeader + elements + bookTabsEnd
}
function buildTabRow(item, snapshots, type) {
const rowName = getRowName(item)
const cardMin = item.min !== undefined ? item.min : snapshots.find(el => el.id === item.id).min //|| 0
const cardMax = item.max || snapshots.find(el => el.id === item.id).max
const rowType = breakUris(getType(item, snapshots, type))
if (rowType === undefined) {
console.warn(`${rowName} had undefined type. ignoring`)
return ''
}
return `${rowName} & ${cardMin}..${cardMax} & ${boxed(rowType)} \\\\`
}
function breakUris(type) {
return type.replace('http://www.alextherapeutics.com/fhir/', 'http://www.alextherapeutics.com/fhir/\\allowbreak ')
}
function boxed(item) {
return `\\parbox{0.5\\linewidth}{${item}}`
}
function getRowName(item) {
const nameWithPath = item.id.substring(item.id.indexOf('.') + 1)
const formatted = nameWithPath.replace(/\./g, ' $\\rightarrow$ ').replace(/:/g, ' $\\mid$ ')
return formatted
}
function getType(item, snapshots, type) {
if (item.fixedUri) {
return `uri=${item.fixedUri}`
}
if (item.id.includes('valueDate')) {
return 'date'
}
if (item.id.includes('valueCodeableConcept')) {
return 'CodeableConcept'
}
if (item.binding) {
const valueSetId = findIdFromCanonical(item.binding.valueSet)
const valueSetName = findProfileNameFromIg(igJson, valueSetId)
return `${valueSetName} (${item.binding.strength})`
}
const types = item.type || snapshots.find(el => el.id === item.id).type
let typeString
if (types.length === 1) {
typeString = buildTypeString(types[0], snapshots)
} else {
typeString = type.code
}
let pattern
if (item.patternString) {
pattern = item.patternString
} else if (item.patternCode) {
pattern = item.patternCode
}
return pattern !== undefined ? `${typeString} = ${pattern}` : typeString
}
function buildTypeString(type, snapshots) {
if (!type.targetProfile) {
return type.code
}
const targetProfileId = findIdFromCanonical(type.targetProfile[0])
const name = findProfileNameFromIg(igJson, targetProfileId)
return `${type.code}(${name})`
}
function findProfileNameFromIg(ig, targetId) {
if (!targetId.includes('-')) return targetId
const def = igJson.definition.resource.find(el => el.reference.reference.includes(targetId))
if (def.exampleCanonical) {
return findProfileNameFromIg(findIdFromCanonical(def.exampleCanonical))
}
return def.name.replace(/\s/g, '')
}
function findIdFromCanonical(canonical) {
const split = canonical.split('/')
return split[split.length - 1]
}