Files
brain/tools/negotiation-section.mjs
T

36 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* negotiation-section — парсер раздела «## Переговоры» план-файла. Возвращает круги
* [{round, position}] с ДОСЛОВНЫМ текстом позиции контроллера (для наставника и арбитража).
* Раздел = «## Переговоры» до следующего «## », внутри — «### Круг N» блоки.
*/
export function parseNegotiationSection(md) {
const text = typeof md === 'string' ? md : '';
// БЕЗ флага m: `$` = конец ТЕКСТА (не строки), иначе нежадный захват обрывается на 1-й строке.
// Заголовки матчим через (?:^|\n), терминатор — следующий «\n## » или конец текста.
const sec = text.match(/(?:^|\n)##\s*Переговоры[^\n]*\n([\s\S]*?)(?=\n##\s|$)/i);
if (!sec) return [];
const body = sec[1];
const out = [];
const re = /(?:^|\n)###\s*Круг\s*(\d+)[^\n]*\n([\s\S]*?)(?=\n###\s|$)/gi;
let m;
while ((m = re.exec(body)) !== null) {
const position = m[2].trim();
out.push({
round: Number(m[1]),
position,
mentor: extractAddressee(position, 'Наставнику'),
judge: extractAddressee(position, 'Судье'),
});
}
return out;
}
/** Достаёт текст, адресованный стороне (`**Наставнику:**` / `**Судье:**`), до следующего
* адресата или конца. Нет адресата → пустая строка (обратная совместимость с position). */
function extractAddressee(body, label) {
const re = new RegExp(`\\*\\*${label}:\\*\\*\\s*([\\s\\S]*?)(?=\\n?\\*\\*(?:Наставнику|Судье):\\*\\*|$)`, 'i');
const mm = String(body || '').match(re);
return mm ? mm[1].trim() : '';
}