@@ -461,35 +461,6 @@ def _backlink_concepts(wiki_dir: Path, doc_name: str, concept_slugs: list[str])
461461 path .write_text (text , encoding = "utf-8" )
462462
463463
464- def _find_section_bounds (lines : list [str ], heading : str ) -> tuple [int , int ] | None :
465- """Return [start, end) line indexes for a section headed by ``heading``."""
466- for i , line in enumerate (lines ):
467- if line != heading :
468- continue
469- start = i + 1
470- end = len (lines )
471- for j in range (start , len (lines )):
472- if lines [j ].startswith ("## " ):
473- end = j
474- break
475- return start , end
476- return None
477-
478-
479- def _find_index_entry_line (lines : list [str ], heading : str , link : str ) -> int | None :
480- """Find an index entry that starts with ``- {link}`` inside one section only."""
481- bounds = _find_section_bounds (lines , heading )
482- if bounds is None :
483- return None
484-
485- start , end = bounds
486- entry_prefix = f"- { link } "
487- for i in range (start , end ):
488- if lines [i ].startswith (entry_prefix ):
489- return i
490- return None
491-
492-
493464def _update_index (
494465 wiki_dir : Path , doc_name : str , concept_names : list [str ],
495466 doc_brief : str = "" , concept_briefs : dict [str , str ] | None = None ,
@@ -513,32 +484,34 @@ def _update_index(
513484 encoding = "utf-8" ,
514485 )
515486
516- lines = index_path .read_text (encoding = "utf-8" ). split ( " \n " )
487+ text = index_path .read_text (encoding = "utf-8" )
517488
518489 doc_link = f"[[summaries/{ doc_name } ]]"
519- if _find_index_entry_line ( lines , "## Documents" , doc_link ) is None :
490+ if doc_link not in text :
520491 doc_entry = f"- { doc_link } ({ doc_type } )"
521492 if doc_brief :
522493 doc_entry += f" — { doc_brief } "
523- doc_bounds = _find_section_bounds (lines , "## Documents" )
524- if doc_bounds is not None :
525- lines .insert (doc_bounds [0 ], doc_entry )
494+ if "## Documents" in text :
495+ text = text .replace ("## Documents\n " , f"## Documents\n { doc_entry } \n " , 1 )
526496
527497 for name in concept_names :
528498 concept_link = f"[[concepts/{ name } ]]"
529499 concept_entry = f"- { concept_link } "
530500 if name in concept_briefs :
531501 concept_entry += f" — { concept_briefs [name ]} "
532- concept_line = _find_index_entry_line (lines , "## Concepts" , concept_link )
533- if concept_line is not None :
502+ if concept_link in text :
534503 if name in concept_briefs :
535- lines [concept_line ] = concept_entry
504+ lines = text .split ("\n " )
505+ for i , line in enumerate (lines ):
506+ if concept_link in line :
507+ lines [i ] = concept_entry
508+ break
509+ text = "\n " .join (lines )
536510 else :
537- concept_bounds = _find_section_bounds (lines , "## Concepts" )
538- if concept_bounds is not None :
539- lines .insert (concept_bounds [0 ], concept_entry )
511+ if "## Concepts" in text :
512+ text = text .replace ("## Concepts\n " , f"## Concepts\n { concept_entry } \n " , 1 )
540513
541- index_path .write_text (" \n " . join ( lines ) , encoding = "utf-8" )
514+ index_path .write_text (text , encoding = "utf-8" )
542515
543516
544517# ---------------------------------------------------------------------------
0 commit comments