fix: bg verses now show range of verses
This commit is contained in:
+24
-8
@@ -212,7 +212,7 @@ def extract_verses(soup: BeautifulSoup, max_chapter=1, max_verse=5):
|
||||
current_verse = None
|
||||
state = "IDLE" # IDLE, TRANSLITERATION, SYNONYMS, TRANSLATION, PURPORT
|
||||
current_chapter = 0
|
||||
chapter_title = ""
|
||||
chapter_titles = {}
|
||||
|
||||
for p in paragraphs:
|
||||
cls = get_css_class(p)
|
||||
@@ -224,7 +224,9 @@ def extract_verses(soup: BeautifulSoup, max_chapter=1, max_verse=5):
|
||||
match = re.match(r"^(\d+)\.\s+(.+)$", decoded, re.DOTALL)
|
||||
if match:
|
||||
current_chapter = int(match.group(1))
|
||||
chapter_title = match.group(2).replace("\n", " ").strip()
|
||||
chapter_titles[current_chapter] = (
|
||||
match.group(2).replace("\n", " ").strip()
|
||||
)
|
||||
if current_chapter > max_chapter:
|
||||
break
|
||||
|
||||
@@ -245,11 +247,25 @@ def extract_verses(soup: BeautifulSoup, max_chapter=1, max_verse=5):
|
||||
if ch > max_chapter or (ch == max_chapter and vs > max_verse):
|
||||
break
|
||||
|
||||
raw_ref = decode_scagoudy(text.strip())
|
||||
# For multi-verse entries like "Bg 1.16, Bg 1.17, Bg 1.18, Bg 1.16-18",
|
||||
# use only the last part: "Bg 1.16-18"
|
||||
if "," in raw_ref:
|
||||
clean_ref = raw_ref.rsplit(",", 1)[-1].strip()
|
||||
else:
|
||||
clean_ref = raw_ref
|
||||
|
||||
# Detect verse range (e.g., "Bg 1.16-18")
|
||||
verse_end = None
|
||||
range_match = re.search(r"Bg\s+\d+\.(\d+)-(\d+)", clean_ref)
|
||||
if range_match:
|
||||
verse_end = int(range_match.group(2))
|
||||
|
||||
current_verse = {
|
||||
"book_title": "Bhagavad-gītā As It Is",
|
||||
"chapter": ch,
|
||||
"verse": vs,
|
||||
"reference": decode_scagoudy(text.strip()),
|
||||
"reference": clean_ref,
|
||||
"devanagari": "",
|
||||
"transliteration": "",
|
||||
"_padas": [], # raw pādas, paired into lines later
|
||||
@@ -257,6 +273,8 @@ def extract_verses(soup: BeautifulSoup, max_chapter=1, max_verse=5):
|
||||
"translation": "",
|
||||
"purport": [],
|
||||
}
|
||||
if verse_end is not None:
|
||||
current_verse["verse_end"] = verse_end
|
||||
state = "VERSE_START"
|
||||
|
||||
# TEXT header (e.g., "TEXT 1")
|
||||
@@ -323,7 +341,7 @@ def extract_verses(soup: BeautifulSoup, max_chapter=1, max_verse=5):
|
||||
_finalize_verse(current_verse)
|
||||
verses.append(current_verse)
|
||||
|
||||
return verses, current_chapter, chapter_title
|
||||
return verses, chapter_titles
|
||||
|
||||
|
||||
def write_json(data, filepath):
|
||||
@@ -344,9 +362,7 @@ def main():
|
||||
soup = BeautifulSoup(f, "html.parser")
|
||||
|
||||
print("Extracting verses (all 18 chapters)...")
|
||||
verses, chapter_num, chapter_title = extract_verses(
|
||||
soup, max_chapter=18, max_verse=999
|
||||
)
|
||||
verses, chapter_titles = extract_verses(soup, max_chapter=18, max_verse=999)
|
||||
|
||||
print(f"\nExtracted {len(verses)} verses")
|
||||
|
||||
@@ -365,7 +381,7 @@ def main():
|
||||
"author": "His Divine Grace A.C. Bhaktivedanta Swami Prabhupāda",
|
||||
"totalChapters": 18,
|
||||
"chapters": [
|
||||
{"number": ch, "totalVerses": count}
|
||||
{"number": ch, "title": chapter_titles.get(ch, ""), "totalVerses": count}
|
||||
for ch, count in sorted(chapters_info.items())
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user