Skip to content

Commit dcafca2

Browse files
committed
Add versification table implementation
1 parent c304a75 commit dcafca2

File tree

6 files changed

+122
-16
lines changed

6 files changed

+122
-16
lines changed

src/SIL.Machine/Corpora/FileParatextProjectSettingsParser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using System.Linq;
3+
using SIL.Scripture;
34

45
namespace SIL.Machine.Corpora
56
{
@@ -10,6 +11,7 @@ public class FileParatextProjectSettingsParser : ParatextProjectSettingsParserBa
1011
public FileParatextProjectSettingsParser(string projectDir)
1112
{
1213
_projectDir = projectDir;
14+
Versification.Table.Implementation = new FileVersificationTable(projectDir);
1315
}
1416

1517
protected override UsfmStylesheet CreateStylesheet(string fileName)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.IO;
2+
3+
namespace SIL.Machine.Corpora
4+
{
5+
public class FileVersificationTable : VersificationTableBase
6+
{
7+
private readonly string _projectDir;
8+
9+
public FileVersificationTable(string projectDir)
10+
{
11+
_projectDir = projectDir;
12+
}
13+
14+
protected override string ProjectName => Path.GetFileName(_projectDir);
15+
16+
protected override bool FileExists(string fileName)
17+
{
18+
return File.Exists(Path.Combine(_projectDir, fileName));
19+
}
20+
21+
protected override Stream OpenFile(string fileName)
22+
{
23+
return File.OpenRead(Path.Combine(_projectDir, fileName));
24+
}
25+
}
26+
}

src/SIL.Machine/Corpora/ParatextProjectSettingsParserBase.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,7 @@ public ParatextProjectSettings Parse()
4040
{
4141
var guid = (string)settingsDoc.Root.Element("Guid");
4242
string versName = ((ScrVersType)scrVersType).ToString() + "-" + guid;
43-
if (Versification.Table.Implementation.Exists(versName))
44-
{
45-
versification = new ScrVers(versName);
46-
}
47-
else
48-
{
49-
using (var reader = new StreamReader(Open("custom.vrs")))
50-
{
51-
versification = Versification.Table.Implementation.Load(
52-
reader,
53-
"custom.vrs",
54-
versification,
55-
versName
56-
);
57-
}
58-
}
43+
versification = new ScrVers(versName);
5944
}
6045

6146
var stylesheetFileName = (string)settingsDoc.Root.Element("StyleSheet") ?? "usfm.sty";
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.IO;
3+
using SIL.Scripture;
4+
5+
namespace SIL.Machine.Corpora
6+
{
7+
public abstract class VersificationTableBase : Versification.Table
8+
{
9+
protected abstract string ProjectName { get; }
10+
11+
protected override Versification Get(string versName)
12+
{
13+
if (!Exists(versName))
14+
{
15+
LoadVersification(ref versName);
16+
}
17+
18+
return base.Get(versName);
19+
}
20+
21+
public override bool VersificationFileExists(string versName)
22+
{
23+
string[] parts = versName.Split(new[] { '-' }, 2);
24+
if (parts.Length == 1)
25+
return base.VersificationFileExists(versName); // Not a custom versification
26+
return FileExists("custom.vrs");
27+
}
28+
29+
private void LoadVersification(ref string versName)
30+
{
31+
string[] parts = versName.Split(new[] { '-' }, 2);
32+
if (parts.Length > 1)
33+
{
34+
bool isValidVersType = Enum.TryParse(parts[0], out ScrVersType versType);
35+
if (!isValidVersType || versType == ScrVersType.Unknown)
36+
versType = ScrVersType.English;
37+
38+
ScrVers baseVers = new ScrVers(versType);
39+
if (!FileExists("custom.vrs"))
40+
{
41+
versName = parts[0];
42+
}
43+
else
44+
{
45+
using (Stream stream = OpenFile("custom.vrs"))
46+
{
47+
Load(
48+
new StreamReader(stream),
49+
ProjectName != null ? Path.Combine(ProjectName, "custom.vrs") : null,
50+
baseVers,
51+
versName
52+
);
53+
}
54+
}
55+
}
56+
}
57+
58+
protected abstract bool FileExists(string fileName);
59+
protected abstract Stream OpenFile(string fileName);
60+
}
61+
}

src/SIL.Machine/Corpora/ZipParatextProjectSettingsParser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.IO;
22
using System.IO.Compression;
33
using System.Linq;
4+
using SIL.Scripture;
45

56
namespace SIL.Machine.Corpora
67
{
@@ -11,6 +12,7 @@ public class ZipParatextProjectSettingsParser : ZipParatextProjectSettingsParser
1112
public ZipParatextProjectSettingsParser(ZipArchive archive)
1213
{
1314
_archive = archive;
15+
Versification.Table.Implementation = new ZipVersificationTable(archive);
1416
}
1517

1618
protected override bool Exists(string fileName)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.IO;
2+
using System.IO.Compression;
3+
4+
namespace SIL.Machine.Corpora
5+
{
6+
public class ZipVersificationTable : VersificationTableBase
7+
{
8+
private readonly ZipArchive _archive;
9+
10+
public ZipVersificationTable(ZipArchive archive)
11+
{
12+
_archive = archive;
13+
}
14+
15+
protected override string ProjectName => null;
16+
17+
protected override bool FileExists(string fileName)
18+
{
19+
return _archive.GetEntry(fileName) != null;
20+
}
21+
22+
protected override Stream OpenFile(string fileName)
23+
{
24+
ZipArchiveEntry entry = _archive.GetEntry(fileName);
25+
if (entry == null)
26+
return null;
27+
return entry.Open();
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)