Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add versification table implementation #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/SIL.Machine/Corpora/FileParatextProjectSettingsParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Linq;
using SIL.Scripture;

namespace SIL.Machine.Corpora
{
Expand All @@ -10,6 +11,7 @@ public class FileParatextProjectSettingsParser : ParatextProjectSettingsParserBa
public FileParatextProjectSettingsParser(string projectDir)
{
_projectDir = projectDir;
Versification.Table.Implementation = new FileVersificationTable(projectDir);
}

protected override UsfmStylesheet CreateStylesheet(string fileName)
Expand Down
26 changes: 26 additions & 0 deletions src/SIL.Machine/Corpora/FileVersificationTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.IO;

namespace SIL.Machine.Corpora
{
public class FileVersificationTable : VersificationTableBase
{
private readonly string _projectDir;

public FileVersificationTable(string projectDir)
{
_projectDir = projectDir;
}

protected override string ProjectName => Path.GetFileName(_projectDir);

protected override bool FileExists(string fileName)
{
return File.Exists(Path.Combine(_projectDir, fileName));
}

protected override Stream OpenFile(string fileName)
{
return File.OpenRead(Path.Combine(_projectDir, fileName));
}
}
}
17 changes: 1 addition & 16 deletions src/SIL.Machine/Corpora/ParatextProjectSettingsParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,7 @@ public ParatextProjectSettings Parse()
{
var guid = (string)settingsDoc.Root.Element("Guid");
string versName = ((ScrVersType)scrVersType).ToString() + "-" + guid;
if (Versification.Table.Implementation.Exists(versName))
{
versification = new ScrVers(versName);
}
else
{
using (var reader = new StreamReader(Open("custom.vrs")))
{
versification = Versification.Table.Implementation.Load(
reader,
"custom.vrs",
versification,
versName
);
}
}
versification = new ScrVers(versName);
}

var stylesheetFileName = (string)settingsDoc.Root.Element("StyleSheet") ?? "usfm.sty";
Expand Down
61 changes: 61 additions & 0 deletions src/SIL.Machine/Corpora/VersificationTableBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.IO;
using SIL.Scripture;

namespace SIL.Machine.Corpora
{
public abstract class VersificationTableBase : Versification.Table
{
protected abstract string ProjectName { get; }

protected override Versification Get(string versName)
{
if (!Exists(versName))
{
LoadVersification(ref versName);
}

return base.Get(versName);
}

public override bool VersificationFileExists(string versName)
{
string[] parts = versName.Split(new[] { '-' }, 2);
if (parts.Length == 1)
return base.VersificationFileExists(versName); // Not a custom versification
return FileExists("custom.vrs");
}

private void LoadVersification(ref string versName)
{
string[] parts = versName.Split(new[] { '-' }, 2);
if (parts.Length > 1)
{
bool isValidVersType = Enum.TryParse(parts[0], out ScrVersType versType);
if (!isValidVersType || versType == ScrVersType.Unknown)
versType = ScrVersType.English;

ScrVers baseVers = new ScrVers(versType);
if (!FileExists("custom.vrs"))
{
versName = parts[0];
}
else
{
using (Stream stream = OpenFile("custom.vrs"))
{
Load(
new StreamReader(stream),
ProjectName != null ? Path.Combine(ProjectName, "custom.vrs") : null,
baseVers,
versName
);
}
}
}
}

protected abstract bool FileExists(string fileName);
protected abstract Stream OpenFile(string fileName);
}
}
2 changes: 2 additions & 0 deletions src/SIL.Machine/Corpora/ZipParatextProjectSettingsParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using SIL.Scripture;

namespace SIL.Machine.Corpora
{
Expand All @@ -11,6 +12,7 @@ public class ZipParatextProjectSettingsParser : ZipParatextProjectSettingsParser
public ZipParatextProjectSettingsParser(ZipArchive archive)
{
_archive = archive;
Versification.Table.Implementation = new ZipVersificationTable(archive);
}

protected override bool Exists(string fileName)
Expand Down
30 changes: 30 additions & 0 deletions src/SIL.Machine/Corpora/ZipVersificationTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.IO;
using System.IO.Compression;

namespace SIL.Machine.Corpora
{
public class ZipVersificationTable : VersificationTableBase
{
private readonly ZipArchive _archive;

public ZipVersificationTable(ZipArchive archive)
{
_archive = archive;
}

protected override string ProjectName => null;

protected override bool FileExists(string fileName)
{
return _archive.GetEntry(fileName) != null;
}

protected override Stream OpenFile(string fileName)
{
ZipArchiveEntry entry = _archive.GetEntry(fileName);
if (entry == null)
return null;
return entry.Open();
}
}
}
Loading