private JadeTemplate getJadeTemplate(String name) throws IOException { Parser parser = new Parser("templates/" + name, JadeTemplateLoader.JADE_LOADER); Node root = parser.parse(); final JadeTemplate jadeTemplate = new JadeTemplate(); jadeTemplate.setTemplateLoader(JadeTemplateLoader.JADE_LOADER); jadeTemplate.setRootNode(root); return jadeTemplate; }
private Node parseASTFilter() { Token token = expect(Filter.class); Filter filterToken = (Filter) token; AttributeList attr = (AttributeList) accept(AttributeList.class); token = expect(Colon.class); FilterNode node = new FilterNode(); node.setValue(filterToken.getValue()); node.setBlock(block()); node.setLineNumber(line()); node.setFileName(filename); node.setAttributes(convertToNodeAttributes(attr)); return node; }
Token incomingToken = peek(); if (incomingToken instanceof CssId) { Token tok = advance(); tagNode.setAttribute("id", tok.getValue(),false); continue; } else if (incomingToken instanceof CssClass) { Token tok = advance(); tagNode.setAttribute("class", tok.getValue(),false); continue; } else if (incomingToken instanceof AttributeList) { if (seenAttrs) { throw new JadeParserException(filename,line(),templateLoader,this.filename + ", line " + this.peek().getLineNumber() + ":\nYou should not have jade tags with multiple attributes."); AttributeList tok = (AttributeList) advance(); List<Attribute> attrs = tok.getAttributes(); tagNode.setSelfClosing(tok.isSelfClosing()); Token tok = this.advance(); tagNode.addAttributes(tok.getValue()); } else { if (peek() instanceof Dot) { dot = true; tagNode.setTextOnly(true); advance(); if (peek() instanceof Text) { tagNode.getBlock().push(parseText()); } else if (peek() instanceof Expression) {
private List<CaseConditionNode> whenBlock() { expect(Indent.class); List<CaseConditionNode> caseConditionalNodes = new LinkedList<CaseConditionNode>(); while (!(peek() instanceof Outdent) && !(peek() instanceof Eos)) { if (peek() instanceof Newline) { advance(); } else { caseConditionalNodes.add(this.parseCaseCondition()); } } if (peek() instanceof Outdent) { expect(Outdent.class); } return caseConditionalNodes; }
private Node parseExtends() { Token token = expect(ExtendsToken.class); ExtendsToken extendsToken = (ExtendsToken) token; String templateName = extendsToken.getValue().trim(); Parser parser = createParser(templateName); parser.setBlocks(blocks); parser.setContexts(contexts); extending = parser; LiteralNode node = new LiteralNode(); node.setValue(""); return node; }
private Node parseInclude() { Token token = expect(Include.class); Include includeToken = (Include) token; String templateName = includeToken.getValue().trim(); FilterNode node = new FilterNode(); node.setValue(includeToken.getFilter()); node.setLineNumber(line()); node.setFileName(filename); TextNode text = new TextNode(); Parser parser = createParser(templateName); parser.setBlocks(new LinkedHashMap<String,BlockNode>(blocks)); parser.setMixins(mixins); contexts.push(parser); Node ast = parser.parse(); contexts.pop(); ast.setFileName(path); if (peek() instanceof Indent && ast != null) { ((BlockNode) ast).getIncludeBlock().push(block());
private Node[] parseInlineTagsInText(String str) { int line = this.line(); Matcher matcher = Pattern.compile("(\\\\)?#\\[((?:.|\\n)*)$").matcher(str); TextNode text = new TextNode(); if (matcher.group(1) != null) { // escape text.setValue(str.substring(0, matcher.start()) + "#[");//Not sure if Matcher.end() is correct Node[] rest = this.parseInlineTagsInText(matcher.group(2)); if (rest[0] instanceof TextNode) { text.setValue(text.getValue() + rest[0].getValue()); inner = new Parser(range.getSrc(), this.filename,this.basePath, this.templateLoader,this.expressionHandler); //Need to be reviewed } catch (IOException e) { throw new JadeParserException(this.filename,line,templateLoader,"Could not parse text"); buffer = ArrayUtils.add(buffer,inner.parse()); return ArrayUtils.addAll(buffer, this.parseInlineTagsInText(rest.substring(range.getEnd() + 1)));
public Node parse() { BlockNode block = new BlockNode(); block.setLineNumber(lexer.getLineno()); block.setFileName(filename); while (!(peek() instanceof Eos)) { if (peek() instanceof Newline) { advance(); } else { Node expr = parseExpr(); if (expr != null) { block.push(expr); } } } if (extending != null) { getContexts().push(extending); Node rootNode = extending.parse(); getContexts().pop(); // hoist mixins Set<String> keySet = this.mixins.keySet(); for (String name : keySet) { rootNode.getNodes().push(this.mixins.get(name)); } return rootNode; } return block; }
private Node parseCase() { String val = expect(CaseToken.class).getValue(); Node node = new CaseNode(); node.setValue(val); node.setLineNumber(line()); Node block = new BlockNode(); block.setLineNumber(line()); block.setFileName(filename); expect(Indent.class); while (!(peek() instanceof Outdent)) { if (peek() instanceof Comment) { advance(); } else if (peek() instanceof Newline) { advance(); } else if (peek() instanceof When) { block.push(parseWhen()); } else if (peek() instanceof Default) { block.push(parseDefault()); } else { throw new JadeParserException(filename,lexer.getLineno(),templateLoader,"Unexpected token \"" + this.peek() + "\", expected \"when\", \"default\" or \"newline\""); } } expect(Outdent.class); node.setBlock(block); return node; } /**
private Node parseConditional() { If conditionalToken = (If) expect(If.class); ConditionalNode conditional = new ConditionalNode(); conditional.setLineNumber(conditionalToken.getLineNumber()); main.setBlock(block()); conditions.add(main); while (peek() instanceof ElseIf) { ElseIf token = (ElseIf) expect(ElseIf.class); IfConditionNode elseIf = new IfConditionNode(token.getValue(), token.getLineNumber()); elseIf.setBlock(block()); conditions.add(elseIf); if (peek() instanceof Else) { Else token = (Else) expect(Else.class); IfConditionNode elseNode = new IfConditionNode(null, token.getLineNumber()); elseNode.setDefault(true); elseNode.setBlock(block()); conditions.add(elseNode);
private Node parseFilter() { Token token = expect(Filter.class); Filter filterToken = (Filter) token; AttributeList attr = (AttributeList) accept(AttributeList.class); lexer.setPipeless(true); Node tNode = parseTextBlock(); lexer.setPipeless(false); FilterNode node = new FilterNode(); node.setValue(filterToken.getValue()); node.setLineNumber(line()); node.setFileName(filename); if(tNode!=null) node.setTextBlock(tNode); else{ node.setTextBlock(new BlockNode()); } if (attr != null) { node.setAttributes(convertToNodeAttributes(attr)); } return node; }
private Parser createParser(String templateName) { templateName = ensureJadeExtension(templateName); try { return new Parser(pathHelper.resolvePath(filename,templateName, basePath, templateLoader.getExtension()), basePath, templateLoader, expressionHandler); } catch (IOException e) { throw new JadeParserException(filename, lexer.getLineno(), templateLoader, "the template [" + templateName + "] could not be opened\n" + e.getMessage()); } }
private Node blockExpansion() { if (peek() instanceof Colon) { Token token = expect(Colon.class); Colon colon = (Colon) token; BlockNode block = new BlockNode(); block.setLineNumber(colon.getLineNumber()); block.setFileName(filename); block.getNodes().add(parseExpr()); return block; } return block(); }
private Node parseEach() { Token token = expect(Each.class); Each eachToken = (Each) token; EachNode node = new EachNode(); node.setValue(eachToken.getValue()); node.setKey(eachToken.getKey()); node.setCode(eachToken.getCode()); node.setLineNumber(eachToken.getLineNumber()); node.setFileName(filename); node.setBlock(block()); if (peek() instanceof Else) { advance(); node.setElseNode(block()); } return node; }
private Node parseTextBlock() { BlockNode blockNode = new BlockNode(); blockNode.setLineNumber(line()); blockNode.setFileName(filename); Token body = peek(); if(!(body instanceof PipelessText)){ return null; } this.advance(); ArrayList<String> values = body.getValues(); Node[] textNodes = {}; for (String value : values) { textNodes = ArrayUtils.addAll(textNodes,parseInlineTagsInText(value)); } blockNode.setNodes(new LinkedList<Node>(Arrays.asList(textNodes))); return blockNode; }
private BlockNode block() { BlockNode block = new BlockNode(); block.setLineNumber(lexer.getLineno()); block.setFileName(filename); expect(Indent.class); while (!(peek() instanceof Outdent)) { if (peek() instanceof Newline) { advance(); } else { Node parseExpr = this.parseExpr(); parseExpr.setFileName(filename); if (parseExpr != null) { block.push(parseExpr); } } } expect(Outdent.class); return block; }
private CaseConditionNode parseCaseCondition() { CaseConditionNode node = new CaseConditionNode(); Token token = null; if (peek() instanceof When) { token = expect(When.class); } else { token = expect(Default.class); node.setDefault(true); } node.setLineNumber(token.getLineNumber()); node.setFileName(filename); node.setValue(token.getValue()); node.setBlock(blockExpansion()); return node; }