Beschrijving (meerregelig) * ## Sectienaam * - [Label](URL): Omschrijving */ class FiveNamesLlmTxt_Importer { /** * Parseert de volledige inhoud van een llms.txt en geeft een settings-array terug * die direct opgeslagen kan worden als 'fivenames_llmtxt_settings'. * * @param string $content Volledige bestandsinhoud. * @return array */ public function parse( $content ) { $settings = [ 'auto_description' => false, 'auto_pages' => false, 'auto_categories' => false, 'optional_description' => '', 'auto_cpt' => [], 'auto_sitemap' => false, 'webhook_url' => '', 'sections' => [], ]; // Normaliseer regeleindes. $content = str_replace( [ "\r\n", "\r" ], "\n", $content ); $lines = explode( "\n", $content ); $current_section = null; $desc_lines = []; $intro_lines = []; $past_header = false; $past_description = false; $first_section_seen = false; foreach ( $lines as $line ) { $line = rtrim( $line ); // ── # Sitetitel (alleen h1, geen h2+) ───────────── if ( preg_match( '/^#(?!#)\s+/', $line ) ) { $past_header = true; continue; } // ── > Blockquote-beschrijving (kan meerregelig zijn) ─ if ( preg_match( '/^>\s?(.*)$/', $line, $m ) ) { $desc_lines[] = $m[1]; // bewaar inhoudsregel (zonder '> ') $past_description = true; continue; } // ── ## Sectie ───────────────────────────────────── if ( preg_match( '/^##\s+(.+)$/', $line, $m ) ) { // Sla vorige sectie op. if ( $current_section !== null ) { $settings['sections'][] = $current_section; } $current_section = [ 'name' => trim( $m[1] ), 'links' => [], ]; $first_section_seen = true; $past_description = true; continue; } // ── - [Label](URL): Omschrijving ────────────────── if ( preg_match( '/^-\s+\[([^\]]*)\]\(([^)]+)\)(?::\s*(.*))?$/', $line, $m ) ) { if ( $current_section !== null ) { $current_section['links'][] = [ 'label' => trim( $m[1] ), 'url' => trim( $m[2] ), 'desc' => isset( $m[3] ) ? trim( $m[3] ) : '', ]; } continue; } // ── Introductietekst (tussen beschrijving en eerste sectie) ── if ( $past_description && ! $first_section_seen && trim( $line ) !== '' ) { $intro_lines[] = trim( $line ); } } // Laatste sectie toevoegen. if ( $current_section !== null ) { $settings['sections'][] = $current_section; } // Beschrijving: bewaar als multi-line blockquote-Markdown // zodat de generator hem ongewijzigd kan uitvoeren. if ( ! empty( $desc_lines ) ) { $non_empty = array_filter( $desc_lines, fn( $l ) => $l !== '' ); if ( ! empty( $non_empty ) ) { $settings['optional_description'] = implode( "\n", array_map( fn( $l ) => '> ' . $l, $non_empty ) ); } } // Introductietekst achter de beschrijving plakken. if ( ! empty( $intro_lines ) ) { $intro = implode( ' ', $intro_lines ); $settings['optional_description'] = trim( $settings['optional_description'] . "\n\n" . $intro ); } // Detecteer of de Optional-sectie al een sitemap-link bevat // → zo ja, auto_sitemap NIET activeren om dubbelen te voorkomen. foreach ( $settings['sections'] as $section ) { if ( strtolower( $section['name'] ) === 'optional' ) { foreach ( $section['links'] as $link ) { if ( false !== strpos( strtolower( $link['url'] ), 'sitemap' ) ) { $settings['auto_sitemap'] = false; break 2; } } } } return $settings; } /** * Geeft een samenvatting van een parse-resultaat terug voor feedback aan de gebruiker. * * @param array $settings Resultaat van parse(). * @return string */ public function summarize( array $settings ) { $section_count = count( $settings['sections'] ); $link_count = 0; foreach ( $settings['sections'] as $s ) { $link_count += count( $s['links'] ); } $has_desc = ! empty( $settings['optional_description'] ); $sections_text = sprintf( /* translators: %d: number of imported sections */ _n( '%d section', '%d sections', $section_count, '5names-llms-txt-manager' ), $section_count ); $links_text = sprintf( /* translators: %d: number of imported links */ _n( '%d link', '%d links', $link_count, '5names-llms-txt-manager' ), $link_count ); $summary = sprintf( /* translators: 1: sections summary, e.g. "3 sections", 2: links summary, e.g. "5 links" */ __( '%1$s, %2$s imported.', '5names-llms-txt-manager' ), $sections_text, $links_text ); if ( $has_desc ) { $summary = sprintf( /* translators: %s: import summary sentence */ __( '%s (including description)', '5names-llms-txt-manager' ), rtrim( $summary, '.' ) ) . '.'; } return $summary; } }