import (
        "os";
        "io";
-       "array";
-       "utf8";
+       "fmt";
 )
 
 // Writer is a filter implementing the io.Write interface.
 // It provides facilities to generate HTML tags and does
-// proper HTML-escaping for text written through it.
+// HTML-escaping for text written through Write. Incoming
+// text is assumed to be UTF-8 encoded.
 
 export type Writer struct {
        // TODO should not export any of the fields
 }
 
 
-/* export */ func (b *Writer) Flush() *os.Error {
-       return nil;
+/* export */ func (p *Writer) Write(buf *[]byte) (written int, err *os.Error) {
+       i0 := 0;
+       for i := i0; i < len(buf); i++ {
+               var s string;
+               switch buf[i] {
+               case '<': s = "<";
+               case '&': s = "&";
+               default: continue;
+               }
+               // write HTML escape instead of buf[i]
+               w1, e1 := p.writer.Write(buf[i0 : i]);
+               if e1 != nil {
+                       return i0 + w1, e1;
+               }
+               w2, e2 := io.WriteString(p.writer, s);
+               if e2 != nil {
+                       return i0 + w1 /* not w2! */, e2;
+               }
+               i0 = i + 1;
+       }
+       written, err = p.writer.Write(buf[i0 : len(buf)]);
+       return len(buf), err;
 }
 
 
-/* export */ func (b *Writer) Write(buf *[]byte) (written int, err *os.Error) {
-       written, err = b.writer.Write(buf);  // BUG 6g - should just have return
-       return written, err;
+// ----------------------------------------------------------------------------
+// HTML-specific interface
+
+/* export */ func (p *Writer) Tag(s string) {
+       // TODO proper error handling
+       io.WriteString(p.writer, s);
 }
 
 
+// ----------------------------------------------------------------------------
+//
+
 export func New(writer io.Write) *Writer {
        return new(Writer).Init(writer);
 }
 
 
 import (
        "os";
+       "io";
        "array";
        "tabwriter";
        "flag";
 
 type Printer struct {
        // output
-       writer *htmlwriter.Writer;
+       text io.Write;
+       tags *htmlwriter.Writer;
        
        // comments
        comments *array.Array;  // the list of all comments
 }
 
 
-func (P *Printer) Init(writer *htmlwriter.Writer, comments *array.Array) {
-       // writer
-       P.writer = writer;
+func (P *Printer) Init(text io.Write, tags *htmlwriter.Writer, comments *array.Array) {
+       // writers
+       P.text = text;
+       P.tags = tags;
        
        // comments
        P.comments = comments;
 // Printing support
 
 func (P *Printer) Printf(format string, s ...) {
-       n, err := fmt.fprintf(P.writer, format, s);
+       n, err := fmt.fprintf(P.text, format, s);
        if err != nil {
                panic("print error - exiting");
        }
 
 // ----------------------------------------------------------------------------
 // HTML support
-// TODO Move this to html writer
 
 func (P *Printer) HtmlPrologue(title string) {
-       if html.BVal() {
-               P.String(0,
+       if P.tags != nil {
+               P.tags.Tag(
                        "<html>\n"
                        "<head>\n"
                        "       <META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n"
 
 
 func (P *Printer) HtmlEpilogue() {
-       if html.BVal() {
-               P.String(0,
+       if P.tags != nil {
+               P.tags.Tag(
                        "</pre>\n"
                        "</body>\n"
                        "<html>\n"
 
 
 func (P *Printer) HtmlIdentifier(pos int, ident string) {
-       if html.BVal() {
-               P.String(pos, `<a href="#` + ident + `">` + ident + `</a>`);
+       if false && P.tags != nil {
+               P.tags.Tag(`<a href="#` + ident + `">` + ident + `</a>`);
        } else {
                P.String(pos, ident);
        }
 
 export func Print(prog *AST.Program) {
        // setup
+       var P Printer;
        padchar := byte(' ');
        if usetabs.BVal() {
                padchar = '\t';
        }
-       twriter := tabwriter.New(os.Stdout, int(tabwidth.IVal()), 1, padchar, true);
-       hwriter := htmlwriter.New(twriter);
-       var P Printer;
-       P.Init(hwriter, prog.comments);
+       var (
+               text = tabwriter.New(os.Stdout, int(tabwidth.IVal()), 1, padchar, true, html.BVal());
+               tags *htmlwriter.Writer;
+       )
+       if html.BVal() {
+               tags = htmlwriter.New(text);
+       }
+       P.Init(text, tags, prog.comments);
 
        P.HtmlPrologue("<the source>");
        P.Program(prog);
        P.HtmlEpilogue();
        
        P.String(0, "");  // flush pending separator/newlines
-       hwriter.Flush();  // ignore errors
-       twriter.Flush();  // ignore errors
+       err := text.Flush();
+       if err != nil {
+               panic("print error - exiting");
+       }
 }