//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { try { Object result = ExpressionHandler.evaluateStringExpression(getValue(), model); if (result == null || !buffer) { return; } String str = result.ToString(); if (escape) { str = Utils.escapeHTML(str); } writer.append(str); if (hasBlock()) { writer.increment(); block.execute(writer, model, template); writer.decrement(); writer.newline(); } } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
public void testEachLoopWithIterableMap() { Dictionary <string, string> users = new Dictionary <string, string>(); users.Add("bob", "Robert Smith"); users.Add("alex", "Alex Supertramp"); Dictionary <String, Object> root = new Dictionary <String, Object>(); root.Add("users", users); JadeModel model = new JadeModel(root); JadeTemplate temp = cfg.getTemplate(getResourcePath("each_loop")); StringWriter outStream = new StringWriter(); try { temp.process(model, outStream); } catch (JadeCompilerException e) { Trace.WriteLine(e); Assert.Fail(); } outStream.Flush(); Assert.AreEqual("<ul><li>Robert Smith</li><li>Alex Supertramp</li></ul>", outStream.ToString()); }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { String name = getValue(); if (name == null) { name = "5"; } String doctypeLine = Doctypes.get(name); if (doctypeLine == null) { doctypeLine = "<!DOCTYPE " + name + ">"; } if (doctypeLine.StartsWith("<?xml")) { template.setMode(Jade4Net.Mode.XML); } else if (doctypeLine.Equals("<!DOCTYPE html>")) { template.setMode(Jade4Net.Mode.HTML); } else { template.setMode(Jade4Net.Mode.XHTML); } writer.append(doctypeLine); }
// throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { MixinNode mixin = model.getMixin(getName()); if (mixin == null) { throw new JadeCompilerException(this, template.getTemplateLoader(), "mixin " + getName() + " is not defined"); } // Clone mixin try { mixin = (MixinNode)mixin.Clone(); } catch (Exception e) { // Can't happen throw; } if (hasBlock()) { List<BlockNode> injectionPoints = getInjectionPoints(mixin.getBlock()); foreach (BlockNode point in injectionPoints) { point.getNodes().AddLast(block); } } model.pushScope(); model.put("block", hasBlock()); writeVariables(model, mixin, template); writeAttributes(model, mixin, template); mixin.getBlock().execute(writer, model, template); model.popScope(); }
public void process(JadeModel model, TextWriter writer) { Compiler.Compiler compiler = new Compiler.Compiler(rootNode); compiler.setPrettyPrint(prettyPrint); compiler.setTemplate(this); compiler.compile(model, writer); }
public static string interpolate(List <Object> prepared, JadeModel model) { StringBuilder result = new StringBuilder(); foreach (Object entry in prepared) { if (entry is String) { result.Append(entry); } else if (entry is ExpressionString) { ExpressionString expression = (ExpressionString)entry; string stringValue = ""; string value = ExpressionHandler.evaluateStringExpression(expression.getValue(), model); if (value != null) { stringValue = value; } if (expression.isEscape()) { stringValue = escapeHTML(stringValue); } result.Append(stringValue); } } return(result.ToString()); }
public JadeModelTest() { var map = new Dictionary<string, object>(); map.Add("hello", "world"); map.Add("foo", "bar"); model = new JadeModel(map); }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { try { Object result = ExpressionHandler.evaluateStringExpression(getValue(), model); if (result == null || !buffer) { return; } String str = result.ToString(); if (escape) { str = Utils.escapeHTML(str); } writer.append(str); if (hasBlock()) { writer.increment(); block.execute(writer, model, template); writer.decrement(); writer.newline(); } } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
private void run(String testName, bool pretty, JadeModel model) { JadeParser parser = null; try { FileTemplateLoader loader = new FileTemplateLoader(TestFileHelper.getCompilerErrorsResourcePath(""), "UTF-8"); parser = new JadeParser(testName, loader); } catch (IOException e) { Trace.WriteLine(e); } Node root = parser.parse(); Jade.Compiler.Compiler compiler = new Jade.Compiler.Compiler(root); compiler.setPrettyPrint(pretty); String expected = readFile(testName + ".html"); model.addFilter("markdown", new MarkdownFilter()); model.addFilter("plain", new PlainFilter()); String html; try { html = compiler.compileToString(model); Assert.AreEqual(expected.Trim(), html.Trim(), testName); Assert.Fail(); } catch (JadeCompilerException e) { Trace.WriteLine(e); } }
public void compile(JadeModel model, TextWriter w) { IndentWriter writer = new IndentWriter(w); writer.setUseIndent(prettyPrint); rootNode.execute(writer, model, template); }
public String compileToString(JadeModel model) { StringWriter writer = new StringWriter(); compile(model, writer); return(writer.ToString()); }
private void writeVariables(JadeModel model, MixinNode mixin, JadeTemplate template) { List <String> names = mixin.getArguments(); List <String> values = arguments; if (names == null) { return; } for (int i = 0; i < names.Count; i++) { String key = names[i]; Object value = null; if (i < values.Count) { value = values[i]; } if (value != null) { try { value = ExpressionHandler.evaluateExpression(values[i], model); } catch (Exception e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } } if (key != null) { model.put(key, value); } } }
private void executeElseNode(JadeModel model, IndentWriter writer, JadeTemplate template) { if (elseNode != null) { elseNode.execute(writer, model, template); } }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { String name = getValue(); if (name == null) { name = "5"; } String doctypeLine = Doctypes.get(name); if (doctypeLine == null) { doctypeLine = "<!DOCTYPE " + name + ">"; } if (doctypeLine.StartsWith("<?xml")) { template.setMode(Jade4Net.Mode.XML); } else if (doctypeLine.Equals("<!DOCTYPE html>")) { template.setMode(Jade4Net.Mode.HTML); } else { template.setMode(Jade4Net.Mode.XHTML); } writer.append(doctypeLine); }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { foreach (Node node in getNodes()) { node.execute(writer, model, template); } }
public static string interpolate(List<Object> prepared, JadeModel model) { StringBuilder result = new StringBuilder(); foreach (Object entry in prepared) { if (entry is String) { result.Append(entry); } else if (entry is ExpressionString) { ExpressionString expression = (ExpressionString) entry; string stringValue = ""; string value = ExpressionHandler.evaluateStringExpression(expression.getValue(), model); if (value != null) { stringValue = value; } if (expression.isEscape()) { stringValue = escapeHTML(stringValue); } result.Append(stringValue); } } return result.ToString(); }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { foreach (Node node in getNodes()) { node.execute(writer, model, template); } }
public JadeModelTest() { var map = new Dictionary <string, object>(); map.Add("hello", "world"); map.Add("foo", "bar"); model = new JadeModel(map); }
private static String templateToString(JadeTemplate template, Dictionary <String, Object> model) // throws JadeCompilerException { JadeModel jadeModel = new JadeModel(model); StringWriter writer = new StringWriter(); template.process(jadeModel, writer); return(writer.ToString()); }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template)// throws JadeCompilerException { if (!isBuffered()) { return; } writer.newline(); writer.append("<!--"); writer.append(value); writer.append("-->"); }
private Object evaluateExpression(ExpressionString attribute, JadeModel model) //throws ExpressionException { String expression = ((ExpressionString)attribute).getValue(); Object result = ExpressionHandler.evaluateExpression(expression, model); if (result is ExpressionString) { return(evaluateExpression((ExpressionString)result, model)); } return(result); }
public void renderTemplate(JadeTemplate template, Dictionary <String, Object> model, TextWriter writer) //throws JadeCompilerException { JadeModel jadeModel = new JadeModel(sharedVariables); foreach (String filterName in filters.Keys) { jadeModel.addFilter(filterName, filters[filterName]); } jadeModel.putAll(model); template.process(jadeModel, writer); }
public void Test() { var defaults = new Dictionary<string, object>(); defaults.Add("hello", "world"); model = new JadeModel(defaults); model.put("new", true); Assert.IsFalse(defaults.ContainsKey("new")); Assert.IsTrue(model.ContainsKey("new")); Assert.AreEqual(model.get("hello"), "world"); }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { if (hasBlock()) { model.setMixin(getName(), this); } else { base.execute(writer, model, template); } }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { Object result; try { result = ExpressionHandler.evaluateExpression(value, model); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } model.put(name, result); }
public void Test() { var defaults = new Dictionary <string, object>(); defaults.Add("hello", "world"); model = new JadeModel(defaults); model.put("new", true); Assert.IsFalse(defaults.ContainsKey("new")); Assert.IsTrue(model.ContainsKey("new")); Assert.AreEqual(model.get("hello"), "world"); }
public static Object evaluateExpression(String expression, JadeModel model) { try { return expression; Lexer.Tokens.Expression e = jexl.createExpression(expression); return null;// e.evaluate(new MapContext(model)); } catch (Exception) { throw;// new ExpressionException(expression, e); } }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { try { String str = Utils.interpolate(preparedValue, model); writer.append(str); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
private void run(IndentWriter writer, JadeModel model, Object result, JadeTemplate template) { if (result is IEnumerable) { runIterator(((IEnumerable) result).GetEnumerator(), model, writer, template); } else if (result.GetType().IsArray) { var iterator = ((IEnumerable)result).GetEnumerator(); runIterator(iterator, model, writer, template); } else if (result is Dictionary<String, Object>) { runMap((Dictionary<String, Object>)result, model, writer, template); } }
public void beanPropertyCondition() { Dictionary<String, Object> map = new Dictionary<String, Object>(); map.Add("bean", getTestBean("beanie")); List<TestBean> moreBeans = new List<TestBean>(); for (int i = 0; i < 5; i++) { moreBeans.Add(getTestBean("x")); } map.Add("beans", moreBeans); JadeModel model = new JadeModel(map); run("bean_property_condition", false, model); }
public static Object evaluateExpression(String expression, JadeModel model) { try { return(expression); Lexer.Tokens.Expression e = jexl.createExpression(expression); return(null);// e.evaluate(new MapContext(model)); } catch (Exception) { throw;// new ExpressionException(expression, e); } }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { Object result; try { result = ExpressionHandler.evaluateExpression(value, model); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } model.put(name, result); }
private void run(IndentWriter writer, JadeModel model, Object result, JadeTemplate template) { if (result is IEnumerable) { runIterator(((IEnumerable)result).GetEnumerator(), model, writer, template); } else if (result.GetType().IsArray) { var iterator = ((IEnumerable)result).GetEnumerator(); runIterator(iterator, model, writer, template); } else if (result is Dictionary <String, Object> ) { runMap((Dictionary <String, Object>)result, model, writer, template); } }
private void runMap(Dictionary <String, Object> result, JadeModel model, IndentWriter writer, JadeTemplate template) { var keys = result.Keys; if (keys.Count == 0) { executeElseNode(model, writer, template); return; } foreach (String key in keys) { model.put(getValue(), result[key]); model.put(getKey(), key); getBlock().execute(writer, model, template); } }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { try { model.pushScope(); while (ExpressionHandler.evaluateBooleanExpression(value, model).Value) { block.execute(writer, model, template); } model.popScope(); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
public void beanPropertyCondition() { Dictionary <String, Object> map = new Dictionary <String, Object>(); map.Add("bean", getTestBean("beanie")); List <TestBean> moreBeans = new List <TestBean>(); for (int i = 0; i < 5; i++) { moreBeans.Add(getTestBean("x")); } map.Add("beans", moreBeans); JadeModel model = new JadeModel(map); run("bean_property_condition", false, model); }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { try { model.pushScope(); while (ExpressionHandler.evaluateBooleanExpression(value, model).Value) { block.execute(writer, model, template); } model.popScope(); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
private void runIterator(IEnumerator iterator, JadeModel model, IndentWriter writer, JadeTemplate template) { int index = 0; if (!iterator.MoveNext()) { executeElseNode(model, writer, template); return; } while (iterator.MoveNext()) { model.put(getValue(), iterator.Current); model.put(getKey(), index); getBlock().execute(writer, model, template); index++; } }
protected Dictionary<String, Object> mergeInheritedAttributes(JadeModel model) { Dictionary<String, Object> mergedAttributes = this.attributes; if (inheritsAttributes) { Object o = model.get("attributes"); if (o != null && o is Dictionary<String, Object>) { Dictionary<String, Object> inheritedAttributes = (Dictionary<String, Object>)o; foreach (var entry in inheritedAttributes) { addAttribute(mergedAttributes, (String)entry.Key, entry.Value); } } } return mergedAttributes; }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { try { foreach (CaseConditionNode caseConditionNode in caseConditionNodes) { if (caseConditionNode.isDefault() || checkCondition(model, caseConditionNode)) { caseConditionNode.execute(writer, model, template); break; } } } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { var filter = model.getFilter(getValue()); String result = textBlock.getValue(); if (filter != null) { result = filter.convert(result, attributes, model); } try { result = Utils.interpolate(result, model, false); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } writer.append(result); }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { foreach (IfConditionNode conditionNode in this.conditions) { try { if (conditionNode.isDefault() || checkCondition(model, conditionNode.getValue()) ^ conditionNode.isInverse()) { conditionNode.getBlock().execute(writer, model, template); return; } } catch (ExpressionException e) { throw new JadeCompilerException(conditionNode, template.getTemplateLoader(), e); } } }
private String getInterpolatedAttributeValue(String name, Object attribute, JadeModel model, JadeTemplate template) //throws JadeCompilerException { if (!preparedAttributeValues.ContainsKey(name)) { preparedAttributeValues.Add(name, Utils.prepareInterpolate((String)attribute, true)); } List <Object> prepared = preparedAttributeValues[name]; try { return(Utils.interpolate(prepared, model)); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
protected Dictionary <String, Object> mergeInheritedAttributes(JadeModel model) { Dictionary <String, Object> mergedAttributes = this.attributes; if (inheritsAttributes) { Object o = model.get("attributes"); if (o != null && o is Dictionary <String, Object> ) { Dictionary <String, Object> inheritedAttributes = (Dictionary <String, Object>)o; foreach (var entry in inheritedAttributes) { addAttribute(mergedAttributes, (String)entry.Key, entry.Value); } } } return(mergedAttributes); }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { try { foreach (CaseConditionNode caseConditionNode in caseConditionNodes) { if (caseConditionNode.isDefault() || checkCondition(model, caseConditionNode)) { caseConditionNode.execute(writer, model, template); break; } } } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) //throws JadeCompilerException { foreach (IfConditionNode conditionNode in this.conditions) { try { if (conditionNode.isDefault() || checkCondition(model, conditionNode.getValue()) ^ conditionNode.isInverse()) { conditionNode.getBlock().execute(writer, model, template); return; } } catch (ExpressionException e) { throw new JadeCompilerException(conditionNode, template.getTemplateLoader(), e); } } }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { Object result; try { result = ExpressionHandler.evaluateExpression(getCode(), model); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } if (result == null) { throw new JadeCompilerException(this, template.getTemplateLoader(), "[" + code + "] has to be iterable but was null"); } model.pushScope(); run(writer, model, result, template); model.popScope(); }
// throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { if (!isBuffered()) { return; } writer.newline(); if (value.StartsWith("if")) { writer.append("<!--[" + value + "]>"); block.execute(writer, model, template); writer.append("<![endif]-->"); } else { writer.append("<!--" + value); block.execute(writer, model, template); writer.append("-->"); } }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { writer.newline(); writer.append("<"); writer.append(name); writer.append(Attributes(model, template)); if (isTerse(template)) { writer.append(">"); return; } if (isSelfClosing(template) || (selfClosing && isEmpty())) { writer.append("/>"); return; } writer.append(">"); if (hasTextNode()) { textNode.execute(writer, model, template); } if (hasBlock()) { writer.increment(); block.execute(writer, model, template); writer.decrement(); writer.newline(); } if (hasCodeNode()) { codeNode.execute(writer, model, template); } writer.append("</"); writer.append(name); writer.append(">"); }
// throws ExpressionException private String getAttributeString(String name, Object attribute, JadeModel model, JadeTemplate template) { String value = null; if (attribute is String) { value = getInterpolatedAttributeValue(name, attribute, model, template); } else if (attribute is bool) { if ((bool)attribute) { value = name; } else { return ""; } if (template.isTerse()) { value = null; } } else if (attribute is ExpressionString) { Object expressionValue = evaluateExpression((ExpressionString)attribute, model); if (expressionValue == null) { return ""; } // TODO: refactor if (expressionValue is bool) { if ((bool)expressionValue) { value = name; } else { return ""; } if (template.isTerse()) { value = null; } } else { value = expressionValue.ToString(); value = Utils.escapeHTML(value); } } else { return ""; } StringBuilder sb = new StringBuilder(); if (name != null) { sb.Append(" ").Append(name); if (value != null) { sb.Append("=").Append('"'); sb.Append(value); sb.Append('"'); } } return sb.ToString(); }
//throws JadeCompilerException private String getInterpolatedAttributeValue(String name, Object attribute, JadeModel model, JadeTemplate template) { if (!preparedAttributeValues.ContainsKey(name)) { preparedAttributeValues.Add(name, Utils.prepareInterpolate((String)attribute, true)); } List<Object> prepared = preparedAttributeValues[name]; try { return Utils.interpolate(prepared, model); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } }
private void run(String testName, bool pretty) { JadeModel model = new JadeModel(getModelMap(testName)); run(testName, pretty, model); }
//throws JadeCompilerException public override void execute(IndentWriter writer, JadeModel model, JadeTemplate template) { block.execute(writer, model, template); }
public abstract void execute(IndentWriter writer, JadeModel model, JadeTemplate template);
private void runMap(Dictionary<String, Object> result, JadeModel model, IndentWriter writer, JadeTemplate template) { var keys = result.Keys; if (keys.Count == 0) { executeElseNode(model, writer, template); return; } foreach (String key in keys) { model.put(getValue(), result[key]); model.put(getKey(), key); getBlock().execute(writer, model, template); } }
private String Attributes(JadeModel model, JadeTemplate template) { StringBuilder sb = new StringBuilder(); Dictionary<String, Object> mergedAttributes = mergeInheritedAttributes(model); foreach (var entry in mergedAttributes) { try { sb.Append(getAttributeString(entry.Key, entry.Value, model, template)); } catch (ExpressionException e) { throw new JadeCompilerException(this, template.getTemplateLoader(), e); } } return sb.ToString(); }
//throws ExpressionException private Object evaluateExpression(ExpressionString attribute, JadeModel model) { String expression = ((ExpressionString)attribute).getValue(); Object result = ExpressionHandler.evaluateExpression(expression, model); if (result is ExpressionString) { return evaluateExpression((ExpressionString)result, model); } return result; }