Skip to content

Commit

Permalink
Tweak the Google Docs external page feature to support native titles
Browse files Browse the repository at this point in the history
The Docs title is used if no i18n one is defined
  • Loading branch information
mikesname committed Aug 26, 2024
1 parent aaf0c50 commit 2b89e45
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 18 deletions.
5 changes: 5 additions & 0 deletions modules/portal/app/assets/css/portal/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2157,3 +2157,8 @@ section:last-child {
@include make-container();
@include make-container-max-widths();
}

// External page styling
.external-page table {
@extend .table, .table-bordered, .table-striped;
}
10 changes: 6 additions & 4 deletions modules/portal/app/controllers/portal/Portal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,12 @@ case class Portal @Inject()(
def externalPage(key: String): Action[AnyContent] = OptionalUserAction.async { implicit request =>
futurePageOr404 {
htmlPages.get(key, noCache = request.getQueryString("noCache").isDefined).map { futureData =>
futureData.map { case (css, html) =>
val title = Messages(s"pages.external.$key.title")
val meta = Map("description" -> Messages(s"pages.external.$key.description"))
Ok(views.html.layout.textLayout(title, meta = meta, styles = css)(html))
futureData.map { case (title, css, html) =>
val titleKey = s"pages.external.$key.title"
val descKey = s"pages.external.$key.description"
val i18nTitle = if (Messages.isDefinedAt(titleKey)) Messages(titleKey) else title
val i18nMeta = Map("description" -> (if(Messages.isDefinedAt(descKey)) Messages(descKey) else ""))
Ok(views.html.layout.textLayout(i18nTitle, meta = i18nMeta, styles = css)(html))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import scala.concurrent.{ExecutionContext, Future}
case class GoogleDocsHtmlPages @Inject ()(ws: WSClient, config: play.api.Configuration)(
implicit cache: AsyncCacheApi, executionContext: ExecutionContext) extends HtmlPages {

private def googleDocBody(url: String): Future[(Html, Html)] = {
private def googleDocBody(url: String): Future[(String, Html, Html)] = {
ws.url(url).addQueryStringParameters(
"e" -> "download",
"exportFormat" -> "html",
Expand All @@ -38,6 +38,9 @@ case class GoogleDocsHtmlPages @Inject ()(ws: WSClient, config: play.api.Configu
import org.jsoup.Jsoup

val doc = Jsoup.parse(r.body)

val title = doc.title().replace(" - Google Docs", "")

val body = doc
.body()
.tagName("div")
Expand All @@ -51,11 +54,11 @@ case class GoogleDocsHtmlPages @Inject ()(ws: WSClient, config: play.api.Configu
body.select("div#banners").remove()
body.select("div#footer").remove()

Html("") -> Html(body.outerHtml())
(title, Html(""), Html(body.outerHtml()))
}
}

override def get(key: String, noCache: Boolean = false)(implicit messages: Messages): Option[Future[(Html, Html)]] = {
override def get(key: String, noCache: Boolean = false)(implicit messages: Messages): Option[Future[(String, Html, Html)]] = {
def getUrl: Option[String] =
config.getOptional[String](s"pages.external.google.$key.${messages.lang.code}") orElse
config.getOptional[String](s"pages.external.google.$key.default")
Expand Down
4 changes: 2 additions & 2 deletions modules/portal/app/services/htmlpages/HtmlPages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait HtmlPages {
* @param key the identifier for this fragment
* @param noCache whether to prevent the fragment coming from the cache
* @param messages an implicit messages instance containing the current language
* @return An optional, future of Css -> Body HTML
* @return An optional, future of (Title, Css, Body HTML)
*/
def get(key: String, noCache: Boolean = false)(implicit messages: Messages): Option[Future[(Html, Html)]]
def get(key: String, noCache: Boolean = false)(implicit messages: Messages): Option[Future[(String, Html, Html)]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ case class AkkaStreamsIndexMediatorHandle(
override def withChannel(actorRef: ActorRef, formatter: String => String, filter: Int => Boolean = _ % 100 == 0): AkkaStreamsIndexMediatorHandle =
copy(chan = Some(actorRef), processFunc = formatter, progressFilter = filter)

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import scala.concurrent.duration._

private val logger = Logger(classOf[AkkaStreamsIndexMediator])
Expand Down Expand Up @@ -79,8 +79,8 @@ case class AkkaStreamsIndexMediatorHandle(
}
.via(httpSinkFlow)
.collect {
case (Success(response), tag) => response
case (Failure(e), tag) => throw e
case (Success(response), _) => response
case (Failure(e), _) => throw e
}
.flatMapConcat(_.entity.withoutSizeLimit().dataBytes)
.toMat(Sink.fold(ByteString.empty)(_ ++ _))(Keep.right)
Expand Down
2 changes: 1 addition & 1 deletion modules/portal/conf/portal.routes
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ GET /about @controllers.portal.Portal.abo
GET /terms @controllers.portal.Portal.terms()

GET /help/datamodel @controllers.portal.Portal.dataModel()
GET /help/$page<\w+> @controllers.portal.Portal.externalPage(page: String)
GET /help/$page<\w[\w-_]+> @controllers.portal.Portal.externalPage(page: String)

# Change locale
GET /prefs @controllers.portal.Portal.prefs()
Expand Down
2 changes: 1 addition & 1 deletion test/integration/portal/PortalSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class PortalSpec extends IntegrationTestRunner {
"fetch external pages" in new ITestApp {
val faq = FakeRequest(portalRoutes.externalPage("faq")).call()
status(faq) must equalTo(OK)
contentAsString(faq) must contain(mockdata.externalPages("faq").toString())
contentAsString(faq) must contain(mockdata.externalPages("faq")._3.toString())
}

"return 404 for external pages with a malformed id (bug #635)" in new ITestApp {
Expand Down
2 changes: 1 addition & 1 deletion test/mockdata/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package object mockdata {
val yahooOpenId = OpenIDAssociation("linda", "https://yahoo.com/openid", Some(moderator))

val externalPages = Map(
"faq" -> Html("<h1>FAQ</h1>")
"faq" -> ("Faq", Html(""), Html("<h1>FAQ</h1>"))
)

// Users...
Expand Down
6 changes: 3 additions & 3 deletions test/services/htmlpages/MockHtmlPages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import scala.concurrent.Future
* Mock external pages.
*/
case class MockHtmlPages() extends HtmlPages {
override def get(key: String, noCache: Boolean)(implicit messages: Messages): Option[Future[(Html, Html)]] = {
mockdata.externalPages.get(key).map { html =>
Some(Future.successful(Html("") -> html))
override def get(key: String, noCache: Boolean)(implicit messages: Messages): Option[Future[(String, Html, Html)]] = {
mockdata.externalPages.get(key).map { data =>
Some(Future.successful(data))
}.getOrElse(throw new ItemNotFound())
}
}

0 comments on commit 2b89e45

Please sign in to comment.