From fda992e84db55c4f769cd7e4407e863c76a44b06 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Mon, 23 Jan 2017 11:12:56 +0100 Subject: Add `first` filter for to allow for first in both array and string --- .../dotty/tools/dottydoc/staticsite/LiquidTemplate.scala | 1 + .../src/dotty/tools/dottydoc/staticsite/filters.scala | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) (limited to 'doc-tool') diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/LiquidTemplate.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/LiquidTemplate.scala index a86a5bb54..87209b47a 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/LiquidTemplate.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/LiquidTemplate.scala @@ -15,6 +15,7 @@ case class LiquidTemplate(contents: String) extends ResourceFinder { /** Register filters to static container */ Filter.registerFilter(new Reverse) + Filter.registerFilter(new First) // For some reason, liqp rejects a straight conversion using `.asJava` private def toJavaMap(map: Map[String, AnyRef]): HashMap[String, Object] = diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/filters.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/filters.scala index 2239f5a50..1569d3873 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/filters.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/filters.scala @@ -23,4 +23,20 @@ object filters { else array.reverse } } + + /** Used to get the first element of arrays and strings: + * + * ```html + * {% assign array = "1,2,3,4,5" | split: "," %} + * {{ array | first }} + * ``` + * The above snippet will render "1" + */ + final class First extends Filter("first") { + override def apply(value: Any, params: AnyRef*): AnyRef = value match { + case str: String if str.nonEmpty => str.charAt(0).toString + case xs: Array[String] if xs.nonEmpty => xs.head + case _ => null + } + } } -- cgit v1.2.3