@citestyle/compiler 0.0.2
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/LICENSE +21 -0
- package/README.md +138 -0
- package/locales/locales-de-DE.xml +734 -0
- package/locales/locales-en-US.xml +864 -0
- package/locales/locales-pt-BR.xml +746 -0
- package/package.json +36 -0
- package/src/cli.js +322 -0
- package/src/codegen.js +992 -0
- package/src/compile.js +43 -0
- package/src/index.js +9 -0
- package/src/locale.js +235 -0
- package/src/parser.js +606 -0
- package/types.d.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Proximify
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# @citestyle/compiler
|
|
2
|
+
|
|
3
|
+
Build-time compiler that transforms [CSL](https://citationstyles.org/) XML files into lightweight JavaScript modules.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @citestyle/compiler
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## CLI Usage
|
|
12
|
+
|
|
13
|
+
### Compile a style
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# Single file to stdout
|
|
17
|
+
citestyle compile apa.csl
|
|
18
|
+
|
|
19
|
+
# Single file to output
|
|
20
|
+
citestyle compile apa.csl -o apa.js
|
|
21
|
+
|
|
22
|
+
# Batch compile all .csl files in a directory
|
|
23
|
+
citestyle compile styles/*.csl -o dist/
|
|
24
|
+
|
|
25
|
+
# With a specific locale
|
|
26
|
+
citestyle compile apa.csl --locale fr-FR -o apa-fr.js
|
|
27
|
+
|
|
28
|
+
# Multi-locale (outputs one file per locale)
|
|
29
|
+
citestyle compile apa.csl --locale en-US,fr-FR -o dist/
|
|
30
|
+
|
|
31
|
+
# CommonJS output
|
|
32
|
+
citestyle compile apa.csl --format cjs -o apa.cjs
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Validate a style
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# Check without compiling — reports warnings
|
|
39
|
+
citestyle check apa.csl
|
|
40
|
+
|
|
41
|
+
# Batch validate
|
|
42
|
+
citestyle check styles/*.csl
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### CLI Options
|
|
46
|
+
|
|
47
|
+
| Option | Description | Default |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| `-o, --output <path>` | Output file or directory | stdout |
|
|
50
|
+
| `--locale <lang>` | Target locale, comma-separated for multi-locale | `en-US` |
|
|
51
|
+
| `--format <esm\|cjs>` | Output module format | `esm` |
|
|
52
|
+
|
|
53
|
+
## Programmatic API
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { compile, parse, resolveLocale, generate } from '@citestyle/compiler'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `compile(cslXml, options?)`
|
|
60
|
+
|
|
61
|
+
Full pipeline: XML string to JavaScript module.
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { readFileSync } from 'node:fs'
|
|
65
|
+
|
|
66
|
+
const cslXml = readFileSync('apa.csl', 'utf-8')
|
|
67
|
+
const { code, meta } = compile(cslXml, { locale: 'en-US' })
|
|
68
|
+
|
|
69
|
+
console.log(meta.title) // "American Psychological Association 7th edition"
|
|
70
|
+
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
|
+
```
|
|
75
|
+
|
|
76
|
+
**Parameters:**
|
|
77
|
+
- `cslXml` (string) -- CSL XML source
|
|
78
|
+
- `options.locale` (string) -- Target locale (default: style's default or `'en-US'`)
|
|
79
|
+
- `options.format` (`'esm'` | `'cjs'`) -- Output format (default: `'esm'`)
|
|
80
|
+
|
|
81
|
+
**Returns:** `{ code: string, meta: { id, title, class, version, defaultLocale } }`
|
|
82
|
+
|
|
83
|
+
### `parse(cslXml)`
|
|
84
|
+
|
|
85
|
+
Parse CSL XML into an AST. Useful for validation or inspection without code generation.
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
const ast = parse(cslXml)
|
|
89
|
+
console.log(ast.info.title)
|
|
90
|
+
console.log(Object.keys(ast.macros))
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `resolveLocale(locale, overrides?)`
|
|
94
|
+
|
|
95
|
+
Resolve locale data by merging: style-level overrides, locale XML file, and en-US fallback.
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
const locale = resolveLocale('fr-FR', ast.localeOverrides)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### `generate(ast, locale, options?)`
|
|
102
|
+
|
|
103
|
+
Generate JavaScript module source from a parsed AST and resolved locale.
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
const ast = parse(cslXml)
|
|
107
|
+
const locale = resolveLocale('en-US', ast.localeOverrides)
|
|
108
|
+
const code = generate(ast, locale, { format: 'esm' })
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Using a Compiled Style
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
import * as apa from './apa.js'
|
|
115
|
+
|
|
116
|
+
const item = {
|
|
117
|
+
id: '1',
|
|
118
|
+
type: 'article-journal',
|
|
119
|
+
title: 'Deep learning for citation analysis',
|
|
120
|
+
author: [{ family: 'Smith', given: 'Jane' }],
|
|
121
|
+
issued: { 'date-parts': [[2024]] },
|
|
122
|
+
'container-title': 'Nature',
|
|
123
|
+
volume: '620',
|
|
124
|
+
page: '100-108',
|
|
125
|
+
DOI: '10.1038/example',
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Format a bibliography entry
|
|
129
|
+
const entry = apa.bibliography(item)
|
|
130
|
+
console.log(entry.text)
|
|
131
|
+
// Smith, J. (2024). Deep learning for citation analysis. Nature, 620, 100–108.
|
|
132
|
+
console.log(entry.html)
|
|
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
|
+
```
|