change visual appearance of links in html
[wix.git] / src / HaskellHelper.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.misc.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.meta.*;
7 import edu.berkeley.sbp.chr.*;
8 import java.io.*;
9
10 public class HaskellHelper {
11     public static boolean isNull(Object o) { return o==null; }
12
13     private static CharParser parser = null;
14     static {
15         synchronized(HaskellHelper.class) {
16             if (parser == null) {
17                 try {
18                     // FIXME: bundle this into the jarfile
19                     InputStream grammarFile = HaskellHelper.class.getClassLoader().getResourceAsStream("wix.g");
20                     Tree<String> res = new CharParser(GrammarAST.getMetaGrammar())
21                         .parse(grammarFile).expand1();
22                     Union grammar = GrammarAST.buildFromAST(res, "s", new GrammarAST.ImportResolver() {
23                             public InputStream getImportStream(String filename) {
24                                 return this.getClass().getClassLoader().getResourceAsStream(filename);
25                             }
26                         });
27                     parser = new CharParser(grammar);
28                 } catch (Exception e) {
29                     throw new RuntimeException(e);
30                 }
31             }
32         }
33     }
34
35     public static Tree help(String targetFile) throws Throwable {
36         Tree ret = null;
37         try {
38             Reader r = new InputStreamReader(new FileInputStream(targetFile));
39             Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
40             ret = parser.parse(input).expand1();
41         } catch (Throwable e) {
42             e.printStackTrace();
43             throw e;
44         }
45         if (ret==null) throw new NullPointerException("CharParser returned null");
46         return ret;
47     }
48
49     public static void main(String[] argv) throws Throwable {
50         if (argv.length != 2) {
51             System.out.println("usage: java -jar wix.jar [-v] <indir> <outdir>");
52             // FIXME: implement this
53             System.out.println("     | java -jar wix.jar [-v] <infile>.wix");
54             System.out.println("");
55             // FIXME: implement these
56             System.out.println("   -v   print text as it is parsed (sbp.verbose=true)");
57             System.out.println("   -vv  like -v, but also dump parse tree");
58             System.out.println("   -vvv like -vv, but also dump wix tree");
59             System.exit(-1);
60             return;
61         }
62         File indir = new File(argv[0]);
63         File outdir = new File(argv[1]);
64         process(indir, "", outdir);
65     }
66
67     private static void process(File indir, String suffix, File outdir) throws Throwable {
68         File f = new File(indir.getAbsolutePath()+suffix);
69         if (!f.exists()) return;
70         if (f.isDirectory()) {
71             for (String s : f.list())
72                 process(indir, suffix + File.separatorChar + s, outdir);
73             return;
74         }
75         if (f.getPath().endsWith(".wix")) {
76             System.out.println();
77             String out = "== " + suffix + " ";
78             while(out.length() < 75) out+="=";
79             System.out.println(ANSI.yellow(out));
80             Class.forName("Main").
81                 getMethod("main", new Class[] { String[].class }).
82                 invoke(null, new Object[] { new String[] { f.getAbsolutePath() } });
83             String outPath = outdir.getAbsolutePath()+suffix;
84             outPath = outPath.substring(0, outPath.length()-".wix".length())+".html";
85             new File(new File(outPath).getParent()).mkdirs();
86             PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+")));
87             pw.println(ret);
88             pw.flush();
89             pw.close();
90             File dest = new File(outPath);
91             if (dest.exists()) {
92                 try {
93                     Process p = Runtime.getRuntime().exec(new String[] {
94                             "diff",
95                             "-Bub",
96                             dest.getAbsolutePath(),
97                             new File(outPath+"+").getAbsolutePath()
98                         });
99                     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
100                     br.readLine();
101                     br.readLine();
102                     for(String s = br.readLine(); s != null; s = br.readLine()) {
103                         if      (s.startsWith("+")) System.out.println(ANSI.green(s));
104                         else if (s.startsWith("-")) System.out.println(ANSI.red(s));
105                         /*else System.out.println(ANSI.blue(s));*/
106                     }
107                     p.waitFor();
108                 } catch (Exception e) {
109                     e.printStackTrace();
110                 }
111             }
112             new File(outPath+"+").renameTo(dest);
113         }
114     }
115
116     public static Object ret;
117     public static void putBack(String o) { ret = o; }
118 }