refactor blog atom feed

pull/9751/head
Thibault Duplessis 2021-09-07 15:41:25 +02:00
parent 7eca87f1d1
commit 5296ed2ccc
4 changed files with 85 additions and 54 deletions

View File

@ -65,7 +65,7 @@ final class Blog(
blogApi.masterContext flatMap { implicit prismic =>
blogApi.recent(prismic.api, 1, MaxPerPage(50), none) map {
_ ?? { docs =>
views.html.blog.atom(docs, env.net.baseUrl).render
views.html.blog.atom(docs).render
}
}
}

View File

@ -112,9 +112,4 @@ trait DateHelper { self: I18nHelper with StringHelper =>
else if (years == 0) s"${pluralize("month", months)} ago"
else s"${pluralize("year", years)} ago"
}
private val atomDateFormatter = ISODateTimeFormat.dateTime
def atomDate(date: DateTime): String = atomDateFormatter print date
def atomDate(field: String)(doc: io.prismic.Document): Option[String] =
doc getDate field map (_.value.toDateTimeAtStartOfDay) map atomDate
}

View File

@ -0,0 +1,39 @@
package views.html.base
import controllers.routes
import org.joda.time.DateTime
import org.joda.time.format.ISODateTimeFormat
import play.api.mvc.Call
import lila.app.templating.Environment._
import lila.app.ui.ScalatagsTemplate._
import lila.common.config.BaseUrl
object atom {
def apply[A](
elems: Seq[A],
htmlCall: Call,
atomCall: Call,
title: String,
updated: Option[DateTime]
)(elem: A => Frag) =
frag(
raw("""<?xml version="1.0" encoding="UTF-8"?>"""),
raw(
"""<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">"""
),
tag("id")(s"$netBaseUrl$htmlCall"),
link(rel := "alternate", tpe := "text/html", href := s"${netBaseUrl}$htmlCall"),
link(rel := "self", tpe := "application/atom+xml", href := s"${netBaseUrl}$atomCall"),
tag("title")(title),
tag("updated")(updated map atomDate),
elems.map { el =>
tag("entry")(elem(el))
},
raw("</feed>")
)
private val atomDateFormatter = ISODateTimeFormat.dateTime
def atomDate(date: DateTime): String = atomDateFormatter print date
}

View File

@ -1,59 +1,56 @@
package views.html.blog
import controllers.routes
import lila.app.templating.Environment._
import lila.app.ui.ScalatagsTemplate._
import lila.common.paginator.Paginator
import lila.common.config.BaseUrl
import controllers.routes
object atom {
import views.html.base.atom.atomDate
def apply(
pager: Paginator[io.prismic.Document],
baseUrl: BaseUrl
pager: Paginator[io.prismic.Document]
)(implicit prismic: lila.blog.BlogApi.Context) =
frag(
raw("""<?xml version="1.0" encoding="UTF-8"?>"""),
raw(
"""<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">"""
),
tag("id")(s"$baseUrl${routes.Blog.index()}"),
link(rel := "alternate", tpe := "text/html", href := s"$baseUrl${routes.Blog.index()}"),
link(rel := "self", tpe := "application/atom+xml", href := s"$baseUrl${routes.Blog.atom}"),
tag("title")("lichess.org blog"),
tag("updated")(pager.currentPageResults.headOption.flatMap(atomDate("blog.date"))),
pager.currentPageResults.map { doc =>
tag("entry")(
tag("id")(s"$baseUrl${routes.Blog.show(doc.id, doc.slug)}"),
tag("published")(atomDate("blog.date")(doc)),
tag("updated")(atomDate("blog.date")(doc)),
link(
rel := "alternate",
tpe := "text/html",
href := s"$baseUrl${routes.Blog.show(doc.id, doc.slug)}"
),
tag("title")(doc.getText("blog.title")),
tag("category")(
tag("term")(doc.getText("blog.category")),
tag("label")(slugify(~doc.getText("blog.category")))
),
tag("content")(tpe := "html")(
doc.getText("blog.shortlede"),
"<br>", // yes, scalatags encodes it.
doc.getImage("blog.image", "main").map { img =>
st.img(src := img.url).render
},
"<br>",
doc
.getHtml("blog.body", prismic.linkResolver)
.map(lila.blog.Youtube.fixStartTimes)
.map(lila.blog.BlogTransform.addProtocol)
),
tag("tag")("media:thumbnail")(attr("url") := doc.getImage(s"blog.image", "main").map(_.url)),
tag("author")(tag("name")(doc.getText("blog.author")))
)
},
raw("</feed>")
)
views.html.base.atom(
elems = pager.currentPageResults,
htmlCall = routes.Blog.index(),
atomCall = routes.Blog.atom,
title = "lichess.org blog",
updated = pager.currentPageResults.headOption flatMap docDate
) { doc =>
frag(
tag("id")(s"$netBaseUrl${routes.Blog.show(doc.id, doc.slug)}"),
tag("published")(docDate(doc) map atomDate),
tag("updated")(docDate(doc) map atomDate),
link(
rel := "alternate",
tpe := "text/html",
href := s"$netBaseUrl${routes.Blog.show(doc.id, doc.slug)}"
),
tag("title")(doc.getText("blog.title")),
tag("category")(
tag("term")(doc.getText("blog.category")),
tag("label")(slugify(~doc.getText("blog.category")))
),
tag("content")(tpe := "html")(
doc.getText("blog.shortlede"),
"<br>", // yes, scalatags encodes it.
doc.getImage("blog.image", "main").map { img =>
st.img(src := img.url).render
},
"<br>",
doc
.getHtml("blog.body", prismic.linkResolver)
.map(lila.blog.Youtube.fixStartTimes)
.map(lila.blog.BlogTransform.addProtocol)
),
tag("tag")("media:thumbnail")(attr("url") := doc.getImage(s"blog.image", "main").map(_.url)),
tag("author")(tag("name")(doc.getText("blog.author")))
)
}
def docDate(doc: io.prismic.Document) =
doc getDate "blog.date" map (_.value.toDateTimeAtStartOfDay)
}