private HtmlNode ProcessRepeatNode(HtmlNode node, HtmlAttribute repeatAttribute, bool isKtag) { BlockNode repeatNode = null; AntlrLibrary.Model.Node result = AntlrLibrary.LexerGenerator.Parse(repeatAttribute.Value.Trim('[', ']')); if (result?.Token?.Value == AntlrLibrary.Model.ACTIONS.Loop) { string[] repeatParts = repeatAttribute.Value.Replace("[[", "").Replace("]]", "").Split(":"); string[] repeatRefParts = repeatParts[0].Split(","); string referenceObject = repeatRefParts[0]; string iterator = repeatRefParts[1]; string startIndex = repeatRefParts[2]; string endIndex = repeatParts[1]; repeatNode = new KRepeatNode(decodeExpression(startIndex), decodeExpression(endIndex), iterator, decodeExpression(referenceObject)); } else if (result?.Token?.Value == AntlrLibrary.Model.ACTIONS.InLoop) { string[] repeatParts = repeatAttribute.Value.Trim('[', ']').Split(" in "); //TODO: Map loopvariable to referenceObject[iterator] in the block scope string loopVariable = repeatParts[0].ToLower(); string iterator = "K_IND_" + currentBlockNumber;//GenerateVariableName(5); string referenceObject = repeatParts[1].ToLower(); string startIndex = "0"; string endIndex = referenceObject + ".length()"; repeatNode = new KRepeatNode(startIndex, endIndex, iterator, referenceObject, loopVariable); } if (repeatNode != null) { var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml("<div></div>"); HtmlNode newNode = htmlDoc.DocumentNode.FirstChild; newNode.AppendChildren(node.ChildNodes); repeatNode.Children.AddRange(GetNodes(newNode, true)); HtmlNode tempNode = node.OwnerDocument.CreateTextNode(GenerateBlockIdentifier()); blockNodeReferenceList.Add(currentBlockNumber, repeatNode); node.RemoveAllChildren(); repeatAttribute.Remove(); if (isKtag & node.ParentNode != null) { node.ParentNode.InsertAfter(tempNode, node); node.Remove(); } else { node.AppendChild(tempNode); } return(node); } return(null); }
private async Task <string> ExecuteRepeatNodeAsync(KRepeatNode node, Dictionary <string, AliasReference> classNameAlias, bool isRepeat) { try { int startIndex = 0; int offset = 0; decimal tmpIndex = 0; if (node.StartIndex.IndexOf("offset") > 0) { if (string.IsNullOrEmpty(page.Offset)) { node.StartIndex = "0"; } else { int.TryParse(viewDetail.currentpagenumber, out int pageNumber); dynamic offsetObj = await evaluator.EvaluateExpressionAsync(page.Offset, classNameAlias); int.TryParse(offsetObj.ToString(), out offset); startIndex = offset * (pageNumber - 1); } } else { dynamic startIndexObj = await evaluator.EvaluateExpressionAsync(node.StartIndex, classNameAlias); //int.TryParse(startIndexObj.ToString(), out startIndex); startIndex = decimal.TryParse(startIndexObj.ToString(), out tmpIndex) ? (int)tmpIndex : 0; } dynamic endIndexObj = await evaluator.EvaluateExpressionAsync(node.EndIndex, classNameAlias); string endIndexString = endIndexObj.ToString(); if (endIndexString == "") { return(""); } int endIndex = decimal.TryParse(endIndexString, out tmpIndex) ? (int)tmpIndex : 0; //int.TryParse(endIndexString, out int endIndex); endIndex += startIndex; dynamic totalObj = await evaluator.EvaluateExpressionAsync(node.Collection + ".length()", classNameAlias); int.TryParse(totalObj.ToString(), out int total); if (endIndex > total) { endIndex = total; } string baseReference = node.Collection.Split('.')[0].Split('[')[0].ToLower(); if (baseClassName == baseReference) { await evaluator.PrefillKIDsAsync(node.Collection.Trim(), startIndex, endIndex); } StringBuilder resultString = new StringBuilder(); for (int i = startIndex; i < endIndex; i++) { Dictionary <string, AliasReference> newClassNameAlias = new Dictionary <string, AliasReference>(); foreach (var alias in classNameAlias) { newClassNameAlias.Add(alias.Key, alias.Value.Clone()); } if (string.IsNullOrEmpty(node.CollectionAlias)) { newClassNameAlias.Add(node.Iterator.Trim(), new AliasReference { iteration = i, maxIteration = endIndex, referenceObject = null }); } else { newClassNameAlias.Add(node.CollectionAlias.Trim(), new AliasReference { iteration = -1, maxIteration = -1, referenceObject = node.Collection.Trim() + "[" + i + "]" }); } resultString.Append(await ProcessNodesAsync(node.Children, newClassNameAlias, isRepeat || (i != startIndex))); } return(resultString.ToString()); } catch (Exception e) { CacheableResult = false; ConsoleLogger.Write($"ERROR: ExecuteRepeatNodeAsync: \t {e.ToString()}, source:" + page.SourcePath); } return(""); }