|
| 1 | +package twg2.parser.test; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import lombok.val; |
| 7 | + |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import twg2.collections.primitiveCollections.CharArrayList; |
| 12 | +import twg2.parser.Inclusion; |
| 13 | +import twg2.parser.condition.text.CharParserMatchable; |
| 14 | +import twg2.parser.fragment.CodeTokenType; |
| 15 | +import twg2.parser.language.CodeLanguage; |
| 16 | +import twg2.parser.tokenizers.CodeTokenizer; |
| 17 | +import twg2.parser.tokenizers.CodeTokenizerBuilder; |
| 18 | +import twg2.parser.workflow.CodeFileSrc; |
| 19 | +import twg2.text.tokenizer.CharConditionPipe; |
| 20 | +import twg2.text.tokenizer.CharConditions; |
| 21 | +import twg2.text.tokenizer.CharParserFactory; |
| 22 | +import twg2.text.tokenizer.CharParserMatchableFactory; |
| 23 | +import twg2.text.tokenizer.StringConditions; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author TeamworkGuy2 |
| 27 | + * @since 2017-10-21 |
| 28 | + */ |
| 29 | +public class HtmlTemplateTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void compileInputSinglelineTest() { |
| 33 | + String ln = "\n"; |
| 34 | + String srcName = "compile-input-test-1"; |
| 35 | + |
| 36 | + String src = |
| 37 | + "<head>" + ln + |
| 38 | + " <body>" + ln + |
| 39 | + " <stuff>things 1</stuff>" + ln + |
| 40 | + " $importHtml(name=\"arc 12\")$" + ln + |
| 41 | + " </body>" + ln + |
| 42 | + "</head>"; |
| 43 | + |
| 44 | + String expect = |
| 45 | + "<head>" + ln + |
| 46 | + " <body>" + ln + |
| 47 | + " <stuff>things 1</stuff>" + ln + |
| 48 | + " custom(arc 12)" + ln + |
| 49 | + " </body>" + ln + |
| 50 | + "</head>"; |
| 51 | + |
| 52 | + StringBuilder dst = new StringBuilder(src); |
| 53 | + compileTemplate(createHtmlParser().build(), src, dst, srcName, true, createHtmlVarsSingleline()); |
| 54 | + |
| 55 | + Assert.assertEquals(expect, dst.toString()); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + @Test |
| 60 | + public void compileInputMultilineTest() { |
| 61 | + String ln = "\n"; |
| 62 | + String srcName = "compile-input-test-2"; |
| 63 | + |
| 64 | + String src = |
| 65 | + "<head>" + ln + |
| 66 | + " <body>" + ln + |
| 67 | + " <stuff>things 1</stuff>" + ln + |
| 68 | + " $importHtml(name=\"arc 12\")$" + ln + |
| 69 | + " </body>" + ln + |
| 70 | + "</head>"; |
| 71 | + |
| 72 | + String expect = |
| 73 | + "<head>" + ln + |
| 74 | + " <body>" + ln + |
| 75 | + " <stuff>things 1</stuff>" + ln + |
| 76 | + " custom: {" + ln + |
| 77 | + " arc 12" + ln + |
| 78 | + " }" + ln + |
| 79 | + " </body>" + ln + |
| 80 | + "</head>"; |
| 81 | + |
| 82 | + StringBuilder dst = new StringBuilder(src); |
| 83 | + compileTemplate(createHtmlParser().build(), src, dst, srcName, true, createHtmlVarsMultiline()); |
| 84 | + |
| 85 | + Assert.assertEquals(expect, dst.toString()); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + private static final CodeTokenizerBuilder<CodeLanguage> createHtmlParser() { |
| 90 | + CharParserFactory parser = createHtmlNamedVarParser("$", "importHtml", "$"); |
| 91 | + CodeTokenizerBuilder<CodeLanguage> docParser = new CodeTokenizerBuilder<CodeLanguage>((CodeLanguage)null).addParser(parser, CodeTokenType.IDENTIFIER); |
| 92 | + return docParser; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + private static final List<TemplateVar> createHtmlVarsSingleline() { |
| 97 | + return Arrays.asList( |
| 98 | + new TemplateVar("$", "$", "importHtml", (s) -> { |
| 99 | + return Arrays.asList("custom(" + s + ")"); |
| 100 | + }) |
| 101 | + ); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + private static final List<TemplateVar> createHtmlVarsMultiline() { |
| 107 | + return Arrays.asList( |
| 108 | + new TemplateVar("$", "$", "importHtml", (s) -> { |
| 109 | + return Arrays.asList("custom: {", "\t" + s, "}"); |
| 110 | + }) |
| 111 | + ); |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + public static final CharParserFactory createHtmlNamedVarParser(String startMark, String templateName, String endMark) { |
| 117 | + CharParserFactory htmlParser = new CharParserMatchableFactory<CharParserMatchable>("HTML Named Var Template", false, Arrays.asList(CharConditionPipe.createPipeAllRequired("HTML Named Var Template", Arrays.asList( |
| 118 | + new StringConditions.Literal("start tag", new String[] { startMark + templateName + "(name=" }, Inclusion.INCLUDE), |
| 119 | + new CharConditions.Start("attribute-string-start", CharArrayList.of('"'), Inclusion.INCLUDE), |
| 120 | + new CharConditions.EndNotPrecededBy("attribute-string-end", CharArrayList.of('"'), Inclusion.INCLUDE, CharArrayList.of('\\')), |
| 121 | + new StringConditions.Literal("end tag", new String[] { ")" + endMark }, Inclusion.INCLUDE) |
| 122 | + )))); |
| 123 | + return htmlParser; |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + public static void compileTemplate(CodeTokenizer parser, String src, StringBuilder srcDst, String srcName, boolean preserveIndentation, List<TemplateVar> vars) { |
| 128 | + char[] srcChars = src.toCharArray(); |
| 129 | + CodeFileSrc docParser = parser.tokenizeDocument(srcChars, 0, srcChars.length, srcName, null); |
| 130 | + |
| 131 | + val childs = docParser.astTree.getChildren(); |
| 132 | + for(int i = childs.size() - 1; i > -1; i--) { |
| 133 | + val child = childs.get(i); |
| 134 | + val frag = child.getData().getToken(); |
| 135 | + val text = child.getData().getText(); |
| 136 | + val startI = frag.getOffsetStart(); |
| 137 | + val endI = frag.getOffsetEnd(); |
| 138 | + boolean foundOne = false; |
| 139 | + |
| 140 | + for(val var : vars) { |
| 141 | + if(var.isMatch(text)) { |
| 142 | + if(foundOne) { |
| 143 | + throw new IllegalStateException("found two vars both matching token " + i + ": " + frag + ", text: '" + text + "'"); |
| 144 | + } |
| 145 | + |
| 146 | + var.insert(child.getData(), true, srcDst, startI, endI - startI); |
| 147 | + foundOne = true; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments