Skip to content

Commit

Permalink
Merge pull request Bridgeconn#5 from amal-J-k/rust
Browse files Browse the repository at this point in the history
fixed some bugs
  • Loading branch information
amal-J-k authored Feb 3, 2025
2 parents 0ad591b + f549677 commit 9199380
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
21 changes: 12 additions & 9 deletions rust-usfm-parser/input.usfm
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
\id GEN some text

\c 1
\p
\v 1 one one
\v 2 one two
\c 2
\p
\v 1 two one
\id hab 45HABGNT92.usfm, Good News Translation, June 2003
\c 3
\s1 A Prayer of Habakkuk
\p
\v 1 This is a prayer of the prophet Habakkuk:
\b
\q1
\v 2 O \nd Lord\nd*, I have heard of what you have done,
\q2 and I am filled with awe.
\q1 Now do again in our times
\q2 the great deeds you used to do.
\q1 Be merciful, even when you are angry.
40 changes: 25 additions & 15 deletions rust-usfm-parser/src/usj_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn node_2_usj(
.utf8_text(usfm.as_bytes())
.expect("Failed to get node text")
.to_string();
//println!("Node Type: {}", node_type);
println!("Node Type: {}", node_type);
match node_type {
"File" => {
node_2_usj_id(&node, content, usfm, parser);
Expand All @@ -94,6 +94,17 @@ fn node_2_usj(
"verseText" => {
//node_2_usj_id(&node, content, usfm, parser);
// println!("Reached here");
let mut node_text = node
.utf8_text(usfm.as_bytes())
.expect("Failed to get node text")
.replace('\n', "")
.to_string();
if(node_text != ""){
// println!("Reached here");
content.push(json!({
"content": node_text,
}));
}
let mut cursor = node.walk();
cursor.goto_first_child(); // Move to the first child
let child_count = node.named_child_count();
Expand All @@ -103,7 +114,9 @@ fn node_2_usj(
let child = cursor.node();
node_2_usj(&child, content, usfm, &parser); // Recursively process child nodes
}

}

"paragraph" | "pi" | "ph" => {
node_2_usj_para(&node, content, usfm, parser);
}
Expand All @@ -126,12 +139,12 @@ fn node_2_usj(
.utf8_text(usfm.as_bytes())
.expect("Failed to get node text")
.to_string();
if(node_text != ""){
content.push(json!({
"type": "text",
"marker": "",
"content": [node_text],
}));
}
}

"table" | "tr" => {
node_2_usj_table(&node, content, usfm, parser);
Expand Down Expand Up @@ -179,7 +192,7 @@ fn node_2_usj_id(
.utf8_text(usfm.as_bytes())
.expect("Failed to get node text")
.to_string();
println!("nodeTYPE={}", node_text);
//println!("nodeTYPE={}", node_text);

let query =
Query::new(&tree_sitter_usfm3::language(), query_source).expect("Failed to create query");
Expand Down Expand Up @@ -327,10 +340,7 @@ fn node_2_usj_ca_va(
let tag_node = node
.named_child(0)
.expect("Expected a child node for style");
let style = node
.utf8_text(usfm.as_bytes())
.expect("Failed to get node text")
.to_string();
let style = node.kind();

// Clean up the style string
let style = if style.starts_with('\\') {
Expand Down Expand Up @@ -432,7 +442,7 @@ fn node_2_usj_verse(
let mut alt_number = None;
let mut publication_number = None;
let mut verse_text = String::new();
let mut verses_with_text = Vec::new();

// Iterate over the captures returned by the query
while let Some(capture) = captures.next() {
// Capture the verse number
Expand Down Expand Up @@ -464,6 +474,8 @@ fn node_2_usj_verse(
}
}
}


// Create the verse JSON object
let ref_sid = format!(
"{}:{}",
Expand All @@ -476,9 +488,7 @@ fn node_2_usj_verse(
"number": verse_number.clone().unwrap_or_default(),
"sid": ref_sid
});

verses_with_text.push(verse_json_obj.clone());
verses_with_text.push(verse_text.trim().into());

// Add alternative and publication numbers if they exist
if let Some(alt) = alt_number {
verse_json_obj["altnumber"] = json!(alt);
Expand All @@ -487,12 +497,12 @@ fn node_2_usj_verse(
verse_json_obj["pubnumber"] = json!(pub_num);
}

for item in verses_with_text {
content.push(item);
}

content.push(verse_json_obj);
}



/*fn node_2_usj(
node: &tree_sitter::Node,
content: &mut Vec<serde_json::Value>,
Expand Down

0 comments on commit 9199380

Please sign in to comment.