package stog

  1. Overview
  2. Docs

Source file config.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
(*********************************************************************************)
(*                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                                          *)
(*                                                                               *)
(*********************************************************************************)

(** *)

let debug = ref false

let str_int_list_wrapper =
  Ocf.Wrapper.(list (pair string (list int)))

type module_levels = {
  module_name : string  [@ocf Ocf.Wrapper.string, ""][@ocf.label "module"];
  levels : (string * int list) list [@ocf str_int_list_wrapper, []];
  } [@@ocf]

type t =
  { ignored : string list
      [@ocf Ocf.Wrapper.list Ocf.Wrapper.string,
            [".*\\.git" ; ".*Makefile" ; ".*tmpl$" ; ".*/stog-output"]
      ]
      [@ocf.doc "Regexps of files to ignore"] ;
    (** list of regexps of filenames to ignore *)

    documents : string list
       [@ocf Ocf.Wrapper.list Ocf.Wrapper.string,
         [ ".*\\.xml$" ; ".*\\.html$" ]
       ]
       [@ocf.doc "Regexps of files containing documents"] ;
    (** list of regexps for document files *)

    not_documents : string list
       [@ocf Ocf.Wrapper.list Ocf.Wrapper.string,
             [ ]
        ]
        [@ocf.doc "Regexps of files matching 'documents' regexps but not containing documents"] ;
    (** list of regexps for file matching documents rules but not being  documents *)

    follow_symlinks : bool
       [@ocf Ocf.Wrapper.bool, false]
       [@ocf.doc "follow symlinks when bulding source file and directory tree"];

    levels : module_levels list
      [@ocf Ocf.Wrapper.list module_levels_wrapper, []]
      [@ocf.doc  "Run levels of modules, for example:
      [ { module: \"html\", levels: [ (\"base\", [ 0 ; 61 ]) ] ) ]} " ];
  } [@@ocf]
;;

let config_dir dir = Filename.concat dir ".stog";;
let config_file dir = Filename.concat (config_dir dir) "config";;
let tmpl_dir dir = Filename.concat (config_dir dir) "templates";;
let cache_dir dir = Filename.concat (config_dir dir) "cache";;
let modules_dir dir = Filename.concat (config_dir dir) "modules";;

let group () =
  let option_t = Ocf.option t_wrapper default_t in
  let g = Ocf.as_group option_t in
  (g, option_t)

let read_config dir =
  let (group, t) = group () in
  let cfg_dir = config_dir dir in
  let cfg_file = config_file dir in
  if not (Sys.file_exists cfg_dir) then
    begin
      Log.warn
        (fun m -> m "Creating non-existent configuration directory %S" cfg_dir);
      Stog_base.Misc.safe_mkdir cfg_dir;
      Ocf.to_file group cfg_file ;
      Ocf.get t
    end
  else
    try
      Ocf.from_file group cfg_file;
      Ocf.get t
    with
      Ocf.Error e -> failwith (Ocf.string_of_error e)
;;