-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-database.py
More file actions
1376 lines (1148 loc) · 57.3 KB
/
update-database.py
File metadata and controls
1376 lines (1148 loc) · 57.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import json
import re
import os
import urllib.parse
from bs4 import BeautifulSoup
from pathlib import Path
import datetime
import subprocess
import shutil
from pathlib import PurePosixPath
from urllib.parse import quote
# =====================================================================
# CONFIGURATION - Modify these settings for your repositories
# =====================================================================
# Format: list of dictionaries with repository information
# Each repo needs:
# - path: Local path where the repo is checked out (relative to GITHUB_WORKSPACE in Actions)
# - url: Base URL where the site is published
# - type: Type of repository (website, blog, docs, other)
REPOSITORIES = [
{
"repo_url": "https://github.com/comphy-lab/comphy-lab.github.io.git", # GitHub repository URL
"path": "comphy-lab.github.io", # Local directory name
"url": "https://comphy-lab.org", # URL where the website is published
"type": "website", # Repository type
# Optional: Custom directory mappings - maps directories to URL paths
"directories": {
"_team": "/team/",
"_research": "/research/",
"_teaching": "/teaching/",
"_join-us": "/join/",
}
},
{
"repo_url": "https://github.com/comphy-lab/CoMPhy-Lab-Blogs.git", # GitHub repository URL
"path": "CoMPhy-Lab-Blogs", # Local directory name
"url": "https://blogs.comphy-lab.org", # URL where the blog is published
"type": "blog", # Repository type
# Optional: Blog-specific settings
"blog_settings": {
"post_dir": "_posts", # Directory containing posts (standard Jekyll structure)
"date_in_url": True, # Whether to include date in URLs (Jekyll style: /YYYY/MM/DD/title/)
"url_prefix": "/blog" # Prefix for blog URLs
}
},
{
"repo_url": "https://github.com/comphy-lab/Viscoelastic3D", # GitHub repository URL
"path": "Viscoelastic3D", # Local directory name
"url": "https://comphy-lab.org/Viscoelastic3D", # URL where the blog is published
"type": "docs", # Repository type
},
{
"repo_url": "https://github.com/comphy-lab/Viscoelastic-Worthington-jets-and-droplets-produced-by-bursting-bubbles", # GitHub repository URL
"path": "Viscoelastic-Worthington-jets-and-droplets-produced-by-bursting-bubbles", # Local directory name
"url": "https://comphy-lab.org/Viscoelastic-Worthington-jets-and-droplets-produced-by-bursting-bubbles", # URL where the blog is published
"type": "docs", # Repository type
},
{
"repo_url": "https://github.com/comphy-lab/BurstingBubble_Herschel-Bulkley", # GitHub repository URL
"path": "BurstingBubble_Herschel-Bulkley", # Local directory name
"url": "https://comphy-lab.org/BurstingBubble_Herschel-Bulkley", # URL where the blog is published
"type": "docs", # Repository type
},
{
"repo_url": "https://github.com/comphy-lab/soapy", # GitHub repository URL
"path": "soapy", # Local directory name
"url": "https://comphy-lab.org/soapy", # URL where the blog is published
"type": "docs", # Repository type
},
{
"repo_url": "https://github.com/comphy-lab/HoleySheet", # GitHub repository URL
"path": "HoleySheet", # Local directory name
"url": "https://comphy-lab.org/HoleySheet", # URL where the blog is published
"type": "docs", # Repository type
},
{
"repo_url": "https://github.com/comphy-lab/MultiRheoFlow",
"path": "MultiRheoFlow",
"url": "https://comphy-lab.org/MultiRheoFlow",
"type": "docs"
},
{
"repo_url": "https://github.com/comphy-lab/fiber",
"path": "fiber",
"url": "https://comphy-lab.org/fiber",
"type": "docs"
},
{
"repo_url": "https://github.com/comphy-lab/JumpingBubbles",
"path": "JumpingBubbles",
"url": "https://comphy-lab.org/JumpingBubbles",
"type": "docs"
},
{
"repo_url": "https://github.com/comphy-lab/Drop-Impact",
"path": "Drop-Impact",
"url": "https://comphy-lab.org/Drop-Impact",
"type": "docs"
},
{
"repo_url": "https://github.com/comphy-lab/Asymmetries-in-coalescence",
"path": "Asymmetries-in-coalescence",
"url": "https://comphy-lab.org/Asymmetries-in-coalescence",
"type": "docs"
},
# Add more repositories as needed
# Example for documentation site:
# {
# "repo_url": "https://github.com/comphy-lab/docs-repo.git",
# "path": "docs-repo",
# "url": "https://docs.comphy-lab.github.io",
# "type": "docs"
# },
]
# Directory to store output files (in the current repo)
OUTPUT_PATH = "search_db.json"
# =====================================================================
# HELPER FUNCTIONS
# =====================================================================
# Helper function to generate proper anchor links
def generate_anchor(text):
"""
Generate a proper anchor ID that matches Jekyll's auto-generated IDs.
Args:
text: The heading text to convert to an anchor
Returns:
A string containing the anchor ID
"""
# Remove date prefix if present (e.g., "2025-01-21 ")
text = re.sub(r'^\d{4}-\d{2}-\d{2}\s+', '', text)
# Remove markdown link syntax if present [[text]]
text = re.sub(r'\[\[(.*?)\]\]', r'\1', text)
# Remove any other markdown formatting
text = re.sub(r'[*_`]', '', text)
# Keep alphanumeric characters, spaces, and hyphens
text = re.sub(r'[^\w\s\-]', '', text)
# Convert to lowercase
text = text.lower()
# Replace spaces with hyphens
text = re.sub(r'\s+', '-', text)
return text
# Parse markdown frontmatter to extract metadata
def parse_frontmatter(content):
front_matter = {}
content_text = content
if content.startswith("---\n"):
parts = content.split("---\n", 2)
if len(parts) >= 3:
yaml_text = parts[1]
content_text = parts[2]
for line in yaml_text.splitlines():
if ":" in line:
key, value = [x.strip() for x in line.split(":", 1)]
front_matter[key] = value
return front_matter, content_text
# Get the base directory for a repository
def get_repo_dir(repo_config):
workspace = os.getenv('GITHUB_WORKSPACE', '.')
return Path(workspace) / repo_config["path"]
# Clone or update a repository
def clone_or_update_repo(repo_config):
repo_dir = get_repo_dir(repo_config)
# If directory exists, update it
if repo_dir.exists():
print(f"Updating existing repository at {repo_dir}")
try:
subprocess.run(["git", "pull"], cwd=repo_dir, check=True)
return True
except subprocess.CalledProcessError as e:
print(f"Error updating repository: {e}")
return False
# If directory doesn't exist, clone it
print(f"Cloning repository to {repo_dir}")
try:
subprocess.run(["git", "clone", repo_config["repo_url"], str(repo_dir)], check=True)
return True
except subprocess.CalledProcessError as e:
print(f"Error cloning repository: {e}")
return False
# Clean up a repository
def cleanup_repo(repo_config):
repo_dir = get_repo_dir(repo_config)
if repo_dir.exists():
print(f"Cleaning up repository at {repo_dir}")
try:
shutil.rmtree(repo_dir)
return True
except Exception as e:
print(f"Error cleaning up repository: {e}")
return False
return True
# Get URL for a file within a repository
def get_file_url(repo_config, file_path, permalink=None):
"""
Generates the public URL for a file based on repository configuration and file path.
Handles different repository types ("blog", "website", "docs") with custom URL logic:
- For blogs, supports date-based URLs and permalinks.
- For websites, maps special directories and handles root files and redirects.
- For documentation, preserves folder structure and appends `.html` as needed.
- Falls back to a path-based URL for other types.
Args:
repo_config: Dictionary containing repository configuration, including type and base URL.
file_path: Path object representing the file's location within the repository.
permalink: Optional permalink string from frontmatter to override default URL generation.
Returns:
The full public URL as a string for the given file.
"""
base_url = repo_config["url"].rstrip('/')
repo_dir = get_repo_dir(repo_config)
rel_path = file_path.relative_to(repo_dir)
# If permalink is provided, use it
if permalink:
permalink = permalink.lstrip('/')
return f"{base_url}/{permalink}"
# Handle based on repository type
if repo_config["type"] == "blog":
# For Jekyll-style blogs with dates in filenames
settings = repo_config.get("blog_settings", {})
if settings.get("date_in_url", True) and re.match(r'^\d{4}-\d{2}-\d{2}-', file_path.stem):
match = re.match(r'^(\d{4})-(\d{2})-(\d{2})-(.*)', file_path.stem)
if match:
year, month, day, slug = match.groups()
url_prefix = settings.get("url_prefix", "")
return f"{base_url}{url_prefix}/{year}/{month}/{day}/{slug}/"
# Fall back to simple path-based URL
path_no_ext = str(rel_path.with_suffix(''))
return f"{base_url}/{path_no_ext}/"
elif repo_config["type"] == "website":
# Check if file is in a special directory
dir_mappings = repo_config.get("directories", {})
for dir_name, url_path in dir_mappings.items():
if dir_name in str(rel_path):
# File is in a mapped directory, construct URL accordingly
# Extract the path after the directory
parts = str(rel_path).split(dir_name + os.sep, 1)
if len(parts) > 1:
file_name = Path(parts[1]).stem
# Special handling for index files in special directories
if file_name.lower() == 'index':
return f"{base_url}{url_path}"
else:
return f"{base_url}{url_path}#{file_name.lower()}"
else:
# File is directly in the mapped directory
return f"{base_url}{url_path}"
# Special handling for root HTML files
if file_path.suffix.lower() == '.html' and len(rel_path.parts) == 1:
# For root HTML files like index.html, about.html, news.html
file_name = file_path.stem
if file_name.lower() == 'index':
return base_url
else:
# Check if this is a redirect file
try:
content = file_path.read_text(encoding='utf-8')
if 'meta http-equiv="refresh"' in content:
# Extract the redirect URL
match = re.search(r'url=([^"\'>\s]+)', content)
if match:
redirect = match.group(1)
if redirect.startswith('/#'):
# It's a section in the index page
return f"{base_url}{redirect[1:]}" # Remove the leading /
elif redirect.startswith('/'):
return f"{base_url}{redirect}"
else:
return f"{base_url}/{redirect}"
except Exception as e:
print(f"Warning: Could not parse redirect meta for {file_path}: {e}")
pass
# Default to root URL with section
return f"{base_url}#{file_name.lower()}"
# Regular file in the root of the website
if len(rel_path.parts) == 1:
# Root level markdown file, like index.md
file_name = rel_path.stem
if file_name.lower() == "index":
return base_url
else:
# For about.md, news.md, etc. - they should be at the root URL with section
return f"{base_url}#{file_name.lower()}"
elif repo_config["type"] == "docs":
# For documentation files, we want to preserve the full folder structure
# and generate URLs in the format base_url/FOLDER_NAME/filename.ext.html
# Convert to POSIX path for consistent handling
path_str = rel_path.as_posix()
# If the file is in a docs directory, remove that prefix
if path_str.startswith("docs/"):
path_str = path_str[len("docs/"):]
# Use PurePosixPath for reliable path handling
p = PurePosixPath(path_str)
dir_path, file_name = str(p.parent), p.name
# Build, percent-encode and return the final URL
# Only append .html if the file doesn't already end with it
suffix = "" if file_name.endswith('.html') else ".html"
target = f"{dir_path}/{file_name}{suffix}" if dir_path != "." else f"{file_name}{suffix}"
return f"{base_url}/{quote(target)}"
# Default handling for other types
path_no_ext = str(rel_path.with_suffix(''))
return f"{base_url}/{path_no_ext}/"
# =====================================================================
# CONTENT PROCESSING FUNCTIONS
# =====================================================================
def split_content_into_chunks(content, max_length=1000, original_title=None):
"""
Split long content into meaningful chunks while preserving context.
Args:
content: The text content to split
max_length: Maximum length for each chunk (default 1000 characters)
original_title: Original title to help generate contextual chunk titles
Returns:
List of tuples containing (chunk_text, chunk_title)
"""
if len(content) <= max_length:
return [(content, None)]
chunks = []
current_chunk = []
current_length = 0
# Split by paragraphs first
paragraphs = re.split(r'\n\n+', content)
for para in paragraphs:
para = para.strip()
if not para:
continue
# If paragraph is too long, split by sentences
if len(para) > max_length:
sentences = re.split(r'(?<=[.!?])\s+(?=[A-Z])', para)
for sentence in sentences:
if current_length + len(sentence) > max_length and current_chunk:
# Store current chunk
chunk_text = ' '.join(current_chunk).strip()
# Generate a meaningful title for this chunk
chunk_title = generate_chunk_title(chunk_text, original_title)
chunks.append((chunk_text, chunk_title))
current_chunk = []
current_length = 0
current_chunk.append(sentence)
current_length += len(sentence)
else:
if current_length + len(para) > max_length and current_chunk:
# Store current chunk
chunk_text = ' '.join(current_chunk).strip()
# Generate a meaningful title for this chunk
chunk_title = generate_chunk_title(chunk_text, original_title)
chunks.append((chunk_text, chunk_title))
current_chunk = []
current_length = 0
current_chunk.append(para)
current_length += len(para)
# Store any remaining content
if current_chunk:
chunk_text = ' '.join(current_chunk).strip()
# Generate a meaningful title for this chunk
chunk_title = generate_chunk_title(chunk_text, original_title)
chunks.append((chunk_text, chunk_title))
return chunks
def generate_chunk_title(chunk_text, original_title=None):
"""
Generate a meaningful title for a content chunk based on its content.
Args:
chunk_text: The text content of the chunk
original_title: The original title of the full content
Returns:
A string containing a meaningful title for the chunk
"""
# Extract first sentence (up to 100 chars) for context
first_sentence = chunk_text.split('.')[0] if '.' in chunk_text else chunk_text
first_sentence = first_sentence[:100].strip()
# Look for keywords or topics in the chunk
keywords = []
# Check for common section indicators
if "introduction" in chunk_text.lower()[:200]:
keywords.append("Introduction")
elif "conclusion" in chunk_text.lower()[:200]:
keywords.append("Conclusion")
elif "summary" in chunk_text.lower()[:200]:
keywords.append("Summary")
elif "method" in chunk_text.lower()[:200]:
keywords.append("Methods")
elif "result" in chunk_text.lower()[:200]:
keywords.append("Results")
elif "example" in chunk_text.lower()[:200]:
keywords.append("Examples")
elif "definition" in chunk_text.lower()[:200]:
keywords.append("Definitions")
# For code chunks, identify language or pattern
code_indicators = {
"def ": "Python Function",
"function ": "Function Definition",
"class ": "Class Definition",
"#include": "C/C++ Code",
"int main": "C/C++ Main",
"public class": "Java Code",
"import ": "Import Statements",
"npm": "Node.js",
"const ": "JavaScript",
"var ": "JavaScript",
"let ": "JavaScript",
"<html": "HTML",
"<div": "HTML",
"SELECT": "SQL Query",
"FROM": "SQL Query"
}
for indicator, label in code_indicators.items():
if indicator in chunk_text[:200]:
keywords.append(label)
break
# Create title based on collected information
if keywords and original_title:
return f"{original_title}: {' - '.join(keywords)}"
elif keywords:
return ' - '.join(keywords)
elif original_title:
# Extract key topic from the first sentence if possible
words = first_sentence.split()
if len(words) > 3:
key_phrase = ' '.join(words[:3]) + "..."
return f"{original_title}: {key_phrase}"
else:
return f"{original_title}: Context"
else:
return "Content Section"
def process_docs_html_file(repo_config, file_path, search_db):
"""
Processes a documentation HTML file and adds its content and code blocks to the search database.
Parses the HTML file to extract the main content and title, splits the content into searchable chunks, and generates entries for both text and code sections. Handles files with compound extensions (e.g., `.c.html`) and assigns appropriate entry types and priorities.
"""
print(f" - {file_path.relative_to(get_repo_dir(repo_config))}")
try:
content = file_path.read_text(encoding='utf-8')
# Parse HTML content
soup = BeautifulSoup(content, 'html.parser')
# Get title from HTML
title_tag = soup.find('title')
# For files like example.c.html, remove both .html and keep the .c
base_name = file_path.stem # removes .html
if '.' in base_name:
# This is a .c.html, .py.html, etc. file
title = title_tag.text.strip() if title_tag else base_name.replace('-', ' ').capitalize()
else:
# Regular HTML file
title = title_tag.text.strip() if title_tag else base_name.replace('-', ' ').capitalize()
# Generate URL for this file using get_file_url
url = get_file_url(repo_config, file_path)
# Extract main content
main_content = soup.find('main') or soup.find('article') or soup.find('div', class_='content') or soup.find('div', id='content')
if not main_content:
# If no main content container found, use the body
main_content = soup.find('body')
if not main_content:
print(f" Warning: Could not find main content in {file_path}")
return
# Extract text content
text_content = main_content.get_text(separator=' ', strip=True)
# Clean up the text
clean_content = re.sub(r'\s+', ' ', text_content).strip()
if len(clean_content) >= 50:
# Split content into chunks if it's too long
content_chunks = split_content_into_chunks(clean_content, original_title=title)
# Create entries for each chunk
for i, (chunk, chunk_title) in enumerate(content_chunks):
if chunk_title:
entry_title = chunk_title
elif len(content_chunks) > 1:
# If no specific title was generated, create a meaningful one
if i == 0:
entry_title = f"{title} - Overview"
elif i == len(content_chunks) - 1:
entry_title = f"{title} - Additional Details"
else:
entry_title = f"{title} - Continued"
else:
entry_title = title
entry = {
'title': entry_title,
'content': chunk,
'url': url,
'type': 'docs_content',
'priority': get_priority(repo_config, file_path)
}
search_db.append(entry)
# Process code blocks and function documentation
code_blocks = main_content.find_all(['pre', 'code'])
for block in code_blocks:
code_content = block.get_text(strip=True)
if len(code_content) >= 50:
# Try to detect what type of code it is
code_type = "Code Example"
if "def " in code_content[:100]:
code_type = "Function Definition"
elif "class " in code_content[:100]:
code_type = "Class Definition"
elif "#include" in code_content[:100]:
code_type = "C/C++ Code"
elif "public class" in code_content[:100]:
code_type = "Java Code"
# Split code content if it's too long
code_chunks = split_content_into_chunks(code_content, max_length=500, original_title=f"{title} - {code_type}")
for i, (chunk, chunk_title) in enumerate(code_chunks):
entry_title = chunk_title if chunk_title else f"{title} - {code_type}"
# Extract function or class name if possible
if "def " in chunk[:100]:
match = re.search(r'def\s+(\w+)', chunk[:100])
if match:
func_name = match.group(1)
entry_title = f"{title} - Function: {func_name}"
elif "class " in chunk[:100]:
match = re.search(r'class\s+(\w+)', chunk[:100])
if match:
class_name = match.group(1)
entry_title = f"{title} - Class: {class_name}"
entry = {
'title': entry_title,
'content': chunk,
'url': url,
'type': 'docs_code',
'priority': get_priority(repo_config, file_path)
}
search_db.append(entry)
except Exception as e:
print(f"Error processing documentation HTML file {file_path}: {e}")
def process_markdown_file(repo_config, file_path, search_db):
print(f" - {file_path.relative_to(get_repo_dir(repo_config))}")
try:
content = file_path.read_text(encoding='utf-8')
front_matter, content_body = parse_frontmatter(content)
# Get permalink if available in frontmatter
permalink = front_matter.get('permalink')
# Generate URL for this file
url = get_file_url(repo_config, file_path, permalink)
# Get title from frontmatter or filename
page_title = front_matter.get('title')
if not page_title:
page_title = file_path.stem.replace('-', ' ').capitalize()
# For blogs, remove date prefix from title if present
if repo_config["type"] == "blog" and re.match(r'^\d{4}-\d{2}-\d{2}-', page_title):
page_title = re.sub(r'^\d{4}-\d{2}-\d{2}-', '', page_title)
# Get repository type for entry type
repo_type = repo_config["type"]
# Special handling for research index file
if repo_type == "website" and "_research" in str(file_path) and file_path.stem.lower() == "index":
process_research_index(repo_config, url, content_body, search_db)
return # Skip default processing for research index
# Special handling for team index file
is_team_index = False
if repo_type == "website" and "_team" in str(file_path) and file_path.stem.lower() == "index":
is_team_index = True
# Split content by headers to create individual entries
if content_body.strip():
# First, try finding headers with regex
# Use content_body here instead of the full content
sections = re.split(r'^#+\s+', content_body, flags=re.MULTILINE)
# Process content before first header (if any)
if sections and sections[0].strip():
clean_content = re.sub(r'<[^>]+>', ' ', sections[0])
clean_content = re.sub(r'\s+', ' ', clean_content).strip()
if len(clean_content) >= 50:
# Split long content into chunks with meaningful titles
content_chunks = split_content_into_chunks(clean_content, original_title=page_title)
for chunk, chunk_title in content_chunks:
if chunk_title:
entry_title = chunk_title
elif len(content_chunks) > 1:
# If this is likely an introduction
entry_title = f"{page_title} - Introduction"
else:
entry_title = page_title
entry = {
'title': entry_title,
'content': chunk,
'url': url,
'type': f"{repo_type}_content",
'priority': get_priority(repo_config, file_path)
}
search_db.append(entry)
# Process remaining sections with headers
for i, section in enumerate(sections[1:], 1):
if not section.strip():
continue
# Extract header and content
lines = section.splitlines()
if not lines:
continue
header = lines[0].strip()
section_content = '\n'.join(lines[1:]).strip()
if not header or not section_content:
continue
if len(section_content) < 50: # Skip very short sections
continue
# Skip navigation-like sections
if re.match(r'^(navigation|menu|contents|index)$', header, re.IGNORECASE):
continue
# Clean HTML tags for indexing
clean_content = re.sub(r'<[^>]+>', ' ', section_content)
clean_content = re.sub(r'\s+', ' ', clean_content).strip()
# Generate anchor ID for section header
anchor = generate_anchor(header)
# Base section title
section_title = f"{page_title} - {header}"
# Special handling for team members and collaborators
section_url = f"{url}#{anchor}"
if is_team_index:
# Team members should have a higher priority
entry_priority = 1 # Highest priority
else:
entry_priority = get_priority(repo_config, file_path)
# Split long content into chunks with context-aware titles
content_chunks = split_content_into_chunks(clean_content, original_title=section_title)
for chunk, chunk_title in content_chunks:
# Use generated title or create a meaningful fallback
entry_title = chunk_title if chunk_title else section_title
entry = {
'title': entry_title,
'content': chunk,
'url': section_url,
'type': f"{repo_type}_section",
'priority': entry_priority
}
search_db.append(entry)
except Exception as e:
print(f"Error processing file {file_path}: {e}")
def process_research_index(repo_config, base_url, content, search_db):
"""Process the _research/index.md file specifically for papers."""
try:
soup = BeautifulSoup(content, 'html.parser')
h3_tags = soup.find_all('h3', id=True)
print(f" Found {len(h3_tags)} potential paper entries in research index.")
for h3 in h3_tags:
paper_id = h3.get('id')
if not paper_id or not paper_id.isdigit(): # Check if ID is numeric (like '[15]')
if paper_id != "thesis": # Allow specific non-numeric IDs like 'thesis'
continue
# Extract title (full citation) from h3
paper_title = h3.get_text(strip=True)
# Construct URL: base_url should be something like https://comphy-lab.org/research/
paper_url = f"{base_url}#{paper_id}"
# Find the next sibling <tags> element
tags_element = h3.find_next_sibling('tags')
tags = []
if tags_element:
tags = re.findall(r'<span>(.*?)</span>', str(tags_element))
# Determine priority - updated to move Featured papers to Priority 1
priority = 1 if 'Featured' in tags else 2
# Create entry (content is the same as title, no chunking)
entry = {
'title': paper_title,
'content': paper_title,
'url': paper_url,
'type': 'paper',
'tags': tags,
'priority': priority
}
search_db.append(entry)
# print(f" Added paper entry: {paper_title[:50]}... URL: {paper_url}")
except Exception as e:
print(f"Error processing research index: {e}")
# Determine priority based on repo type and file location
def get_priority(repo_config, file_path):
"""
Determines the indexing priority of a repository file.
This function calculates a numerical priority based on the repository configuration
and the file's location relative to the repository root. For website repositories, it
first checks if the file is within any configured directory and returns a priority
reflecting its order. If not, root-level HTML or markdown files receive a low priority
(8) while all other website files are assigned the lowest priority (10). For blog
repositories, files in the '_posts' directory with a date-formatted name are given a
higher priority (2) if they are less than 90 days old; otherwise, they receive a medium
priority (3). Documentation repositories assign a priority of 3 to files with 'api' in
their path and 4 to all others. Non-specified repository types default to a priority
of 4.
Args:
repo_config: A dictionary containing repository settings, including type and optional
directory mappings.
file_path: A Path object representing the file to be prioritized.
Returns:
int: The calculated priority for the file.
"""
repo_type = repo_config["type"]
repo_dir = get_repo_dir(repo_config)
rel_path = file_path.relative_to(repo_dir)
path_str = str(rel_path)
# Website repository special priorities
if repo_type == "website":
# Check if file is in a mapped directory
dir_mappings = repo_config.get("directories", {})
# Create a priority map based on the order of directories in the configuration
priority_map = {}
for i, dir_name in enumerate(dir_mappings.keys()):
# Assign priorities starting from 1 (highest) in the order they appear in the config
priority_map[dir_name] = i + 1
# Check if file is in any of the mapped directories
for dir_name, priority in priority_map.items():
# Skip _research directory here, as it's handled specifically
if dir_name == "_research":
continue
if dir_name in path_str:
return priority
# Root HTML files (like index.html, about.html, news.html) get low priority
if file_path.suffix.lower() == '.html' and len(rel_path.parts) == 1:
return 8 # Low priority for root HTML files
# Root markdown files get low priority
if file_path.suffix.lower() == '.md' and len(rel_path.parts) == 1:
return 8 # Low priority for root markdown files
# Default priority for other website content
return 10 # Lowest priority for other website content
# Blog posts (medium priority)
elif repo_type == "blog":
# Recent blog posts could get higher priority
if "_posts/" in path_str and re.match(r'\d{4}-\d{2}-\d{2}', file_path.stem):
# Extract date from filename
date_match = re.match(r'(\d{4})-(\d{2})-(\d{2})', file_path.stem)
if date_match:
post_date = datetime.datetime(
int(date_match.group(1)),
int(date_match.group(2)),
int(date_match.group(3))
)
# If post is from last 3 months, give it higher priority
if (datetime.datetime.now() - post_date).days < 90:
return 2
return 3
# Documentation (medium-low priority)
elif repo_type == "docs":
# API docs might get higher priority
if "api" in path_str.lower():
return 3
return 4
# Default priority for other content
return 4
# Process website-specific elements
def process_website_specific(repo_config, file_path, front_matter, content, search_db):
repo_dir = get_repo_dir(repo_config)
rel_path = file_path.relative_to(repo_dir)
path_str = str(rel_path)
# Team member processing
if "_team/" in path_str:
# Team members are already processed in the general function
pass
# Research content specific processing - REMOVED, handled in process_markdown_file
# elif "_research/" in path_str:
# pass
# Teaching content specific processing
elif "_teaching/" in path_str:
# Process course details (div sections)
soup = BeautifulSoup(content, 'html.parser')
course_details = soup.find('div', class_='course-details')
if course_details:
detail_items = course_details.find_all('div', class_='course-details__item')
for item in detail_items:
heading = item.find('h4')
detail_content = item.find('p')
if not heading or not detail_content:
continue
# Clean up heading (remove HTML tags)
clean_heading = heading.get_text().strip()
# Get title from frontmatter or filename
title = front_matter.get('title')
if not title:
title = file_path.stem.replace('-', ' ')
title = re.sub(r'^\d{4}-', '', title)
# Get permalink from frontmatter or default
permalink = front_matter.get('permalink', '/teaching/')
# Create entry for course detail
entry = {
'title': f"{title} - {clean_heading}",
'content': detail_content.get_text().strip(),
'url': f"{repo_config['url']}{permalink}",
'type': 'teaching_detail',
'priority': 3 # Updated: Medium priority for teaching content
}
search_db.append(entry)
# Process blog-specific elements
def process_blog_specific(repo_config, file_path, front_matter, content, url, title, search_db):
# Clean up the content first (removing metadata lines)
clean_content = re.sub(r'^(created|status|modified|author|date published):.*$', '', content, flags=re.MULTILINE)
clean_content = re.sub(r'\n+', '\n', clean_content).strip()
# Split content into paragraphs
paragraphs = re.split(r'\n\n+', clean_content)
paragraphs = [p.strip() for p in paragraphs if p.strip()]
# Process paragraphs for blog content
for para in paragraphs:
# Skip code blocks and HTML
if para.startswith('```') or para.startswith('<'):
continue
if re.match(r'^[\s#*\-]+$', para): # Skip lines that are just formatting
continue
# Split long paragraphs into smaller chunks
if len(para) > 300:
# Split by sentences
sentences = re.split(r'(?<=[.!?])\s+(?=[A-Z])', para)
current_chunk = []
current_length = 0
for sentence in sentences:
if current_length + len(sentence) > 300:
# Store current chunk if not empty
if current_chunk:
chunk_text = ' '.join(current_chunk).strip()
if len(chunk_text) >= 50: # Only store substantial chunks
search_db.append({
'title': title,
'content': chunk_text,
'url': url,
'type': 'blog_excerpt',
'priority': get_priority(repo_config, file_path)
})
current_chunk = []
current_length = 0
current_chunk.append(sentence)
current_length += len(sentence)
# Store any remaining content
if current_chunk:
chunk_text = ' '.join(current_chunk).strip()
if len(chunk_text) >= 50:
search_db.append({
'title': title,
'content': chunk_text,
'url': url,
'type': 'blog_excerpt',
'priority': get_priority(repo_config, file_path)
})
else:
# For shorter paragraphs, store as is if substantial
if len(para) >= 50:
search_db.append({
'title': title,
'content': para,
'url': url,
'type': 'blog_excerpt',
'priority': get_priority(repo_config, file_path)
})
# Process documentation-specific elements
def process_docs_specific(repo_config, file_path, front_matter, content, search_db):
# Add specialized processing for documentation sites
# This is a placeholder - customize based on your docs structure
pass
# Process files from a repository
def should_exclude_file(file_path):
"""
Check if a file should be excluded from processing.
Args:
file_path: Path object representing the file