Revised core to avoid use of Batteries

This commit is contained in:
Stavros Polymenis 2017-11-15 00:06:53 +00:00
parent 1fff19a18c
commit aacc0daa99
2 changed files with 15 additions and 18 deletions

View File

@ -74,7 +74,8 @@ module Make (Store : Store.T) = struct
let latest_entry archive fragment =
let notes = Store.to_list ~order:recency_order meta_lens archive.store in
try Some (List.find (fun e -> BatString.exists (e.Meta.title) fragment) notes)
let containing_fragment e = Re_str.(string_match (regexp fragment)) (e.Meta.title) 0 in
try Some (List.find containing_fragment notes)
with Not_found -> None
let note_with_id archive id = Store.note_with_id archive.store id
@ -82,6 +83,7 @@ module Make (Store : Store.T) = struct
let with_note archive note = Store.with_note archive.store note
let sublist ~from ~n list = BatList.(take n (drop from list))
let sublist ~from ~n list =
List.fold_left (fun (i, elms) e -> (succ i, if i >= from && i <= n then e::elms else elms))
end

View File

@ -27,27 +27,22 @@ let meta_pair_of_string line =
else (Re_str.(replace_first (regexp "^[ -] ") "" line), "")
let meta_of_string front_matter =
let fields = List.map meta_pair_of_string (BatString.nsplit front_matter "\n") in
let fields = List.map meta_pair_of_string (String.split_on_char '\n' front_matter) in
List.fold_left Meta.with_kv (Meta.blank ()) fields
exception Syntax_error of string
let front_matter_body_split s =
if BatString.starts_with s "---"
then let l = Re_str.(bounded_split (regexp "^---$")) s 2 in List.(nth l 0, nth l 1)
else (
let has_meta =
let tokens a c =
if a = None
then match c with ':' -> Some true | '\n' | ' ' -> Some false | _ -> None
else a
in
match BatString.fold_left tokens None s with Some true -> true | _ -> false
in
if has_meta
then BatString.split s "\n\n"
else ("", s)
)
match String.sub s 0 3 with
| "---" ->
let l = Re_str.(bounded_split (regexp "^---$")) s 2 in
List.(nth l 0, nth l 1)
| _ ->
if Re_str.(string_match (regexp ".*:.*")) s 0
then match Re_str.(bounded_split (regexp "^\n\n")) s 2 with
| front::body::[] -> (front, body)
| _ -> ("", s)
else ("", s)
let of_string s =
let (front_matter, body) = front_matter_body_split s in