let no_pp = ref false let do_stdout = ref false let process instream outstream = let lexbuf = Lexing.from_channel instream in let result = Parser.html Lexer.token lexbuf in let treelist = Mhtml.build result in let included = Mhtml.expand_includes treelist in let extracted = Mhtml.extract_macros included in let expanded = Mhtml.expand_macros extracted in let shelled = Mhtml.do_shell_tag expanded in let shelled = Mhtml.expand_macros shelled in output_string outstream (if !no_pp then (Mhtml.raw_string shelled) else (Mhtml.pp_string shelled)) let parse_args () = let files = ref [] in let i = "-I", Arg.String Mhtml.add_incdir, "Add a directory to search during includes (not functional)" and p = "-p", Arg.Set no_pp, "Do not pretty print. Good for space-sensitive html." and s = "-s", Arg.Set do_stdout, "Use stdout." and usage = "Usage: mhtml [options] file.mhtml [file.mhtml ...]\n\n\ Does a macro expansion on given files.\n\ Outputs file.html [file.html ...]\n" and anon_arg arg = files := arg :: !files in let args = [i; p; s] in Arg.parse args anon_arg usage; List.rev !files let prepair filename = let newfile = ref "" in if Pcre.pmatch ~pat:"\\.mhtml" filename then newfile := Pcre.qreplace ~pat:"\\.mhtml" ~templ:".html" filename else newfile := filename ^ ".mhtml"; if not !do_stdout then print_string ("Converting " ^ filename ^ " -> " ^ !newfile ^ "\n") else print_string ("content-type: text/html\n\n"); try let infile = open_in filename in let outfile = if !do_stdout then stdout else open_out !newfile in if (input_char infile = '#') && (input_char infile = '!') then ignore (input_line infile) else seek_in infile 0; Node.curFile := filename; process infile outfile; close_in infile; close_out outfile with Sys_error(s) -> print_string s; print_newline() let _ = let files = parse_args () in if files = [] then process stdin stdout else List.iter prepair files (* End of main.ml *)