Skip to content
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
88 changes: 88 additions & 0 deletions USFMToolsSharp.Linter/LinterModules/MissingChapters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using USFMToolsSharp.Linter.Models;
using USFMToolsSharp.Models.Markers;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Linq;
using static USFMToolsSharp.Linter.Models.Versisification;

namespace USFMToolsSharp.Linter.LinterModules
{
/// <summary>
/// Identifies Missing Chapters in a Book.
/// <remark> A new book is defined on ID Markers </remark>
/// </summary>
public class MissingChapters : ILinterModule
{
public Versisification ScriptureConfig;
public MissingChapters(Versisification input)
{
ScriptureConfig = input;
}
public List<LinterResult> Lint(USFMDocument input)
{
List<LinterResult> results = new List<LinterResult>();
Dictionary<string, List<int>> chapterCounts = populateBookData();
List<Marker> chapters = new List<Marker>();
List<string> bookIDs = new List<string>();

string currentID ="";


foreach (Marker marker in input.Contents)
{
if(marker is IDMarker)
{
currentID = ((IDMarker)marker).TextIdentifier.Split(' ')[0].ToUpper();
bookIDs.Add(currentID);
}
if(marker is CMarker)
{
chapters.Add(marker);
if (chapterCounts.ContainsKey(currentID))
{
chapterCounts[currentID].Remove(((CMarker)marker).Number);
}
}

}
foreach(KeyValuePair<string, List<int>> entry in chapterCounts)
{
if (bookIDs.Contains(entry.Key))
{
foreach (int missingChapter in entry.Value)
{
results.Add(new LinterResult
{
Position = chapters[chapters.Count - 1].Position,
Level = LinterLevel.Warning,
Message = $"Missing Chapter {missingChapter} in the book of {entry.Key}."
});

}
}

}

return results;
}
/// <summary>
/// Extracts the Book ID (Abbreviation) and Total Number of Chapters into a Dictionary
/// </summary>
/// <returns></returns>
private Dictionary<string,List<int>> populateBookData()
{
Dictionary<string, List<int>> output = new Dictionary<string, List<int>>();
foreach (KeyValuePair<string,Book> book in ScriptureConfig.metadata)
{
output[book.Key] = Enumerable.Range(1, book.Value.TotalChapters).ToList();
}
return output;
}

}
}
35 changes: 35 additions & 0 deletions USFMToolsSharp.Linter/Models/Versification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;

namespace USFMToolsSharp.Linter.Models
{
public class Versisification
{

public Dictionary<string, Book> metadata { get; set; }

public class Chapter
{
public int ChapterNum { get; set; }
public int TotalVerses { get; set; }

}
public class Book
{
public List<Chapter> Chapters { get; set; }
public int BookID { get; set; }
public string BookName { get; set; }
public int TotalChapters { get; set; }

}
public Versisification(string filename)
{
string jsonData = File.ReadAllText(filename);
metadata = JsonConvert.DeserializeObject<Dictionary<string, Book>>(jsonData);

}
}
}
7 changes: 7 additions & 0 deletions USFMToolsSharp.Linter/USFMLinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ namespace USFMToolsSharp.Linter
{
public class USFMLinter
{
public Versisification metadata;
public List<ILinterModule> linters = new List<ILinterModule>() {
new FindUnknownMarkers(),
new VerseMarkerValidation(),
new MissingEndMarkers(),
new UnpairedEndMarkers(),
new MissingTableRows()

};
public USFMLinter(Versisification input)
{
metadata = input;
linters.Add(new MissingChapters(metadata));
}
public List<LinterResult> Lint(USFMDocument input)
{
List<LinterResult> output = new List<LinterResult>();
Expand Down
7 changes: 7 additions & 0 deletions USFMToolsSharp.Linter/USFMToolsSharp.Linter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="USFMToolsSharp" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="MetaData.json">
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metadata is metadata and not MetaData

<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>