[stringtemplate-interest] <pre> tags and recursive structures

Johan Stuyts j.stuyts at javathinker.com
Wed Aug 1 10:42:31 PDT 2007


> Hi. The one weakness of the autoindent is that I have no mechanism
> yet to "unindent" any of the lines. :(

I took a look at the code to see if it was easy to add this functionality,
and it was :-)

My changes make it possible to add an extra option, newIndentationContext,
to a template invocation. This will store the current indentation stack,
and replace it with an empty one. When the invoked template returns, the
new indentation stack is discarded and the previous one is restored. Here
is an example template group that uses this option:

group newIndentationContextTest;

page() ::= <<
<html>
    <head>
      <title>New indentation context test</title>
    </head>
    <body>
      <pre>
        $foo();newIndentationContext="dummy"$
      </pre>
    </body>
</html>

>>

foo() ::= <<
foo
    $bar()$
>>

bar() ::= <<
bar
$baz()$
>>

baz() ::= <<
baz
>>

This is the output that will be generated (using the AutoIndentWriter):

<html>
    <head>
      <title>New indentation context test</title>
    </head>
    <body>
      <pre>
foo
    bar
    baz
      </pre>
    </body>
</html>

See below for the changes that are needed to make it work.

Johan


--- original-src/org/antlr/stringtemplate/AutoIndentWriter.java	2007-08-01
15:32:10.234375000 +0200
+++ src/org/antlr/stringtemplate/AutoIndentWriter.java	2007-08-01
17:19:28.921875000 +0200
@@ -54,6 +54,7 @@
   	 *  from 0..n-1.  List<String>
   	 */
   	protected List indents = new ArrayList();
+	protected List indentationContexts = new ArrayList();

   	/** Stack of integer anchors (char positions in line); avoid Integer
   	 *  creation overhead.
@@ -94,6 +95,16 @@
           return (String)indents.remove(indents.size()-1);
       }

+    public void pushIndentationContext() {
+    	indentationContexts.add(indents);
+    	indents = new ArrayList();
+    	indents.add(null);
+    }
+
+    public void popIndentationContext() {
+    	indents =
(List)indentationContexts.remove(indentationContexts.size() - 1);
+    }
+
   	public void pushAnchorPoint() {
   		if ( (anchors_sp +1)>=anchors.length ) {
   			int[] a = new int[anchors.length*2];
--- original-src/org/antlr/stringtemplate/language/ASTExpr.java	2007-08-01
15:32:10.265625000 +0200
+++ src/org/antlr/stringtemplate/language/ASTExpr.java	2007-08-01
15:33:18.312500000 +0200
@@ -125,6 +125,10 @@
           }
   		out.pushIndentation(getIndentation());
   		// handle options, anchor, wrap, separator...
+		StringTemplateAST newIndentationContextAST =
(StringTemplateAST)getOption("newIndentationContext");
+		if ( newIndentationContextAST!=null ) { // any non-empty expr means
true; check presence
+			out.pushIndentationContext();
+		}
   		StringTemplateAST anchorAST = (StringTemplateAST)getOption("anchor");
   		if ( anchorAST!=null ) { // any non-empty expr means true; check
presence
   			out.pushAnchorPoint();
@@ -140,6 +144,9 @@
           catch (RecognitionException re) {
               self.error("can't evaluate tree: "+exprTree.toStringList(),
re);
           }
+		if ( newIndentationContextAST!=null ) {
+			out.popIndentationContext();
+		}
           out.popIndentation();
   		if ( anchorAST!=null ) {
   			out.popAnchorPoint();
---
original-src/org/antlr/stringtemplate/StringTemplateWriter.java	2007-08-01
15:32:10.296875000 +0200
+++ src/org/antlr/stringtemplate/StringTemplateWriter.java	2007-08-01
15:33:18.421875000 +0200
@@ -43,6 +43,10 @@

       String popIndentation();

+	void pushIndentationContext();
+
+	void popIndentationContext();
+
   	void pushAnchorPoint();

   	void popAnchorPoint();
---
original-src/org/antlr/stringtemplate/test/TestStringTemplate.java	2007-08-01
15:32:10.296875000 +0200
+++ src/org/antlr/stringtemplate/test/TestStringTemplate.java	2007-08-01
18:59:23.890625000 +0200
@@ -2090,6 +2090,10 @@
   			public String popIndentation() {
   				return null;
   			}
+			public void pushIndentationContext() {
+			}
+			public void popIndentationContext() {
+			}
   			public void pushAnchorPoint() {
   			}
   			public void popAnchorPoint() {


More information about the stringtemplate-interest mailing list