MCPcopy Index your code
hub / github.com/VectifyAI/PageIndex / list_to_tree

Function list_to_tree

pageindex/utils.py:324–370  ·  view source on GitHub ↗
(data)

Source from the content-addressed store, hash-verified

322
323
324def list_to_tree(data):
325 def get_parent_structure(structure):
326 """Helper function to get the parent structure code"""
327 if not structure:
328 return None
329 parts = str(structure).split('.')
330 return '.'.join(parts[:-1]) if len(parts) > 1 else None
331
332 # First pass: Create nodes and track parent-child relationships
333 nodes = {}
334 root_nodes = []
335
336 for item in data:
337 structure = item.get('structure')
338 node = {
339 'title': item.get('title'),
340 'start_index': item.get('start_index'),
341 'end_index': item.get('end_index'),
342 'nodes': []
343 }
344
345 nodes[structure] = node
346
347 # Find parent
348 parent_structure = get_parent_structure(structure)
349
350 if parent_structure:
351 # Add as child to parent if parent exists
352 if parent_structure in nodes:
353 nodes[parent_structure]['nodes'].append(node)
354 else:
355 root_nodes.append(node)
356 else:
357 # No parent, this is a root node
358 root_nodes.append(node)
359
360 # Helper function to clean empty children arrays
361 def clean_node(node):
362 if not node['nodes']:
363 del node['nodes']
364 else:
365 for child in node['nodes']:
366 clean_node(child)
367 return node
368
369 # Clean and return the tree
370 return [clean_node(node) for node in root_nodes]
371
372def add_preface_if_needed(data):
373 if not isinstance(data, list) or not data:

Callers 1

post_processingFunction · 0.85

Calls 2

get_parent_structureFunction · 0.85
clean_nodeFunction · 0.85

Tested by

no test coverage detected