@citestyle/compiler 0.0.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -63
- package/package.json +18 -4
package/README.md
CHANGED
|
@@ -1,90 +1,128 @@
|
|
|
1
1
|
# @citestyle/compiler
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Transform any [CSL](https://citationstyles.org/) style into a lightweight JavaScript module at build time. The compiled output is a self-contained ES module (~3-5KB) that formats citations and bibliographies as pure function calls — no XML parsing, no runtime interpreter, no dependencies beyond `@citestyle/core`.
|
|
4
|
+
|
|
5
|
+
This is a **build tool**. Install it as a dev dependency, compile your styles once, and ship only the tiny output modules to production.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
|
-
npm install @citestyle/compiler
|
|
10
|
+
npm install -D @citestyle/compiler
|
|
9
11
|
```
|
|
10
12
|
|
|
11
|
-
## CLI
|
|
13
|
+
## CLI
|
|
12
14
|
|
|
13
15
|
### Compile a style
|
|
14
16
|
|
|
15
17
|
```bash
|
|
16
|
-
#
|
|
17
|
-
citestyle compile apa.csl
|
|
18
|
-
|
|
19
|
-
# Single file to output
|
|
20
|
-
citestyle compile apa.csl -o apa.js
|
|
18
|
+
# Compile to a file
|
|
19
|
+
npx citestyle compile apa.csl -o apa.js
|
|
21
20
|
|
|
22
|
-
#
|
|
23
|
-
citestyle compile
|
|
21
|
+
# Compile with a specific locale
|
|
22
|
+
npx citestyle compile apa.csl --locale fr-FR -o apa-fr.js
|
|
24
23
|
|
|
25
|
-
#
|
|
26
|
-
citestyle compile
|
|
24
|
+
# Batch compile every .csl file in a directory
|
|
25
|
+
npx citestyle compile styles/*.csl -o dist/
|
|
27
26
|
|
|
28
|
-
# Multi-locale (
|
|
29
|
-
citestyle compile apa.csl --locale en-US,fr-FR -o dist/
|
|
27
|
+
# Multi-locale (one output file per locale)
|
|
28
|
+
npx citestyle compile apa.csl --locale en-US,fr-FR -o dist/
|
|
30
29
|
|
|
31
30
|
# CommonJS output
|
|
32
|
-
citestyle compile apa.csl --format cjs -o apa.cjs
|
|
31
|
+
npx citestyle compile apa.csl --format cjs -o apa.cjs
|
|
32
|
+
|
|
33
|
+
# Print to stdout (pipe to other tools)
|
|
34
|
+
npx citestyle compile apa.csl
|
|
33
35
|
```
|
|
34
36
|
|
|
35
|
-
### Validate
|
|
37
|
+
### Validate without compiling
|
|
36
38
|
|
|
37
39
|
```bash
|
|
38
|
-
# Check
|
|
39
|
-
citestyle check apa.csl
|
|
40
|
+
# Check a style for warnings without generating output
|
|
41
|
+
npx citestyle check apa.csl
|
|
40
42
|
|
|
41
43
|
# Batch validate
|
|
42
|
-
citestyle check styles/*.csl
|
|
44
|
+
npx citestyle check styles/*.csl
|
|
43
45
|
```
|
|
44
46
|
|
|
45
|
-
### CLI
|
|
47
|
+
### CLI options
|
|
46
48
|
|
|
47
49
|
| Option | Description | Default |
|
|
48
50
|
|---|---|---|
|
|
49
51
|
| `-o, --output <path>` | Output file or directory | stdout |
|
|
50
|
-
| `--locale <lang>` | Target locale, comma-separated
|
|
52
|
+
| `--locale <lang>` | Target locale(s), comma-separated | `en-US` |
|
|
51
53
|
| `--format <esm\|cjs>` | Output module format | `esm` |
|
|
52
54
|
|
|
55
|
+
## Using a compiled style
|
|
56
|
+
|
|
57
|
+
The compiled module exports pure functions. No registry needed for basic formatting:
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
import * as apa from './apa.js'
|
|
61
|
+
|
|
62
|
+
const item = {
|
|
63
|
+
id: '1',
|
|
64
|
+
type: 'article-journal',
|
|
65
|
+
title: 'Deep learning for citation analysis',
|
|
66
|
+
author: [{ family: 'Smith', given: 'Jane' }],
|
|
67
|
+
issued: { 'date-parts': [[2024]] },
|
|
68
|
+
'container-title': 'Nature',
|
|
69
|
+
volume: '620',
|
|
70
|
+
page: '100-108',
|
|
71
|
+
DOI: '10.1038/example',
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Bibliography entry — returns structured output, not a flat string
|
|
75
|
+
const entry = apa.bibliography(item)
|
|
76
|
+
entry.text // Smith, J. (2024). Deep learning for citation analysis. Nature, 620, 100–108.
|
|
77
|
+
entry.html // Semantic HTML with <i>, CSS classes (.csl-author, .csl-title, ...), linked DOI
|
|
78
|
+
entry.parts // { authors, year, title, container, doi, ... }
|
|
79
|
+
entry.links // { doi: 'https://doi.org/10.1038/example' }
|
|
80
|
+
|
|
81
|
+
// Inline citation
|
|
82
|
+
const cite = apa.citation([{ item }])
|
|
83
|
+
cite.text // (Smith, 2024)
|
|
84
|
+
|
|
85
|
+
// Style metadata
|
|
86
|
+
apa.meta.title // "American Psychological Association 7th edition"
|
|
87
|
+
apa.meta.class // "in-text"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
For documents with multiple citations (where you need numbering, disambiguation, and sorting), use [`@citestyle/registry`](../registry).
|
|
91
|
+
|
|
53
92
|
## Programmatic API
|
|
54
93
|
|
|
55
|
-
```
|
|
94
|
+
```javascript
|
|
56
95
|
import { compile, parse, resolveLocale, generate } from '@citestyle/compiler'
|
|
57
96
|
```
|
|
58
97
|
|
|
59
98
|
### `compile(cslXml, options?)`
|
|
60
99
|
|
|
61
|
-
Full pipeline: XML string to JavaScript module.
|
|
100
|
+
Full pipeline: CSL XML string to JavaScript module source.
|
|
62
101
|
|
|
63
|
-
```
|
|
64
|
-
import { readFileSync } from 'node:fs'
|
|
102
|
+
```javascript
|
|
103
|
+
import { readFileSync, writeFileSync } from 'node:fs'
|
|
104
|
+
import { compile } from '@citestyle/compiler'
|
|
65
105
|
|
|
66
106
|
const cslXml = readFileSync('apa.csl', 'utf-8')
|
|
67
107
|
const { code, meta } = compile(cslXml, { locale: 'en-US' })
|
|
68
108
|
|
|
109
|
+
writeFileSync('apa.js', code)
|
|
110
|
+
|
|
69
111
|
console.log(meta.title) // "American Psychological Association 7th edition"
|
|
70
112
|
console.log(meta.class) // "in-text"
|
|
71
|
-
|
|
72
|
-
// `code` is a complete ES module string — write it to a file
|
|
73
|
-
writeFileSync('apa.js', code)
|
|
74
113
|
```
|
|
75
114
|
|
|
76
|
-
**
|
|
77
|
-
- `
|
|
78
|
-
- `
|
|
79
|
-
- `options.format` (`'esm'` | `'cjs'`) -- Output format (default: `'esm'`)
|
|
115
|
+
**Options:**
|
|
116
|
+
- `locale` (string) — Target locale (default: style's default or `'en-US'`)
|
|
117
|
+
- `format` (`'esm'` | `'cjs'`) — Output format (default: `'esm'`)
|
|
80
118
|
|
|
81
119
|
**Returns:** `{ code: string, meta: { id, title, class, version, defaultLocale } }`
|
|
82
120
|
|
|
83
121
|
### `parse(cslXml)`
|
|
84
122
|
|
|
85
|
-
Parse CSL XML into an AST. Useful for validation or
|
|
123
|
+
Parse CSL XML into an AST without generating code. Useful for validation, inspection, or building custom tooling.
|
|
86
124
|
|
|
87
|
-
```
|
|
125
|
+
```javascript
|
|
88
126
|
const ast = parse(cslXml)
|
|
89
127
|
console.log(ast.info.title)
|
|
90
128
|
console.log(Object.keys(ast.macros))
|
|
@@ -92,47 +130,36 @@ console.log(Object.keys(ast.macros))
|
|
|
92
130
|
|
|
93
131
|
### `resolveLocale(locale, overrides?)`
|
|
94
132
|
|
|
95
|
-
Resolve locale data by merging: style-level overrides
|
|
133
|
+
Resolve locale data by merging: style-level term overrides → locale XML file → en-US fallback.
|
|
96
134
|
|
|
97
|
-
```
|
|
135
|
+
```javascript
|
|
98
136
|
const locale = resolveLocale('fr-FR', ast.localeOverrides)
|
|
99
137
|
```
|
|
100
138
|
|
|
101
139
|
### `generate(ast, locale, options?)`
|
|
102
140
|
|
|
103
|
-
Generate JavaScript module source from a parsed AST and resolved locale.
|
|
141
|
+
Generate JavaScript module source from a parsed AST and resolved locale. Use this when you need control over the parse and locale resolution steps separately.
|
|
104
142
|
|
|
105
|
-
```
|
|
143
|
+
```javascript
|
|
106
144
|
const ast = parse(cslXml)
|
|
107
145
|
const locale = resolveLocale('en-US', ast.localeOverrides)
|
|
108
146
|
const code = generate(ast, locale, { format: 'esm' })
|
|
109
147
|
```
|
|
110
148
|
|
|
111
|
-
##
|
|
149
|
+
## What gets compiled
|
|
112
150
|
|
|
113
|
-
|
|
114
|
-
import * as apa from './apa.js'
|
|
151
|
+
Every CSL construct maps directly to JavaScript:
|
|
115
152
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
volume: '620',
|
|
124
|
-
page: '100-108',
|
|
125
|
-
DOI: '10.1038/example',
|
|
126
|
-
}
|
|
153
|
+
| CSL XML | Compiled output |
|
|
154
|
+
|---|---|
|
|
155
|
+
| `<macro name="author">` | `function macro_author(item, ctx) { ... }` |
|
|
156
|
+
| `<choose><if type="article-journal">` | `if (item.type === 'article-journal')` |
|
|
157
|
+
| `<names variable="author">` | `formatNames(item.author, { /* config baked in */ })` |
|
|
158
|
+
| `<group delimiter=", ">` | Array collect → filter empties → join |
|
|
159
|
+
| Locale terms (`"et al."`, `"Retrieved from"`) | Inlined string constants |
|
|
127
160
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
// Semantic HTML with <i>, CSS classes, linked DOI
|
|
134
|
-
|
|
135
|
-
// Format an inline citation
|
|
136
|
-
const citation = apa.citation([{ item }])
|
|
137
|
-
console.log(citation.text) // (Smith, 2024)
|
|
138
|
-
```
|
|
161
|
+
The result is a module that exports `bibliography()`, `citation()`, `bibliographySort()`, and `meta` — pure functions with no XML, no interpretation, and no runtime locale resolution.
|
|
162
|
+
|
|
163
|
+
## Compatibility
|
|
164
|
+
|
|
165
|
+
44 styles from the [official CSL repository](https://github.com/citation-style-language/styles) have been compiled and verified, spanning author-date, numeric, note, and label citation formats across English, German, Portuguese, French, and Spanish locales.
|
package/package.json
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@citestyle/compiler",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Build-time compiler: CSL XML → JavaScript modules",
|
|
5
|
+
"author": "Uniweb",
|
|
6
|
+
"homepage": "https://github.com/uniweb/csl/tree/main/packages/compiler",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/uniweb/csl.git",
|
|
10
|
+
"directory": "packages/compiler"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"csl",
|
|
14
|
+
"citation",
|
|
15
|
+
"compiler",
|
|
16
|
+
"citation-style-language",
|
|
17
|
+
"bibliography",
|
|
18
|
+
"build-tool"
|
|
19
|
+
],
|
|
5
20
|
"type": "module",
|
|
6
21
|
"exports": {
|
|
7
22
|
".": {
|
|
@@ -19,7 +34,7 @@
|
|
|
19
34
|
"README.md"
|
|
20
35
|
],
|
|
21
36
|
"dependencies": {
|
|
22
|
-
"@citestyle/types": "0.0
|
|
37
|
+
"@citestyle/types": "1.0.0",
|
|
23
38
|
"@xmldom/xmldom": "^0.8.11"
|
|
24
39
|
},
|
|
25
40
|
"devDependencies": {
|
|
@@ -30,7 +45,6 @@
|
|
|
30
45
|
},
|
|
31
46
|
"license": "MIT",
|
|
32
47
|
"scripts": {
|
|
33
|
-
"test": "vitest run"
|
|
34
|
-
"lint": "echo 'TODO: configure linter'"
|
|
48
|
+
"test": "vitest run"
|
|
35
49
|
}
|
|
36
50
|
}
|