Dashboard > RIFE > ... > Tips and Tricks > Pretty-print HTML output
RIFE Log In | Sign Up   View a printable version of the current page.
Pretty-print HTML output


Added by Stéphane Meslin-Weber, last edited by Geert Bevin on Jan 03, 2007  (view change)
Labels: 
(None)

As all perfectionists know, it's not the redered html/css/flash/etc that matters, it's how clean and tidy the actual source code is!

With this in mind, here's an example Element you can inherit from to produce nicely indented HTML source from your elements using JTidy:

TidyElement.java
package uk.co.tangency.forum.elements;

import java.io.ByteArrayInputStream;

import org.w3c.tidy.Tidy;

import com.uwyn.rife.database.Datasource;
import com.uwyn.rife.engine.Element;
import com.uwyn.rife.engine.exceptions.EngineException;
import com.uwyn.rife.template.Template;
import com.uwyn.rife.template.exceptions.TemplateException;

public class TidyElement extends Element {
    private Tidy tidy;
    private boolean bypassTidy;
    
    public void setBypassTidy(boolean bypassTidy) {
        this.bypassTidy = bypassTidy;
    }
    
    public void initialize() {
        // TODO: see if we can't use a static Tidy instance here...
        tidy = new Tidy();
        tidy.setXHTML(true);
        tidy.setQuiet(true);
        tidy.setSmartIndent(true);
        tidy.setSpaces(2);
        tidy.setTabsize(1);
        tidy.setDropEmptyParas(true);
        tidy.setHideComments(true);
        tidy.setWraplen(200);
        tidy.setTrimEmptyElements(true);
        tidy.setOnlyErrors(true);
        super.initialize();
    }
    
    public void print(Template t) {
        if(!bypassTidy && !isEmbedded()) {
            processTemplate(t);
            ByteArrayInputStream is = new ByteArrayInputStream(t.getContent().getBytes());
            tidy.parse(is, getOutputStream());
        } else {
            super.print(t);
        }
    }
}



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators