From d2db1aa0da1175fc81ff74186690b6a8a870e719 Mon Sep 17 00:00:00 2001 From: Damien Daspit Date: Mon, 29 Jul 2024 15:22:20 -0500 Subject: [PATCH] Add ParatextProjectTextUpdaterBase class --- .../Corpora/FileParatextProjectTextUpdater.cs | 25 ++++++++++ .../Corpora/ParatextProjectTextUpdaterBase.cs | 48 +++++++++++++++++++ ...tUpdater.cs => UpdateUsfmParserHandler.cs} | 4 +- ...sts.cs => UpdateUsfmParserHandlerTests.cs} | 4 +- .../Corpora/UsfmManualTests.cs | 4 +- 5 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 src/SIL.Machine/Corpora/FileParatextProjectTextUpdater.cs create mode 100644 src/SIL.Machine/Corpora/ParatextProjectTextUpdaterBase.cs rename src/SIL.Machine/Corpora/{UsfmTextUpdater.cs => UpdateUsfmParserHandler.cs} (99%) rename tests/SIL.Machine.Tests/Corpora/{UsfmTextUpdaterTests.cs => UpdateUsfmParserHandlerTests.cs} (99%) diff --git a/src/SIL.Machine/Corpora/FileParatextProjectTextUpdater.cs b/src/SIL.Machine/Corpora/FileParatextProjectTextUpdater.cs new file mode 100644 index 000000000..c9c9dd958 --- /dev/null +++ b/src/SIL.Machine/Corpora/FileParatextProjectTextUpdater.cs @@ -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)); + } + } +} diff --git a/src/SIL.Machine/Corpora/ParatextProjectTextUpdaterBase.cs b/src/SIL.Machine/Corpora/ParatextProjectTextUpdaterBase.cs new file mode 100644 index 000000000..7cacc7df1 --- /dev/null +++ b/src/SIL.Machine/Corpora/ParatextProjectTextUpdaterBase.cs @@ -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, 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); + } +} diff --git a/src/SIL.Machine/Corpora/UsfmTextUpdater.cs b/src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs similarity index 99% rename from src/SIL.Machine/Corpora/UsfmTextUpdater.cs rename to src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs index 6d6cf01d8..ce16c03d3 100644 --- a/src/SIL.Machine/Corpora/UsfmTextUpdater.cs +++ b/src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs @@ -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, string)> _rows; private readonly List _tokens; @@ -20,7 +20,7 @@ public class UsfmTextUpdater : ScriptureRefUsfmParserHandlerBase private int _rowIndex; private int _tokenIndex; - public UsfmTextUpdater( + public UpdateUsfmParserHandler( IReadOnlyList<(IReadOnlyList, string)> rows = null, string idText = null, bool stripAllText = false, diff --git a/tests/SIL.Machine.Tests/Corpora/UsfmTextUpdaterTests.cs b/tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs similarity index 99% rename from tests/SIL.Machine.Tests/Corpora/UsfmTextUpdaterTests.cs rename to tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs index 4264fbe3d..ad6ce166c 100644 --- a/tests/SIL.Machine.Tests/Corpora/UsfmTextUpdaterTests.cs +++ b/tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs @@ -3,7 +3,7 @@ namespace SIL.Machine.Corpora; [TestFixture] -public class UsfmTextUpdaterTests +public class UpdateUsfmParserHandlerTests { [Test] public void GetUsfm_Verse_CharStyle() @@ -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, diff --git a/tests/SIL.Machine.Tests/Corpora/UsfmManualTests.cs b/tests/SIL.Machine.Tests/Corpora/UsfmManualTests.cs index f0b636cfb..ceaa93c75 100644 --- a/tests/SIL.Machine.Tests/Corpora/UsfmManualTests.cs +++ b/tests/SIL.Machine.Tests/Corpora/UsfmManualTests.cs @@ -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); @@ -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);