+ (* Do macro expansion before anything else, because macros could
+ * contain <html> sections, etc.
+ *)
+ let is_macro line =
+ try
+ let subs = Pcre.exec ~rex:macro_re line in
+ let name = Pcre.get_substring subs 1 in
+ let rows = PGSQL(dbh) "select 1 from macros
+ where hostid = $hostid and name = $name" in
+ (match rows with
+ | [] -> false (* Not an actual macro name from the database. *)
+ | [_] -> true (* Is an actual macro name. *)
+ | _ -> assert false (* Uniqueness should stop this from happening. *)
+ )
+ with
+ Not_found -> false
+ in
+ let expand_macro line =
+ try
+ let subs = Pcre.exec ~rex:macro_re line in
+ let name = Pcre.get_substring subs 1 in
+ let content =
+ List.hd (
+ PGSQL(dbh) "select content from macros
+ where hostid = $hostid and name = $name"
+ ) in
+ (* Split the content into lines of text. *)
+ let lines = Pcre.split ~rex:split_lines_re content in
+ lines
+ with
+ (Not_found | Failure "hd" | ExtList.List.Empty_list) as exn ->
+ failwith ("Wikilib: expand_macro: you should never see this: " ^
+ Printexc.to_string exn)
+ in
+ let rec loop = function
+ | [] -> []
+ | line :: xs when is_macro line -> expand_macro line @ loop xs
+ | x :: xs -> x :: loop xs
+ in
+ let lines = loop lines in
+