(* COCANWIKI scripts. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: cocanwiki_template.ml,v 1.3 2004/09/08 15:46:52 rich Exp $ * * This module wraps around the Template library. It provides caching * of templates and fills in standard fields on a host-specific basis. * *) open Unix open Cocanwiki_files let base = let base = try Sys.getenv "COCANWIKI_TEMPLATES" with Not_found -> "/usr/share/cocanwiki/templates" in let is_dir path = try (Unix.stat path).Unix.st_kind = Unix.S_DIR with Unix.Unix_error _ -> false in if not (is_dir base) then failwith ("environment variable $COCANWIKI_TEMPLATES " ^ "must be set to point to my 'templates' directory " ^ "(see README file for more details)"); base (* Cache of precompiled templates, arranged by full path. *) let cache = Hashtbl.create 32 let _get_template filename = let path = base // filename in let stat = Unix.stat path in let mtime = stat.st_mtime in try let template, old_mtime = Hashtbl.find cache path in if old_mtime < mtime then ( (* The template has changed on disk since it was compiled. Reload. *) let template = Template.template path in Hashtbl.replace cache path (template, mtime); template ) else template with Not_found -> (* Template not seen before, so load it. *) let template = Template.template path in Hashtbl.replace cache path (template, mtime); template let get_template (dbh : Dbi.connection) hostid filename = let template = _get_template filename in (* Get standard fields concerning this host from the database. *) (* XXX *) template