source: code/trunk/cmd/txt/atom.ml@ 75

Last change on this file since 75 was 73, checked in by Izuru Yakumo, 8 months ago

Because sweet girls are the best, officially rebranding Logarion to Kosuzu

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 2.9 KB
Line 
1let ext = ".atom"
2
3let esc = Converter.Html.esc
4
5let element tag content = "<" ^ tag ^ ">" ^ content ^ "</" ^ tag ^ ">"
6
7let opt_element tag_name content =
8 if content <> ""
9 then element tag_name content
10 else ""
11
12module P = Parsers.Plain_text.Make (Converter.Html)
13
14let id txt = "<id>urn:txtid:" ^ Kosuzu.(txt.Text.id) ^ "</id>\n"
15let title text = "<title>" ^ esc text.Kosuzu.Text.title ^ "</title>\n"
16
17let authors text =
18 let u acc addr = acc ^ element "uri" addr in
19 let open Kosuzu in
20 let fn txt a =
21 a ^ "<author>" ^ (opt_element "name" @@ esc txt.Person.name)
22 ^ (List.fold_left u "" txt.Person.addresses)
23 ^ "</author>\n" in
24 Person.Set.fold fn text.Text.authors ""
25
26let updated txt = let open Kosuzu in
27 "<updated>"^ Date.(txt.Text.date |> listing |> rfc_string) ^"</updated>\n"
28
29let htm_entry base_url text =
30 let open Kosuzu in
31 let u = Text.short_id text in
32 "<entry>\n<link rel=\"alternate\" href=\"" ^ base_url ^ "/" ^ u ^ ".htm\" />\n"
33 ^ title text ^ id text ^ updated text ^ authors text
34 ^ (opt_element "summary" @@ esc @@ Text.str "abstract" text)
35 ^ String_set.fold (fun elt a -> a ^ "<category term=\"" ^ esc elt ^ "\"/>\n") (Text.set "topics" text) ""
36 ^ "</entry>\n"
37
38let gmi_entry base_url text =
39 let open Kosuzu in
40 let u = Text.short_id text in
41 "<entry>\n<link rel=\"alternate\" href=\"" ^ base_url ^ "/" ^ u ^ ".gmi\" />\n"
42 ^ title text ^ id text ^ updated text ^ authors text
43 ^ (opt_element "summary" @@ esc @@ Text.str "abstract" text)
44 ^ String_set.fold (fun elt a -> a ^ "<category term=\"" ^ elt ^ "\"/>\n") (Text.set "topics" text) ""
45 ^ "</entry>\n"
46
47let base_url kv protocol = try
48 let locs = Kosuzu.Store.KV.find "Locations" kv in
49 let _i = Str.(search_forward (regexp (protocol ^ "://[^;]*")) locs 0) in
50 Str.(matched_string locs)
51 with Not_found -> Printf.eprintf "Missing location for %s, add it to txt.conf\n" protocol; ""
52
53let indices alternate_type c =
54 let file name = Kosuzu.File_store.file (Filename.concat c.Conversion.dir name) in
55 let title = try Kosuzu.Store.KV.find "Title" c.Conversion.kv with Not_found -> "" in
56 let entry, fname, protocol_regexp = match alternate_type with
57 | "text/gemini" -> gmi_entry, "gmi.atom", "gemini"
58 | "text/html" | _ -> htm_entry, "feed.atom", "https?"
59 in
60 let base_url = base_url c.kv protocol_regexp in
61 let self = Filename.concat base_url fname in
62 file fname @@ (*TODO: alternate & self per url*)
63 {|<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:base="|} ^ base_url ^ {|"><title>|}
64 ^ title ^ {|</title><link rel="alternate" type="|} ^ alternate_type ^ {|" href="|}
65 ^ base_url ^ {|/" /><link rel="self" type="application/atom+xml" href="|}
66 ^ self ^ {|" /><id>urn:txtid:|} ^ c.Conversion.id ^ "</id><updated>"
67 ^ Kosuzu.Date.now () ^ "</updated>\n"
68 ^ List.fold_left (fun acc t -> acc ^ entry base_url t) "" c.texts
69 ^ "</feed>"
70
71let converter format = Conversion.{ ext; page = None; indices = Some (indices format) }
Note: See TracBrowser for help on using the repository browser.