Skip to content

Commit

Permalink
Add ParatextProjectTextUpdaterBase class
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaspit committed Jul 29, 2024
1 parent e87c1a4 commit d2db1aa
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
25 changes: 25 additions & 0 deletions src/SIL.Machine/Corpora/FileParatextProjectTextUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.IO;

namespace SIL.Machine.Corpora
{
public class FileParatextProjectTextUpdater : ParatextProjectTextUpdaterBase
{
private readonly string _projectDir;

public FileParatextProjectTextUpdater(string projectDir)
: base(new FileParatextProjectSettingsParser(projectDir))
{
_projectDir = projectDir;
}

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

protected override Stream Open(string fileName)
{
return File.OpenRead(Path.Combine(_projectDir, fileName));
}
}
}
48 changes: 48 additions & 0 deletions src/SIL.Machine/Corpora/ParatextProjectTextUpdaterBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.IO;

namespace SIL.Machine.Corpora
{
public abstract class ParatextProjectTextUpdaterBase
{
private readonly ParatextProjectSettingsParserBase _settingsParser;

protected ParatextProjectTextUpdaterBase(ParatextProjectSettingsParserBase settingsParser)
{
_settingsParser = settingsParser;
}

public string UpdateUsfm(
string bookId,
IReadOnlyList<(IReadOnlyList<ScriptureRef>, string)> rows,
string fullName = null,
bool stripAllText = false,
bool preferExistingText = true
)
{
ParatextProjectSettings settings = _settingsParser.Parse();

string fileName = settings.GetBookFileName(bookId);
if (!Exists(fileName))
return null;

string usfm;
using (var reader = new StreamReader(Open(fileName)))
{
usfm = reader.ReadToEnd();
}

var handler = new UpdateUsfmParserHandler(
rows,
fullName is null ? null : $"- {fullName}",
stripAllText,
preferExistingText: preferExistingText
);
UsfmParser.Parse(usfm, handler, settings.Stylesheet, settings.Versification);
return handler.GetUsfm(settings.Stylesheet);
}

protected abstract bool Exists(string fileName);
protected abstract Stream Open(string fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SIL.Machine.Corpora
* This is a USFM parser handler that can be used to replace the existing text in a USFM file with the specified
* text.
*/
public class UsfmTextUpdater : ScriptureRefUsfmParserHandlerBase
public class UpdateUsfmParserHandler : ScriptureRefUsfmParserHandlerBase
{
private readonly IReadOnlyList<(IReadOnlyList<ScriptureRef>, string)> _rows;
private readonly List<UsfmToken> _tokens;
Expand All @@ -20,7 +20,7 @@ public class UsfmTextUpdater : ScriptureRefUsfmParserHandlerBase
private int _rowIndex;
private int _tokenIndex;

public UsfmTextUpdater(
public UpdateUsfmParserHandler(
IReadOnlyList<(IReadOnlyList<ScriptureRef>, string)> rows = null,
string idText = null,
bool stripAllText = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace SIL.Machine.Corpora;

[TestFixture]
public class UsfmTextUpdaterTests
public class UpdateUsfmParserHandlerTests
{
[Test]
public void GetUsfm_Verse_CharStyle()
Expand Down Expand Up @@ -446,7 +446,7 @@ private static string UpdateUsfm(
source = ReadUsfm();
else
source = source.Trim().ReplaceLineEndings("\r\n") + "\r\n";
var updater = new UsfmTextUpdater(
var updater = new UpdateUsfmParserHandler(
rows,
idText,
stripAllText: stripAllText,
Expand Down
4 changes: 2 additions & 2 deletions tests/SIL.Machine.Tests/Corpora/UsfmManualTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ string sfmFileName in Directory.EnumerateFiles(
)
)
{
var updater = new UsfmTextUpdater(pretranslations, stripAllText: true, preferExistingText: false);
var updater = new UpdateUsfmParserHandler(pretranslations, stripAllText: true, preferExistingText: false);
string usfm = await File.ReadAllTextAsync(sfmFileName);
UsfmParser.Parse(usfm, updater, targetSettings.Stylesheet, targetSettings.Versification);
string newUsfm = updater.GetUsfm(targetSettings.Stylesheet);
Expand Down Expand Up @@ -95,7 +95,7 @@ string sfmFileName in Directory.EnumerateFiles(
)
)
{
var updater = new UsfmTextUpdater(pretranslations, stripAllText: true, preferExistingText: true);
var updater = new UpdateUsfmParserHandler(pretranslations, stripAllText: true, preferExistingText: true);
string usfm = await File.ReadAllTextAsync(sfmFileName);
UsfmParser.Parse(usfm, updater, settings.Stylesheet, settings.Versification);
string newUsfm = updater.GetUsfm(settings.Stylesheet);
Expand Down

0 comments on commit d2db1aa

Please sign in to comment.