(* Ocaml - syntax hilighting in LaTeX Copyright (C) 2009 BODIN Antoine This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *) let parse f_in f_out = let operatortolatex k = match k with '*' | '+' | '-' | '/' | '.' -> "{\\color{Yellow}"^(Char.escaped k)^"}" | '=' | '<' | '>' -> "{\\color{Blue}"^(Char.escaped k)^"}" | '(' | ')' -> "{\\color{Green}"^(Char.escaped k)^"}" | '|' -> "{\\color{Red}"^(Char.escaped k)^"}" | ':' -> (Char.escaped k) | _ -> (Char.escaped k) in let keywordtolatex k = match k with "let" | "rec" -> "{\\color{Blue}"^k^"}" | "function" | "fun" | "when" -> "{\\color{Red}"^k^"}" | "try" | "match" | "with" -> k | "for" | "while" | "do" | "done" -> k | "if" | "then" | "else" -> k | _ -> print_endline ("["^k^"]"); k in let codehighlight n = output_string f_out ("\\texttt{\n\\begin{tabular}{ c | l }\n1 & ") ; let i = ref 1 in try while true do match input_char f_in with ' ' -> output_string f_out "~" | '\n'-> output_string f_out ("\\\\ \n"^(incr i; string_of_int !i)^" & ") | '}' -> failwith "end of code" | c -> let rec f () = match input_char f_in with ' ' | '\n' -> "" | c -> (operatortolatex c)^f() in output_string f_out ((keywordtolatex ((operatortolatex c)^(f())))^" ") done with _ -> output_string f_out "\\\\ \n \\end{tabular}\n}\n" in let _ = try while true do let line = input_line f_in in try let offset = (Str.search_forward (Str.regexp "\\\\ocaml{") line 0) in output_string f_out ((Str.string_before line offset)^"\\emph{"); seek_in f_in (pos_in f_in - (String.length line - offset) + 6); try while true do match input_char f_in with '}' -> failwith "end of title" | c -> print_char c; output_char f_out c done with _ -> seek_in f_in (pos_in f_in +1); output_string f_out "}\\\\\n"; codehighlight () with Not_found -> output_string f_out (line^"\n") done with End_of_file -> (); in close_in f_in; close_out f_out let _ = match Sys.argv with [|_;a;b|] -> parse (open_in_bin a) (open_out_bin b) | [|_;a|] -> parse (open_in_bin a) (open_out_bin (a^".otex")) | _ -> print_endline "use : ocaml2latex.ml [file_to_parse] ~[output]"