Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A first draft of an Atom syndication format plugin. #43

Merged
merged 5 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/yocaml/rss.ml
Original file line number Diff line number Diff line change
Expand Up @@ -465,18 +465,14 @@ module Channel = struct
=
Format.fprintf
ppf
"<?xml version=\"%s\" encoding=\"%s\" ?><rss version=\"2.0\" \
"<?xml version=%S encoding=%S ?><rss version=\"2.0\" \
xmlns:atom=\"http://www.w3.org/2005/Atom\">%a</rss>"
xml_version
encoding
(pp ~default_time)
;;

let to_rss
?(xml_version = "1.0")
?(encoding = "UTF-8")
?(default_time = 10, 0, 0)
=
Format.asprintf "%a" $ pp_rss ~xml_version ~encoding ~default_time
let to_rss ?xml_version ?encoding ?(default_time = 10, 0, 0) =
Format.asprintf "%a" $ pp_rss ?xml_version ?encoding ~default_time
;;
end
7 changes: 7 additions & 0 deletions lib/yocaml_syndication/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(library
(name yocaml_syndication)
(public_name yocaml_syndication)
(libraries yocaml syndic preface ptime))

(documentation
(package yocaml_syndication))
77 changes: 77 additions & 0 deletions lib/yocaml_syndication/yocaml_syndication.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module Atom = struct
type t = Syndic.Atom.feed

let default_generator =
{ Syndic.Atom.version = Some "dev"
; uri = Some (Uri.of_string "https://github.com/xhtmlboi/yocaml")
; content = "YOcaml"
}
;;

let make
?authors
?categories
?contributors
?(generator = default_generator)
?icon
?links
?logo
?rights
?subtitle
~id
~title
~updated
=
Syndic.Atom.feed
?authors
?categories
?contributors
?icon
~generator
?links
?logo
?rights
?subtitle
~id
~title
~updated
;;

let pp ppf feed =
Format.fprintf ppf "%s" (Syndic.Atom.to_xml feed |> Syndic.XML.to_string)
;;

let pp_atom ?(xml_version = "1.0") ?(encoding = "UTF-8") ppf feed =
Format.fprintf
ppf
"<?xml version=%S encoding=%S?>%s"
xml_version
encoding
(Syndic.Atom.to_xml feed |> Syndic.XML.to_string)
;;

let to_atom ?xml_version ?encoding feed =
Format.asprintf "%a" (pp_atom ?xml_version ?encoding) feed
;;

let entry_of_article url authors article =
let module Article = Yocaml.Metadata.Article in
let (year, month, day), time =
Article.date article |> Yocaml.Date.to_pair
in
let time = Option.value time ~default:(0, 0, 0) in
match
Ptime.of_date_time
((year, Yocaml.Date.month_to_int month, day), (time, 0))
with
| None -> failwith "article date is not representable!"
| Some updated ->
Syndic.Atom.entry
~id:url
~title:(Text (Article.article_title article))
~authors
~updated
~summary:(Text (Article.article_description article))
()
;;
end
42 changes: 42 additions & 0 deletions lib/yocaml_syndication/yocaml_syndication.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(** An Atom representation. *)

(** {1 Describe an Atom feed} *)

module Atom : sig
(** See http://cumulus.github.io/Syndic/syndic/Syndic__/Syndic_atom/index.html. *)

type t = Syndic.Atom.feed

val make
: ?authors:Syndic.Atom.author list
-> ?categories:Syndic.Atom.category list
-> ?contributors:Syndic.Atom.author list
-> ?generator:Syndic.Atom.generator
-> ?icon:Uri.t
-> ?links:Syndic.Atom.link list
-> ?logo:Uri.t
-> ?rights:Syndic.Atom.text_construct
-> ?subtitle:Syndic.Atom.text_construct
-> id:Uri.t
-> title:Syndic.Atom.text_construct
-> updated:Syndic.Date.t
-> Syndic.Atom.entry list
-> t

val pp : Format.formatter -> t -> unit

val pp_atom
: ?xml_version:string
-> ?encoding:string
-> Format.formatter
-> t
-> unit

val to_atom : ?xml_version:string -> ?encoding:string -> t -> string

val entry_of_article
: Uri.t
-> Syndic.Atom.author * Syndic.Atom.author list
-> Yocaml.Metadata.Article.t
-> Syndic.Atom.entry
end
29 changes: 29 additions & 0 deletions yocaml_syndication.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
opam-version: "2.0"
version: "dev"
synopsis: "YOCaml Syndication"
maintainer: "[email protected]"
authors: [
"The XHTMLBoy <[email protected]>"
"Xavier Van de Woestyne <[email protected]>"
]

build: [
[ "dune" "subst" ] {dev}
[ "dune" "build" "-p" name "-j" jobs ]
[ "dune" "runtest" "-p" name ] {with-test}
[ "dune" "build" "@doc" "-p" name ] {with-doc}
]

license: "GPL-3.0-or-later"
tags: [ "shell" "bin" "make" "static" "blog" "generator" ]
homepage: "https://github.com/xhtmlboi/yocaml"
dev-repo: "git+https://github.com/xhtmlboi/yocaml.git"
bug-reports: "https://github.com/xhtmlboi/yocaml/issues"

depends: [
"ocaml" { >= "4.11.1" }
"dune" { >= "2.8" }
"odoc" {with-doc}
"syndic" { >= "1.6.1"}
"yocaml" {= version}
]