forked from alexeykadochnik0v/js--pro
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfix-criteria-format.js
More file actions
111 lines (101 loc) · 3.08 KB
/
fix-criteria-format.js
File metadata and controls
111 lines (101 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const fs = require("fs");
const path = require("path");
const ROOT = path.join(__dirname, "lessons");
function listTaskFiles(dir) {
const out = [];
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, entry.name);
if (entry.isDirectory()) {
out.push(...listTaskFiles(p));
} else if (entry.isFile() && entry.name === "task.md") {
out.push(p);
}
}
return out;
}
function fixCriteria(md) {
const lines = md.split(/\r?\n/);
const out = [];
let i = 0;
while (i < lines.length) {
out.push(lines[i]);
if (lines[i].trim() === "## Критерии") {
i++;
// Copy pass/fail line(s) as-is
while (i < lines.length && lines[i].trim() === "") {
out.push(lines[i++]);
}
if (i < lines.length) {
out.push(lines[i++]);
}
// Normalize list until we reach a blank line followed by "Максимальный балл - 5" or until next heading
while (i < lines.length) {
const cur = lines[i];
const trimmed = cur.trim();
const next = lines[i + 1] ?? "";
if (trimmed.startsWith("## ") || trimmed.startsWith("# ")) break;
// Stop before max score line so we can re-insert with proper spacing
if (trimmed.replace(/\s+/g, " ") === "Максимальный балл - 5") {
i++;
break;
}
// Convert numbered list to bullets
const m = cur.match(/^(\s*)(\d+)\.\s+(.*)$/);
if (m) {
out.push(`${m[1]}- ${m[3]}`);
i++;
continue;
}
// Already bullet, keep
if (trimmed.startsWith("- ")) {
out.push(cur);
i++;
continue;
}
// Empty line inside list: keep
if (trimmed === "") {
out.push(cur);
i++;
continue;
}
// If it's an indented continuation of previous bullet/number, keep
if (/^\s{2,}\S/.test(cur)) {
out.push(cur);
i++;
continue;
}
// Otherwise, if it's the pass/fail line or other paragraph, keep
out.push(cur);
i++;
}
// Ensure a blank line before max score and the exact line
if (out.length && out[out.length - 1].trim() !== "") out.push("");
out.push("Максимальный балл - 5");
// Skip any duplicate/variant max score lines that might follow
while (
i < lines.length &&
lines[i].trim().replace(/\s+/g, " ") === "Максимальный балл - 5"
)
i++;
// Continue loop without pushing current line (already handled)
continue;
}
i++;
}
return out.join("\n");
}
function run() {
const files = listTaskFiles(ROOT);
let changed = 0;
for (const file of files) {
const orig = fs.readFileSync(file, "utf8");
const next = fixCriteria(orig);
if (next !== orig) {
fs.writeFileSync(file, next, "utf8");
changed++;
console.log(`fixed: ${path.relative(process.cwd(), file)}`);
}
}
console.log(`Done. Changed ${changed} file(s).`);
}
run();