1 | let ext = ".atom"
|
---|
2 |
|
---|
3 | let esc = Converter.Html.esc
|
---|
4 |
|
---|
5 | let element tag content = "<" ^ tag ^ ">" ^ content ^ "</" ^ tag ^ ">"
|
---|
6 |
|
---|
7 | let opt_element tag_name content =
|
---|
8 | if content <> ""
|
---|
9 | then element tag_name content
|
---|
10 | else ""
|
---|
11 |
|
---|
12 | module P = Parsers.Plain_text.Make (Converter.Html)
|
---|
13 |
|
---|
14 | let id txt = "<id>urn:txtid:" ^ Logarion.(txt.Text.id) ^ "</id>\n"
|
---|
15 | let title text = "<title>" ^ esc text.Logarion.Text.title ^ "</title>\n"
|
---|
16 |
|
---|
17 | let authors text =
|
---|
18 | let u acc addr = acc ^ element "uri" addr in
|
---|
19 | let open Logarion 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 |
|
---|
26 | let updated txt = let open Logarion in
|
---|
27 | "<updated>"^ Date.(txt.Text.date |> listing |> rfc_string) ^"</updated>\n"
|
---|
28 |
|
---|
29 | let htm_entry base_url text =
|
---|
30 | let open Logarion 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 |
|
---|
38 | let gmi_entry base_url text =
|
---|
39 | let open Logarion 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 |
|
---|
47 | let base_url kv protocol = try
|
---|
48 | let locs = Logarion.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 |
|
---|
53 | let indices alternate_type c =
|
---|
54 | let file name = Logarion.File_store.file (Filename.concat c.Conversion.dir name) in
|
---|
55 | let title = try Logarion.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 | ^ Logarion.Date.now () ^ "</updated>\n"
|
---|
68 | ^ List.fold_left (fun acc t -> acc ^ entry base_url t) "" c.texts
|
---|
69 | ^ "</feed>"
|
---|
70 |
|
---|
71 | let converter format = Conversion.{ ext; page = None; indices = Some (indices format) }
|
---|