docrev 0.6.13 → 0.7.6

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.
@@ -0,0 +1,419 @@
1
+ /**
2
+ * DOI commands: doi, orcid
3
+ *
4
+ * Commands for DOI validation, fetching, and ORCID profile lookup.
5
+ */
6
+
7
+ import {
8
+ chalk,
9
+ fs,
10
+ path,
11
+ fmt,
12
+ } from './context.js';
13
+
14
+ /**
15
+ * Register DOI commands with the program
16
+ * @param {import('commander').Command} program
17
+ */
18
+ export function register(program) {
19
+ // ==========================================================================
20
+ // DOI command - Validate and fetch DOIs
21
+ // ==========================================================================
22
+
23
+ program
24
+ .command('doi')
25
+ .description('Validate DOIs in bibliography or fetch citations from DOI')
26
+ .argument('<action>', 'Action: check, fetch, add, lookup')
27
+ .argument('[input]', 'DOI (for fetch/add) or .bib file (for check)')
28
+ .option('-b, --bib <file>', 'Bibliography file', 'references.bib')
29
+ .option('--strict', 'Fail on missing DOIs for articles')
30
+ .option('--no-resolve', 'Only check format, skip resolution check')
31
+ .option('--confidence <level>', 'Minimum confidence: high, medium, low (default: medium)', 'medium')
32
+ .action(async (action, input, options) => {
33
+ const { parseBibEntries, checkBibDois, fetchBibtex, addToBib, isValidDoiFormat, lookupDoi, lookupMissingDois } = await import('../doi.js');
34
+
35
+ if (action === 'check') {
36
+ const bibPath = input || options.bib;
37
+
38
+ if (!fs.existsSync(bibPath)) {
39
+ console.error(fmt.status('error', `File not found: ${bibPath}`));
40
+ process.exit(1);
41
+ }
42
+
43
+ console.log(fmt.header(`DOI Check: ${path.basename(bibPath)}`));
44
+ console.log();
45
+
46
+ const spin = fmt.spinner('Validating DOIs...').start();
47
+
48
+ try {
49
+ const results = await checkBibDois(bibPath, {
50
+ checkMissing: options.strict,
51
+ });
52
+
53
+ spin.stop();
54
+
55
+ // Group results by status
56
+ const valid = results.entries.filter(e => e.status === 'valid');
57
+ const invalid = results.entries.filter(e => e.status === 'invalid');
58
+ const missing = results.entries.filter(e => e.status === 'missing');
59
+ const skipped = results.entries.filter(e => e.status === 'skipped');
60
+
61
+ // Summary table
62
+ const summaryRows = [
63
+ [chalk.green('Valid'), chalk.green(valid.length.toString())],
64
+ [invalid.length > 0 ? chalk.red('Invalid') : 'Invalid', invalid.length > 0 ? chalk.red(invalid.length.toString()) : '0'],
65
+ [missing.length > 0 ? chalk.yellow('Missing (articles)') : 'Missing', missing.length > 0 ? chalk.yellow(missing.length.toString()) : '0'],
66
+ [chalk.dim('Skipped'), chalk.dim(skipped.length.toString())],
67
+ ];
68
+ console.log(fmt.table(['Status', 'Count'], summaryRows));
69
+ console.log();
70
+
71
+ // Show invalid DOIs
72
+ if (invalid.length > 0) {
73
+ console.log(chalk.red('Invalid DOIs:'));
74
+ for (const e of invalid) {
75
+ console.log(` ${chalk.bold(e.key)}: ${e.doi || 'N/A'}`);
76
+ console.log(chalk.dim(` ${e.message}`));
77
+ }
78
+ console.log();
79
+ }
80
+
81
+ // Show missing (articles without DOI)
82
+ if (missing.length > 0) {
83
+ console.log(chalk.yellow('Missing DOIs (should have DOI):'));
84
+ for (const e of missing) {
85
+ console.log(` ${chalk.bold(e.key)} [${e.type}]`);
86
+ if (e.title) console.log(chalk.dim(` "${e.title}"`));
87
+ }
88
+ console.log();
89
+ }
90
+
91
+ // Show skipped breakdown
92
+ if (skipped.length > 0) {
93
+ // Count by reason
94
+ const manualSkip = skipped.filter(e => e.message === 'Marked as no-doi');
95
+ const bookTypes = skipped.filter(e => e.message?.includes('typically has no DOI'));
96
+ const noField = skipped.filter(e => e.message === 'No DOI field');
97
+
98
+ console.log(chalk.dim('Skipped entries:'));
99
+ if (manualSkip.length > 0) {
100
+ console.log(chalk.dim(` ${manualSkip.length} marked with nodoi={true}`));
101
+ }
102
+ if (bookTypes.length > 0) {
103
+ const types = [...new Set(bookTypes.map(e => e.type))].join(', ');
104
+ console.log(chalk.dim(` ${bookTypes.length} by type (${types})`));
105
+ }
106
+ if (noField.length > 0) {
107
+ console.log(chalk.dim(` ${noField.length} with no DOI field`));
108
+ }
109
+ console.log();
110
+ }
111
+
112
+ // Final status
113
+ if (invalid.length === 0 && missing.length === 0) {
114
+ console.log(fmt.status('success', 'All DOIs valid'));
115
+ } else if (invalid.length > 0) {
116
+ console.log(fmt.status('error', `${invalid.length} invalid DOI(s) found`));
117
+ if (options.strict) process.exit(1);
118
+ } else {
119
+ console.log(fmt.status('warning', `${missing.length} article(s) missing DOI`));
120
+ }
121
+
122
+ // Hint about skipping
123
+ console.log();
124
+ console.log(chalk.dim('To skip DOI check for an entry, add: nodoi = {true}'));
125
+ console.log(chalk.dim('Or add comment before entry: % no-doi'));
126
+
127
+ } catch (err) {
128
+ spin.stop();
129
+ console.error(fmt.status('error', err.message));
130
+ process.exit(1);
131
+ }
132
+
133
+ } else if (action === 'fetch') {
134
+ if (!input) {
135
+ console.error(fmt.status('error', 'DOI required'));
136
+ console.log(chalk.dim('Usage: rev doi fetch 10.1234/example'));
137
+ process.exit(1);
138
+ }
139
+
140
+ const spin = fmt.spinner(`Fetching BibTeX for ${input}...`).start();
141
+
142
+ try {
143
+ const result = await fetchBibtex(input);
144
+
145
+ if (result.success) {
146
+ spin.success('BibTeX retrieved');
147
+ console.log();
148
+ console.log(result.bibtex);
149
+ } else {
150
+ spin.error(result.error);
151
+ process.exit(1);
152
+ }
153
+ } catch (err) {
154
+ spin.error(err.message);
155
+ process.exit(1);
156
+ }
157
+
158
+ } else if (action === 'add') {
159
+ if (!input) {
160
+ console.error(fmt.status('error', 'DOI required'));
161
+ console.log(chalk.dim('Usage: rev doi add 10.1234/example'));
162
+ process.exit(1);
163
+ }
164
+
165
+ const bibPath = options.bib;
166
+ const spin = fmt.spinner(`Fetching and adding ${input}...`).start();
167
+
168
+ try {
169
+ const fetchResult = await fetchBibtex(input);
170
+
171
+ if (!fetchResult.success) {
172
+ spin.error(fetchResult.error);
173
+ process.exit(1);
174
+ }
175
+
176
+ const addResult = addToBib(bibPath, fetchResult.bibtex);
177
+
178
+ if (addResult.success) {
179
+ spin.success(`Added @${addResult.key} to ${bibPath}`);
180
+ } else {
181
+ spin.error(addResult.error);
182
+ process.exit(1);
183
+ }
184
+ } catch (err) {
185
+ spin.error(err.message);
186
+ process.exit(1);
187
+ }
188
+
189
+ } else if (action === 'lookup') {
190
+ const bibPath = input || options.bib;
191
+
192
+ if (!fs.existsSync(bibPath)) {
193
+ console.error(fmt.status('error', `File not found: ${bibPath}`));
194
+ process.exit(1);
195
+ }
196
+
197
+ console.log(fmt.header(`DOI Lookup: ${path.basename(bibPath)}`));
198
+ console.log();
199
+
200
+ const entries = parseBibEntries(bibPath);
201
+ const missing = entries.filter(e => !e.doi && !e.skip && e.expectDoi);
202
+
203
+ if (missing.length === 0) {
204
+ console.log(fmt.status('success', 'No entries need DOI lookup'));
205
+ return;
206
+ }
207
+
208
+ console.log(chalk.dim(`Found ${missing.length} entries without DOIs to search...\n`));
209
+
210
+ let found = 0;
211
+ let notFound = 0;
212
+ let lowConfidence = 0;
213
+ const results = [];
214
+
215
+ for (let i = 0; i < missing.length; i++) {
216
+ const entry = missing[i];
217
+
218
+ // Extract first author last name
219
+ let author = '';
220
+ if (entry.authorRaw) {
221
+ const firstAuthor = entry.authorRaw.split(' and ')[0];
222
+ // Handle "Last, First" or "First Last" formats
223
+ if (firstAuthor.includes(',')) {
224
+ author = firstAuthor.split(',')[0].trim();
225
+ } else {
226
+ const parts = firstAuthor.trim().split(/\s+/);
227
+ author = parts[parts.length - 1]; // Last word is usually surname
228
+ }
229
+ }
230
+
231
+ process.stdout.write(`\r${chalk.dim(`[${i + 1}/${missing.length}]`)} ${entry.key}...`);
232
+
233
+ const result = await lookupDoi(entry.title, author, entry.year, entry.journal);
234
+
235
+ if (result.found) {
236
+ if (result.confidence === 'high') {
237
+ found++;
238
+ results.push({ entry, result, status: 'found' });
239
+ } else if (result.confidence === 'medium') {
240
+ found++;
241
+ results.push({ entry, result, status: 'found' });
242
+ } else {
243
+ lowConfidence++;
244
+ results.push({ entry, result, status: 'low' });
245
+ }
246
+ } else {
247
+ notFound++;
248
+ results.push({ entry, result, status: 'not-found' });
249
+ }
250
+
251
+ // Rate limiting
252
+ await new Promise(r => setTimeout(r, 200));
253
+ }
254
+
255
+ // Clear progress line
256
+ process.stdout.write('\r\x1B[K');
257
+
258
+ // Show results
259
+ console.log(fmt.table(
260
+ ['Status', 'Count'],
261
+ [
262
+ [chalk.green('Found (high/medium confidence)'), chalk.green(found.toString())],
263
+ [chalk.yellow('Found (low confidence)'), chalk.yellow(lowConfidence.toString())],
264
+ [chalk.dim('Not found'), chalk.dim(notFound.toString())],
265
+ ]
266
+ ));
267
+ console.log();
268
+
269
+ // Filter by confidence level
270
+ const confLevel = options.confidence || 'medium';
271
+ const confLevels = { high: 3, medium: 2, low: 1 };
272
+ const minConf = confLevels[confLevel] || 2;
273
+
274
+ const filteredResults = results.filter(r => {
275
+ if (r.status === 'not-found') return false;
276
+ const resultConf = confLevels[r.result.confidence] || 1;
277
+ return resultConf >= minConf;
278
+ });
279
+
280
+ const hiddenCount = results.filter(r => {
281
+ if (r.status === 'not-found') return false;
282
+ const resultConf = confLevels[r.result.confidence] || 1;
283
+ return resultConf < minConf;
284
+ }).length;
285
+
286
+ if (filteredResults.length > 0) {
287
+ console.log(chalk.cyan(`Found DOIs (${confLevel}+ confidence):`));
288
+ console.log();
289
+
290
+ for (const { entry, result } of filteredResults) {
291
+ const conf = result.confidence === 'high' ? chalk.green('●') :
292
+ result.confidence === 'medium' ? chalk.yellow('●') :
293
+ chalk.red('○');
294
+
295
+ // Check year match
296
+ const entryYear = entry.year;
297
+ const foundYear = result.metadata?.year;
298
+ const yearExact = entryYear && foundYear && entryYear === foundYear;
299
+ const yearClose = entryYear && foundYear && Math.abs(entryYear - foundYear) === 1;
300
+ const yearMismatch = entryYear && foundYear && Math.abs(entryYear - foundYear) > 1;
301
+
302
+ console.log(` ${conf} ${chalk.bold(entry.key)} (${entryYear || '?'})`);
303
+ console.log(chalk.dim(` Title: ${entry.title}`));
304
+ console.log(chalk.cyan(` DOI: ${result.doi}`));
305
+
306
+ if (result.metadata?.journal) {
307
+ let yearDisplay;
308
+ if (yearExact) {
309
+ yearDisplay = chalk.green(`(${foundYear})`);
310
+ } else if (yearClose) {
311
+ yearDisplay = chalk.yellow(`(${foundYear}) ≈`);
312
+ } else if (yearMismatch) {
313
+ yearDisplay = chalk.red.bold(`(${foundYear}) ⚠ YEAR MISMATCH`);
314
+ } else {
315
+ yearDisplay = chalk.dim(`(${foundYear || '?'})`);
316
+ }
317
+ console.log(` ${chalk.dim('Found:')} ${result.metadata.journal} ${yearDisplay}`);
318
+ }
319
+
320
+ // Extra warning for year mismatch
321
+ if (yearMismatch) {
322
+ console.log(chalk.red(` ⚠ Expected ${entryYear}, found ${foundYear} - verify this is correct!`));
323
+ }
324
+
325
+ console.log();
326
+ }
327
+
328
+ // Offer to add DOIs
329
+ console.log(chalk.dim('To add a DOI to your .bib file:'));
330
+ console.log(chalk.dim(' 1. Open references.bib'));
331
+ console.log(chalk.dim(' 2. Add: doi = {10.xxxx/xxxxx}'));
332
+ console.log();
333
+ console.log(chalk.dim('Or use: rev doi add <doi> to fetch full BibTeX'));
334
+ }
335
+
336
+ // Show hidden count
337
+ if (hiddenCount > 0) {
338
+ console.log(chalk.yellow(`\n${hiddenCount} lower-confidence matches hidden.`));
339
+ if (confLevel === 'high') {
340
+ console.log(chalk.dim('Use --confidence medium or --confidence low to show more.'));
341
+ } else if (confLevel === 'medium') {
342
+ console.log(chalk.dim('Use --confidence low to show all matches.'));
343
+ }
344
+ }
345
+
346
+ // Show not found
347
+ if (notFound > 0) {
348
+ console.log(chalk.dim(`${notFound} entries could not be matched. These may be:`));
349
+ console.log(chalk.dim(' - Books, theses, or reports (often no DOI)'));
350
+ console.log(chalk.dim(' - Very old papers (pre-DOI era)'));
351
+ console.log(chalk.dim(' - Title mismatch (special characters, abbreviations)'));
352
+ }
353
+
354
+ } else {
355
+ console.error(fmt.status('error', `Unknown action: ${action}`));
356
+ console.log(chalk.dim('Actions: check, fetch, add, lookup'));
357
+ process.exit(1);
358
+ }
359
+ });
360
+
361
+ // ==========================================================================
362
+ // ORCID command - Fetch author info from ORCID
363
+ // ==========================================================================
364
+
365
+ program
366
+ .command('orcid')
367
+ .description('Fetch author information from ORCID')
368
+ .argument('<orcid>', 'ORCID iD (e.g., 0000-0002-1825-0097)')
369
+ .option('--yaml', 'Output as YAML for rev.yaml authors section')
370
+ .option('--badge', 'Output markdown badge')
371
+ .action(async (orcidInput, options) => {
372
+ const { fetchOrcidProfile, fetchOrcidWorkCount, formatAuthorYaml, getOrcidBadge, cleanOrcid, isValidOrcid } = await import('../orcid.js');
373
+
374
+ const orcid = cleanOrcid(orcidInput);
375
+
376
+ if (!isValidOrcid(orcid)) {
377
+ console.error(fmt.status('error', `Invalid ORCID format: ${orcidInput}`));
378
+ console.log(chalk.dim('Expected format: 0000-0000-0000-0000'));
379
+ console.log(chalk.dim('Or: https://orcid.org/0000-0000-0000-0000'));
380
+ process.exit(1);
381
+ }
382
+
383
+ console.log(chalk.cyan(`Fetching ORCID profile...`));
384
+
385
+ try {
386
+ const profile = await fetchOrcidProfile(orcid);
387
+ const workCount = await fetchOrcidWorkCount(orcid);
388
+
389
+ if (options.yaml) {
390
+ console.log();
391
+ console.log(formatAuthorYaml(profile));
392
+ return;
393
+ }
394
+
395
+ if (options.badge) {
396
+ console.log();
397
+ console.log(getOrcidBadge(orcid));
398
+ return;
399
+ }
400
+
401
+ console.log();
402
+ console.log(fmt.header('ORCID Profile'));
403
+ console.log();
404
+ console.log(` ${chalk.bold('Name:')} ${profile.name || chalk.dim('(not public)')}`);
405
+ console.log(` ${chalk.bold('ORCID:')} ${chalk.green(profile.orcid)}`);
406
+ console.log(` ${chalk.bold('Affiliation:')} ${profile.affiliation || chalk.dim('(not public)')}`);
407
+ console.log(` ${chalk.bold('Email:')} ${profile.email || chalk.dim('(not public)')}`);
408
+ console.log(` ${chalk.bold('Works:')} ${workCount} publication(s)`);
409
+ console.log();
410
+ console.log(chalk.dim(` Profile: https://orcid.org/${profile.orcid}`));
411
+ console.log();
412
+ console.log(chalk.dim(' Use --yaml to output for rev.yaml authors section'));
413
+ console.log(chalk.dim(' Use --badge to get markdown badge'));
414
+ } catch (err) {
415
+ console.error(fmt.status('error', err.message));
416
+ process.exit(1);
417
+ }
418
+ });
419
+ }