File: plugin.ml

package info (click to toggle)
caml2html 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 300 kB
  • sloc: ml: 1,996; makefile: 162
file content (69 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(* $Id$ *)

open Printf

type handler =
    [ `Command of string
    | `Function of (string -> string option) ]

let plugins = Hashtbl.create 20

let add = Hashtbl.replace plugins
let remove = Hashtbl.remove plugins
let exists = Hashtbl.mem plugins
let find = Hashtbl.find plugins


let count_newlines s = 
  let n = ref 0 in
  String.iter (
    function
	'\n' -> incr n
      | _ -> ()
  ) s;
  !n

let expand name s =
  let h =
    try find name
    with Not_found ->
      failwith (sprintf "Plugin %s doesn't exist." name)
  in
  match h with
      `Function f -> f s
    | `Command cmd ->
	let p = Unix.open_process cmd in
	let ic, oc = p in
	output_string oc s;
	close_out oc;
	let buf = Buffer.create 1024 in
	try
	  while true do
	    Buffer.add_string buf (input_line ic);
	    Buffer.add_char buf '\n'
	  done;
	  assert false
	with End_of_file ->
	  match Unix.close_process p with
	      Unix.WEXITED 0 -> Some (Buffer.contents buf)
	    | _ -> None

	    
let html_handler =
  `Function (fun s -> Some s)

let _ = 
  add "html" html_handler


let register_command s =
  try
    let i = String.index s ':' in
    let name = String.sub s 0 i in
    let cmd = String.sub s (i+1) (String.length s - i - 1) in
    if name = "" || cmd = "" then
      raise Not_found
    else
      add name (`Command cmd)
  with Not_found ->
    failwith (sprintf "Cannot register %S: wrong syntax" s)