gmi.atom feed

This commit is contained in:
orbifx 2021-06-22 14:52:07 +01:00
parent 90ec1d244c
commit 0f2336e398
2 changed files with 48 additions and 30 deletions

View File

@ -9,29 +9,49 @@ let opt_element tag_name content =
module P = Parsers.Plain_text.Make (Converter.Html)
let entry base_url text =
let id txt = "<id>urn:uuid:" ^ Logarion.(Id.to_string txt.Text.uuid) ^ "</id>"
let title text = "<title>" ^ esc text.Logarion.Text.title ^ "</title>"
let authors text =
let u acc addr = acc ^ element "uri" (Uri.to_string addr) in
let open Logarion in
let fn txt a =
a ^ "<author>" ^ (opt_element "name" @@ esc txt.Person.name)
^ (List.fold_left u "" txt.Person.addresses)
^ "</author>" in
Person.Set.fold fn text.Text.authors ""
let updated txt = let open Logarion in
"<updated>"^ Date.(txt.Text.date |> listing |> rfc_string) ^"</updated>"
let htm_entry base_url text =
let open Logarion in
let u = Text.alias text in
let authors elt a =
a ^ "<author>"
^ (opt_element "name" @@ esc elt.Person.name)
^ (List.fold_left (fun acc addr -> acc ^ element "uri" (Uri.to_string addr)) "" elt.Person.addresses)
^ "</author>"
in
"<entry><title>" ^ esc text.title ^ "</title><id>urn:uuid:" ^ Id.to_string text.uuid ^ "</id><link rel=\"alternate\" href=\""
^ base_url ^ "/" ^ u ^ ".htm\" /><updated>"
^ Date.(text.date |> listing |> rfc_string) ^ "</updated>"
^ Person.Set.fold authors text.authors ""
"<entry><link rel=\"alternate\" href=\"" ^ base_url ^ "/" ^ u ^ ".htm\" />"
^ title text ^ id text ^ updated text ^ authors text
^ (opt_element "summary" @@ esc @@ Text.str "abstract" text)
^ String_set.fold (fun elt a -> a ^ "<category term=\"" ^ elt ^ "\"/>") (Text.set "topics" text) ""
^ "<content type=\"xhtml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">"
^ P.of_string text.body ""
^ "</div></content></entry>"
^ "</div></content></entry>\n"
let feed title archive_id base_url texts =
let gmi_entry base_url text =
let open Logarion in
let u = Text.alias text in
"<entry><link rel=\"alternate\" href=\"" ^ base_url ^ "/" ^ u ^ ".gmi\" />"
^ title text ^ id text ^ updated text ^ authors text
^ (opt_element "summary" @@ esc @@ Text.str "abstract" text)
^ String_set.fold (fun elt a -> a ^ "<category term=\"" ^ elt ^ "\"/>") (Text.set "topics" text) ""
^ "</entry>\n"
let feed title archive_id base_url alternate_type texts =
let entry, self = match alternate_type with
| "text/gemini" -> gmi_entry, base_url^"/gmi.atom"
| "text/html" | _ -> htm_entry, base_url^"/feed.atom" in
{|<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:base="|} ^ base_url ^ {|"><title>|}
^ title ^ {|</title><link rel="alternate" type="text/html" href="|}
^ base_url ^ {|/"/><link rel="self" type="application/atom+xml" href="|}
^ base_url ^ {|/feed.atom" /><id>urn:uuid:|} ^ Logarion.Id.to_string archive_id ^ "</id><updated>"
^ Ptime.to_rfc3339 (Ptime_clock.now ()) ^ "</updated>"
^ List.fold_left (fun acc t -> acc ^ entry base_url t) "" texts ^ "</feed>"
^ title ^ {|</title><link rel="alternate" type="|} ^ alternate_type ^ {|" href="|}
^ base_url ^ {|/" /><link rel="self" type="application/atom+xml" href="|}
^ self ^ {|" /><id>urn:uuid:|} ^ Logarion.Id.to_string archive_id ^ "</id><updated>"
^ Ptime.to_rfc3339 (Ptime_clock.now ()) ^ "</updated>\n"
^ List.fold_left (fun acc t -> acc ^ entry base_url t) "" texts
^ "</feed>"

View File

@ -30,19 +30,21 @@ let index_writer types dir archive topic_roots topic_map indexed_texts =
List.iter
(fun topic -> file ("/index." ^ topic ^ ".htm")
(Html.topic_sub_index name topic_map topic indexed_texts))
topic_roots
topic_roots;
let base_url = try Store.KV.find "HTTP-URL" archive.File_store.kv
with Not_found -> prerr_endline "Missing `HTTP-URL:` in config"; "" in
file "/feed.atom" (Atom.feed archive.A.name archive.A.id base_url "text/html" indexed_texts)
);
let base_url = try Store.KV.find "HTTP-URL" archive.File_store.kv
with Not_found -> prerr_endline "Missing `HTTP-URL:` in config"; ""
in
file "/feed.atom" (Atom.feed archive.A.name archive.A.id base_url indexed_texts);
if "gmi" = types || "all" = types then (
file "/index.gmi" (Gemini.topic_main_index name topic_roots indexed_texts);
file "/index.date.gmi" (Gemini.date_index name indexed_texts);
List.iter
(fun topic -> file ("/index." ^ topic ^ ".gmi")
(Gemini.topic_sub_index name topic_map topic indexed_texts))
topic_roots
topic_roots;
let base_url = try Store.KV.find "GEMINI-URL" archive.File_store.kv
with Not_found -> prerr_endline "Missing `GEMINI-URL:` in config"; "" in
file "/gmi.atom" (Atom.feed archive.A.name archive.A.id base_url "text/gemini" indexed_texts)
)
let txt_writer types dir name ((text, _store_item) as r) =
@ -77,11 +79,7 @@ let convert_dir types cmd_dir =
open Cmdliner
let term =
let directory = Arg.(value & pos 0 string ""
& info [] ~docv:"target directory" ~doc:"Directory to convert into") in
let directory = Arg.(value & pos 0 string "" & info [] ~docv:"target directory" ~doc:"Directory to convert into") in
let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"TYPES" ~doc:"Convert to type") in
Term.(const convert_dir $ types $ directory),
Term.info
"convert" ~doc:"convert archive"
~man:[ `S "DESCRIPTION"; `P "Convert Logarion archive" ]
Term.info "convert" ~doc:"convert archive" ~man:[ `S "DESCRIPTION"; `P "Convert Logarion archive" ]