Changeset 3 in code for trunk/cli/html.ml


Ignore:
Timestamp:
Apr 15, 2022, 1:17:01 PM (3 years ago)
Author:
fox
Message:
  • Removed 'txt init'

Format

  • New B32 ID

Index

  • New option: txt index --print
  • Move scheme to peers
  • Replace peer.*.conf files with index packed locations Instead of adding a URL to peers.*.conf, run txt pull <url>

Conversion

  • Rewritten converters
  • txt-convert looks for a .convert.conf containing key: value lines.
  • Specifiable topic-roots from .convert.conf.
  • Added Topics: key, with comma seperated topics.

If set only those topics will appear in the main index and used as topic roots.
Other topics will have sub-indices generated, but won't be listed in the main index.

  • HTML converter header & footer options
  • HTML-index renamed to HTM-index

Internal

  • Change types: uuid:Uuid -> id:string
  • File_store merges identical texts
  • Use peer ID for store path, store peers' texts in .local/share/texts
  • Simple URN resolution for converter

Continue to next feed if parsing one fails

  • Phasing-out Archive, replaced by improved packs
  • Eliminate Bos, Cohttp, lwt, uri, tls, Re, Ptime, dependencies
  • Lock version for Cmdliner, fix dune-project
  • Optional resursive store
  • Improve header_pack
  • Fix recursive mkdir
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/html.ml

    r2 r3  
    1 let wrap (title:string) (subtitle:string) body =
    2   {|<!DOCTYPE HTML>|}
    3   ^ {|<html><head><title>|}
    4   ^ subtitle ^ " | " ^ title
    5   ^ {|</title><link rel="stylesheet" href="main.css">|}
    6   ^ {|<link rel="alternate" href="feed.atom" type="application/atom+xml">|}
    7   ^ {|<meta charset="utf-8"/>|}
    8   ^ {|<meta name="viewport" content="width=device-width, initial-scale=1.0">|}
    9   ^ {|</head><body><header><a href=".">|} ^ title
    10   ^ {|</a> <nav><a href="feed.atom" id="feed">feed</a></nav></header>|} ^ body
    11   ^ "</body></html>"
     1type templates_t = { header: string option; footer: string option }
     2type t = { templates : templates_t }
     3
     4let ext = ".htm"
     5let empty_templates = { header = None; footer = None }
     6let default_opts = { templates = empty_templates }
     7
     8let init kv =
     9        let open Logarion in
     10        let header = match  Store.KV.find "HTM-header" kv with
     11                | fname -> Some (File_store.to_string fname)
     12                | exception Not_found -> None in
     13        let footer = match  Store.KV.find "HTM-footer" kv with
     14                | fname -> Some (File_store.to_string fname)
     15                | exception Not_found -> None in
     16        { templates = { header; footer} }
     17
     18let wrap c htm text_title body =
     19        let site_title = try Logarion.Store.KV.find "Title" c.Conversion.kv
     20                with Not_found -> "" in
     21        let replace x = let open Str in
     22                global_replace (regexp "{{archive-title}}") site_title x
     23                |> global_replace (regexp "{{text-title}}") text_title
     24        in
     25        let header = match htm.templates.header with
     26        | Some x -> replace x
     27        | None -> "<header><a href='.'>" ^ site_title ^
     28                "</a><nav><a href='feed.atom' id='feed'>feed</a></nav></header>"
     29        in
     30        let footer = match htm.templates.footer with  None -> "" | Some x -> replace x in
     31  "<!DOCTYPE HTML><html><head><title>" ^ text_title ^ " • " ^ site_title ^ "</title>\n\
     32  <link rel='stylesheet' href='main.css'>\
     33  <link rel='alternate' href='feed.atom' type='application/atom+xml'>\
     34  <meta charset='utf-8'/><meta name='viewport' content='width=device-width, initial-scale=1.0'>\
     35  </head><body>\n" ^ header ^ body ^ footer ^ "</body></html>"
    1236
    1337let topic_link root topic =
    1438  let replaced_space = String.map (function ' '->'+' | x->x) in
    15   {|<a href="index.|} ^ root ^ {|.htm#|} ^ replaced_space topic ^ {|">|}
     39  "<a href='index." ^ replaced_space root ^ ".htm#" ^ replaced_space topic ^ "'>"
    1640  ^ String.capitalize_ascii topic ^ "</a>"
    1741
    18 let page archive_title text =
     42module HtmlConverter = struct
     43        include Converter.Html
     44        let angled_uri u a = if String.sub u 0 10 <> "urn:txtid:" then
     45                angled_uri u a else angled_uri (String.(sub u 10 (length u - 10)) ^ ext) a
     46end
     47
     48let page htm conversion text =
    1949  let open Logarion in
    2050  let open Text in
    21   let module T = Parsers.Plain_text.Make (Converter.Html) in
     51  let module T = Parsers.Plain_text.Make (HtmlConverter) in
    2252  let sep_append ?(sep=", ") a x = match a,x with "",_ -> x | _, "" -> a | _ -> a ^ sep ^ x in
    23   let opt_kv key value = if String.length value > 0 then "<dt>" ^ key ^ "<dd>" ^ value else "" in
     53  let opt_kv key value = if String.length value > 0
     54        then "<dt>" ^ key ^ "<dd>" ^ value else "" in
    2455(*  let author acc auth = sep_append acc Person.(auth.name ^ " ") in*)
    2556  let authors = (Person.Set.to_string text.authors ^ " ") in
     
    3970    ^ opt_kv "Topics: " (topic_links (set "topics" text))
    4071    ^ opt_kv "Keywords: " keywords
    41     ^ opt_kv "Id: " (Id.to_string text.uuid)
     72    ^ opt_kv "Id: " text.id
    4273    ^ {|</dl></header><pre style="white-space:pre-wrap">|} in
    43   wrap archive_title text.title ((T.of_string text.body header) ^ "</pre></article>")
     74  wrap conversion htm text.title ((T.of_string text.body header) ^ "</pre></article>")
    4475
    4576let to_dated_links ?(limit) meta_list =
     
    5889    "" meta_list
    5990
    60 let date_index ?(limit) title meta_list =
     91let date_index ?(limit) conv htm meta_list =
    6192  match limit with
    62   | Some limit -> wrap title "Index" (to_dated_links ~limit meta_list)
    63   | None -> wrap title "Index" (to_dated_links meta_list)
     93  | Some limit -> wrap conv htm "Index" (to_dated_links ~limit meta_list)
     94  | None -> wrap conv htm "Index" (to_dated_links meta_list)
    6495
    6596let fold_topic_roots topic_roots =
     
    113144  "<nav><h1>Texts</h1>" ^ item_group topic_roots ^ "</nav>"
    114145
    115 let topic_main_index title topic_roots metas =
    116   wrap title "Topics"
     146let topic_main_index conv htm topic_roots metas =
     147  wrap conv htm "Topics"
    117148    (fold_topic_roots topic_roots
    118149     ^ "<nav><h1>Latest</h1>" ^ to_dated_links ~limit:10 metas
    119150     ^ {|<a href="index.date.htm">More by date</a></nav>|} )
    120151
    121 let topic_sub_index title topic_map topic_root metas =
    122   wrap title topic_root
     152let topic_sub_index conv htm topic_map topic_root metas =
     153  wrap conv htm topic_root
    123154    (fold_topics topic_map [topic_root] metas
    124155(*     ^ {|<a href=".atom" id="feed">|}^ String.capitalize_ascii topic_root ^{| feed </a>|}*)
    125156     ^ listing_index topic_map [topic_root] "" metas)
     157
     158open Logarion
     159let indices htm c =
     160        let file name = Logarion.File_store.file (Filename.concat c.Conversion.dir name) in
     161        let index_name = try Store.KV.find "HTM-index" c.Conversion.kv with Not_found -> "index.html" in
     162        let title = try Store.KV.find "Title" c.Conversion.kv with Not_found -> "" in
     163
     164        if index_name <> "" then
     165                file index_name (topic_main_index c htm c.topic_roots c.texts);
     166
     167        file "index.date.htm" (date_index c htm c.texts);
     168
     169        List.iter
     170                (fun root -> file ("index." ^ root ^ ".htm") (topic_sub_index c htm c.topics root c.texts))
     171                c.topic_roots;
     172
     173        let base_url = try
     174                let _i = Str.(search_forward (regexp "https?://[^;]*") (Store.KV.find "Locations" c.kv) 0) in
     175                Str.(matched_string (Store.KV.find "Locations" c.kv))
     176                with Not_found -> prerr_endline "Missing location for HTTP(S)"; "" in
     177        file "feed.atom" (Atom.feed title c.id base_url "text/html" c.texts)
Note: See TracChangeset for help on using the changeset viewer.