Skip to content

Commit

Permalink
Merge pull request #7 from sabdelmalik/BibleTaggingUtil_5.2.1
Browse files Browse the repository at this point in the history
Ensure tag is saved if edited manually
  • Loading branch information
sabdelmalik authored Jan 29, 2024
2 parents e6a2557 + 7008dc1 commit 371b0af
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/BibleTaggingUtil/BibleTaggingForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.VisualBasic.Logging;
using System.Xml.Linq;
using System.Text;
using System.Text.Json;

namespace BibleTaggingUtil
{
Expand Down
2 changes: 1 addition & 1 deletion src/BibleTaggingUtil/BibleTaggingUtil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Copyright>Copyright © 2023 by Sami Abdel Malik</Copyright>
<RepositoryUrl>https://github.com/sabdelmalik/BibleTagging</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>5.2.0.0</Version>
<Version>5.2.1.0</Version>
<Title>Bible Text Tagging with Strong's Numbers</Title>
<BaseOutputPath></BaseOutputPath>
</PropertyGroup>
Expand Down
43 changes: 43 additions & 0 deletions src/BibleTaggingUtil/BibleVersions/BibleVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -424,8 +427,11 @@ private bool LoadOsisBibleFileInternal(string filePath, bool more)
string currentSID = string.Empty;
Verse verse = null;

//Stopwatch sw = new Stopwatch();
//sw.Start();
try
{
container.UpdateProgress("Loading " + bibleName,0);
string osisDoc = string.Empty;
using (StreamReader sr = new StreamReader(filePath))
{
Expand Down Expand Up @@ -580,6 +586,43 @@ private bool LoadOsisBibleFileInternal(string filePath, bool more)
}
}


//var bibleJson = JsonSerializer.Serialize(bible, new JsonSerializerOptions
//{
// Converters = { new JsonBibleConverter() },
// WriteIndented = true,
//});

//var bibleJson = JsonSerializer.Serialize(bible, new JsonSerializerOptions
//{
// WriteIndented = true,
//});

//sw.Stop();
//long leg1 = sw.ElapsedMilliseconds;
//sw.Restart();

//FileStream s = new FileStream(@"C:\temp\Bible.ser",FileMode.Create);
//BinaryFormatter b = new BinaryFormatter();
//b.Serialize(s, bible);
//s.Close();


//JsonBibleConverter conv = new JsonBibleConverter();
//string bibleJson = conv.Write(bible);
//File.WriteAllText(@"C:\temp\Bible.Json", bibleJson);
//sw.Stop();
//long leg2 = sw.ElapsedMilliseconds;
//sw.Restart();

//FileStream s1 = new FileStream(@"C:\temp\Bible.ser", FileMode.Open);
//BinaryFormatter b1 = new BinaryFormatter();
//bible = (Dictionary<string, Verse>)b1.Deserialize(s1);

//conv.Read(@"C:\temp\Bible.Json", bible, bookNames, bookNamesList);
//sw.Stop();
//long leg3 = sw.ElapsedMilliseconds;

return result;
}

Expand Down
183 changes: 183 additions & 0 deletions src/BibleTaggingUtil/BibleVersions/JsonBibleConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using static System.Windows.Forms.Design.AxImporter;

namespace BibleTaggingUtil.BibleVersions
{
internal class JsonBibleConverter
{
public string Write(Dictionary<string, Verse> bible)
{
string result = string.Empty;
try
{
var options = new JsonWriterOptions
{
Indented = true
};

using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream, options);
writer.WriteStartObject();
{
writer.WritePropertyName("Bible");
writer.WriteStartObject();
{
foreach (var (reference, verse) in bible)
{
writer.WritePropertyName(reference);
writer.WriteStartObject();
{
WriteVerse(writer, verse);
}
writer.WriteEndObject();
}
}
writer.WriteEndObject();
}
writer.WriteEndObject();
writer.Flush();

result = Encoding.UTF8.GetString(stream.ToArray());
}
catch (Exception ex)
{
var cm = System.Reflection.MethodBase.GetCurrentMethod();
var name = cm.DeclaringType.FullName + "." + cm.Name;
Tracing.TraceException(name, ex.Message);
}
return result;
}

public void Read(string jsonPath, Dictionary<string, Verse> bible, Dictionary<string, string> bookNames, List<string> bookNamesList)
{
string jsonString = string.Empty;
using (StreamReader sr = new StreamReader(jsonPath))
{
jsonString = sr.ReadToEnd();
}
JsonNode root = JsonNode.Parse(jsonString);
JsonNode bibleNode = root["Bible"];
bible.Clear();
int wordIndex = 0;
var verses = bibleNode.AsObject();
foreach (var verse in verses)
{
string currentReference = verse.Key;
Verse theVerse = new Verse();
wordIndex = 0;
var value = verse.Value["Verse"];
var dirty = value["Dirty"];
var verseWords = value["VerseWords"].AsArray();
foreach (JsonObject verseWord in verseWords)
{
string? reference = (string)verseWord["Reference"];
string? morf = (string)verseWord["Morphology"];
string? word = (string)verseWord["Word"];
#region Strong's Numbers
string? s = (string)verseWord["StrongString"];
string[] strongs= new string[0];
bool isHebrew = Utils.GetTestament(reference)== BibleTestament.OT;
if(s != null)
{
strongs = s.Split(new char[] { ' ' });
for (int i = 0; i<strongs.Length; i++)
{
strongs[i] = strongs[i].Trim().Replace(">","").Replace("<","");
if(strongs[i].Length > 0)
strongs[i] = strongs[i].Substring(1);
}
}
#endregion Strong's Numbers
string? hebrew = (string)verseWord["Hebrew"];
string? greek = (string)verseWord["Greek"];
string? xlit = (string)verseWord["Transliteration"];
VerseWord theWord = new VerseWord(isHebrew ? hebrew : greek, word, strongs, xlit, reference, morf);
theVerse[wordIndex++] = theWord;
}
bible[currentReference] = theVerse;
}
}
private void WriteVerse(Utf8JsonWriter writer, Verse verse)
{
try
{
writer.WritePropertyName("Verse");

writer.WriteStartObject();

writer.WritePropertyName(nameof(verse.Count));
writer.WriteStringValue(verse.Count.ToString());

writer.WritePropertyName(nameof(verse.Dirty));
writer.WriteStringValue(verse.Dirty ? "true" : "false");

writer.WritePropertyName("VerseWords");
writer.WriteStartArray();
for (int i = 0; i < verse.Count; i++)
{
WriteVerseWord(writer, verse[i]);
}
writer.WriteEndArray();

writer.WriteEndObject();
}
catch (Exception ex)
{
var cm = System.Reflection.MethodBase.GetCurrentMethod();
var name = cm.DeclaringType.FullName + "." + cm.Name;
Tracing.TraceException(name, ex.Message);
}

}

private void WriteVerseWord(Utf8JsonWriter writer, VerseWord verseWord)
{
try
{
if (verseWord.Reference == "Gen 20:3")
{
int s = 0;
}
writer.WriteStartObject();

writer.WritePropertyName(nameof(verseWord.Reference));
writer.WriteStringValue(verseWord.Reference);

writer.WritePropertyName(nameof(verseWord.Morphology));
writer.WriteStringValue(verseWord.Morphology);

writer.WritePropertyName(nameof(verseWord.Word));
writer.WriteStringValue(verseWord.Word);

writer.WritePropertyName(nameof(verseWord.StrongString));
writer.WriteStringValue(verseWord.StrongString);

writer.WritePropertyName(nameof(verseWord.Hebrew));
writer.WriteStringValue(verseWord.Hebrew);

writer.WritePropertyName(nameof(verseWord.Greek));
writer.WriteStringValue(verseWord.Greek);

writer.WritePropertyName(nameof(verseWord.Transliteration));
writer.WriteStringValue(verseWord.Transliteration);

writer.WriteEndObject();
}
catch (Exception ex)
{
var cm = System.Reflection.MethodBase.GetCurrentMethod();
var name = cm.DeclaringType.FullName + "." + cm.Name;
Tracing.TraceException(name, ex.Message);
}
}
}
}
42 changes: 2 additions & 40 deletions src/BibleTaggingUtil/BibleVersions/TargetOsisVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,13 @@ private void LoadT(string fileName)
osisDoc = sr.ReadToEnd();
}

container.UpdateProgress("Loading " + bibleName, (100 * bookCount) / 66);

// Build the book's offsets map
//BuildBookOffsets();
BuildOsisSegments();

bookCount = 0;
container.UpdateProgress("Loading " + bibleName, (100 * bookCount) / 66);
// Create an OSIS Bible Map
//foreach (string book in bookOffsets.Keys)
//{
Expand Down Expand Up @@ -211,45 +212,6 @@ private void BookLoaded(string book, Dictionary<string, OsisVerse> localBible)
bookTemp[book] = localBible;
}
}
//private void LoadBook(Object threadContext) //string book)
//{
// string book = (string)threadContext;

// Dictionary<string, OsisVerse> localBible = new Dictionary<string, OsisVerse>();
// try
// {
// Regex regex = new Regex(
// string.Format(@"<verse\s*sID\=""(.*)""\s*osisID\=""{0}\.([0-9]+)\.([0-9]+)""\s*/>", book));

// MatchCollection VerseMatches = regex.Matches(osisDoc, bookOffsets[book]);
// if (VerseMatches.Count > 0)
// {
// foreach (Match VerseMatch in VerseMatches)
// {
// OsisVerse? osisVerse = GetVersesTags(book, VerseMatch);
// if (osisVerse != null)
// {
// //lock (this)
// {
// localBible.Add(osisVerse.VerseRefX, osisVerse);
// }
// // Verse verseWords = osisVerse.GetVerseWords();
// // bible.Add(osisVerse.VerseRefX, verseWords);
// }
// }
// }
// }
// catch (Exception ex)
// {
// var cm = System.Reflection.MethodBase.GetCurrentMethod();
// var name = cm.DeclaringType.FullName + "." + cm.Name;
// Tracing.TraceException(name, ex.Message);
// }
// finally
// {
// BookLoaded(book, localBible);
// }
//}
private void LoadBookFromSegments(Object threadContext) //string book)
{
string book = (string)threadContext;
Expand Down
1 change: 0 additions & 1 deletion src/BibleTaggingUtil/Editor/EditorPanel.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 371b0af

Please sign in to comment.