package stog_extern

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file stog_extern.ml

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(*********************************************************************************)
(*                Stog                                                           *)
(*                                                                               *)
(*    Copyright (C) 2012-2024 INRIA All rights reserved.                         *)
(*    Author: Maxence Guesdon, INRIA Saclay                                      *)
(*                                                                               *)
(*    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, version 3 of the License.       *)
(*                                                                               *)
(*    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, write to the Free Software        *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    As a special exception, you have permission to link this program           *)
(*    with the OCaml compiler and distribute executables, as long as you         *)
(*    follow the requirements of the GNU GPL in regard to all of the             *)
(*    software in the executable aside from the OCaml compiler.                  *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** *)

open Stog.Types;;

module XR = Xtmpl.Rewrite
module Xml = Xtmpl.Xml

let module_name = "extern";;
let rc_file stog = Stog.Plug.plugin_config_file stog module_name;;

module W = Ocf.Wrapper
type action =
  {
    types : string list
        [@ocf W.list W.string, []]
        [@ocf.doc "list of document types concerned"] ;
    name : string
        [@ocf W.string, "cat" ]
        [@ocf.doc "name of run level in which to apply the command"] ;
    command : string
        [@ocf W.string, "cat"]
        [@ocf.doc "command, taking XML document in input and outputting the new XML document"] ;
  } [@@ocf]

type data =
    { actions : action list
      [@ocf W.list action_wrapper, []] ;
    } [@@ocf]

let group data =
  let option_t = Ocf.option data_wrapper data in
  let g = Ocf.as_group option_t in
  (g, option_t)

let load_config stog =
  let (group, t) = group default_data in
  let rc_file = rc_file stog in
  if not (Sys.file_exists rc_file) then Ocf.to_file group rc_file ;
  try
    Ocf.from_file group rc_file;
    Ocf.get t
  with
  | Ocf.Error e -> failwith (Ocf.string_of_error e)
;;

let apply_to_doc types command stog doc_id =
  let doc = Stog.Types.doc stog doc_id in
  match List.mem doc.doc_type types with
    false -> None
  | true ->
      let in_file = Filename.temp_file "stog" ".xml.in" in
      let out_file = (Filename.chop_extension in_file) ^ ".out" in
      try
        let xml =
          match doc.doc_out with
          | None -> doc.doc_body
          | Some xml -> xml
        in
        Stog_base.Misc.file_of_string ~file: in_file (XR.to_string xml) ;
        let com = Printf.sprintf "cat %s | %s > %s"
          (Filename.quote in_file)
            command
            (Filename.quote out_file)
        in
        let rm () =
          try Sys.remove in_file with _ -> ();
              try Sys.remove out_file with _ -> ()
        in
        match Sys.command com with
          0 ->
            let xmldoc = XR.doc_from_file out_file in
            let xml = xmldoc.XR.elements in
            rm ();
            let doc = { doc with doc_out = Some xml } in
            Some (doc_id, doc)
        | n ->
        failwith (Printf.sprintf "Command exited with %d: %s" n com)
      with
        e ->
          let msg =
            match e with
            | Failure msg | Sys_error msg -> msg
            | _ -> Printexc.to_string e
          in
          Stog.Log.err (fun m -> m "Doc %S: %s"
             (Stog.Path.to_string doc.doc_path) msg);
          None

(** FIXME: parallelize this when we'll use lwt everywhere *)
let apply types command env stog docs =
  let docs = List.map
    (apply_to_doc types command stog)
      (Stog.Types.Doc_set.elements docs)
  in
  List.fold_left
    (fun stog -> function
       | None -> stog
       | Some (doc_id, doc) -> Stog.Types.set_doc stog doc_id doc)
    stog docs

let level_fun_of_action a =
  (a.name, Stog.Engine.Fun_stog (apply a.types a.command))

let level_funs stog =
  let config = load_config stog in
  List.map level_fun_of_action config.actions
;;

let default_levels = Stog.Types.Str_map.empty

let make_module stog ?levels () =
  let level_funs = level_funs stog in
  let levels = Stog.Html.mk_levels
    module_name level_funs default_levels ?levels ()
  in
  let module M =
  struct
    type data = unit
    let modul = {
        Stog.Engine.mod_name = module_name ;
        mod_levels = levels ;
        mod_data = ()
       }

    type cache_data = unit
    let cache_load _stog data doc t = data
    let cache_store _stog data doc = ()
  end
  in
  (module M : Stog.Engine.Module)
;;

let f stog =
  let levels =
    try Some (Stog.Types.Str_map.find module_name stog.Stog.Types.stog_levels)
    with Not_found -> None
  in
  make_module stog ?levels ()
;;

let () = Stog.Engine.register_module module_name f;;
OCaml

Innovation. Community. Security.