Skip to content

Commit

Permalink
Word Export: LT-21891: Add guidewords for reversals (#225)
Browse files Browse the repository at this point in the history
Change-Id: I737e73956a413561a338738050f847316f6e3089
  • Loading branch information
mark-sil authored Dec 5, 2024
1 parent a543f08 commit 314ab9c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
46 changes: 25 additions & 21 deletions Src/xWorks/LcmWordGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void SavePublishedDocx(int[] entryHvos, DictionaryPublicationDecor
{ ContentGenerator = generator, StylesGenerator = generator};
settings.StylesGenerator.AddGlobalStyles(configuration, readOnlyPropertyTable);
string lastHeader = null;
string firstHeadwordStyle = null;
string firstGuidewordStyle = null;
var entryContents = new Tuple<ICmObject, IFragment>[entryCount];
var entryActions = new List<Action>();

Expand Down Expand Up @@ -121,9 +121,9 @@ public static void SavePublishedDocx(int[] entryHvos, DictionaryPublicationDecor
// Append the entry to the word doc
fragment.Append(entry.Item2);

if (string.IsNullOrEmpty(firstHeadwordStyle))
if (string.IsNullOrEmpty(firstGuidewordStyle))
{
firstHeadwordStyle = GetFirstHeadwordStyle((DocFragment)entry.Item2);
firstGuidewordStyle = GetFirstGuidewordStyle((DocFragment)entry.Item2, configuration.Type);
}
}
}
Expand Down Expand Up @@ -181,7 +181,7 @@ public static void SavePublishedDocx(int[] entryHvos, DictionaryPublicationDecor
var headerParts = fragment.mainDocPart.HeaderParts;
if (!headerParts.Any())
{
AddPageHeaderPartsToPackage(fragment.DocFrag, firstHeadwordStyle);
AddPageHeaderPartsToPackage(fragment.DocFrag, firstGuidewordStyle);
}

// Add document settings
Expand Down Expand Up @@ -2008,35 +2008,35 @@ public static NumberingDefinitionsPart AddNumberingPartToPackage(WordprocessingD
}

// Add the page HeaderParts to the document.
public static void AddPageHeaderPartsToPackage(WordprocessingDocument doc, string headwordStyle)
public static void AddPageHeaderPartsToPackage(WordprocessingDocument doc, string guidewordStyle)
{
// Generate header for even pages.
HeaderPart even = doc.MainDocumentPart.AddNewPart<HeaderPart>(WordStylesGenerator.PageHeaderIdEven);
GenerateHeaderPartContent(even, true, headwordStyle);
GenerateHeaderPartContent(even, true, guidewordStyle);

// Generate header for odd pages.
HeaderPart odd = doc.MainDocumentPart.AddNewPart<HeaderPart>(WordStylesGenerator.PageHeaderIdOdd);
GenerateHeaderPartContent(odd, false, headwordStyle);
GenerateHeaderPartContent(odd, false, guidewordStyle);
}

/// <summary>
/// Adds the page number and the first or last headword to the HeaderPart.
/// Adds the page number and the first or last guideword to the HeaderPart.
/// </summary>
/// <param name="part">HeaderPart to modify.</param>
/// <param name="even">True = generate content for even pages.
/// False = generate content for odd pages.</param>
/// <param name="headwordStyle">The style that will be used to find the first or last headword on the page.</param>
private static void GenerateHeaderPartContent(HeaderPart part, bool even, string headwordStyle)
/// <param name="guidewordStyle">The style that will be used to find the first or last guideword on the page.</param>
private static void GenerateHeaderPartContent(HeaderPart part, bool even, string guidewordStyle)
{
ParagraphStyleId paraStyleId = new ParagraphStyleId() { Val = WordStylesGenerator.PageHeaderStyleName };
Paragraph para = new Paragraph(new ParagraphProperties(paraStyleId));

if (even)
{
if (!string.IsNullOrEmpty(headwordStyle))
if (!string.IsNullOrEmpty(guidewordStyle))
{
// Add the first headword on the page to the header.
para.Append(new Run(new SimpleField() { Instruction = "STYLEREF " + headwordStyle + " \\* MERGEFORMAT" }));
// Add the first guideword on the page to the header.
para.Append(new Run(new SimpleField() { Instruction = "STYLEREF \"" + guidewordStyle + "\" \\* MERGEFORMAT" }));
}
para.Append(new WP.Run(new WP.TabChar()));
// Add the page number to the header.
Expand All @@ -2047,10 +2047,10 @@ private static void GenerateHeaderPartContent(HeaderPart part, bool even, string
// Add the page number to the header.
para.Append(new WP.Run(new SimpleField() { Instruction = "PAGE" }));
para.Append(new WP.Run(new WP.TabChar()));
if (!string.IsNullOrEmpty(headwordStyle))
if (!string.IsNullOrEmpty(guidewordStyle))
{
// Add the last headword on the page to the header.
para.Append(new WP.Run(new SimpleField() { Instruction = "STYLEREF " + headwordStyle + " \\l \\* MERGEFORMAT" }));
// Add the last guideword on the page to the header.
para.Append(new WP.Run(new SimpleField() { Instruction = "STYLEREF \"" + guidewordStyle + "\" \\l \\* MERGEFORMAT" }));
}
}

Expand Down Expand Up @@ -2931,16 +2931,20 @@ internal static bool IsWritingSystemRightToLeft(LcmCache cache, int wsId)
}

/// <summary>
/// Get the full style name for the first RunStyle that begins with "Headword".
/// Get the full style name for the first RunStyle that begins with the guideword style.
/// </summary>
/// <returns>The full style name that begins with "Headword".
/// <param name="type">Indicates if we are are exporting a Reversal or regular dictionary.</param>
/// <returns>The full style name that begins with the guideword style.
/// Null if none are found.</returns>
public static string GetFirstHeadwordStyle(DocFragment frag)
public static string GetFirstGuidewordStyle(DocFragment frag, DictionaryConfigurationModel.ConfigType type)
{
// Find the first run style with a value that begins with "Headword".
string guidewordStyle = type == DictionaryConfigurationModel.ConfigType.Reversal ?
WordStylesGenerator.ReversalFormDisplayName : WordStylesGenerator.HeadwordDisplayName;

// Find the first run style with a value that begins with the guideword style.
foreach (RunStyle runStyle in frag.DocBody.Descendants<RunStyle>())
{
if (runStyle.Val.Value.StartsWith(WordStylesGenerator.HeadwordDisplayName))
if (runStyle.Val.Value.StartsWith(guidewordStyle))
{
return runStyle.Val.Value;
}
Expand Down
1 change: 1 addition & 0 deletions Src/xWorks/WordStylesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class WordStylesGenerator
internal const string WritingSystemStyleName = "Writing System Abbreviation";
internal const string WritingSystemDisplayName = "Writing System Abbreviation";
internal const string HeadwordDisplayName = "Headword";
internal const string ReversalFormDisplayName = "Reversal Form";
internal const string StyleSeparator = " : ";
internal const string LangTagPre = "[lang=\'";
internal const string LangTagPost = "\']";
Expand Down
2 changes: 1 addition & 1 deletion Src/xWorks/xWorksTests/LcmWordGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ public void GetFirstHeadwordStyle()
var result = ConfiguredLcmGenerator.GenerateContentForEntry(entry, mainEntryNode, null, DefaultSettings, 0) as DocFragment;

//SUT
string firstHeadwordStyle = LcmWordGenerator.GetFirstHeadwordStyle(result);
string firstHeadwordStyle = LcmWordGenerator.GetFirstGuidewordStyle(result, DictionaryConfigurationModel.ConfigType.Root);

Assert.True(firstHeadwordStyle == "Headword[lang='en']");
}
Expand Down

0 comments on commit 314ab9c

Please sign in to comment.