Changeset 19 in code for trunk/cli


Ignore:
Timestamp:
Oct 26, 2022, 7:36:02 PM (2 years ago)
Author:
fox
Message:

Accept comma separated converter names, separate Atom from Html and Gemini converters.

Note: atom must be called separately now because of the separation. Example

txt convert -t htm,atom xyz

Location:
trunk/cli
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/atom.ml

    r3 r19  
     1let ext = ".atom"
     2
    13let esc = Converter.Html.esc
    24
     
    4547  ^ "</entry>\n"
    4648
    47 let feed title archive_id base_url alternate_type texts =
    48         let entry, self = match alternate_type with
    49                 | "text/gemini" -> gmi_entry, base_url^"/gmi.atom"
    50                 | "text/html" | _ -> htm_entry, base_url^"/feed.atom" in
    51   {|<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:base="|} ^ base_url ^ {|"><title>|}
    52   ^ title ^ {|</title><link rel="alternate" type="|} ^ alternate_type ^ {|" href="|}
    53   ^ base_url ^ {|/" /><link rel="self" type="application/atom+xml" href="|}
    54   ^ self ^ {|" /><id>urn:uuid:|} ^ archive_id ^ "</id><updated>"
    55   ^ Logarion.Date.now () ^ "</updated>\n"
    56   ^ List.fold_left (fun acc t -> acc ^ entry base_url t) "" texts
    57   ^ "</feed>"
     49let base_url kv protocol = try
     50        let locs = Logarion.Store.KV.find "Locations" kv in
     51        let _i = Str.(search_forward (regexp (protocol ^ "://[^;]*")) locs 0) in
     52        Str.(matched_string locs)
     53        with Not_found -> Printf.eprintf "Missing location for %s" protocol; ""
     54
     55let indices alternate_type c =
     56        let file name = Logarion.File_store.file (Filename.concat c.Conversion.dir name) in
     57        let title = try Logarion.Store.KV.find "Title" c.Conversion.kv with Not_found -> "" in
     58        let entry, fname, protocol_regexp = match alternate_type with
     59        | "text/gemini" -> gmi_entry, "gmi.atom", "gemini"
     60        | "text/html" | _ -> htm_entry, "feed.atom", "https?"
     61        in
     62        let base_url = base_url c.kv protocol_regexp in
     63        let self = Filename.concat base_url fname in
     64        file fname @@
     65          {|<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:base="|} ^ base_url ^ {|"><title>|}
     66          ^ title ^ {|</title><link rel="alternate" type="|} ^ alternate_type ^ {|" href="|}
     67          ^ base_url ^ {|/" /><link rel="self" type="application/atom+xml" href="|}
     68          ^ self ^ {|" /><id>urn:uuid:|} ^ c.Conversion.id ^ "</id><updated>"
     69          ^ Logarion.Date.now () ^ "</updated>\n"
     70          ^ List.fold_left (fun acc t -> acc ^ entry base_url t) "" c.texts
     71          ^ "</feed>"
     72
     73let converter format = Conversion.{ ext; page = None; indices = Some (indices format) }
  • trunk/cli/conversion.ml

    r3 r19  
    1010type fn_t = {
    1111        ext: string;
    12         page: t -> Logarion.Text.t -> string;
    13         indices: t -> unit;
     12        page: (t -> Logarion.Text.t -> string) option;
     13        indices: (t -> unit) option;
    1414}
  • trunk/cli/convert.ml

    r15 r19  
    1010                List.fold_left
    1111                        (fun a f ->
    12                                 let dest = dest ^ f.Conversion.ext in
    13                                 if is_older source dest then (File_store.file dest (f.Conversion.page r text); true) else false
    14                                 || a)
     12                                match f.Conversion.page with None -> false || a
     13                                | Some page ->
     14                                        let dest = dest ^ f.Conversion.ext in
     15                                        (if is_older source dest then (File_store.file dest (page r text); true) else false)
     16                                        || a)
    1517                        false cs
    1618        | x -> Printf.eprintf "Can't convert Content-Type: %s file: %s" x text.Text.title; false
    1719
    1820let converters types kv =
     21        let n = String.split_on_char ',' types in
    1922        let t = [] in
    20         let t = if ("htm" = types || "all" = types) then
    21                 (let htm = Html.init kv in
    22                  Conversion.{ ext = Html.ext; page = Html.page htm; indices = Html.indices htm })::t
    23                 else t in
    24         let t = if ("gmi" = types || "all" = types) then
    25                 Conversion.{ ext = Gemini.ext; page = Gemini.page; indices = Gemini.indices}::t else t in
     23        let t = if List.(mem "all" n || mem "htm" n) then (Html.converter kv)::t else t in
     24        let t = if List.(mem "all" n || mem "atom" n) then (Atom.converter "text/html")::t else t in
     25        let t = if List.(mem "all" n || mem "gmi" n) then (Gemini.converter)::t else t in
     26        let t = if List.(mem "all" n || mem "gmi-atom" n) then (Atom.converter "text/gemini")::t else t in
    2627        t
    2728
     
    3637                with Not_found -> Topic_set.roots topics in
    3738        let repo = Conversion.{ repo with topic_roots; topics; texts } in
    38         if not noindex then List.iter (fun c -> c.Conversion.indices repo) converters;
     39        if not noindex then List.iter (fun c -> match c.Conversion.indices with None -> () | Some f -> f repo) converters;
    3940        Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts)
    4041
     
    4849                        let kv = let f = Filename.concat dir ".convert.conf" in (* TODO: better place to store convert conf? *)
    4950                                if Sys.file_exists f then File_store.of_kv_file f else Store.KV.empty in
    50                         let kv = if Store.KV.mem "Title" kv then kv
    51                                 else Store.KV.add "Title" info.Header_pack.title kv in
     51                        let kv = if Store.KV.mem "Title" kv then kv else Store.KV.add "Title" info.Header_pack.title kv in
    5252                        let kv = Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in
    5353                        let kv = Store.KV.add "Peers" (String.concat ";\n" Header_pack.(to_str_list peers)) kv in
  • trunk/cli/gemini.ml

    r15 r19  
    9191        let index_name = try Store.KV.find "Gemini-index" r.kv with Not_found -> "index.gmi" in
    9292        let title = try Store.KV.find "Title" r.Conversion.kv with Not_found -> "" in
    93 
    94         if index_name <> "" then
    95                 file index_name (topic_main_index r title r.topic_roots r.texts);
    96 
     93        if index_name <> "" then file index_name (topic_main_index r title r.topic_roots r.texts);
    9794        file "index.date.gmi" (date_index title r.texts);
    98 
    9995        List.iter
    10096                (fun topic -> file ("index." ^ topic ^ ".gmi")
    10197                 (topic_sub_index title r.topics topic r.texts))
    102                 r.topic_roots;
     98                r.topic_roots
    10399
    104         let base_url = try
    105                 let _i = Str.(search_forward (regexp "gemini?://[^;]*") (Store.KV.find "Locations" r.kv) 0) in
    106                 Str.(matched_string (Store.KV.find "Locations" r.kv))
    107                 with Not_found -> prerr_endline "Missing location for Gemini"; "" in
    108         file "gmi.atom" (Atom.feed title r.id base_url "text/gemini" r.texts)
     100let converter = Conversion.{ ext; page = Some page; indices = Some indices}
  • trunk/cli/html.ml

    r18 r19  
    1515        { templates = { header; footer} }
    1616
    17 let wrap c htm text_title body =
    18         let site_title = try Logarion.Store.KV.find "Title" c.Conversion.kv
     17let wrap conv htm text_title body =
     18        let site_title = try Logarion.Store.KV.find "Title" conv.Conversion.kv
    1919                with Not_found -> "" in
    2020        let replace x = let open Str in
     
    5858        let keywords = str_set "keywords" text in
    5959        let header =
    60                 let time x = {|<time datetime="|} ^ x ^ {|">|} ^ x ^ "</time>" in
     60                let time x = Printf.sprintf {|<time datetime="%s">%s</time>|}
     61                        (Date.rfc_string x) (Date.pretty_date x) in
    6162                let topic_links x =
    6263                        let to_linked t a =
     
    6768                ^ opt_kv "Title:" text.title
    6869                ^ opt_kv "Authors:" authors
    69                 ^ opt_kv "Date: " (time (Date.(pretty_date @@ listing text.date)))
     70                ^ opt_kv "Date: " (time (Date.listing text.date))
    7071                ^ opt_kv "Series: " (str_set "series" text)
    7172                ^ opt_kv "Topics: " (topic_links (set "topics" text))
     
    162163                ^ listing_index topic_map [topic_root] "" metas)
    163164
    164 open Logarion
    165165let indices htm c =
    166166        let file name = Logarion.File_store.file (Filename.concat c.Conversion.dir name) in
    167         let index_name = try Store.KV.find "HTM-index" c.Conversion.kv with Not_found -> "index.html" in
    168         let title = try Store.KV.find "Title" c.Conversion.kv with Not_found -> "" in
    169 
    170         if index_name <> "" then
    171                 file index_name (topic_main_index c htm c.topic_roots c.texts);
    172 
     167        let index_name = try Logarion.Store.KV.find "HTM-index" c.Conversion.kv with Not_found -> "index.html" in
     168        if index_name <> "" then file index_name (topic_main_index c htm c.topic_roots c.texts);
    173169        file "index.date.htm" (date_index c htm c.texts);
    174 
    175170        List.iter
    176171                (fun root -> file ("index." ^ root ^ ".htm") (topic_sub_index c htm c.topics root c.texts))
    177                 c.topic_roots;
     172                c.topic_roots
    178173
    179         let base_url = try
    180                 let locs = Store.KV.find "Locations" c.kv in
    181                 let _i = Str.(search_forward (regexp "https?://[^;]*") locs 0) in
    182                 Str.(matched_string locs)
    183                 with Not_found -> prerr_endline "Missing location for HTTP(S)"; "" in
    184         file "feed.atom" (Atom.feed title c.id base_url "text/html" c.texts)
     174let converter kv =
     175        let htm = init kv in
     176        Conversion.{ ext; page = Some (page htm); indices = Some (indices htm) }
Note: See TracChangeset for help on using the changeset viewer.