
// GenHTML.java
// Girish Managoli
// https://opensource.com/article/19/12/generate-code-apache-velocity
// Modified by Andrew Davison, ad@coe.psu.ac.th, July 2023

/* Generate an HTML file with the specified title and body 
   text. Save in a file called "<title>.html"

  Usage:
    > compile GenHTML.java
    > run GenHTML  <page title> <body text>
   e.g.
    > run GenHTML "Hello" "Hello world<br>From Andrew"
           // generates Hello.html

*/

import java.io.*;
import java.util.*;

import org.apache.velocity.app.*;
import org.apache.velocity.*;
import org.apache.velocity.tools.generic.*;


public class GenHTML
{
  private static final String TEMPLATE_FNM = "html.vm";


  public static void main(String args[]) throws Exception
  {
    if (args.length != 2) {
      System.out.println("Usage: run GenHTML <page title> <body text>");
      return;
    }

    Velocity.init();
    System.out.println("Loading template: " + TEMPLATE_FNM);
    Template template = Velocity.getTemplate(TEMPLATE_FNM);
     
    VelocityContext vc = new VelocityContext();
    vc.put("dateTool", new ComparisonDateTool());  // load tool
    vc.put("title", args[0]);
    vc.put("text", args[1]);

    String fnm = args[0]+".html";
    System.out.println("\nGenerating " + fnm);

    Writer w = new FileWriter(new File(fnm));
    template.merge(vc, w);
    w.flush();
    w.close();
  } /* main */

}  // end of GenHTML class
