Changeset 20 in code


Ignore:
Timestamp:
Oct 30, 2022, 2:48:02 PM (2 years ago)
Author:
fox
Message:

Begin unifying conf and pck code; inline CSS; optional CSS & Atom

Location:
trunk/cli
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/conversion.ml

    r19 r20  
    11open Logarion
    22type t = {
    3         id: string; dir: string;
     3        id: string;
     4        dir: string;
    45        kv: string Store.KV.t;
    56        topic_roots: string list;
     
    1314        indices: (t -> unit) option;
    1415}
     16
     17let empty () = {
     18        id = ""; dir = "";
     19        kv = Store.KV.empty;
     20        topic_roots = [];
     21        topics = Topic_set.Map.empty;
     22        texts = []
     23}
  • trunk/cli/convert.ml

    r19 r20  
    11open Logarion
    22
    3 let is_older source dest = try
    4         Unix.((stat dest).st_mtime < (stat source).st_mtime) with _-> true
     3(*TODO: move to converters (style, feed checks)*)
     4let is_older s d = try Unix.((stat d).st_mtime < (stat s).st_mtime) with _-> true
    55
    66let convert cs r (text, files) = match Text.str "Content-Type" text with
     
    2727        t
    2828
    29 let directory converters noindex dir id kv =
    30         let empty = Topic_set.Map.empty in
    31         let repo = Conversion.{ id; dir; kv; topic_roots = []; topics = empty; texts = [] } in
     29let directory converters noindex repo =
    3230        let fn (ts,ls,acc) ((elt,_) as r) =
    3331                (Topic_set.to_map ts (Text.set "topics" elt)), elt::ls,
    3432                if convert converters repo r then acc+1 else acc in
    35         let topics, texts, count = File_store.(fold ~dir ~order:newest fn (empty,[],0)) in
    36         let topic_roots = try List.rev @@ String_set.list_of_csv (Store.KV.find "Topics" kv)
     33        let topics, texts, count =
     34                File_store.(fold ~dir:repo.Conversion.dir ~order:newest fn (Topic_set.Map.empty,[],0)) in
     35        let topic_roots = try List.rev @@ String_set.list_of_csv (Store.KV.find "Topics" repo.kv)
    3736                with Not_found -> Topic_set.roots topics in
    3837        let repo = Conversion.{ repo with topic_roots; topics; texts } in
     
    4039        Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts)
    4140
    42 let at_path types noindex path =
    43         match path with "" -> prerr_endline "unspecified text file or directory"
    44         | dir when Sys.file_exists dir && Sys.is_directory dir ->
    45                 let fname = Filename.concat dir "index.pck" in
    46                 (match Header_pack.of_string @@ File_store.to_string fname with
    47                 | Error s -> prerr_endline s
     41let load_kv dir =
     42        let conf = Filename.concat dir ".convert.conf" in (* TODO: better name? *)
     43        let kv = if Sys.file_exists conf then File_store.of_kv_file conf else Store.KV.empty in
     44        let idx = Filename.concat dir "index.pck" in
     45        if not (Sys.file_exists idx) then kv else
     46                match Header_pack.of_string @@ File_store.to_string (idx) with
     47                | Error s -> prerr_endline s; kv
    4848                | Ok { info; peers; _ } ->
    49                         let kv = let f = Filename.concat dir ".convert.conf" in (* TODO: better place to store convert conf? *)
    50                                 if Sys.file_exists f then File_store.of_kv_file f else Store.KV.empty in
     49                        let kv = if Store.KV.mem "Id" kv then kv else Store.KV.add "Id" info.Header_pack.id kv in
    5150                        let kv = if Store.KV.mem "Title" kv then kv else Store.KV.add "Title" info.Header_pack.title kv in
    5251                        let kv = Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in
    5352                        let kv = Store.KV.add "Peers" (String.concat ";\n" Header_pack.(to_str_list peers)) kv in
    54                         let cs = converters types kv in
    55                         directory cs noindex dir info.Header_pack.id kv)
     53                        kv
     54
     55let at_path types noindex path = match path with
     56        | "" -> prerr_endline "unspecified text file or directory"
    5657        | path when Sys.file_exists path ->
    57                 let repo = Conversion.{
    58                         id = ""; dir = ""; kv = Store.KV.empty; topic_roots = [];
    59                         topics = Topic_set.Map.empty; texts = [] } in
    60                 let cs = converters types repo.kv in
    61                 (match File_store.to_text path with
    62                 | Ok text -> ignore @@ convert cs repo (text, [path])
    63                 | Error s -> prerr_endline s)
     58                if Sys.is_directory path then (
     59                        let kv = load_kv path in
     60                        let repo = { (Conversion.empty ()) with dir = path; kv } in
     61                        directory (converters types kv) noindex repo
     62                ) else (
     63                        match File_store.to_text path with
     64                        | Error s -> prerr_endline s
     65                        | Ok text ->
     66                                let repo = { (Conversion.empty ()) with dir = ""; kv = load_kv "" } in
     67                                ignore @@ convert (converters types repo.kv) repo (text, [path])
     68                )
    6469        | path -> Printf.eprintf "Path doesn't exist: %s" path
    6570
     
    6772let term =
    6873        let path = Arg.(value & pos 0 string "" & info [] ~docv:"path"
    69                 ~doc:"Text file or directory to convert. Ff directory is provided, it must contain an index.pck (see: txt index)") in
     74                ~doc:"Text file or directory to convert. If directory is provided, it must contain an index.pck (see: txt index)") in
    7075        let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"output type"
    7176                ~doc:"Convert to file type") in
  • trunk/cli/html.ml

    r19 r20  
    11type templates_t = { header: string option; footer: string option }
    2 type t = { templates : templates_t }
     2type t = { templates : templates_t; style : string }
    33
    44let ext = ".htm"
    55let empty_templates = { header = None; footer = None }
    6 let default_opts = { templates = empty_templates }
     6let default_opts = { templates = empty_templates; style = "" }
    77
    88let init kv =
     
    1313        let header = to_string "HTM-header" kv in
    1414        let footer = to_string "HTM-footer" kv in
    15         { templates = { header; footer} }
     15        let style = match to_string "HTM-style" kv with
     16                | Some s -> Printf.sprintf "<style>%s</style>" s | None -> "" in
     17        { templates = { header; footer}; style }
    1618
    1719let wrap conv htm text_title body =
    18         let site_title = try Logarion.Store.KV.find "Title" conv.Conversion.kv
    19                 with Not_found -> "" in
     20        let site_title = try Logarion.Store.KV.find "Title" conv.Conversion.kv with Not_found -> "" in
    2021        let replace x = let open Str in
    21                 global_replace (regexp "{{archive-title}}") site_title x
     22                   global_replace (regexp "{{archive-title}}") site_title x
    2223                |> global_replace (regexp "{{text-title}}") text_title
    2324        in
     25        let feed = try Logarion.Store.KV.find "HTM-feed" conv.Conversion.kv
     26                with Not_found -> if Sys.file_exists (Filename.concat conv.Conversion.dir "feed.atom")
     27                        then "feed.atom" else "" in
    2428        let header = match htm.templates.header with
    25         | Some x -> replace x
    26         | None -> "<header><a href='.'>" ^ site_title ^
    27                 "</a><nav><a href='feed.atom' id='feed'>feed</a></nav></header>"
     29                | Some x -> replace x
     30                | None -> Printf.(sprintf "<header><a href='.'>%s</a></header>%s" site_title
     31                                (if feed <> "" then sprintf "<nav><a href='%s' id='feed'>feed</a></nav>" feed else ""))
    2832        in
    2933        let footer = match htm.templates.footer with None -> "" | Some x -> replace x in
    30         Printf.sprintf "<!DOCTYPE HTML><html><head><title>%s%s</title>\n\
    31         <link rel='stylesheet' href='main.css'>\
    32         <link rel='alternate' href='feed.atom' type='application/atom+xml'>\
     34        Printf.sprintf "<!DOCTYPE HTML><html><head><title>%s%s</title>\n%s\n%s\
    3335        <meta charset='utf-8'/><meta name='viewport' content='width=device-width, initial-scale=1.0'>\
    3436        </head><body>\n%s%s%s</body></html>"
    3537        text_title (if site_title <> "" then (" • " ^ site_title) else "")
     38        htm.style
     39        (if feed <> "" then Printf.sprintf "<link rel='alternate' href='%s' type='application/atom+xml'>" feed else "")
    3640        header body footer
    3741
     
    151155                ^ "<nav><h1>Latest</h1><ul>" ^ to_dated_links ~limit:8 metas
    152156                ^ {|</ul><a href="index.date.htm">More by date</a>|}
    153                 ^ let peers = Logarion.Store.KV.find "Peers" conv.kv in
     157                ^ let peers = try Logarion.Store.KV.find "Peers" conv.kv with Not_found -> "" in
    154158                        (if peers = "" then "" else
    155159                                List.fold_left (fun a s -> Printf.sprintf {|%s<li><a href="%s">%s</a>|} a s s) "<h1>Peers</h1><ul>"
Note: See TracChangeset for help on using the changeset viewer.