docrev 0.2.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.
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Response letter generator
3
+ * Extract comments and replies from markdown files for journal resubmission
4
+ */
5
+
6
+ import * as fs from 'fs';
7
+ import * as path from 'path';
8
+
9
+ /**
10
+ * Parse a comment with potential replies
11
+ * Format: {>>Author: comment<<} {>>Reply Author: reply<<}
12
+ * @param {string} text
13
+ * @returns {Array<{author: string, text: string, replies: Array, context: string, file: string, line: number}>}
14
+ */
15
+ export function parseCommentsWithReplies(text, file = '') {
16
+ const comments = [];
17
+ const lines = text.split('\n');
18
+
19
+ // Pattern for comments: {>>Author: text<<}
20
+ const commentPattern = /\{>>([^:]+):\s*([^<]+)<<\}/g;
21
+
22
+ for (let lineNum = 0; lineNum < lines.length; lineNum++) {
23
+ const line = lines[lineNum];
24
+ const matches = [...line.matchAll(commentPattern)];
25
+
26
+ if (matches.length === 0) continue;
27
+
28
+ // Get context (surrounding text without comments)
29
+ const contextLine = line.replace(/\{>>[^<]+<<\}/g, '').trim();
30
+ const context = contextLine.slice(0, 100) + (contextLine.length > 100 ? '...' : '');
31
+
32
+ // First match is the original comment, rest are replies
33
+ const [first, ...rest] = matches;
34
+
35
+ comments.push({
36
+ author: first[1].trim(),
37
+ text: first[2].trim(),
38
+ replies: rest.map(m => ({
39
+ author: m[1].trim(),
40
+ text: m[2].trim(),
41
+ })),
42
+ context,
43
+ file,
44
+ line: lineNum + 1,
45
+ });
46
+ }
47
+
48
+ return comments;
49
+ }
50
+
51
+ /**
52
+ * Group comments by reviewer
53
+ * @param {Array} comments
54
+ * @returns {Map<string, Array>}
55
+ */
56
+ export function groupByReviewer(comments) {
57
+ const grouped = new Map();
58
+
59
+ for (const comment of comments) {
60
+ const reviewer = comment.author;
61
+ if (!grouped.has(reviewer)) {
62
+ grouped.set(reviewer, []);
63
+ }
64
+ grouped.get(reviewer).push(comment);
65
+ }
66
+
67
+ return grouped;
68
+ }
69
+
70
+ /**
71
+ * Generate response letter in Markdown format
72
+ * @param {Array} comments - All comments from all files
73
+ * @param {object} options
74
+ * @returns {string}
75
+ */
76
+ export function generateResponseLetter(comments, options = {}) {
77
+ const {
78
+ title = 'Response to Reviewers',
79
+ authorName = 'Author',
80
+ includeContext = true,
81
+ includeLocation = true,
82
+ } = options;
83
+
84
+ const lines = [];
85
+ lines.push(`# ${title}`);
86
+ lines.push('');
87
+ lines.push(`We thank the reviewers for their constructive feedback. Below we address each comment.`);
88
+ lines.push('');
89
+
90
+ // Group by reviewer
91
+ const grouped = groupByReviewer(comments);
92
+
93
+ // Sort reviewers (put known reviewer names first, then others)
94
+ const reviewers = [...grouped.keys()].sort((a, b) => {
95
+ // "Reviewer" names first, then alphabetical
96
+ const aIsReviewer = a.toLowerCase().includes('reviewer');
97
+ const bIsReviewer = b.toLowerCase().includes('reviewer');
98
+ if (aIsReviewer && !bIsReviewer) return -1;
99
+ if (!aIsReviewer && bIsReviewer) return 1;
100
+ return a.localeCompare(b);
101
+ });
102
+
103
+ for (const reviewer of reviewers) {
104
+ // Skip if this is the author's own comments (replies)
105
+ if (reviewer.toLowerCase() === authorName.toLowerCase()) continue;
106
+ if (reviewer.toLowerCase() === 'claude') continue;
107
+
108
+ const reviewerComments = grouped.get(reviewer);
109
+ lines.push(`## ${reviewer}`);
110
+ lines.push('');
111
+
112
+ for (let i = 0; i < reviewerComments.length; i++) {
113
+ const c = reviewerComments[i];
114
+
115
+ lines.push(`### Comment ${i + 1}`);
116
+ if (includeLocation) {
117
+ lines.push(`*${c.file}:${c.line}*`);
118
+ }
119
+ lines.push('');
120
+
121
+ // Original comment
122
+ lines.push(`> **${c.author}:** ${c.text}`);
123
+ lines.push('');
124
+
125
+ // Context if available
126
+ if (includeContext && c.context) {
127
+ lines.push(`*Context:* "${c.context}"`);
128
+ lines.push('');
129
+ }
130
+
131
+ // Replies
132
+ if (c.replies.length > 0) {
133
+ lines.push('**Response:**');
134
+ lines.push('');
135
+ for (const reply of c.replies) {
136
+ lines.push(`${reply.text}`);
137
+ }
138
+ } else {
139
+ lines.push('**Response:**');
140
+ lines.push('');
141
+ lines.push('*[TODO: Add response]*');
142
+ }
143
+ lines.push('');
144
+ lines.push('---');
145
+ lines.push('');
146
+ }
147
+ }
148
+
149
+ // Summary stats
150
+ const totalComments = comments.filter(c =>
151
+ !c.author.toLowerCase().includes('claude') &&
152
+ c.author.toLowerCase() !== authorName.toLowerCase()
153
+ ).length;
154
+ const answered = comments.filter(c => c.replies.length > 0).length;
155
+
156
+ lines.push('## Summary');
157
+ lines.push('');
158
+ lines.push(`- Total comments: ${totalComments}`);
159
+ lines.push(`- Addressed: ${answered}`);
160
+ lines.push(`- Pending: ${totalComments - answered}`);
161
+
162
+ return lines.join('\n');
163
+ }
164
+
165
+ /**
166
+ * Collect comments from multiple files
167
+ * @param {string[]} files - Array of file paths
168
+ * @returns {Array}
169
+ */
170
+ export function collectComments(files) {
171
+ const allComments = [];
172
+
173
+ for (const file of files) {
174
+ if (!fs.existsSync(file)) continue;
175
+
176
+ const text = fs.readFileSync(file, 'utf-8');
177
+ const comments = parseCommentsWithReplies(text, path.basename(file));
178
+ allComments.push(...comments);
179
+ }
180
+
181
+ return allComments;
182
+ }
package/lib/review.js ADDED
@@ -0,0 +1,208 @@
1
+ /**
2
+ * Interactive review TUI for track changes
3
+ */
4
+
5
+ import * as readline from 'readline';
6
+ import chalk from 'chalk';
7
+ import { getTrackChanges, getComments, applyDecision } from './annotations.js';
8
+
9
+ /**
10
+ * Format an annotation for display
11
+ * @param {object} annotation
12
+ * @param {number} index
13
+ * @param {number} total
14
+ * @returns {string}
15
+ */
16
+ function formatAnnotation(annotation, index, total) {
17
+ const header = chalk.dim(`─── Change ${index + 1}/${total} (line ${annotation.line}) ───`);
18
+
19
+ let action;
20
+ let display;
21
+
22
+ switch (annotation.type) {
23
+ case 'insert':
24
+ action = chalk.green(`+ Insert: "${annotation.content}"`);
25
+ display =
26
+ chalk.dim(annotation.before) +
27
+ chalk.green.bold(`[${annotation.content}]`) +
28
+ chalk.dim(annotation.after);
29
+ break;
30
+ case 'delete':
31
+ action = chalk.red(`- Delete: "${annotation.content}"`);
32
+ display =
33
+ chalk.dim(annotation.before) +
34
+ chalk.red.strikethrough(`[${annotation.content}]`) +
35
+ chalk.dim(annotation.after);
36
+ break;
37
+ case 'substitute':
38
+ action = chalk.yellow(`~ Change: "${annotation.content}" → "${annotation.replacement}"`);
39
+ display =
40
+ chalk.dim(annotation.before) +
41
+ chalk.red.strikethrough(`[${annotation.content}]`) +
42
+ chalk.dim(' → ') +
43
+ chalk.green.bold(`[${annotation.replacement}]`) +
44
+ chalk.dim(annotation.after);
45
+ break;
46
+ }
47
+
48
+ return `\n${header}\n\n ${action}\n\n ${display}\n`;
49
+ }
50
+
51
+ /**
52
+ * Prompt for a single keypress
53
+ * @param {string} prompt
54
+ * @param {string[]} validKeys
55
+ * @returns {Promise<string>}
56
+ */
57
+ function promptKey(prompt, validKeys) {
58
+ return new Promise((resolve) => {
59
+ const rl = readline.createInterface({
60
+ input: process.stdin,
61
+ output: process.stdout,
62
+ });
63
+
64
+ // Enable raw mode for single keypress
65
+ if (process.stdin.isTTY) {
66
+ process.stdin.setRawMode(true);
67
+ }
68
+ process.stdin.resume();
69
+
70
+ process.stdout.write(prompt);
71
+
72
+ process.stdin.once('data', (key) => {
73
+ const char = key.toString().toLowerCase();
74
+
75
+ if (process.stdin.isTTY) {
76
+ process.stdin.setRawMode(false);
77
+ }
78
+ rl.close();
79
+
80
+ if (char === '\u0003') {
81
+ // Ctrl+C
82
+ process.exit(0);
83
+ }
84
+
85
+ if (validKeys.includes(char)) {
86
+ console.log(char);
87
+ resolve(char);
88
+ } else {
89
+ console.log();
90
+ resolve(promptKey(prompt, validKeys));
91
+ }
92
+ });
93
+ });
94
+ }
95
+
96
+ /**
97
+ * Run interactive review session
98
+ * @param {string} text
99
+ * @returns {Promise<{text: string, accepted: number, rejected: number, skipped: number}>}
100
+ */
101
+ export async function interactiveReview(text) {
102
+ const changes = getTrackChanges(text);
103
+ const comments = getComments(text);
104
+
105
+ if (changes.length === 0) {
106
+ console.log(chalk.green('No track changes found.'));
107
+ if (comments.length > 0) {
108
+ console.log(chalk.yellow(`${comments.length} comment(s) remain in the document.`));
109
+ }
110
+ return { text, accepted: 0, rejected: 0, skipped: 0 };
111
+ }
112
+
113
+ console.log(chalk.cyan(`\nFound ${changes.length} track change(s)\n`));
114
+
115
+ let accepted = 0;
116
+ let rejected = 0;
117
+ let skipped = 0;
118
+ let currentText = text;
119
+
120
+ for (let i = 0; i < changes.length; i++) {
121
+ const change = changes[i];
122
+ console.log(formatAnnotation(change, i, changes.length));
123
+
124
+ const prompt = chalk.dim('[a]ccept [r]eject [s]kip | accept [A]ll reject a[L]l [q]uit: ');
125
+ const choice = await promptKey(prompt, ['a', 'r', 's', 'A', 'L', 'q']);
126
+
127
+ switch (choice) {
128
+ case 'q':
129
+ console.log(chalk.yellow('\nAborted. No changes saved.'));
130
+ return { text, accepted: 0, rejected: 0, skipped: changes.length };
131
+
132
+ case 'A':
133
+ // Accept all remaining
134
+ for (let j = i; j < changes.length; j++) {
135
+ currentText = applyDecision(currentText, changes[j], true);
136
+ }
137
+ accepted += changes.length - i;
138
+ console.log(chalk.green(`\nAccepted all ${changes.length - i} remaining changes.`));
139
+ i = changes.length; // Exit loop
140
+ break;
141
+
142
+ case 'L':
143
+ // Reject all remaining
144
+ for (let j = i; j < changes.length; j++) {
145
+ currentText = applyDecision(currentText, changes[j], false);
146
+ }
147
+ rejected += changes.length - i;
148
+ console.log(chalk.red(`\nRejected all ${changes.length - i} remaining changes.`));
149
+ i = changes.length; // Exit loop
150
+ break;
151
+
152
+ case 'a':
153
+ currentText = applyDecision(currentText, change, true);
154
+ accepted++;
155
+ break;
156
+
157
+ case 'r':
158
+ currentText = applyDecision(currentText, change, false);
159
+ rejected++;
160
+ break;
161
+
162
+ case 's':
163
+ skipped++;
164
+ break;
165
+ }
166
+ }
167
+
168
+ console.log(chalk.cyan('\n─── Summary ───'));
169
+ console.log(chalk.green(`Accepted: ${accepted}`));
170
+ console.log(chalk.red(`Rejected: ${rejected}`));
171
+ console.log(chalk.yellow(`Skipped: ${skipped}`));
172
+
173
+ if (comments.length > 0) {
174
+ console.log(chalk.blue(`\n${comments.length} comment(s) preserved.`));
175
+ }
176
+
177
+ return { text: currentText, accepted, rejected, skipped };
178
+ }
179
+
180
+ /**
181
+ * List all comments
182
+ * @param {string} text
183
+ */
184
+ export function listComments(text) {
185
+ const comments = getComments(text);
186
+
187
+ if (comments.length === 0) {
188
+ console.log(chalk.green('No comments found.'));
189
+ return;
190
+ }
191
+
192
+ console.log(chalk.cyan(`\nFound ${comments.length} comment(s):\n`));
193
+
194
+ for (let i = 0; i < comments.length; i++) {
195
+ const c = comments[i];
196
+ const author = c.author || 'Anonymous';
197
+ const header = chalk.blue(`[${i + 1}] ${author}`) + chalk.dim(` (line ${c.line})`);
198
+
199
+ console.log(header);
200
+ console.log(` ${c.content}`);
201
+ console.log(
202
+ chalk.dim(` Context: ...${c.before.slice(-25)}`) +
203
+ chalk.yellow('*') +
204
+ chalk.dim(`${c.after.slice(0, 25)}...`)
205
+ );
206
+ console.log();
207
+ }
208
+ }