(* * Copyright (c) 2005 Anil Madhavapeddy * Copyright (c) 2010 Alexander Markov * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * $Id: atom.ml,v 1.2 2005/09/20 19:09:06 avsm Exp $ *) open Lwt open Utils open CalendarLib let entry_of_change ~main_service make_iri prefix path c : Atom_schema.Entry.t Lwt.t = Vcs.get_diff prefix path c.Vcs.hash >>= fun diff -> Vcs.wdiff diff >>= fun wdiff -> let xhtml : {{ Xhtml_types_duce._div }} = {{
[
      {{ Vcs.xhtml_of_full_wdiff ~main_service wdiff }} ] }} in
  let updated = match c.Vcs.date with
    | Vcs.Rfc s -> s
    | Vcs.Calendar d -> rfc3339_of_calendar d in
  let escaped_xhtml = Duce_printer.string_of_xhtml xhtml in
  let iri = utf (make_iri c.Vcs.hash) in
  return {{
    [
      (utf escaped_xhtml)
      {{ utf c.Vcs.title }}
      <updated>{: updated :}
      <id>iri
      <link href=iri>[]
    ]
  }}

let of_changes ~main_service ~title ~link make_iri prefix path changes :
    Atom_schema.feed Lwt.t =
  Lwt_list.map_s (entry_of_change ~main_service make_iri prefix path) changes >>= fun l ->
  return {{{: l :}}} >>= fun entries ->
  let up =
    let f =
      try (Unix.stat path).Unix.st_mtime
      with _ -> 0. in
    rfc3339_of_calendar (Calendar.from_unixfloat f) in
  return {{
    <feed xmlns="http://www.w3.org/2005/Atom">[
      <title>(utf title)
      <updated>{: up :}
      <id>(utf link)
      <link href=(utf link)>[]
      !entries
    ]
  }}

let of_page ~main_service ~title ~link make_iri prefix path =
  Vcs.get_changes prefix ~count:15 ~path >>= fun changes ->
  of_changes ~main_service ~title ~link make_iri prefix path changes

let of_repo ~main_service ~title ~link make_iri prefix =
  Vcs.get_changes prefix ~count:15 >>= fun changes ->
  of_changes ~main_service ~title ~link make_iri prefix prefix changes