Skip to content

Commit

Permalink
Add supports for custom index fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbaturin committed Jul 20, 2019
1 parent 4e93711 commit da79f2f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/autoindex.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,25 @@ type 'a index_entry = {
excerpt: 'a Soup.node option;
date: 'a Soup.node option;
author: 'a Soup.node option;
custom_fields : (string * Yojson.Safe.t) list
}

let rec get_custom_fields fields soup =
let inner_html e = Utils.inner_html e |> CCOpt.get_or ~default:"" in
let get_field f soup =
if f.select_all then
`List (Soup.select f.field_selector soup |> Soup.to_list |> List.map (fun e -> `String (inner_html e)))
else let e = Soup.select_one f.field_selector soup in
match e with
| None -> `Null
| Some e -> `String (inner_html e)
in
match fields with
| [] -> []
| f :: fs ->
let field = (f.field_name, get_field f soup) in
field :: (get_custom_fields fs soup)

let get_entry settings url nav_path soup =
{
url = url;
Expand All @@ -19,6 +36,7 @@ let get_entry settings url nav_path soup =
excerpt = Utils.select_any_of settings.index_excerpt_selector soup;
date = Utils.select_any_of settings.index_date_selector soup;
author = Utils.select_any_of settings.index_author_selector soup;
custom_fields = get_custom_fields settings.index_custom_fields soup
}

(** Compares entries by their dates according to these rules:
Expand Down Expand Up @@ -86,6 +104,7 @@ let json_of_entry e =
let fields = List.map (fun (k, v) -> (k, json_of_element v)) fields in
let fields = ("url", `String e.url) :: fields in
let fields = ("nav_path", `List (List.map (fun x -> `String x) e.nav_path)) :: fields in
let fields = List.append fields e.custom_fields in
`Assoc fields

let json_of_entries settings es =
Expand Down
28 changes: 28 additions & 0 deletions src/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let read_config path =
| Toml.Parser.Error (err, _) -> Error (Printf.sprintf "Could not parse config file %s: %s" path err)

let get_table name config = TomlLenses.(get config (key name |-- table))
let get_table_result err name config = get_table name config |> CCOpt.to_result err

let get_string k tbl = TomlLenses.(get tbl (key k |-- string))
let get_string_default default_value k tbl = get_string k tbl |> default default_value
Expand Down Expand Up @@ -92,6 +93,32 @@ let _get_preprocessors config =
| None -> []
| Some pt -> assoc_of_table get_string pt

let _get_index_queries index_table =
let bind = CCResult.(>>=) in
let get_query k queries =
let%m qt = get_table_result "value is not an inline table" k queries in
let%m selector = get_string_result "selector option is missing or value is not a string" "selector" qt in
let select_all = get_bool_default false "select_all" qt in
Ok {field_name = k; field_selector = selector; select_all = select_all}
in
let rec get_queries ks queries acc =
match ks with
| [] -> acc
| k :: ks ->
begin
let q = get_query k queries in
match q with
| Error e ->
let () = Logs.warn @@ fun m -> m "Malformed index field query \"%s\": %s" k e in
get_queries ks queries acc
| Ok q -> get_queries ks queries (q :: acc)
end
in
let qt = get_table "custom_fields" index_table in
match qt with
| None -> []
| Some qt -> get_queries (list_config_keys qt) qt []

let _get_index_settings settings config =
let st = get_table Defaults.index_settings_table config in
match st with
Expand All @@ -108,6 +135,7 @@ let _get_index_settings settings config =
index_date_format = get_string_default settings.index_date_format "index_date_format" st;
index_item_template = get_string_default settings.index_item_template "index_item_template" st;
index_processor = get_string "index_processor" st;
index_custom_fields = _get_index_queries st;
}

let _update_settings settings config =
Expand Down
9 changes: 9 additions & 0 deletions src/defaults.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
(** global settings that can be updated from config file or command line options *)
type index_field = {
field_name : string;
field_selector : string;
select_all : bool
}

type settings = {
(** show debug information *)
verbose : bool;
Expand Down Expand Up @@ -43,6 +49,7 @@ type settings = {
index_date_format : string;
index_item_template : string;
index_processor : string option;
index_custom_fields : index_field list;

preprocessors : (string * string) list
}
Expand Down Expand Up @@ -81,6 +88,8 @@ let default_settings = {
index_date_format = "%F";
index_item_template = "<div> </div>";
index_processor = None;
index_custom_fields = [];

preprocessors = []
}

Expand Down

0 comments on commit da79f2f

Please sign in to comment.