resolve ambiguity between urls and email addresses
[wix.git] / src / ScalaHelper.java
1 // Copyright 2011 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.util.*;
7 import edu.berkeley.sbp.meta.*;
8 import edu.berkeley.sbp.chr.*;
9 import java.io.*;
10
11 public class ScalaHelper {
12     public static boolean isNull(Object o) { return o==null; }
13
14     private static CharParser parser = null;
15     static {
16         synchronized(ScalaHelper.class) {
17             if (parser == null) {
18                 try {
19                     // FIXME: bundle this into the jarfile
20                     InputStream grammarFile = ScalaHelper.class.getClassLoader().getResourceAsStream("wix.g");
21                     Tree<String> res = new CharParser(GrammarAST.getMetaGrammar())
22                         .parse(grammarFile).expand1();
23                     Union grammar = GrammarAST.buildFromAST(res, "s", new GrammarAST.ImportResolver() {
24                             public InputStream getImportStream(String filename) {
25                                 return this.getClass().getClassLoader().getResourceAsStream(filename);
26                             }
27                         });
28                     parser = new CharParser(grammar);
29                 } catch (Exception e) {
30                     throw new RuntimeException(e);
31                 }
32             }
33         }
34     }
35
36     public static Tree parseFile(String targetFile) throws Throwable {
37         Tree ret = null;
38         try {
39             Reader r = new InputStreamReader(new FileInputStream(targetFile));
40             Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
41             ret = parser.parse(input).expand1();
42         } catch (Throwable e) {
43             e.printStackTrace();
44             throw e;
45         }
46         if (ret==null) throw new NullPointerException("CharParser returned null");
47         return ret;
48     }
49
50     public static void main(String[] argv, TreeToString t2s) throws Throwable {
51         if (argv.length == 0) {
52             System.out.println("usage: java -jar wix.jar [-v] [-f] <indir> <outdir>");
53             // FIXME: implement this
54             System.out.println("     | java -jar wix.jar [-v] [-f] <infile>.wix");
55             System.out.println("");
56             // FIXME: implement these
57             System.out.println("   -v   print text as it is parsed (sbp.verbose=true)");
58             System.out.println("   -vv  like -v, but also dump parse tree");
59             System.out.println("   -vvv like -vv, but also dump wix tree");
60             System.out.println("   -f   recompile documents regardless of last-modified-time");
61             System.exit(-1);
62             return;
63         }
64         int i = 0;
65         for(;; i++) {
66             if (argv[i].equals("-v")) { verbose = true; continue; }
67             if (argv[i].equals("-f")) { force = true; continue; }
68             break;
69         }
70         File indir = new File(argv[i]);
71         File outdir = new File(argv[i+1]);
72         if (!indir.isDirectory()) {
73             process(new File(indir.getParent()), indir.getName(), outdir, t2s);
74         } else {
75             process(indir, "", outdir, t2s);
76         }
77     }
78
79     private static boolean verbose = false;
80     private static boolean force   = false;
81
82     private static void process(File indir, String suffix, File outdir, TreeToString t2s) throws Throwable {
83         File f = new File(indir.getAbsolutePath()+File.separatorChar+suffix);
84         //System.out.println(f+" "+indir + " " + suffix + " " + outdir);
85         if (!f.exists()) return;
86         if (f.isDirectory()) {
87             for (String s : f.list())
88                 process(indir, suffix + File.separatorChar + s, outdir, t2s);
89             return;
90         }
91         if (!f.getPath().endsWith(".wix")) {
92             boolean skip = false;
93             if (f.getName().equals(".DS_Store")) skip = true;
94             if (f.getName().equals("._.DS_Store")) skip = true;
95             if (f.getName().endsWith("-")) skip = true;
96             if (!skip) {
97                 File dest = new File(outdir.getAbsolutePath()+File.separatorChar+suffix);
98                 if (dest.exists() && dest.lastModified()==f.lastModified() && dest.length()==f.length()) {
99                     System.out.println(ANSI.yellow("no change: "+f.getPath()));
100                     return;
101                 }
102                 System.out.println(ANSI.green("copying: "+f.getPath()));
103                 File dest_ = new File(outdir.getAbsolutePath()+File.separatorChar+suffix+"-");
104                 new File(dest_.getParent()).mkdirs();
105                 FileOutputStream fos = new FileOutputStream(dest_);
106                 FileInputStream fis = new FileInputStream(f);
107                 byte[] buf = new byte[1024];
108                 while(true) {
109                     int numread = fis.read(buf, 0, buf.length);
110                     if (numread==-1) break;
111                     fos.write(buf, 0, numread);
112                 }
113                 fos.close();
114                 fis.close();
115                 dest_.renameTo(dest);
116                 dest.setLastModified(f.lastModified());
117             }
118         } else {
119             String out = "== " + suffix + " ";
120             while(out.length() < 75) out+="=";
121             System.out.println(ANSI.yellow(out));
122             //System.out.println();
123             String outPath = outdir.getAbsolutePath()+File.separatorChar+suffix;
124             outPath = outPath.substring(0, outPath.length()-".wix".length())+".html";
125             if (new File(outPath).exists() && new File(outPath).lastModified() > f.lastModified() && !force) return;
126
127             Tree tree = parseFile(f.getAbsolutePath());
128             String ret = t2s.run(tree);
129             try {
130                 new File(new File(outPath).getParent()).mkdirs();
131                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+")));
132                 pw.println(ret);
133                 pw.flush();
134                 pw.close();
135                 File dest = new File(outPath);
136                 if (dest.exists()) {
137                     Process p = Runtime.getRuntime().exec(new String[] {
138                             "diff",
139                             "-Bub",
140                             dest.getAbsolutePath(),
141                             new File(outPath+"+").getAbsolutePath()
142                         });
143                     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
144                     br.readLine();
145                     br.readLine();
146                     for(String s = br.readLine(); s != null; s = br.readLine()) {
147                         if      (s.startsWith("+")) System.out.println(ANSI.green(s));
148                         else if (s.startsWith("-")) System.out.println(ANSI.red(s));
149                         /*else System.out.println(ANSI.blue(s));*/
150                     }
151                     p.waitFor();
152                 }
153                 new File(outPath+"+").renameTo(dest);
154                 if (dest.lastModified() <= f.lastModified())
155                     dest.setLastModified(f.lastModified()+1);
156             } catch (Exception e) {
157                 e.printStackTrace();
158             }
159         }
160     }
161
162     public static Object ret;
163     public static void putBack(String o) { ret = o; }
164 }