private static List <TemplateBlock> ToSmsTemplatesBlockModel(TableBody table) { return(table.Rows.Select(x => { var p = x.Elements[5].SafeTrim(); var res = new TemplateBlock { TriggerName = x.Elements[x.Elements.Count - 2], //Название триггера всегда предпоследнее TriggerGroupName = x.Elements.Last(), //Название группы триггеров всегда последнее TemplateBody = x.Elements[3], TemplateBodyTranslation = x.Elements[4], TriggerDescription = x.Elements[1], Type = InteractionType.Sms, MessageType = MessageType.Service }; if (p == "любое") { res.AnyTimeAllowed = true; } else { res.AnyTimeAllowed = false; res.AllowedFromHour = 6; res.AllowedToHour = 23; } return res; }).Select(x => TemplateBlock.Process(x)).ToList()); }
public static CompiledTemplate Compile(Template template, NameResolver nameResolver) { if (template == null) { throw new ArgumentNullException(nameof(template)); } return(template switch { LiteralText text => new CompiledLiteralText(text.Text), FormattedExpression expression => new CompiledFormattedExpression( ExpressionCompiler.Compile(expression.Expression, nameResolver), expression.Format, expression.Alignment), TemplateBlock block => new CompiledTemplateBlock(block.Elements.Select(e => Compile(e, nameResolver)).ToArray()), _ => throw new NotSupportedException() });
private static List <TemplateBlock> ToEmailTemplatesBlockModel(TableBody table) { return(table.Rows.Select(x => new TemplateBlock { TriggerName = x.Elements[x.Elements.Count - 2], //Название триггера всегда предпоследнее TriggerGroupName = x.Elements.Last(), //Название группы триггеров всегда последнее Header = x.Elements[3], HeaderTranslation = x.Elements[4], TemplateBody = x.Elements[5], TemplateBodyTranslation = x.Elements[6], TriggerDescription = x.Elements[1], Type = InteractionType.Email, AnyTimeAllowed = true, //Все Email можно отправлять когда захочется AllowedFromHour = null, AllowedToHour = null, }).Select(x => TemplateBlock.Process(x)).ToList()); }
public void ParseBlock_WithDoubleTransition_DoesNotThrow() { // Arrange var testTemplateWithDoubleTransitionCode = " @<p foo='@@'>Foo #@item</p>"; var testTemplateWithDoubleTransition = new TemplateBlock( new MarkupBlock( Factory.MarkupTransition(), new MarkupTagBlock( Factory.Markup("<p"), new MarkupBlock( new AttributeBlockChunkGenerator("foo", new LocationTagged <string>(" foo='", 46, 0, 46), new LocationTagged <string>("'", 54, 0, 54)), Factory.Markup(" foo='").With(SpanChunkGenerator.Null), new MarkupBlock( Factory.Markup("@").With(new LiteralAttributeChunkGenerator(new LocationTagged <string>(string.Empty, 52, 0, 52), new LocationTagged <string>("@", 52, 0, 52))).Accepts(AcceptedCharacters.None), Factory.Markup("@").With(SpanChunkGenerator.Null).Accepts(AcceptedCharacters.None)), Factory.Markup("'").With(SpanChunkGenerator.Null)), Factory.Markup(">").Accepts(AcceptedCharacters.None)), Factory.Markup("Foo #"), new ExpressionBlock( Factory.CodeTransition(), Factory.Code("item") .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) .Accepts(AcceptedCharacters.NonWhiteSpace) ), new MarkupTagBlock( Factory.Markup("</p>").Accepts(AcceptedCharacters.None)) ) ); var expected = new StatementBlock( Factory.MetaCode("{").Accepts(AcceptedCharacters.None), Factory.Code(" var foo = bar; Html.ExecuteTemplate(foo, ") .AsStatement() .AutoCompleteWith(autoCompleteString: null), testTemplateWithDoubleTransition, Factory.Code("); ").AsStatement(), Factory.MetaCode("}").Accepts(AcceptedCharacters.None)); // Act & Assert ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo," + testTemplateWithDoubleTransitionCode + "); }", expected); }
public void Write(TextWriter textWriter, PageContext pageContext) { if (textWriter == null) throw new ArgumentNullException("textWriter"); if (pageContext == null) pageContext = new PageContext(this, new Dictionary<string, object>(), true); var blocks = pageContext.RenderHtml ? this.HtmlBlocks : this.MarkdownBlocks; if (Interlocked.Increment(ref timesRun) == 1) { lock (readWriteLock) { try { isBusy = true; this.ExecutionContext.BaseType = Markdown.MarkdownBaseType; this.ExecutionContext.TypeProperties = Markdown.MarkdownGlobalHelpers; pageContext.MarkdownPage = this; var initHtmlContext = pageContext.Create(this, true); var initMarkdownContext = pageContext.Create(this, false); foreach (var block in this.HtmlBlocks) { lastBlockProcessed = block; block.DoFirstRun(initHtmlContext); } foreach (var block in this.MarkdownBlocks) { lastBlockProcessed = block; block.DoFirstRun(initMarkdownContext); } this.evaluator = this.ExecutionContext.Build(); foreach (var block in this.HtmlBlocks) { lastBlockProcessed = block; block.AfterFirstRun(evaluator); } foreach (var block in this.MarkdownBlocks) { lastBlockProcessed = block; block.AfterFirstRun(evaluator); } AddDependentPages(blocks); lastBlockProcessed = null; initException = null; hasCompletedFirstRun = true; } catch (Exception ex) { initException = ex; throw; } finally { isBusy = false; } } } lock (readWriteLock) { while (isBusy) Monitor.Wait(readWriteLock); } if (initException != null) { timesRun = 0; throw initException; } MarkdownViewBase instance = null; if (this.evaluator != null) { instance = (MarkdownViewBase)this.evaluator.CreateInstance(); object model; pageContext.ScopeArgs.TryGetValue(ModelName, out model); instance.Init(Markdown.AppHost, this, pageContext.ScopeArgs, model, pageContext.RenderHtml); instance.ViewEngine = Markdown; } foreach (var block in blocks) { block.Write(instance, textWriter, pageContext.ScopeArgs); } if (instance != null) { instance.OnLoad(); } }
public static bool TryParse( string template, [MaybeNullWhen(false)] out Template parsed, [MaybeNullWhen(true)] out string error) { if (template == null) { throw new ArgumentNullException(nameof(template)); } parsed = null; var elements = new List <Template>(); var i = 0; while (i < template.Length) { var ch = template[i]; if (ch == '{') { i++; if (i == template.Length) { error = "Character `{` must be escaped by doubling in literal text."; return(false); } if (template[i] == '{') { elements.Add(new LiteralText("{")); i++; } else { // No line/column tracking var tokens = Tokenizer.GreedyTokenize(new TextSpan(template, new Position(i, 0, 0), template.Length - i)); var expr = ExpressionTokenParsers.TryPartialParse(tokens); if (!expr.HasValue) { // Error message accuracy is not great here error = $"Invalid expression, {expr.FormatErrorMessageFragment()}."; return(false); } if (expr.Remainder.Position == tokens.Count()) { i = tokens.Last().Position.Absolute + tokens.Last().Span.Length; } else { i = tokens.ElementAt(expr.Remainder.Position).Position.Absolute; } if (i == template.Length) { error = "Un-closed hole, `}` expected."; return(false); } Alignment?alignment = null; if (template[i] == ',') { i++; if (i >= template.Length || template[i] == '}') { error = "Incomplete alignment specifier, expected width."; return(false); } AlignmentDirection direction; if (template[i] == '-') { direction = AlignmentDirection.Left; i++; if (i >= template.Length || template[i] == '}') { error = "Incomplete alignment specifier, expected digits."; return(false); } } else { direction = AlignmentDirection.Right; } if (!char.IsDigit(template[i])) { error = "Invalid alignment specifier, expected digits."; return(false); } var width = 0; while (i < template.Length && char.IsDigit(template[i])) { width = 10 * width + CharUnicodeInfo.GetDecimalDigitValue(template[i]); i++; } alignment = new Alignment(direction, width); } string?format = null; if (template[i] == ':') { i++; var formatBuilder = new StringBuilder(); while (i < template.Length && template[i] != '}') { formatBuilder.Append(template[i]); i++; } format = formatBuilder.ToString(); } if (i == template.Length) { error = "Un-closed hole, `}` expected."; return(false); } if (template[i] != '}') { error = $"Invalid expression, unexpected `{template[i]}`."; return(false); } i++; elements.Add(new FormattedExpression(expr.Value, format, alignment)); } } else if (ch == '}') { i++; if (i == template.Length || template[i] != '}') { error = "Character `}` must be escaped by doubling in literal text."; return(false); } elements.Add(new LiteralText("}")); i++; } else { var literal = new StringBuilder(); do { literal.Append(template[i]); i++; } while (i < template.Length && template[i] != '{' && template[i] != '}'); elements.Add(new LiteralText(literal.ToString())); } } if (elements.Count == 1) { parsed = elements.Single(); } else { parsed = new TemplateBlock(elements.ToArray()); } error = null; return(true); }