view trunk/src/docgen/document/writer.d @ 452:f658ec4a15dd

Simple docgen ui util, some fixes.
author Jari-Matti M?kel? <jmjm@iki.fi>
date Wed, 24 Oct 2007 22:31:38 +0300
parents 3f44c38bf870
children 4e5b35df3060
line wrap: on
line source

/**
 * Author: Jari-Matti Mäkelä
 * License: GPL3
 */
module docgen.document.writer;

public import docgen.misc.misc;
import tango.io.model.IConduit : OutputStream;
import tango.util.time.Date;
import tango.util.time.Clock;
import tango.text.convert.Sprint;
import tango.io.stream.FileStream;
import tango.io.Stdout;
import tango.io.Print: Print;
import tango.text.convert.Layout : Layout;
public import dil.Module;

char[] timeNow() {
  auto date = Clock.toDate;
  auto sprint = new Sprint!(char);
  return sprint.format("{0} {1} {2} {3}",
    date.asDay(),
    date.asMonth(),
    date.day,
    date.year);
}

char[] loadTemplate(char[] style, char[] format, char[] templateName) {
  char[] fn = "docgen/templates/"~style~"/"~format~"/"~templateName~".tpl";
  
  scope(failure) {
    Stderr("Warning: error opening template "~fn~".");
    return null;
  }

  auto file = new FileInput(fn);
  auto content = new char[file.length];
  auto bytesRead = file.read(content);
  
  assert(bytesRead == file.length, "Error reading template");
  
  file.close();
  
  return content;
}

const templateNames = [
  "firstpage"[], "toc"[], "modules"[],
  "listings"[], "dependencies"[], "index"[],
  "lastpage"[],
  "graphics"[], "listing"[]
];

interface DocumentWriter {
  void setOutput(OutputStream[] outputs);

  void generateFirstPage();

  void generateTOC(Module[] modules);

  void generateModuleSection();

  void generateListingSection();

  void generateDepGraphSection();

  void generateIndexSection();

  void generateLastPage();
  
  /**
   * Writes a tag for the given image to the output stream
   */ 
  void addGraphics(char[] imageFile);
  
  /**
   * Writes a tag for the given source listing to the output stream;
   */
  void addListing(char[] moduleName, char[] contents, bool inline = true);
}

interface DocumentWriterFactory : WriterFactory {
  DocumentWriter createDocumentWriter(OutputStream[] outputs);
}

template AbstractDocumentWriter(int n, char[] format) {
  abstract class AbstractDocumentWriter : AbstractWriter!(DocumentWriterFactory, n), DocumentWriter {
    protected char[][char[]] templates;
         
    this(DocumentWriterFactory factory, OutputStream[] outputs) {
      super(factory, outputs);
    
      foreach(tpl; templateNames) {
        templates[tpl] = loadTemplate(factory.options.templates.templateStyle, format, tpl);
      }
    }

    void setOutput(OutputStream[] outputs) {
      this.outputs = outputs;
    }

    void generateTOC(Module[] modules) {
      auto print = new Print!(char)(new Layout!(char), outputs[0]);
    
      print.format(templates["toc"]);
    }

    void generateModuleSection() {
      auto print = new Print!(char)(new Layout!(char), outputs[0]);
    
      print.format(templates["modules"]);
    }

    void generateListingSection() {
      auto print = new Print!(char)(new Layout!(char), outputs[0]);
    
      print.format(templates["listings"]);
    }

    void generateDepGraphSection() {
      auto print = new Print!(char)(new Layout!(char), outputs[0]);
    
      print.format(templates["dependencies"]);
    }

    void generateIndexSection() {
      auto print = new Print!(char)(new Layout!(char), outputs[0]);
    
      print.format(templates["index"]);
    }

    void generateLastPage() {
      auto print = new Print!(char)(new Layout!(char), outputs[0]);
    
      print.format(templates["lastpage"]);
    }

    // --- page components

    void addGraphics(char[] imageFile) {
      auto print = new Print!(char)(new Layout!(char), outputs[0]);
    
      print.format(templates["graphics"], imageFile);
    }
    
    void addListing(char[] moduleName, char[] contents, bool inline) {
      auto print = new Print!(char)(new Layout!(char), outputs[0]);
    
      print.format(templates["listing"], moduleName, contents);
    }
  }
}