source: code/trunk/lib/header_pack.ml@ 2

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

Samhain 21

Converter

  • type selection
  • subdir conversion
  • htm extension

Gemini

  • index.gmi
  • topics and latest
  • gmi.atom feed

Add pull (http(s)) operation

  • peers.pub.conf and peers.priv.conf

HTML5 format & fixes by Novaburst
Phony target (thanks Gergely)

May

Basic unit renamed from Note to Text.
New modular text-parser, internal to Logarion, for generic notation parsing. The default input format is now a much plainer text.
Logarion created texts have part of the UUID in filename.
Logarion's index re-written in Messagepack format. Removed indices command. They are generated during convert.

File size: 3.4 KB
Line 
1type info_t = { version: int; name: string; archivists: string list }
2type text_t = { id: Msgpck.t; time: Msgpck.t; title: Msgpck.t; authors: Msgpck.t }
3type t = { info: info_t; fields: string list; texts: Msgpck.t; peers: Msgpck.t }
4
5let of_id id = Msgpck.Bytes (Id.to_bytes id)
6let to_id pck_id = Id.of_bytes Msgpck.(to_bytes pck_id)
7
8let person p = Msgpck.String (Person.to_string p)
9let persons ps = List.rev @@ Person.Set.fold (fun p a -> person p :: a) ps []
10
11let of_set field t =
12 List.rev @@ String_set.fold (fun s a -> Msgpck.String s :: a) (Text.set field t) []
13
14let date = function
15 | None -> Int32.zero
16 | Some date ->
17 let days, ps = Ptime.Span.to_d_ps (Ptime.to_span date) in
18 Int32.add Int32.(mul (of_int days) 86400l) Int64.(to_int32 (div ps 1000000000000L))
19
20let to_sec = function
21 Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
22
23let public_peers () =
24 Peers.fold_file (fun x a -> Msgpck.String x :: a) [] Peers.public_fname
25
26let fields = Msgpck.(List [String "id"; String "time"; String "title"; String "authors"; String "topics"])
27let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
28
29let to_pack a t =
30 let open Text in
31 Msgpck.(List [
32 Bytes (Id.to_bytes t.uuid); of_uint32 (date (Date.listing t.date));
33 String t.title; List (persons t.authors); List (of_set "topics" t)
34 ]) :: a
35
36let pack_filename ?(filename="index.pck") archive =
37 let dir = Store.KV.find "Export-Dir" archive.File_store.kv in (*raises Not_found*)
38 dir ^ "/" ^ filename
39
40let to_info = function
41 | Msgpck.List (v::n::a::[]) ->
42 let archivists = List.map Msgpck.to_string (Msgpck.to_list a) in
43 Msgpck.({version = to_int v; name = to_string n; archivists})
44 | _ -> invalid_arg "Pack header"
45
46let unpack = function
47 | Msgpck.List (i::f::texts::[]) ->
48 Some { info = to_info i; fields = to_fields f; texts; peers = Msgpck.List [] }
49 | Msgpck.List (i::f::texts::peers::[]) ->
50 Some { info = to_info i; fields = to_fields f; texts; peers }
51 | _ -> None
52
53let list filename = try
54 let texts_list = function
55 | Msgpck.List (_info :: _fields :: [texts]) -> Msgpck.to_list texts
56 | _ -> prerr_endline "malformed feed"; [] in
57 let _pos, data = Msgpck.StringBuf.read @@ File_store.to_string filename in
58 Ok (texts_list data)
59 with Not_found -> Error "unspecified export dir"
60
61let contains text = function
62 | Msgpck.List (id::_time::title::_authors::_topics::[]) ->
63 (match Id.of_bytes (Msgpck.to_bytes id) with
64 | None -> prerr_endline ("Invalid id for " ^ Msgpck.to_string title); false
65 | Some id -> text.Text.uuid = id)
66 | _ -> prerr_endline ("Invalid record pattern"); false
67
68let pack archive records =
69 let header_pack = List.fold_left to_pack [] records in
70 let info = Msgpck.(List [Int 0; String archive.File_store.name; List (persons archive.archivists)]) in
71 Bytes.to_string @@ Msgpck.Bytes.to_string
72 (List [info; fields; Msgpck.List header_pack; Msgpck.List (public_peers ())])
73
74let add archive records =
75 let fname = pack_filename archive in
76 let append published (t, _f) = if List.exists (contains t) published then published else to_pack published t in
77 match list fname with Error e -> prerr_endline e | Ok published_list ->
78 let header_pack = List.fold_left append published_list records in
79 let archive = Msgpck.(List [Int 0; String archive.File_store.name;
80 List (persons archive.archivists)]) in
81 File_store.file fname @@ Bytes.to_string
82 @@ Msgpck.Bytes.to_string (List [archive; fields; Msgpck.List header_pack])
83
84let unpublish _archive _records = ()
Note: See TracBrowser for help on using the repository browser.