Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass a fragment to AddLexReferences and AddCollection #62

Merged
merged 5 commits into from
May 30, 2024
Merged
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
80 changes: 42 additions & 38 deletions Src/xWorks/ConfiguredLcmGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ internal static StringBuilder GenerateLetterHeaderIfNeeded(ICmObject entry,
}

/// <summary>
/// This method uses a ThreadPool to execute the given individualActions in parallel.
/// It waits for all the individualActions to complete and then returns.
/// </summary>
internal static void SpawnEntryGenerationThreadsAndWait(List<Action> individualActions, IThreadedProgress progress)
/// This method uses a ThreadPool to execute the given individualActions in parallel.
/// It waits for all the individualActions to complete and then returns.
/// </summary>
internal static void SpawnEntryGenerationThreadsAndWait(List<Action> individualActions, IThreadedProgress progress)
{
var actionCount = individualActions.Count;
//Note that our COM classes all implement the STA threading model, while the ThreadPool always uses MTA model threads.
Expand Down Expand Up @@ -1366,7 +1366,7 @@ private static IFragment GenerateContentForCollection(object collectionField, Co
{
// To be used for things like shared grammatical info
var sharedCollectionInfo = settings.ContentGenerator.CreateFragment();
var bldr = settings.ContentGenerator.CreateFragment();
var frag = settings.ContentGenerator.CreateFragment();
IEnumerable collection;
if (collectionField is IEnumerable)
{
Expand All @@ -1384,19 +1384,19 @@ private static IFragment GenerateContentForCollection(object collectionField, Co

if (config.DictionaryNodeOptions is DictionaryNodeSenseOptions)
{
bldr.Append(GenerateContentForSenses(config, pubDecorator, settings, collection, info, ref sharedCollectionInfo));
frag.Append(GenerateContentForSenses(config, pubDecorator, settings, collection, info, ref sharedCollectionInfo));
}
else
{
FilterAndSortCollectionIfNeeded(ref collection, pubDecorator, config.SubField ?? config.FieldDescription);
ConfigurableDictionaryNode lexEntryTypeNode;
if (IsVariantEntryType(config, out lexEntryTypeNode))
{
bldr.Append(GenerateContentForEntryRefCollection(config, collection, cmOwner, pubDecorator, settings, lexEntryTypeNode, false));
frag.Append(GenerateContentForEntryRefCollection(config, collection, cmOwner, pubDecorator, settings, lexEntryTypeNode, false));
}
else if (IsComplexEntryType(config, out lexEntryTypeNode))
{
bldr.Append(GenerateContentForEntryRefCollection(config, collection, cmOwner, pubDecorator, settings, lexEntryTypeNode, true));
frag.Append(GenerateContentForEntryRefCollection(config, collection, cmOwner, pubDecorator, settings, lexEntryTypeNode, true));
}
else if (IsPrimaryEntryReference(config, out lexEntryTypeNode))
{
Expand All @@ -1413,13 +1413,13 @@ private static IFragment GenerateContentForCollection(object collectionField, Co
bool first = true;
foreach (var entry in lerCollection.Where(item => !item.ComplexEntryTypesRS.Any() && !item.VariantEntryTypesRS.Any()))
{
bldr.Append(GenerateCollectionItemContent(config, pubDecorator, entry, collectionOwner, settings, first, lexEntryTypeNode));
frag.Append(GenerateCollectionItemContent(config, pubDecorator, entry, collectionOwner, settings, first, lexEntryTypeNode));
first = false;
}
// Display refs of each type
GenerateContentForLexEntryRefsByType(config, lerCollection, collectionOwner, pubDecorator, settings, bldr, lexEntryTypeNode,
GenerateContentForLexEntryRefsByType(config, lerCollection, collectionOwner, pubDecorator, settings, frag, lexEntryTypeNode,
true); // complex
GenerateContentForLexEntryRefsByType(config, lerCollection, collectionOwner, pubDecorator, settings, bldr, lexEntryTypeNode,
GenerateContentForLexEntryRefsByType(config, lerCollection, collectionOwner, pubDecorator, settings, frag, lexEntryTypeNode,
false); // variants
}
else
Expand All @@ -1428,36 +1428,36 @@ private static IFragment GenerateContentForCollection(object collectionField, Co
bool first = true;
foreach (var item in lerCollection)
{
bldr.Append(GenerateCollectionItemContent(config, pubDecorator, item, collectionOwner, settings, first));
frag.Append(GenerateCollectionItemContent(config, pubDecorator, item, collectionOwner, settings, first));
first = false;
}
}
}
else if (config.FieldDescription.StartsWith("Subentries"))
{
GenerateContentForSubentries(config, collection, cmOwner, pubDecorator, settings, bldr);
GenerateContentForSubentries(config, collection, cmOwner, pubDecorator, settings, frag);
}
else if (IsLexReferenceCollection(config))
{
GenerateContentForLexRefCollection(config, collection.Cast<ILexReference>(), cmOwner, pubDecorator, settings, bldr);
GenerateContentForLexRefCollection(config, collection.Cast<ILexReference>(), cmOwner, pubDecorator, settings, frag);
}
else
{
bool first = true;
foreach (var item in collection)
{
bldr.Append(GenerateCollectionItemContent(config, pubDecorator, item, collectionOwner, settings, first));
frag.Append(GenerateCollectionItemContent(config, pubDecorator, item, collectionOwner, settings, first));
first = false;
}
}
}

if (bldr.Length() > 0 || sharedCollectionInfo.Length() > 0)
if (frag.Length() > 0 || sharedCollectionInfo.Length() > 0)
{
var className = settings.StylesGenerator.AddStyles(config).Trim('.');
return config.DictionaryNodeOptions is DictionaryNodeSenseOptions ?
settings.ContentGenerator.WriteProcessedSenses(false, bldr, config, className, sharedCollectionInfo) :
settings.ContentGenerator.WriteProcessedCollection(false, bldr, config, className);
settings.ContentGenerator.WriteProcessedSenses(false, frag, config, className, sharedCollectionInfo) :
settings.ContentGenerator.WriteProcessedCollection(false, frag, config, className);
}
return settings.ContentGenerator.CreateFragment();
}
Expand Down Expand Up @@ -1523,7 +1523,7 @@ private static bool IsPrimaryEntryReference(ConfigurableDictionaryNode config, o
private static IFragment GenerateContentForEntryRefCollection(ConfigurableDictionaryNode config, IEnumerable collection, ICmObject collectionOwner,
DictionaryPublicationDecorator pubDecorator, GeneratorSettings settings, ConfigurableDictionaryNode typeNode, bool isComplex)
{
var bldr = settings.ContentGenerator.CreateFragment();
var frag = settings.ContentGenerator.CreateFragment();

var lerCollection = collection.Cast<ILexEntryRef>().ToList();
// ComplexFormsNotSubentries is a filtered version of VisibleComplexFormBackRefs, so it doesn't have it's own VirtualOrdering.
Expand All @@ -1543,23 +1543,23 @@ private static IFragment GenerateContentForEntryRefCollection(ConfigurableDictio
bool first = true;
foreach (var entry in lerCollection.Where(item => !item.ComplexEntryTypesRS.Any() && !item.VariantEntryTypesRS.Any()))
{
bldr.Append(GenerateCollectionItemContent(config, pubDecorator, entry, collectionOwner, settings, first, typeNode));
frag.Append(GenerateCollectionItemContent(config, pubDecorator, entry, collectionOwner, settings, first, typeNode));
first = false;
}
// Display refs of each type
GenerateContentForLexEntryRefsByType(config, lerCollection, collectionOwner, pubDecorator, settings, bldr, typeNode, isComplex);
GenerateContentForLexEntryRefsByType(config, lerCollection, collectionOwner, pubDecorator, settings, frag, typeNode, isComplex);
}
else
{
Debug.WriteLine("Unable to group " + config.FieldDescription + " by LexRefType; generating sequentially");
bool first = true;
foreach (var item in lerCollection)
{
bldr.Append(GenerateCollectionItemContent(config, pubDecorator, item, collectionOwner, settings, first));
frag.Append(GenerateCollectionItemContent(config, pubDecorator, item, collectionOwner, settings, first));
first = false;
}
}
return bldr;
return frag;
}

private static void GenerateContentForLexEntryRefsByType(ConfigurableDictionaryNode config, List<ILexEntryRef> lerCollection, object collectionOwner, DictionaryPublicationDecorator pubDecorator,
Expand All @@ -1580,36 +1580,40 @@ private static void GenerateContentForLexEntryRefsByType(ConfigurableDictionaryN
// Generate XHTML by Type
foreach (var typeGuid in lexEntryTypesFiltered)
{
var innerBldr = new StringBuilder();
var combinedContent = settings.ContentGenerator.CreateFragment();
bool first = true;
foreach (var lexEntRef in lerCollection)
{
if (isComplex ? lexEntRef.ComplexEntryTypesRS.Any(t => t.Guid == typeGuid) : lexEntRef.VariantEntryTypesRS.Any(t => t.Guid == typeGuid))
{
innerBldr.Append(GenerateCollectionItemContent(config, pubDecorator, lexEntRef, collectionOwner, settings, first, typeNode));
first = false;
var content = GenerateCollectionItemContent(config, pubDecorator, lexEntRef, collectionOwner, settings, first, typeNode);
if (!content.IsNullOrEmpty())
{
combinedContent.Append(content);
first = false;
}
}
}

if (innerBldr.Length > 0)
if (!first)
{
var lexEntryType = lexEntryTypes.First(t => t.Guid.Equals(typeGuid));
// Display the Type iff there were refs of this Type (and we are factoring)
// Display the Type if there were refs of this Type (and we are factoring)
var generateLexType = typeNode != null;
var lexTypeContent = generateLexType
? GenerateCollectionItemContent(typeNode, pubDecorator, lexEntryType,
lexEntryType.Owner, settings, first)
lexEntryType.Owner, settings, false)
: null;
var className = generateLexType ? settings.StylesGenerator.AddStyles(typeNode).Trim('.') : null;
var refsByType = settings.ContentGenerator.AddLexReferences(generateLexType,
lexTypeContent, config, className, innerBldr.ToString(), IsTypeBeforeForm(config));
lexTypeContent, config, className, combinedContent, IsTypeBeforeForm(config));
bldr.Append(refsByType);
}
}
}

private static void GenerateContentForSubentries(ConfigurableDictionaryNode config, IEnumerable collection, ICmObject collectionOwner,
DictionaryPublicationDecorator pubDecorator, GeneratorSettings settings, IFragment bldr)
DictionaryPublicationDecorator pubDecorator, GeneratorSettings settings, IFragment frag)
{
var listOptions = config.DictionaryNodeOptions as DictionaryNodeListOptions;
var typeNode = config.ReferencedOrDirectChildren.FirstOrDefault(n => n.FieldDescription == LookupComplexEntryType);
Expand All @@ -1627,7 +1631,7 @@ private static void GenerateContentForSubentries(ConfigurableDictionaryNode conf
{
if (subentries[i].Item1 == null || !subentries[i].Item1.ComplexEntryTypesRS.Any())
{
bldr.Append(GenerateCollectionItemContent(config, pubDecorator, subentries[i].Item2, collectionOwner, settings, first));
frag.Append(GenerateCollectionItemContent(config, pubDecorator, subentries[i].Item2, collectionOwner, settings, first));
first = false;
subentries.RemoveAt(i--);
}
Expand All @@ -1639,7 +1643,7 @@ private static void GenerateContentForSubentries(ConfigurableDictionaryNode conf
{
if (subentries[i].Item1.ComplexEntryTypesRS.Any(t => t.Guid == typeGuid))
{
bldr.Append(GenerateCollectionItemContent(config, pubDecorator, subentries[i].Item2, collectionOwner, settings, first));
frag.Append(GenerateCollectionItemContent(config, pubDecorator, subentries[i].Item2, collectionOwner, settings, first));
first = false;
subentries.RemoveAt(i--);
}
Expand All @@ -1652,7 +1656,7 @@ private static void GenerateContentForSubentries(ConfigurableDictionaryNode conf
bool first = true;
foreach (var item in collection)
{
bldr.Append(GenerateCollectionItemContent(config, pubDecorator, item, collectionOwner, settings, first));
frag.Append(GenerateCollectionItemContent(config, pubDecorator, item, collectionOwner, settings, first));
first = false;
}
}
Expand Down Expand Up @@ -2100,19 +2104,19 @@ private static IFragment GenerateCrossReferenceChildren(ConfigurableDictionaryNo
switch (child.FieldDescription)
{
case "ConfigTargets":
var contentBldr = new StringBuilder();
var content = settings.ContentGenerator.CreateFragment();
foreach (var referenceListItem in referenceList)
{
var referenceItem = referenceListItem.Item2;
var targetItem = referenceListItem.Item1;
contentBldr.Append(GenerateCollectionItemContent(child, publicationDecorator, targetItem, referenceItem, settings, first));
content.Append(GenerateCollectionItemContent(child, publicationDecorator, targetItem, referenceItem, settings, first));
first = false;
}
if (contentBldr.Length > 0)
if (!content.IsNullOrEmpty())
{
// targets
settings.ContentGenerator.AddCollection(xw, IsBlockProperty(child),
CssGenerator.GetClassAttributeForConfig(child), config, contentBldr.ToString());
CssGenerator.GetClassAttributeForConfig(child), config, content);
}
break;
case "OwnerType":
Expand Down
4 changes: 2 additions & 2 deletions Src/xWorks/ILcmContentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ IFragment GenerateGroupingNode(object field, string className, ConfigurableDicti
void StartEntry(IFragmentWriter writer, ConfigurableDictionaryNode config, string className, Guid entryGuid, int index, RecordClerk clerk);
void AddEntryData(IFragmentWriter writer, List<ConfiguredLcmGenerator.ConfigFragment> pieces);
void EndEntry(IFragmentWriter writer);
void AddCollection(IFragmentWriter writer, bool isBlockProperty, string className, ConfigurableDictionaryNode config, string content);
void AddCollection(IFragmentWriter writer, bool isBlockProperty, string className, ConfigurableDictionaryNode config, IFragment content);
void BeginObjectProperty(IFragmentWriter writer, bool isBlockProperty, string getCollectionItemClassAttribute);
void EndObject(IFragmentWriter writer);
void WriteProcessedContents(IFragmentWriter writer, IFragment contents);
IFragment AddImage(string classAttribute, string srcAttribute, string pictureGuid);
IFragment AddImageCaption(string captionContent);
IFragment GenerateSenseNumber(string formattedSenseNumber, string senseNumberWs, ConfigurableDictionaryNode config);
IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent, ConfigurableDictionaryNode config, string className, string referencesContent, bool typeBefore);
IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent, ConfigurableDictionaryNode config, string className, IFragment referencesContent, bool typeBefore);
void BeginCrossReference(IFragmentWriter writer, bool isBlockProperty, string className);
void EndCrossReference(IFragmentWriter writer);
IFragment WriteProcessedSenses(bool isBlock, IFragment senseContent, ConfigurableDictionaryNode config, string className, IFragment sharedCollectionInfo);
Expand Down
8 changes: 4 additions & 4 deletions Src/xWorks/LcmJsonGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ public void EndEntry(IFragmentWriter xw)
((JsonFragmentWriter)xw).EndObject();
}

public void AddCollection(IFragmentWriter writer, bool isBlockProperty, string className, ConfigurableDictionaryNode config, string content)
public void AddCollection(IFragmentWriter writer, bool isBlockProperty, string className, ConfigurableDictionaryNode config, IFragment content)
{
((JsonFragmentWriter)writer).InsertPropertyName(className);
BeginArray(writer);
WriteProcessedContents(writer, new StringFragment(content));
WriteProcessedContents(writer, content);
EndArray(writer);
}

Expand Down Expand Up @@ -364,7 +364,7 @@ public IFragment GenerateSenseNumber(string formattedSenseNumber, string wsId, C
}

public IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent, ConfigurableDictionaryNode config, string className,
string referencesContent, bool typeBefore)
IFragment referencesContent, bool typeBefore)
{
var bldr = new StringBuilder();
var fragment = new StringFragment(bldr);
Expand All @@ -382,7 +382,7 @@ public IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent
// Write an array with the references.
xw.WritePropertyName("references");
xw.WriteStartArray();
xw.WriteRaw(referencesContent);
xw.WriteRaw(referencesContent.ToString());
xw.WriteEndArray();
// Write properties related to the factored type (if any and if after).
if (generateLexType && !typeBefore)
Expand Down
8 changes: 4 additions & 4 deletions Src/xWorks/LcmWordGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,15 +1323,15 @@ public void EndEntry(IFragmentWriter writer)
{
return;
}
public void AddCollection(IFragmentWriter writer, bool isBlockProperty, string className, ConfigurableDictionaryNode config, string content)
public void AddCollection(IFragmentWriter writer, bool isBlockProperty, string className, ConfigurableDictionaryNode config, IFragment content)
{
var frag = ((WordFragmentWriter)writer).WordFragment;
if (isBlockProperty && (config.StyleType == ConfigurableDictionaryNode.StyleTypes.Paragraph))
frag.AddStyleLink(config.Style, config, ConfigurableDictionaryNode.StyleTypes.Paragraph);
else if (!isBlockProperty)
frag.AddStyleLink(config.Style, config, ConfigurableDictionaryNode.StyleTypes.Character);

if (!string.IsNullOrEmpty(content))
if (!content.IsNullOrEmpty())
{
frag.Append(content);
}
Expand Down Expand Up @@ -1381,7 +1381,7 @@ public IFragment GenerateSenseNumber(string formattedSenseNumber, string senseNu
senseNum.AddStyleLink(WordStylesGenerator.SenseNumberStyleName, senseConfigNode, ConfigurableDictionaryNode.StyleTypes.Character);
return senseNum;
}
public IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent, ConfigurableDictionaryNode config, string className, string referencesContent, bool typeBefore)
public IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent, ConfigurableDictionaryNode config, string className, IFragment referencesContent, bool typeBefore)
{
var fragment = new DocFragment();
// Generate the factored ref types element (if before).
Expand All @@ -1390,7 +1390,7 @@ public IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent
fragment.Append(WriteProcessedObject(false, lexTypeContent, config, className));
}
// Then add all the contents for the LexReferences (e.g. headwords)
fragment.Append(new DocFragment(referencesContent));
fragment.Append(referencesContent);
// Generate the factored ref types element (if after).
if (generateLexType && !typeBefore)
{
Expand Down
8 changes: 4 additions & 4 deletions Src/xWorks/LcmXhtmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -884,12 +884,12 @@ public void EndEntry(IFragmentWriter writer)
}

public void AddCollection(IFragmentWriter writer, bool isBlockProperty,
string className, ConfigurableDictionaryNode config, string content)
string className, ConfigurableDictionaryNode config, IFragment content)
{
var xw = ((XmlFragmentWriter)writer).Writer;
xw.WriteStartElement(isBlockProperty ? "div" : "span");
xw.WriteAttributeString("class", className);
xw.WriteRaw(content);
xw.WriteRaw(content.ToString());
xw.WriteEndElement();
}

Expand Down Expand Up @@ -961,7 +961,7 @@ public IFragment GenerateSenseNumber(string formattedSenseNumber, string senseNu
}

public IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent, ConfigurableDictionaryNode config, string className,
string referencesContent, bool typeBefore)
IFragment referencesContent, bool typeBefore)
{
var bldr = new StringBuilder(100);
var fragment = new StringFragment(bldr);
Expand All @@ -971,7 +971,7 @@ public IFragment AddLexReferences(bool generateLexType, IFragment lexTypeContent
bldr.Append(WriteProcessedObject(false, lexTypeContent, config, className));
}
// Then add all the contents for the LexReferences (e.g. headwords)
bldr.Append(referencesContent);
bldr.Append(referencesContent.ToString());
// Generate the factored ref types element (if after).
if (generateLexType && !typeBefore)
{
Expand Down
Loading