source: code/trunk/lib/text.ml@ 38

Last change on this file since 38 was 38, checked in by fox, 2 years ago

Support References field

File size: 3.8 KB
Line 
1module String_map = Map.Make (String)
2type t = {
3 id: Id.t;
4 title: string;
5 authors: Person.Set.t;
6 date: Date.t;
7 string_map: string String_map.t;
8 stringset_map: String_set.t String_map.t;
9 body: string;
10 }
11
12let blank ?(id=(Id.generate ())) () = {
13 id;
14 title = "";
15 authors = Person.Set.empty;
16 date = Date.({ created = now (); edited = ""});
17 string_map = String_map.empty;
18 stringset_map = String_map.empty;
19 body = "";
20 }
21
22let compare = Stdlib.compare
23let newest a b = Date.(compare a.date b.date)
24let oldest a b = Date.(compare b.date a.date)
25let str key m = try String_map.find (String.lowercase_ascii key) m.string_map with Not_found -> ""
26let set key m = try String_map.find (String.lowercase_ascii key) m.stringset_map with Not_found -> String_set.empty
27let str_set key m = String_set.to_string @@ set key m
28let with_str_set m key str = { m with
29 stringset_map = String_map.add (String.lowercase_ascii key) (String_set.of_string str) m.stringset_map
30 }
31
32let with_kv x (k,v) =
33 let trim = String.trim in
34 match String.lowercase_ascii k with
35 | "body" -> { x with body = String.trim v }
36 | "title"-> { x with title = trim v }
37 | "id" -> (match v with "" -> x | s -> { x with id = s })
38 | "author"
39 | "authors" -> { x with authors = Person.Set.of_string (trim v)}
40 | "date" -> { x with date = Date.{ x.date with created = Date.of_string v }}
41 | "date-edited"-> { x with date = Date.{ x.date with edited = Date.of_string v }}
42 | "licences" | "topics" | "keywords" | "references" | "series" as k -> with_str_set x k v
43 | k -> { x with string_map = String_map.add k (trim v) x.string_map }
44
45let kv_of_string line = match Str.(bounded_split (regexp ": *")) line 2 with
46 | [ key; value ] -> Str.(replace_first (regexp "^#\\+") "" key), value
47 | [ key ] -> Str.(replace_first (regexp "^#\\+") "" key), ""
48 | _ -> "",""
49
50let of_header front_matter =
51 let fields = List.map kv_of_string (Str.(split (regexp "\n")) front_matter) in
52 List.fold_left with_kv (blank ~id:Id.nil ()) fields
53
54let front_matter_body_split s =
55 if Str.(string_match (regexp ".*:.*")) s 0
56 then match Str.(bounded_split (regexp "^$")) s 2 with
57 | front::body::[] -> (front, body)
58 | _ -> ("", s)
59 else ("", s)
60
61let of_string s =
62 let front_matter, body = front_matter_body_split s in
63 try
64 let note = { (of_header front_matter) with body } in
65 if note.id <> Id.nil then Ok note else Error "Missing ID header"
66 with _ -> Error ("Failed parsing" ^ s)
67
68let to_string x =
69 let has_len v = String.length v > 0 in
70 let s field value = if has_len value then field ^ ": " ^ value ^ "\n" else "" in
71 let a value = if Person.Set.is_empty value then "" else "Authors: " ^ Person.Set.to_string value ^ "\n" in
72 let d field value = match value with "" -> "" | s -> field ^ ": " ^ Date.rfc_string s ^ "\n" in
73 let rows = [
74 s "ID" x.id;
75 d "Date" x.date.Date.created;
76 d "Edited" x.date.Date.edited;
77 s "Title" x.title;
78 a x.authors;
79 s "Licences" (str_set "licences" x);
80 s "Topics" (str_set "topics" x);
81 s "Keywords" (str_set "keywords" x);
82 s "References"(str_set "references" x);
83 s "Series" (str_set "series" x);
84 s "Abstract" (str "abstract" x);
85 s "Alias" (str "Alias" x)
86 ] in
87 String.concat "" rows ^ "\n" ^ x.body
88
89let string_alias t =
90 let is_reserved = function
91 | '!' | '*' | '\'' | '(' | ')' | ';' | ':' | '@' | '&' | '=' | '+' | '$'
92 | ',' | '/' | '?' | '#' | '[' | ']' | ' ' | '\t' | '\x00' -> true
93 | _ -> false
94 in
95 let b = Buffer.create (String.length t) in
96 let filter char =
97 let open Buffer in
98 if is_reserved char then (try (if nth b (pred (length b)) <> '-' then add_char b '-') with Invalid_argument _ -> prerr_endline "reserved")
99 else add_char b char
100 in
101 String.(iter filter (lowercase_ascii t));
102 Buffer.contents b
103
104let alias t = match str "alias" t with "" -> string_alias t.title | x -> x
105let short_id t = Id.short t.id
Note: See TracBrowser for help on using the repository browser.