Skip to content

Commit

Permalink
Convert LeftMargin, RightMargin, and FirstLineIndent to floats
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaspit committed Oct 18, 2024
1 parent 31adc51 commit b8fdb04
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
43 changes: 19 additions & 24 deletions src/SIL.Machine/Corpora/UsfmStylesheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ public class UsfmStylesheet
{ "note", UsfmTextProperties.Note }
};

private readonly Dictionary<string, UsfmTag> _markers;
private readonly Dictionary<string, UsfmTag> _tags;

public UsfmStylesheet(string stylesheetFileName, string alternateStylesheetFileName = null)
{
_markers = new Dictionary<string, UsfmTag>();
_tags = new Dictionary<string, UsfmTag>();
Parse(stylesheetFileName);
if (!string.IsNullOrEmpty(alternateStylesheetFileName))
Parse(alternateStylesheetFileName);
}

public UsfmTag GetTag(string marker)
{
if (_markers.TryGetValue(marker, out UsfmTag tag))
if (_tags.TryGetValue(marker, out UsfmTag tag))
return tag;

if (IsCellRange(marker, out string baseMarker, out _) && _markers.TryGetValue(baseMarker, out tag))
if (IsCellRange(marker, out string baseMarker, out _) && _tags.TryGetValue(baseMarker, out tag))
return tag;

tag = CreateTag(marker);
Expand Down Expand Up @@ -143,7 +143,6 @@ private void Parse(string stylesheetFileName)

List<StylesheetEntry> entries = SplitStylesheet(lines);

HashSet<string> foundStyles = new HashSet<string>();
for (int i = 0; i < entries.Count; ++i)
{
StylesheetEntry entry = entries[i];
Expand All @@ -155,30 +154,28 @@ private void Parse(string stylesheetFileName)
if (parts.Length > 1 && parts[1] == "-")
{
// If the entry looks like "\marker xy -" remove the tag and its end tag if any
_markers.Remove(parts[0]);
_markers.Remove(parts[0] + "*");
_tags.Remove(parts[0]);
_tags.Remove(parts[0] + "*");
continue;
}

UsfmTag marker = CreateTag(entry.Text);
UsfmTag endMarker = ParseTagEntry(marker, entries, i + 1);
UsfmTag tag = CreateTag(entry.Text);
UsfmTag endTag = ParseTagEntry(tag, entries, i + 1);

if (endMarker != null && !_markers.ContainsKey(endMarker.Marker))
_markers[endMarker.Marker] = endMarker;

foundStyles.Add(entry.Text);
if (endTag != null && !_tags.ContainsKey(endTag.Marker))
_tags[endTag.Marker] = endTag;
}
}

private UsfmTag CreateTag(string marker)
{
// If tag already exists update with addtl info (normally from custom.sty)
if (!_markers.TryGetValue(marker, out UsfmTag tag))
if (!_tags.TryGetValue(marker, out UsfmTag tag))
{
tag = new UsfmTag(marker);
if (marker != "c" && marker != "v")
tag.TextProperties = UsfmTextProperties.Publishable;
_markers[marker] = tag;
_tags[marker] = tag;
}

return tag;
Expand Down Expand Up @@ -255,19 +252,19 @@ private static UsfmTag ParseTagEntry(UsfmTag tag, List<StylesheetEntry> styleshe
break;

case "leftmargin":
int leftMargin;
if (ParseInteger(entry, out leftMargin))
float leftMargin;
if (ParseFloat(entry, out leftMargin))
tag.LeftMargin = leftMargin;
break;

case "rightmargin":
int rightMargin;
if (ParseInteger(entry, out rightMargin))
float rightMargin;
if (ParseFloat(entry, out rightMargin))
tag.RightMargin = rightMargin;
break;

case "firstlineindent":
int firstLineIndent;
float firstLineIndent;
if (ParseFloat(entry, out firstLineIndent))
tag.FirstLineIndent = firstLineIndent;
break;
Expand Down Expand Up @@ -413,19 +410,17 @@ private static bool ParseInteger(StylesheetEntry entry, out int result)
&& result >= 0;
}

private static bool ParseFloat(StylesheetEntry entry, out int result)
private static bool ParseFloat(StylesheetEntry entry, out float result)
{
float floatResult;
if (
float.TryParse(
entry.Text,
NumberStyles.Float | NumberStyles.AllowThousands,
CultureInfo.InvariantCulture,
out floatResult
out result
)
)
{
result = (int)(floatResult * 1000);
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/SIL.Machine/Corpora/UsfmTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public UsfmTag(string marker)

public string EndMarker { get; set; }

public int FirstLineIndent { get; set; }
public float FirstLineIndent { get; set; }

public string FontName { get; set; }

Expand All @@ -101,7 +101,7 @@ public UsfmTag(string marker)

public UsfmJustification Justification { get; set; }

public int LeftMargin { get; set; }
public float LeftMargin { get; set; }

public int LineSpacing { get; set; }

Expand All @@ -115,7 +115,7 @@ public UsfmTag(string marker)

public int Rank { get; set; }

public int RightMargin { get; set; }
public float RightMargin { get; set; }

public bool SmallCaps { get; set; }

Expand Down

0 comments on commit b8fdb04

Please sign in to comment.