Skip to content

Commit

Permalink
Add functionality to Paratext project settings parser (#163)
Browse files Browse the repository at this point in the history
- add ParatextProjectSettings.GetBookFileName()
- add ZipParatextProjectSettingsParserBase class
  • Loading branch information
ddaspit authored Feb 1, 2024
1 parent 2063815 commit 19f5789
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 35 deletions.
32 changes: 31 additions & 1 deletion src/SIL.Machine/Corpora/ParatextProjectSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Globalization;
using System.Text;
using SIL.Scripture;

namespace SIL.Machine.Corpora
Expand Down Expand Up @@ -43,5 +44,34 @@ string biblicalTermsFileName
public string BiblicalTermsListType { get; }
public string BiblicalTermsProjectName { get; }
public string BiblicalTermsFileName { get; }

public string GetBookFileName(string bookId)
{
string bookPart;
if (FileNameForm == "MAT")
bookPart = bookId;
else if (FileNameForm == "40" || FileNameForm == "41")
bookPart = GetBookFileNameDigits(bookId);
else
bookPart = GetBookFileNameDigits(bookId) + bookId;
return FileNamePrefix + bookPart + FileNameSuffix;
}

private static string GetBookFileNameDigits(string bookId)
{
int bookNum = Canon.BookIdToNumber(bookId);

if (bookNum < 10)
return "0" + bookNum;
if (bookNum < 40)
return bookNum.ToString(CultureInfo.InvariantCulture);
if (bookNum < 100)
return (bookNum + 1).ToString(CultureInfo.InvariantCulture);
if (bookNum < 110)
return "A" + (bookNum - 100);
if (bookNum < 120)
return "B" + (bookNum - 110);
return "C" + (bookNum - 120);
}
}
}
35 changes: 1 addition & 34 deletions src/SIL.Machine/Corpora/ZipParatextProjectSettingsParser.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using SIL.IO;

namespace SIL.Machine.Corpora
{
public class ZipParatextProjectSettingsParser : ParatextProjectSettingsParserBase
public class ZipParatextProjectSettingsParser : ZipParatextProjectSettingsParserBase
{
private readonly ZipArchive _archive;

Expand All @@ -14,38 +13,6 @@ public ZipParatextProjectSettingsParser(ZipArchive archive)
_archive = archive;
}

protected override UsfmStylesheet CreateStylesheet(string fileName)
{
TempFile stylesheetTempFile = null;
TempFile customStylesheetTempFile = null;
try
{
string stylesheetPath = fileName;
ZipArchiveEntry stylesheetEntry = _archive.GetEntry(fileName);
if (stylesheetEntry != null)
{
stylesheetTempFile = TempFile.CreateAndGetPathButDontMakeTheFile();
stylesheetEntry.ExtractToFile(stylesheetTempFile.Path);
stylesheetPath = stylesheetTempFile.Path;
}

string customStylesheetPath = null;
ZipArchiveEntry customStylesheetEntry = _archive.GetEntry("custom.sty");
if (customStylesheetEntry != null)
{
customStylesheetTempFile = TempFile.CreateAndGetPathButDontMakeTheFile();
customStylesheetEntry.ExtractToFile(customStylesheetTempFile.Path);
customStylesheetPath = customStylesheetTempFile.Path;
}
return new UsfmStylesheet(stylesheetPath, customStylesheetPath);
}
finally
{
stylesheetTempFile?.Dispose();
customStylesheetTempFile?.Dispose();
}
}

protected override bool Exists(string fileName)
{
return _archive.GetEntry(fileName) != null;
Expand Down
46 changes: 46 additions & 0 deletions src/SIL.Machine/Corpora/ZipParatextProjectSettingsParserBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.IO;
using SIL.IO;

namespace SIL.Machine.Corpora
{
public abstract class ZipParatextProjectSettingsParserBase : ParatextProjectSettingsParserBase
{
protected override UsfmStylesheet CreateStylesheet(string fileName)
{
TempFile stylesheetTempFile = null;
TempFile customStylesheetTempFile = null;
try
{
string stylesheetPath = fileName;
if (Exists(fileName))
{
stylesheetTempFile = TempFile.CreateAndGetPathButDontMakeTheFile();
using (Stream source = Open(fileName))
using (Stream target = File.OpenWrite(stylesheetTempFile.Path))
{
source.CopyTo(target);
}
stylesheetPath = stylesheetTempFile.Path;
}

string customStylesheetPath = null;
if (Exists("custom.sty"))
{
customStylesheetTempFile = TempFile.CreateAndGetPathButDontMakeTheFile();
using (Stream source = Open("custom.sty"))
using (Stream target = File.OpenWrite(customStylesheetTempFile.Path))
{
source.CopyTo(target);
}
customStylesheetPath = customStylesheetTempFile.Path;
}
return new UsfmStylesheet(stylesheetPath, customStylesheetPath);
}
finally
{
stylesheetTempFile?.Dispose();
customStylesheetTempFile?.Dispose();
}
}
}
}
83 changes: 83 additions & 0 deletions tests/SIL.Machine.Tests/Corpora/ParatextProjectSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Text;
using NUnit.Framework;
using SIL.Scripture;

namespace SIL.Machine.Corpora
{
[TestFixture]
public class ParatextProjectSettingsTests
{
[Test]
public void GetBookFileName_BookNum()
{
ParatextProjectSettings settings = CreateSettings("41");
Assert.That(settings.GetBookFileName("MRK"), Is.EqualTo("PROJ42.SFM"));
}

[Test]
public void GetBookFileName_BookNumBookId()
{
ParatextProjectSettings settings = CreateSettings("41MAT");
Assert.That(settings.GetBookFileName("MRK"), Is.EqualTo("PROJ42MRK.SFM"));
}

[Test]
public void GetBookFileName_BookId()
{
ParatextProjectSettings settings = CreateSettings("MAT");
Assert.That(settings.GetBookFileName("MRK"), Is.EqualTo("PROJMRK.SFM"));
}

[Test]
public void GetBookFileName_BookNumDoubleDigit()
{
ParatextProjectSettings settings = CreateSettings("41");
Assert.That(settings.GetBookFileName("GEN"), Is.EqualTo("PROJ01.SFM"));
}

[Test]
public void GetBookFileName_BookNumXXG()
{
ParatextProjectSettings settings = CreateSettings("41");
Assert.That(settings.GetBookFileName("XXG"), Is.EqualTo("PROJ100.SFM"));
}

[Test]
public void GetBookFileName_BookNumPrefixA()
{
ParatextProjectSettings settings = CreateSettings("41");
Assert.That(settings.GetBookFileName("FRT"), Is.EqualTo("PROJA0.SFM"));
}

[Test]
public void GetBookFileName_BookNumPrefixB()
{
ParatextProjectSettings settings = CreateSettings("41");
Assert.That(settings.GetBookFileName("TDX"), Is.EqualTo("PROJB0.SFM"));
}

[Test]
public void GetBookFileName_BookNumPrefixC()
{
ParatextProjectSettings settings = CreateSettings("41");
Assert.That(settings.GetBookFileName("3MQ"), Is.EqualTo("PROJC0.SFM"));
}

private static ParatextProjectSettings CreateSettings(string fileNameForm)
{
return new ParatextProjectSettings(
"Name",
"Name",
Encoding.UTF8,
ScrVers.English,
new UsfmStylesheet("usfm.sty"),
"PROJ",
fileNameForm,
".SFM",
"Major",
"",
"BiblicalTerms.xml"
);
}
}
}

0 comments on commit 19f5789

Please sign in to comment.