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