docrev 0.10.1 → 0.10.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.
Files changed (119) hide show
  1. package/.gitattributes +1 -1
  2. package/CHANGELOG.md +184 -173
  3. package/PLAN-tables-and-postprocess.md +850 -850
  4. package/README.md +431 -431
  5. package/bin/rev.js +11 -11
  6. package/bin/rev.ts +145 -145
  7. package/completions/rev.bash +127 -127
  8. package/completions/rev.ps1 +210 -210
  9. package/completions/rev.zsh +207 -207
  10. package/dist/lib/anchor-match.d.ts +1 -1
  11. package/dist/lib/anchor-match.d.ts.map +1 -1
  12. package/dist/lib/anchor-match.js +47 -17
  13. package/dist/lib/anchor-match.js.map +1 -1
  14. package/dist/lib/build.js +4 -4
  15. package/dist/lib/commands/sync.d.ts.map +1 -1
  16. package/dist/lib/commands/sync.js +78 -100
  17. package/dist/lib/commands/sync.js.map +1 -1
  18. package/dist/lib/commands/utilities.js +164 -164
  19. package/dist/lib/commands/word-tools.js +8 -8
  20. package/dist/lib/grammar.js +3 -3
  21. package/dist/lib/macro-filter.lua +201 -201
  22. package/dist/lib/pdf-comments.js +44 -44
  23. package/dist/lib/plugins.js +57 -57
  24. package/dist/lib/pptx-color-filter.lua +37 -37
  25. package/dist/lib/pptx-themes.js +115 -115
  26. package/dist/lib/sections.d.ts.map +1 -1
  27. package/dist/lib/sections.js +5 -2
  28. package/dist/lib/sections.js.map +1 -1
  29. package/dist/lib/spelling.js +2 -2
  30. package/dist/lib/templates.js +387 -387
  31. package/dist/lib/themes.js +51 -51
  32. package/eslint.config.js +27 -27
  33. package/issues.md +180 -0
  34. package/lib/anchor-match.ts +308 -276
  35. package/lib/annotations.ts +644 -644
  36. package/lib/build.ts +1766 -1766
  37. package/lib/citations.ts +160 -160
  38. package/lib/commands/build.ts +855 -855
  39. package/lib/commands/citations.ts +515 -515
  40. package/lib/commands/comments.ts +1050 -1050
  41. package/lib/commands/context.ts +176 -176
  42. package/lib/commands/core.ts +309 -309
  43. package/lib/commands/doi.ts +435 -435
  44. package/lib/commands/file-ops.ts +372 -372
  45. package/lib/commands/history.ts +320 -320
  46. package/lib/commands/index.ts +87 -87
  47. package/lib/commands/init.ts +259 -259
  48. package/lib/commands/merge-resolve.ts +378 -378
  49. package/lib/commands/preview.ts +178 -178
  50. package/lib/commands/project-info.ts +244 -244
  51. package/lib/commands/quality.ts +517 -517
  52. package/lib/commands/response.ts +454 -454
  53. package/lib/commands/section-boundaries.ts +82 -82
  54. package/lib/commands/sections.ts +451 -451
  55. package/lib/commands/sync.ts +676 -709
  56. package/lib/commands/text-ops.ts +449 -449
  57. package/lib/commands/utilities.ts +448 -448
  58. package/lib/commands/verify-anchors.ts +272 -272
  59. package/lib/commands/word-tools.ts +340 -340
  60. package/lib/comment-realign.ts +517 -517
  61. package/lib/config.ts +84 -84
  62. package/lib/crossref.ts +781 -781
  63. package/lib/csl.ts +191 -191
  64. package/lib/dependencies.ts +98 -98
  65. package/lib/diff-engine.ts +465 -465
  66. package/lib/doi-cache.ts +115 -115
  67. package/lib/doi.ts +897 -897
  68. package/lib/equations.ts +506 -506
  69. package/lib/errors.ts +346 -346
  70. package/lib/format.ts +541 -541
  71. package/lib/git.ts +326 -326
  72. package/lib/grammar.ts +303 -303
  73. package/lib/image-registry.ts +180 -180
  74. package/lib/import.ts +911 -911
  75. package/lib/journals.ts +543 -543
  76. package/lib/macro-filter.lua +201 -201
  77. package/lib/macros.ts +273 -273
  78. package/lib/merge.ts +633 -633
  79. package/lib/orcid.ts +144 -144
  80. package/lib/pdf-comments.ts +263 -263
  81. package/lib/pdf-import.ts +524 -524
  82. package/lib/plugins.ts +362 -362
  83. package/lib/postprocess.ts +188 -188
  84. package/lib/pptx-color-filter.lua +37 -37
  85. package/lib/pptx-template.ts +469 -469
  86. package/lib/pptx-themes.ts +483 -483
  87. package/lib/protect-restore.ts +520 -520
  88. package/lib/rate-limiter.ts +94 -94
  89. package/lib/response.ts +197 -197
  90. package/lib/restore-references.ts +240 -240
  91. package/lib/review.ts +327 -327
  92. package/lib/schema.ts +488 -488
  93. package/lib/scientific-words.ts +73 -73
  94. package/lib/sections.ts +428 -425
  95. package/lib/slides.ts +756 -756
  96. package/lib/spelling.ts +334 -334
  97. package/lib/templates.ts +526 -526
  98. package/lib/themes.ts +742 -742
  99. package/lib/trackchanges.ts +247 -247
  100. package/lib/tui.ts +450 -450
  101. package/lib/types.ts +550 -550
  102. package/lib/undo.ts +250 -250
  103. package/lib/utils.ts +69 -69
  104. package/lib/variables.ts +179 -179
  105. package/lib/word-extraction.ts +806 -806
  106. package/lib/word.ts +643 -643
  107. package/lib/wordcomments.ts +840 -840
  108. package/package.json +137 -137
  109. package/scripts/postbuild.js +47 -47
  110. package/site/assets/extra.css +208 -0
  111. package/site/commands.html +926 -0
  112. package/site/configuration.html +469 -0
  113. package/site/index.html +288 -0
  114. package/site/troubleshooting.html +461 -0
  115. package/site/workflow.html +518 -0
  116. package/skill/REFERENCE.md +539 -539
  117. package/skill/SKILL.md +295 -295
  118. package/tsconfig.json +26 -26
  119. package/types/index.d.ts +525 -525
@@ -1,515 +1,515 @@
1
- /**
2
- * Citation commands: citations, figures, equations, pdf-comments
3
- *
4
- * Commands for validating citations, managing figures/tables, and equation handling.
5
- */
6
-
7
- import {
8
- chalk,
9
- fs,
10
- path,
11
- fmt,
12
- } from './context.js';
13
- import type { Command } from 'commander';
14
-
15
- interface CitationsOptions {
16
- bib: string;
17
- }
18
-
19
- interface EquationsOptions {
20
- output?: string;
21
- }
22
-
23
- interface PdfCommentsOptions {
24
- append?: string;
25
- json?: boolean;
26
- byPage?: boolean;
27
- byAuthor?: boolean;
28
- withText?: boolean;
29
- }
30
-
31
- /**
32
- * Register citation commands with the program
33
- */
34
- export function register(program: Command): void {
35
- // ==========================================================================
36
- // CITATIONS command - Validate citations against .bib file
37
- // ==========================================================================
38
-
39
- program
40
- .command('citations')
41
- .alias('cite')
42
- .description('Validate citations against bibliography')
43
- .argument('[files...]', 'Markdown files to check (default: all section files)')
44
- .option('-b, --bib <file>', 'Bibliography file', 'references.bib')
45
- .action(async (files: string[], options: CitationsOptions) => {
46
- const { getCitationStats } = await import('../citations.js');
47
-
48
- // If no files specified, find all .md files
49
- let mdFiles = files;
50
- if (!mdFiles || mdFiles.length === 0) {
51
- mdFiles = fs.readdirSync('.').filter(f =>
52
- f.endsWith('.md') && !['README.md', 'CLAUDE.md'].includes(f)
53
- );
54
- }
55
-
56
- if (!fs.existsSync(options.bib)) {
57
- console.error(fmt.status('error', `Bibliography not found: ${options.bib}`));
58
- process.exit(1);
59
- }
60
-
61
- const stats = getCitationStats(mdFiles, options.bib);
62
-
63
- console.log(fmt.header('Citation Check'));
64
- console.log();
65
-
66
- // Summary table
67
- const rows: string[][] = [
68
- ['Total citations', stats.totalCitations.toString()],
69
- ['Unique keys cited', stats.uniqueCited.toString()],
70
- ['Bib entries', stats.bibEntries.toString()],
71
- [chalk.green('Valid'), chalk.green(stats.valid.toString())],
72
- [stats.missing > 0 ? chalk.red('Missing') : 'Missing', stats.missing > 0 ? chalk.red(stats.missing.toString()) : '0'],
73
- [chalk.dim('Unused in bib'), chalk.dim(stats.unused.toString())],
74
- ];
75
- console.log(fmt.table(['Metric', 'Count'], rows));
76
-
77
- // Show missing keys
78
- if (stats.missingKeys.length > 0) {
79
- console.log();
80
- console.log(fmt.status('error', 'Missing citations:'));
81
- for (const key of stats.missingKeys) {
82
- console.log(chalk.red(` - ${key}`));
83
- }
84
- }
85
-
86
- // Show unused (if verbose)
87
- if (stats.unusedKeys.length > 0 && stats.unusedKeys.length <= 10) {
88
- console.log();
89
- console.log(chalk.dim('Unused bib entries:'));
90
- for (const key of stats.unusedKeys.slice(0, 10)) {
91
- console.log(chalk.dim(` - ${key}`));
92
- }
93
- if (stats.unusedKeys.length > 10) {
94
- console.log(chalk.dim(` ... and ${stats.unusedKeys.length - 10} more`));
95
- }
96
- }
97
-
98
- console.log();
99
- if (stats.missing === 0) {
100
- console.log(fmt.status('success', 'All citations valid'));
101
- } else {
102
- console.log(fmt.status('warning', `${stats.missing} citation(s) missing from ${options.bib}`));
103
- process.exit(1);
104
- }
105
- });
106
-
107
- // ==========================================================================
108
- // FIGURES command - Figure/table inventory
109
- // ==========================================================================
110
-
111
- program
112
- .command('figures')
113
- .alias('figs')
114
- .description('List all figures and tables with reference counts')
115
- .argument('[files...]', 'Markdown files to scan')
116
- .action(async (files: string[]) => {
117
- const { buildRegistry } = await import('../crossref.js');
118
-
119
- // If no files specified, find all .md files
120
- let mdFiles = files;
121
- if (!mdFiles || mdFiles.length === 0) {
122
- mdFiles = fs.readdirSync('.').filter(f =>
123
- f.endsWith('.md') && !['README.md', 'CLAUDE.md'].includes(f)
124
- );
125
- }
126
-
127
- // Build registry
128
- const registry = buildRegistry('.');
129
-
130
- // Count references in files
131
- const refCounts = new Map<string, number>();
132
- for (const file of mdFiles) {
133
- if (!fs.existsSync(file)) continue;
134
- const text = fs.readFileSync(file, 'utf-8');
135
-
136
- // Count @fig: and @tbl: references
137
- const figRefs = text.matchAll(/@fig:([a-zA-Z0-9_-]+)/g);
138
- for (const match of figRefs) {
139
- const key = `fig:${match[1]}`;
140
- refCounts.set(key, (refCounts.get(key) || 0) + 1);
141
- }
142
-
143
- const tblRefs = text.matchAll(/@tbl:([a-zA-Z0-9_-]+)/g);
144
- for (const match of tblRefs) {
145
- const key = `tbl:${match[1]}`;
146
- refCounts.set(key, (refCounts.get(key) || 0) + 1);
147
- }
148
- }
149
-
150
- console.log(fmt.header('Figure & Table Inventory'));
151
- console.log();
152
-
153
- // Figures
154
- if (registry.figures.size > 0) {
155
- const figRows = [...registry.figures.entries()].map(([label, info]) => {
156
- const key = `fig:${label}`;
157
- const refs = refCounts.get(key) || 0;
158
- const num = info.isSupp ? `S${info.num}` : info.num.toString();
159
- return [
160
- `Figure ${num}`,
161
- chalk.cyan(`@fig:${label}`),
162
- info.file,
163
- refs > 0 ? chalk.green(refs.toString()) : chalk.yellow('0'),
164
- ];
165
- });
166
- console.log(fmt.table(['#', 'Label', 'File', 'Refs'], figRows));
167
- console.log();
168
- }
169
-
170
- // Tables
171
- if (registry.tables.size > 0) {
172
- const tblRows = [...registry.tables.entries()].map(([label, info]) => {
173
- const key = `tbl:${label}`;
174
- const refs = refCounts.get(key) || 0;
175
- const num = info.isSupp ? `S${info.num}` : info.num.toString();
176
- return [
177
- `Table ${num}`,
178
- chalk.cyan(`@tbl:${label}`),
179
- info.file,
180
- refs > 0 ? chalk.green(refs.toString()) : chalk.yellow('0'),
181
- ];
182
- });
183
- console.log(fmt.table(['#', 'Label', 'File', 'Refs'], tblRows));
184
- console.log();
185
- }
186
-
187
- if (registry.figures.size === 0 && registry.tables.size === 0) {
188
- console.log(chalk.dim('No figures or tables found.'));
189
- console.log(chalk.dim('Add anchors like {#fig:label} to your figures.'));
190
- }
191
-
192
- // Warn about unreferenced
193
- const unreferenced: string[] = [];
194
- for (const [label] of registry.figures) {
195
- if (!refCounts.get(`fig:${label}`)) unreferenced.push(`@fig:${label}`);
196
- }
197
- for (const [label] of registry.tables) {
198
- if (!refCounts.get(`tbl:${label}`)) unreferenced.push(`@tbl:${label}`);
199
- }
200
-
201
- if (unreferenced.length > 0) {
202
- console.log(fmt.status('warning', `${unreferenced.length} unreferenced figure(s)/table(s)`));
203
- }
204
- });
205
-
206
- // ==========================================================================
207
- // EQUATIONS command - Extract and convert equations
208
- // ==========================================================================
209
-
210
- program
211
- .command('equations')
212
- .alias('eq')
213
- .description('Extract equations or convert to Word')
214
- .argument('<action>', 'Action: list, extract, convert, from-word')
215
- .argument('[input]', 'Input file (.md for extract/convert, .docx for from-word)')
216
- .option('-o, --output <file>', 'Output file')
217
- .action(async (action: string, input: string | undefined, options: EquationsOptions) => {
218
- const { extractEquations, getEquationStats, createEquationsDoc, extractEquationsFromWord, getWordEquationStats } = await import('../equations.js');
219
-
220
- if (action === 'from-word') {
221
- // Extract equations from Word document
222
- if (!input) {
223
- console.error(fmt.status('error', 'Word document required'));
224
- process.exit(1);
225
- }
226
-
227
- if (!input.endsWith('.docx')) {
228
- console.error(fmt.status('error', 'Input must be a .docx file'));
229
- process.exit(1);
230
- }
231
-
232
- const spin = fmt.spinner(`Extracting equations from ${path.basename(input)}...`).start();
233
-
234
- const result = await extractEquationsFromWord(input);
235
-
236
- if (!result.success) {
237
- spin.error(result.error!);
238
- process.exit(1);
239
- }
240
-
241
- spin.stop();
242
- console.log(fmt.header('Equations from Word'));
243
- console.log();
244
-
245
- if (result.equations.length === 0) {
246
- console.log(chalk.dim('No equations found in document.'));
247
- return;
248
- }
249
-
250
- const display = result.equations.filter(e => e.type === 'display');
251
- const inline = result.equations.filter(e => e.type === 'inline');
252
-
253
- console.log(chalk.dim(`Found ${result.equations.length} equations (${display.length} display, ${inline.length} inline)`));
254
- console.log();
255
-
256
- // Show equations
257
- for (let i = 0; i < result.equations.length; i++) {
258
- const eq = result.equations[i];
259
- const typeLabel = eq.type === 'display' ? chalk.cyan('[display]') : chalk.yellow('[inline]');
260
-
261
- if (eq.latex) {
262
- console.log(`${chalk.bold((i + 1).toString())}. ${typeLabel}`);
263
- console.log(chalk.dim(' LaTeX:'), eq.latex.length > 80 ? eq.latex.substring(0, 77) + '...' : eq.latex);
264
- } else {
265
- console.log(`${chalk.bold((i + 1).toString())}. ${typeLabel} ${chalk.red('[conversion failed]')}`);
266
- }
267
- }
268
-
269
- // Optionally save to file
270
- if (options.output) {
271
- const latex = result.equations
272
- .filter(e => e.latex)
273
- .map((e, i) => `%% Equation ${i + 1} (${e.type})\n${e.type === 'display' ? '$$' : '$'}${e.latex}${e.type === 'display' ? '$$' : '$'}`)
274
- .join('\n\n');
275
-
276
- fs.writeFileSync(options.output, latex, 'utf-8');
277
- console.log();
278
- console.log(fmt.status('success', `Saved ${result.equations.filter(e => e.latex).length} equations to ${options.output}`));
279
- }
280
-
281
- } else if (action === 'list') {
282
- // List equations in all section files
283
- const mdFiles = fs.readdirSync('.').filter(f =>
284
- f.endsWith('.md') && !['README.md', 'CLAUDE.md'].includes(f)
285
- );
286
-
287
- const stats = getEquationStats(mdFiles);
288
-
289
- console.log(fmt.header('Equations'));
290
- console.log();
291
-
292
- if (stats.byFile.length === 0) {
293
- console.log(chalk.dim('No equations found.'));
294
- return;
295
- }
296
-
297
- const rows = stats.byFile.map(f => [
298
- f.file,
299
- f.display > 0 ? chalk.cyan(f.display.toString()) : chalk.dim('-'),
300
- f.inline > 0 ? chalk.yellow(f.inline.toString()) : chalk.dim('-'),
301
- ]);
302
- rows.push([
303
- chalk.bold('Total'),
304
- chalk.bold.cyan(stats.display.toString()),
305
- chalk.bold.yellow(stats.inline.toString()),
306
- ]);
307
-
308
- console.log(fmt.table(['File', 'Display', 'Inline'], rows));
309
-
310
- } else if (action === 'extract') {
311
- if (!input) {
312
- console.error(fmt.status('error', 'Input file required'));
313
- process.exit(1);
314
- }
315
-
316
- const output = options.output || input.replace('.md', '-equations.md');
317
- const result = await createEquationsDoc(input, output);
318
-
319
- if (result.success) {
320
- console.log(fmt.status('success', result.message));
321
- console.log(chalk.dim(` ${result.stats!.display} display, ${result.stats!.inline} inline equations`));
322
- } else {
323
- console.error(fmt.status('error', result.message));
324
- process.exit(1);
325
- }
326
-
327
- } else if (action === 'convert') {
328
- if (!input) {
329
- console.error(fmt.status('error', 'Input file required'));
330
- process.exit(1);
331
- }
332
-
333
- const output = options.output || input.replace('.md', '.docx');
334
-
335
- const spin = fmt.spinner(`Converting ${path.basename(input)} to Word...`).start();
336
-
337
- try {
338
- const { exec } = await import('child_process');
339
- const { promisify } = await import('util');
340
- const execAsync = promisify(exec);
341
-
342
- await execAsync(`pandoc "${input}" -o "${output}" --mathml`);
343
- spin.success(`Created ${output}`);
344
- } catch (err) {
345
- const error = err as Error;
346
- spin.error(error.message);
347
- process.exit(1);
348
- }
349
- } else {
350
- console.error(fmt.status('error', `Unknown action: ${action}`));
351
- console.log(chalk.dim('Actions: list, extract, convert, from-word'));
352
- process.exit(1);
353
- }
354
- });
355
-
356
- // ==========================================================================
357
- // PDF-COMMENTS command - Extract comments from PDF
358
- // ==========================================================================
359
-
360
- program
361
- .command('pdf-comments')
362
- .alias('pdf')
363
- .description('Extract and manage comments from annotated PDFs')
364
- .argument('<pdf>', 'PDF file with annotations')
365
- .option('-a, --append <file>', 'Append comments to markdown file')
366
- .option('--json', 'Output as JSON')
367
- .option('--by-page', 'Group comments by page')
368
- .option('--by-author', 'Group comments by author')
369
- .option('--with-text', 'Extract highlighted text (slower but shows what was highlighted)')
370
- .action(async (pdf: string, options: PdfCommentsOptions) => {
371
- if (!fs.existsSync(pdf)) {
372
- console.error(fmt.status('error', `File not found: ${pdf}`));
373
- process.exit(1);
374
- }
375
-
376
- if (!pdf.toLowerCase().endsWith('.pdf')) {
377
- console.error(fmt.status('error', 'File must be a PDF'));
378
- process.exit(1);
379
- }
380
-
381
- const {
382
- extractPdfComments,
383
- extractPdfAnnotationsWithText,
384
- formatPdfComments,
385
- getPdfCommentStats,
386
- insertPdfCommentsIntoMarkdown,
387
- formatAnnotationWithText,
388
- } = await import('../pdf-import.js');
389
-
390
- const spin = fmt.spinner(`Extracting comments from ${path.basename(pdf)}...`).start();
391
-
392
- try {
393
- let comments: any[];
394
-
395
- if (options.withText) {
396
- // Use the new text extraction feature
397
- const annotations = await extractPdfAnnotationsWithText(pdf);
398
- spin.stop();
399
-
400
- if (annotations.length === 0) {
401
- console.log(fmt.status('info', 'No annotations found in PDF.'));
402
- return;
403
- }
404
-
405
- // Convert to comment format with highlighted text
406
- comments = annotations.map(a => ({
407
- author: a.author || 'Reviewer',
408
- text: a.highlightedText
409
- ? `"${a.highlightedText}"${a.contents ? ' → ' + a.contents : ''}`
410
- : a.contents,
411
- page: a.page,
412
- type: a.type,
413
- date: a.date,
414
- highlightedText: a.highlightedText,
415
- })).filter(c => c.text);
416
- } else {
417
- comments = await extractPdfComments(pdf);
418
- spin.stop();
419
- }
420
-
421
- if (comments.length === 0) {
422
- console.log(fmt.status('info', 'No comments found in PDF.'));
423
- return;
424
- }
425
-
426
- const stats = getPdfCommentStats(comments);
427
-
428
- // JSON output
429
- if (options.json) {
430
- console.log(JSON.stringify({ comments, stats }, null, 2));
431
- return;
432
- }
433
-
434
- // Append to markdown file
435
- if (options.append) {
436
- if (!fs.existsSync(options.append)) {
437
- console.error(fmt.status('error', `Markdown file not found: ${options.append}`));
438
- process.exit(1);
439
- }
440
-
441
- const markdown = fs.readFileSync(options.append, 'utf-8');
442
- const updated = insertPdfCommentsIntoMarkdown(markdown, comments);
443
- fs.writeFileSync(options.append, updated, 'utf-8');
444
-
445
- console.log(fmt.status('success', `Added ${comments.length} comments to ${options.append}`));
446
- return;
447
- }
448
-
449
- // Display comments
450
- console.log(fmt.header(`PDF Comments: ${path.basename(pdf)}`));
451
- console.log();
452
-
453
- if (options.byAuthor) {
454
- // Group by author
455
- const byAuthor: Record<string, any[]> = {};
456
- for (const c of comments) {
457
- const author = c.author || 'Unknown';
458
- if (!byAuthor[author]) byAuthor[author] = [];
459
- byAuthor[author].push(c);
460
- }
461
-
462
- for (const [author, authorComments] of Object.entries(byAuthor)) {
463
- console.log(chalk.bold(`${author} (${authorComments.length}):`));
464
- for (const c of authorComments) {
465
- if (c.highlightedText) {
466
- console.log(` [p.${c.page}] ${chalk.yellow(`"${c.highlightedText}"`)}${c.text !== c.highlightedText ? ` → ${c.text.replace(`"${c.highlightedText}" → `, '')}` : ''}`);
467
- } else {
468
- console.log(` [p.${c.page}] ${c.text}`);
469
- }
470
- }
471
- console.log();
472
- }
473
- } else {
474
- // Default: by page
475
- if (options.withText) {
476
- let currentPage = 0;
477
- for (const c of comments) {
478
- if (c.page !== currentPage) {
479
- if (currentPage > 0) console.log();
480
- console.log(`Page ${c.page}:`);
481
- currentPage = c.page;
482
- }
483
- if (c.highlightedText) {
484
- console.log(` ${chalk.yellow(`"${c.highlightedText}"`)} → ${c.text.replace(`"${c.highlightedText}" → `, '')}`);
485
- } else {
486
- console.log(` ${c.text}`);
487
- }
488
- }
489
- console.log();
490
- } else {
491
- console.log(formatPdfComments(comments));
492
- console.log();
493
- }
494
- }
495
-
496
- // Summary
497
- const authorList = Object.entries(stats.byAuthor)
498
- .map(([author, count]) => `${author} (${count})`)
499
- .join(', ');
500
- console.log(chalk.dim(`Total: ${stats.total} comments from ${authorList}`));
501
- console.log();
502
- if (!options.withText) {
503
- console.log(chalk.dim(`Tip: Use --with-text to extract the highlighted text content`));
504
- }
505
- console.log(chalk.dim(`Tip: Use --append <file.md> to add comments to your markdown`));
506
-
507
- } catch (err) {
508
- spin.stop();
509
- const error = err as Error;
510
- console.error(fmt.status('error', `Failed to extract PDF comments: ${error.message}`));
511
- if (process.env.DEBUG) console.error(error.stack);
512
- process.exit(1);
513
- }
514
- });
515
- }
1
+ /**
2
+ * Citation commands: citations, figures, equations, pdf-comments
3
+ *
4
+ * Commands for validating citations, managing figures/tables, and equation handling.
5
+ */
6
+
7
+ import {
8
+ chalk,
9
+ fs,
10
+ path,
11
+ fmt,
12
+ } from './context.js';
13
+ import type { Command } from 'commander';
14
+
15
+ interface CitationsOptions {
16
+ bib: string;
17
+ }
18
+
19
+ interface EquationsOptions {
20
+ output?: string;
21
+ }
22
+
23
+ interface PdfCommentsOptions {
24
+ append?: string;
25
+ json?: boolean;
26
+ byPage?: boolean;
27
+ byAuthor?: boolean;
28
+ withText?: boolean;
29
+ }
30
+
31
+ /**
32
+ * Register citation commands with the program
33
+ */
34
+ export function register(program: Command): void {
35
+ // ==========================================================================
36
+ // CITATIONS command - Validate citations against .bib file
37
+ // ==========================================================================
38
+
39
+ program
40
+ .command('citations')
41
+ .alias('cite')
42
+ .description('Validate citations against bibliography')
43
+ .argument('[files...]', 'Markdown files to check (default: all section files)')
44
+ .option('-b, --bib <file>', 'Bibliography file', 'references.bib')
45
+ .action(async (files: string[], options: CitationsOptions) => {
46
+ const { getCitationStats } = await import('../citations.js');
47
+
48
+ // If no files specified, find all .md files
49
+ let mdFiles = files;
50
+ if (!mdFiles || mdFiles.length === 0) {
51
+ mdFiles = fs.readdirSync('.').filter(f =>
52
+ f.endsWith('.md') && !['README.md', 'CLAUDE.md'].includes(f)
53
+ );
54
+ }
55
+
56
+ if (!fs.existsSync(options.bib)) {
57
+ console.error(fmt.status('error', `Bibliography not found: ${options.bib}`));
58
+ process.exit(1);
59
+ }
60
+
61
+ const stats = getCitationStats(mdFiles, options.bib);
62
+
63
+ console.log(fmt.header('Citation Check'));
64
+ console.log();
65
+
66
+ // Summary table
67
+ const rows: string[][] = [
68
+ ['Total citations', stats.totalCitations.toString()],
69
+ ['Unique keys cited', stats.uniqueCited.toString()],
70
+ ['Bib entries', stats.bibEntries.toString()],
71
+ [chalk.green('Valid'), chalk.green(stats.valid.toString())],
72
+ [stats.missing > 0 ? chalk.red('Missing') : 'Missing', stats.missing > 0 ? chalk.red(stats.missing.toString()) : '0'],
73
+ [chalk.dim('Unused in bib'), chalk.dim(stats.unused.toString())],
74
+ ];
75
+ console.log(fmt.table(['Metric', 'Count'], rows));
76
+
77
+ // Show missing keys
78
+ if (stats.missingKeys.length > 0) {
79
+ console.log();
80
+ console.log(fmt.status('error', 'Missing citations:'));
81
+ for (const key of stats.missingKeys) {
82
+ console.log(chalk.red(` - ${key}`));
83
+ }
84
+ }
85
+
86
+ // Show unused (if verbose)
87
+ if (stats.unusedKeys.length > 0 && stats.unusedKeys.length <= 10) {
88
+ console.log();
89
+ console.log(chalk.dim('Unused bib entries:'));
90
+ for (const key of stats.unusedKeys.slice(0, 10)) {
91
+ console.log(chalk.dim(` - ${key}`));
92
+ }
93
+ if (stats.unusedKeys.length > 10) {
94
+ console.log(chalk.dim(` ... and ${stats.unusedKeys.length - 10} more`));
95
+ }
96
+ }
97
+
98
+ console.log();
99
+ if (stats.missing === 0) {
100
+ console.log(fmt.status('success', 'All citations valid'));
101
+ } else {
102
+ console.log(fmt.status('warning', `${stats.missing} citation(s) missing from ${options.bib}`));
103
+ process.exit(1);
104
+ }
105
+ });
106
+
107
+ // ==========================================================================
108
+ // FIGURES command - Figure/table inventory
109
+ // ==========================================================================
110
+
111
+ program
112
+ .command('figures')
113
+ .alias('figs')
114
+ .description('List all figures and tables with reference counts')
115
+ .argument('[files...]', 'Markdown files to scan')
116
+ .action(async (files: string[]) => {
117
+ const { buildRegistry } = await import('../crossref.js');
118
+
119
+ // If no files specified, find all .md files
120
+ let mdFiles = files;
121
+ if (!mdFiles || mdFiles.length === 0) {
122
+ mdFiles = fs.readdirSync('.').filter(f =>
123
+ f.endsWith('.md') && !['README.md', 'CLAUDE.md'].includes(f)
124
+ );
125
+ }
126
+
127
+ // Build registry
128
+ const registry = buildRegistry('.');
129
+
130
+ // Count references in files
131
+ const refCounts = new Map<string, number>();
132
+ for (const file of mdFiles) {
133
+ if (!fs.existsSync(file)) continue;
134
+ const text = fs.readFileSync(file, 'utf-8');
135
+
136
+ // Count @fig: and @tbl: references
137
+ const figRefs = text.matchAll(/@fig:([a-zA-Z0-9_-]+)/g);
138
+ for (const match of figRefs) {
139
+ const key = `fig:${match[1]}`;
140
+ refCounts.set(key, (refCounts.get(key) || 0) + 1);
141
+ }
142
+
143
+ const tblRefs = text.matchAll(/@tbl:([a-zA-Z0-9_-]+)/g);
144
+ for (const match of tblRefs) {
145
+ const key = `tbl:${match[1]}`;
146
+ refCounts.set(key, (refCounts.get(key) || 0) + 1);
147
+ }
148
+ }
149
+
150
+ console.log(fmt.header('Figure & Table Inventory'));
151
+ console.log();
152
+
153
+ // Figures
154
+ if (registry.figures.size > 0) {
155
+ const figRows = [...registry.figures.entries()].map(([label, info]) => {
156
+ const key = `fig:${label}`;
157
+ const refs = refCounts.get(key) || 0;
158
+ const num = info.isSupp ? `S${info.num}` : info.num.toString();
159
+ return [
160
+ `Figure ${num}`,
161
+ chalk.cyan(`@fig:${label}`),
162
+ info.file,
163
+ refs > 0 ? chalk.green(refs.toString()) : chalk.yellow('0'),
164
+ ];
165
+ });
166
+ console.log(fmt.table(['#', 'Label', 'File', 'Refs'], figRows));
167
+ console.log();
168
+ }
169
+
170
+ // Tables
171
+ if (registry.tables.size > 0) {
172
+ const tblRows = [...registry.tables.entries()].map(([label, info]) => {
173
+ const key = `tbl:${label}`;
174
+ const refs = refCounts.get(key) || 0;
175
+ const num = info.isSupp ? `S${info.num}` : info.num.toString();
176
+ return [
177
+ `Table ${num}`,
178
+ chalk.cyan(`@tbl:${label}`),
179
+ info.file,
180
+ refs > 0 ? chalk.green(refs.toString()) : chalk.yellow('0'),
181
+ ];
182
+ });
183
+ console.log(fmt.table(['#', 'Label', 'File', 'Refs'], tblRows));
184
+ console.log();
185
+ }
186
+
187
+ if (registry.figures.size === 0 && registry.tables.size === 0) {
188
+ console.log(chalk.dim('No figures or tables found.'));
189
+ console.log(chalk.dim('Add anchors like {#fig:label} to your figures.'));
190
+ }
191
+
192
+ // Warn about unreferenced
193
+ const unreferenced: string[] = [];
194
+ for (const [label] of registry.figures) {
195
+ if (!refCounts.get(`fig:${label}`)) unreferenced.push(`@fig:${label}`);
196
+ }
197
+ for (const [label] of registry.tables) {
198
+ if (!refCounts.get(`tbl:${label}`)) unreferenced.push(`@tbl:${label}`);
199
+ }
200
+
201
+ if (unreferenced.length > 0) {
202
+ console.log(fmt.status('warning', `${unreferenced.length} unreferenced figure(s)/table(s)`));
203
+ }
204
+ });
205
+
206
+ // ==========================================================================
207
+ // EQUATIONS command - Extract and convert equations
208
+ // ==========================================================================
209
+
210
+ program
211
+ .command('equations')
212
+ .alias('eq')
213
+ .description('Extract equations or convert to Word')
214
+ .argument('<action>', 'Action: list, extract, convert, from-word')
215
+ .argument('[input]', 'Input file (.md for extract/convert, .docx for from-word)')
216
+ .option('-o, --output <file>', 'Output file')
217
+ .action(async (action: string, input: string | undefined, options: EquationsOptions) => {
218
+ const { extractEquations, getEquationStats, createEquationsDoc, extractEquationsFromWord, getWordEquationStats } = await import('../equations.js');
219
+
220
+ if (action === 'from-word') {
221
+ // Extract equations from Word document
222
+ if (!input) {
223
+ console.error(fmt.status('error', 'Word document required'));
224
+ process.exit(1);
225
+ }
226
+
227
+ if (!input.endsWith('.docx')) {
228
+ console.error(fmt.status('error', 'Input must be a .docx file'));
229
+ process.exit(1);
230
+ }
231
+
232
+ const spin = fmt.spinner(`Extracting equations from ${path.basename(input)}...`).start();
233
+
234
+ const result = await extractEquationsFromWord(input);
235
+
236
+ if (!result.success) {
237
+ spin.error(result.error!);
238
+ process.exit(1);
239
+ }
240
+
241
+ spin.stop();
242
+ console.log(fmt.header('Equations from Word'));
243
+ console.log();
244
+
245
+ if (result.equations.length === 0) {
246
+ console.log(chalk.dim('No equations found in document.'));
247
+ return;
248
+ }
249
+
250
+ const display = result.equations.filter(e => e.type === 'display');
251
+ const inline = result.equations.filter(e => e.type === 'inline');
252
+
253
+ console.log(chalk.dim(`Found ${result.equations.length} equations (${display.length} display, ${inline.length} inline)`));
254
+ console.log();
255
+
256
+ // Show equations
257
+ for (let i = 0; i < result.equations.length; i++) {
258
+ const eq = result.equations[i];
259
+ const typeLabel = eq.type === 'display' ? chalk.cyan('[display]') : chalk.yellow('[inline]');
260
+
261
+ if (eq.latex) {
262
+ console.log(`${chalk.bold((i + 1).toString())}. ${typeLabel}`);
263
+ console.log(chalk.dim(' LaTeX:'), eq.latex.length > 80 ? eq.latex.substring(0, 77) + '...' : eq.latex);
264
+ } else {
265
+ console.log(`${chalk.bold((i + 1).toString())}. ${typeLabel} ${chalk.red('[conversion failed]')}`);
266
+ }
267
+ }
268
+
269
+ // Optionally save to file
270
+ if (options.output) {
271
+ const latex = result.equations
272
+ .filter(e => e.latex)
273
+ .map((e, i) => `%% Equation ${i + 1} (${e.type})\n${e.type === 'display' ? '$$' : '$'}${e.latex}${e.type === 'display' ? '$$' : '$'}`)
274
+ .join('\n\n');
275
+
276
+ fs.writeFileSync(options.output, latex, 'utf-8');
277
+ console.log();
278
+ console.log(fmt.status('success', `Saved ${result.equations.filter(e => e.latex).length} equations to ${options.output}`));
279
+ }
280
+
281
+ } else if (action === 'list') {
282
+ // List equations in all section files
283
+ const mdFiles = fs.readdirSync('.').filter(f =>
284
+ f.endsWith('.md') && !['README.md', 'CLAUDE.md'].includes(f)
285
+ );
286
+
287
+ const stats = getEquationStats(mdFiles);
288
+
289
+ console.log(fmt.header('Equations'));
290
+ console.log();
291
+
292
+ if (stats.byFile.length === 0) {
293
+ console.log(chalk.dim('No equations found.'));
294
+ return;
295
+ }
296
+
297
+ const rows = stats.byFile.map(f => [
298
+ f.file,
299
+ f.display > 0 ? chalk.cyan(f.display.toString()) : chalk.dim('-'),
300
+ f.inline > 0 ? chalk.yellow(f.inline.toString()) : chalk.dim('-'),
301
+ ]);
302
+ rows.push([
303
+ chalk.bold('Total'),
304
+ chalk.bold.cyan(stats.display.toString()),
305
+ chalk.bold.yellow(stats.inline.toString()),
306
+ ]);
307
+
308
+ console.log(fmt.table(['File', 'Display', 'Inline'], rows));
309
+
310
+ } else if (action === 'extract') {
311
+ if (!input) {
312
+ console.error(fmt.status('error', 'Input file required'));
313
+ process.exit(1);
314
+ }
315
+
316
+ const output = options.output || input.replace('.md', '-equations.md');
317
+ const result = await createEquationsDoc(input, output);
318
+
319
+ if (result.success) {
320
+ console.log(fmt.status('success', result.message));
321
+ console.log(chalk.dim(` ${result.stats!.display} display, ${result.stats!.inline} inline equations`));
322
+ } else {
323
+ console.error(fmt.status('error', result.message));
324
+ process.exit(1);
325
+ }
326
+
327
+ } else if (action === 'convert') {
328
+ if (!input) {
329
+ console.error(fmt.status('error', 'Input file required'));
330
+ process.exit(1);
331
+ }
332
+
333
+ const output = options.output || input.replace('.md', '.docx');
334
+
335
+ const spin = fmt.spinner(`Converting ${path.basename(input)} to Word...`).start();
336
+
337
+ try {
338
+ const { exec } = await import('child_process');
339
+ const { promisify } = await import('util');
340
+ const execAsync = promisify(exec);
341
+
342
+ await execAsync(`pandoc "${input}" -o "${output}" --mathml`);
343
+ spin.success(`Created ${output}`);
344
+ } catch (err) {
345
+ const error = err as Error;
346
+ spin.error(error.message);
347
+ process.exit(1);
348
+ }
349
+ } else {
350
+ console.error(fmt.status('error', `Unknown action: ${action}`));
351
+ console.log(chalk.dim('Actions: list, extract, convert, from-word'));
352
+ process.exit(1);
353
+ }
354
+ });
355
+
356
+ // ==========================================================================
357
+ // PDF-COMMENTS command - Extract comments from PDF
358
+ // ==========================================================================
359
+
360
+ program
361
+ .command('pdf-comments')
362
+ .alias('pdf')
363
+ .description('Extract and manage comments from annotated PDFs')
364
+ .argument('<pdf>', 'PDF file with annotations')
365
+ .option('-a, --append <file>', 'Append comments to markdown file')
366
+ .option('--json', 'Output as JSON')
367
+ .option('--by-page', 'Group comments by page')
368
+ .option('--by-author', 'Group comments by author')
369
+ .option('--with-text', 'Extract highlighted text (slower but shows what was highlighted)')
370
+ .action(async (pdf: string, options: PdfCommentsOptions) => {
371
+ if (!fs.existsSync(pdf)) {
372
+ console.error(fmt.status('error', `File not found: ${pdf}`));
373
+ process.exit(1);
374
+ }
375
+
376
+ if (!pdf.toLowerCase().endsWith('.pdf')) {
377
+ console.error(fmt.status('error', 'File must be a PDF'));
378
+ process.exit(1);
379
+ }
380
+
381
+ const {
382
+ extractPdfComments,
383
+ extractPdfAnnotationsWithText,
384
+ formatPdfComments,
385
+ getPdfCommentStats,
386
+ insertPdfCommentsIntoMarkdown,
387
+ formatAnnotationWithText,
388
+ } = await import('../pdf-import.js');
389
+
390
+ const spin = fmt.spinner(`Extracting comments from ${path.basename(pdf)}...`).start();
391
+
392
+ try {
393
+ let comments: any[];
394
+
395
+ if (options.withText) {
396
+ // Use the new text extraction feature
397
+ const annotations = await extractPdfAnnotationsWithText(pdf);
398
+ spin.stop();
399
+
400
+ if (annotations.length === 0) {
401
+ console.log(fmt.status('info', 'No annotations found in PDF.'));
402
+ return;
403
+ }
404
+
405
+ // Convert to comment format with highlighted text
406
+ comments = annotations.map(a => ({
407
+ author: a.author || 'Reviewer',
408
+ text: a.highlightedText
409
+ ? `"${a.highlightedText}"${a.contents ? ' → ' + a.contents : ''}`
410
+ : a.contents,
411
+ page: a.page,
412
+ type: a.type,
413
+ date: a.date,
414
+ highlightedText: a.highlightedText,
415
+ })).filter(c => c.text);
416
+ } else {
417
+ comments = await extractPdfComments(pdf);
418
+ spin.stop();
419
+ }
420
+
421
+ if (comments.length === 0) {
422
+ console.log(fmt.status('info', 'No comments found in PDF.'));
423
+ return;
424
+ }
425
+
426
+ const stats = getPdfCommentStats(comments);
427
+
428
+ // JSON output
429
+ if (options.json) {
430
+ console.log(JSON.stringify({ comments, stats }, null, 2));
431
+ return;
432
+ }
433
+
434
+ // Append to markdown file
435
+ if (options.append) {
436
+ if (!fs.existsSync(options.append)) {
437
+ console.error(fmt.status('error', `Markdown file not found: ${options.append}`));
438
+ process.exit(1);
439
+ }
440
+
441
+ const markdown = fs.readFileSync(options.append, 'utf-8');
442
+ const updated = insertPdfCommentsIntoMarkdown(markdown, comments);
443
+ fs.writeFileSync(options.append, updated, 'utf-8');
444
+
445
+ console.log(fmt.status('success', `Added ${comments.length} comments to ${options.append}`));
446
+ return;
447
+ }
448
+
449
+ // Display comments
450
+ console.log(fmt.header(`PDF Comments: ${path.basename(pdf)}`));
451
+ console.log();
452
+
453
+ if (options.byAuthor) {
454
+ // Group by author
455
+ const byAuthor: Record<string, any[]> = {};
456
+ for (const c of comments) {
457
+ const author = c.author || 'Unknown';
458
+ if (!byAuthor[author]) byAuthor[author] = [];
459
+ byAuthor[author].push(c);
460
+ }
461
+
462
+ for (const [author, authorComments] of Object.entries(byAuthor)) {
463
+ console.log(chalk.bold(`${author} (${authorComments.length}):`));
464
+ for (const c of authorComments) {
465
+ if (c.highlightedText) {
466
+ console.log(` [p.${c.page}] ${chalk.yellow(`"${c.highlightedText}"`)}${c.text !== c.highlightedText ? ` → ${c.text.replace(`"${c.highlightedText}" → `, '')}` : ''}`);
467
+ } else {
468
+ console.log(` [p.${c.page}] ${c.text}`);
469
+ }
470
+ }
471
+ console.log();
472
+ }
473
+ } else {
474
+ // Default: by page
475
+ if (options.withText) {
476
+ let currentPage = 0;
477
+ for (const c of comments) {
478
+ if (c.page !== currentPage) {
479
+ if (currentPage > 0) console.log();
480
+ console.log(`Page ${c.page}:`);
481
+ currentPage = c.page;
482
+ }
483
+ if (c.highlightedText) {
484
+ console.log(` ${chalk.yellow(`"${c.highlightedText}"`)} → ${c.text.replace(`"${c.highlightedText}" → `, '')}`);
485
+ } else {
486
+ console.log(` ${c.text}`);
487
+ }
488
+ }
489
+ console.log();
490
+ } else {
491
+ console.log(formatPdfComments(comments));
492
+ console.log();
493
+ }
494
+ }
495
+
496
+ // Summary
497
+ const authorList = Object.entries(stats.byAuthor)
498
+ .map(([author, count]) => `${author} (${count})`)
499
+ .join(', ');
500
+ console.log(chalk.dim(`Total: ${stats.total} comments from ${authorList}`));
501
+ console.log();
502
+ if (!options.withText) {
503
+ console.log(chalk.dim(`Tip: Use --with-text to extract the highlighted text content`));
504
+ }
505
+ console.log(chalk.dim(`Tip: Use --append <file.md> to add comments to your markdown`));
506
+
507
+ } catch (err) {
508
+ spin.stop();
509
+ const error = err as Error;
510
+ console.error(fmt.status('error', `Failed to extract PDF comments: ${error.message}`));
511
+ if (process.env.DEBUG) console.error(error.stack);
512
+ process.exit(1);
513
+ }
514
+ });
515
+ }