MCPcopy Create free account
hub / github.com/NatsUIJM/autoContents / parse_toc_json

Function parse_toc_json

mainprogress/pdf_metadata_extractor.py:175–188  ·  view source on GitHub ↗

安全解析 LLM 输出的 JSON,剥离 Markdown 标记

(text: str)

Source from the content-addressed store, hash-verified

173 clean_text = re.sub(r'^```(?:json)?\s*', '', text.strip(), flags=re.MULTILINE)
174 clean_text = re.sub(r'\s*```$', '', clean_text, flags=re.MULTILINE)
175 data = json.loads(clean_text)
176 return data.get("toc_start"), data.get("toc_end")
177 except Exception as e:
178 error_msg = f"解析目录 JSON 失败:{e}, 原始文本:{text}"
179 logger.error(error_msg)
180 write_log(error_msg)
181 return None, None
182
183def merge_continuous_ranges(page_list: list) -> tuple:
184 """
185 将分散的页码列表合并为最大的连续区间。
186 逻辑:排序后,如果相邻页码差值 <= 1,视为连续。
187 返回最大连续区间的 (start, end)。
188 """
189 if not page_list:
190 return None, None
191

Callers 1

process_windowFunction · 0.85

Calls 1

write_logFunction · 0.70

Tested by

no test coverage detected