UNPKG

ai-pp3

Version:

CLI tool combining multimodal AI analysis with RawTherapee's engine to generate optimized PP3 profiles for RAW photography

104 lines 4.11 kB
import { splitContentIntoSections } from "./section-parser.js"; /** * Creates a map of section names to section content */ export function createSectionMap(sections) { const sectionMap = new Map(); for (const section of sections) { const sectionHeaderMatch = /^\[(.*?)\]/.exec(section); if (sectionHeaderMatch) { const sectionName = sectionHeaderMatch[1]; sectionMap.set(sectionName, section); } } return sectionMap; } /** * Applies changes to sections in the section map */ export function applySectionChanges(sectionMap, sectionChanges, verbose) { for (const change of sectionChanges) { const { sectionName, parameters } = change; if (verbose) { console.log(`Applying changes to section [${sectionName}]`); } // Get the original section content const originalSection = sectionMap.get(sectionName); if (!originalSection) { if (verbose) { console.warn(`Section [${sectionName}] not found in original content`); } continue; } // Apply parameter changes to the section const updatedSection = applyParameterChanges(originalSection, parameters, sectionName, verbose); // Update the section in the map sectionMap.set(sectionName, updatedSection); } } /** * Applies parameter changes to a section */ export function applyParameterChanges(sectionContent, parameters, sectionName, verbose) { const sectionLines = sectionContent.split("\n"); const updatedLines = [...sectionLines]; for (const [parameterName, parameterLine] of parameters.entries()) { const parameterRegex = new RegExp(`^\\s*${parameterName}\\s*=.*$`, "m"); const parameterLineIndex = sectionLines.findIndex((line) => parameterRegex.test(line)); if (parameterLineIndex !== -1) { // Replace the parameter line updatedLines[parameterLineIndex] = parameterLine; if (verbose) { console.log(` Changed: ${sectionLines[parameterLineIndex]} -> ${parameterLine}`); } } else if (verbose) { console.warn(` Parameter ${parameterName} not found in section [${sectionName}]`); } } return updatedLines.join("\n"); } /** * Reconstructs content from section map preserving original order */ export function reconstructContent(sectionOrders, sectionMap) { return sectionOrders .map((sectionName) => sectionMap.get(sectionName) ?? "") .filter(Boolean) .join("\n"); } /** * Reconstructs PP3 content from various section arrays */ export function reconstructPP3Content(sectionOrders, editedSections, includedSections, excludedSections) { return sectionOrders .map((sectionName) => { return (editedSections.find((section) => section.startsWith(`[${sectionName}]`)) ?? includedSections.find((section) => section.startsWith(`[${sectionName}]`)) ?? excludedSections.find((section) => section.startsWith(`[${sectionName}]`)) ?? ""); }) .join("\n"); } /** * Applies direct section changes to PP3 content * @param content - The original PP3 content * @param sectionChanges - Array of section changes to apply * @param verbose - Whether to log verbose information * @returns Updated PP3 content with changes applied */ export function applyDirectSectionChanges(content, sectionChanges, verbose) { if (sectionChanges.length === 0) { if (verbose) console.log("No section changes to apply"); return content; } // Split content into sections for easier manipulation const { sections, sectionOrders } = splitContentIntoSections(content); const sectionMap = createSectionMap(sections); // Apply changes to each section applySectionChanges(sectionMap, sectionChanges, verbose); // Reconstruct the content preserving the original section order return reconstructContent(sectionOrders, sectionMap); } //# sourceMappingURL=section-manipulation.js.map