forked from playframework/twirl
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scala3): WIP working on scala 3 cross compilation. playframework…
- Loading branch information
Showing
18 changed files
with
1,096 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
api/shared/src/main/scala-2.13/play/twirl/api/BaseScalaTemplate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl.api | ||
|
||
import java.util.Optional | ||
import scala.collection.immutable | ||
import scala.jdk.CollectionConverters._ | ||
import scala.reflect.ClassTag | ||
|
||
case class BaseScalaTemplate[T <: Appendable[T], F <: Format[T]](format: F) { | ||
// The overloaded methods are here for speed. The compiled templates | ||
// can take advantage of them for a 12% performance boost | ||
def _display_(x: AnyVal): T = format.escape(x.toString) | ||
def _display_(x: String): T = if (x eq null) format.empty else format.escape(x) | ||
def _display_(x: Unit): T = format.empty | ||
def _display_(x: scala.xml.NodeSeq): T = if (x eq null) format.empty else format.raw(x.toString()) | ||
def _display_(x: T): T = if (x eq null) format.empty else x | ||
|
||
def _display_(o: Any)(implicit m: ClassTag[T]): T = { | ||
o match { | ||
case escaped if escaped != null && escaped.getClass == m.runtimeClass => escaped.asInstanceOf[T] | ||
case () => format.empty | ||
case None => format.empty | ||
case Some(v) => _display_(v) | ||
case key: Optional[_] => | ||
(if (key.isPresent) Some(key.get) else None) match { | ||
case None => format.empty | ||
case Some(v) => _display_(v) | ||
case _ => format.empty | ||
} | ||
case xml: scala.xml.NodeSeq => format.raw(xml.toString()) | ||
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_)) | ||
case escapeds: TraversableOnce[_] => format.fill(escapeds.iterator.map(_display_).toList) | ||
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList) | ||
case escapeds: java.util.List[_] => | ||
format.fill(escapeds.asScala.map(_display_).toList) | ||
case string: String => format.escape(string) | ||
case v if v != null => format.escape(v.toString) | ||
case _ => format.empty | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
api/shared/src/main/scala-2.13/play/twirl/api/TwirlHelperImports.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl.api | ||
|
||
import scala.language.implicitConversions | ||
|
||
/** | ||
* Imports for useful Twirl helpers. | ||
*/ | ||
object TwirlHelperImports { | ||
|
||
/** Allows Java collections to be used as if they were Scala collections. */ | ||
implicit def twirlJavaCollectionToScala[T](x: java.lang.Iterable[T]): Iterable[T] = { | ||
import scala.jdk.CollectionConverters._ | ||
x.asScala | ||
} | ||
|
||
/** Allows inline formatting of java.util.Date */ | ||
implicit class TwirlRichDate(date: java.util.Date) { | ||
def format(pattern: String): String = { | ||
new java.text.SimpleDateFormat(pattern).format(date) | ||
} | ||
} | ||
|
||
/** Adds a when method to Strings to control when they are rendered. */ | ||
implicit class TwirlRichString(string: String) { | ||
def when(predicate: => Boolean): String = { | ||
predicate match { | ||
case true => string | ||
case false => "" | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
api/shared/src/main/scala-2.13/play/twirl/api/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl | ||
|
||
import scala.reflect.ClassTag | ||
|
||
package object api { | ||
|
||
/** | ||
* Brings the template engine as a | ||
* [[http://docs.scala-lang.org/overviews/core/string-interpolation.html string interpolator]]. | ||
* | ||
* Basic usage: | ||
* | ||
* {{{ | ||
* import play.twirl.api.StringInterpolation | ||
* | ||
* val name = "Martin" | ||
* val htmlFragment: Html = html"<div>Hello \$name</div>" | ||
* }}} | ||
* | ||
* Three interpolators are available: `html`, `xml` and `js`. | ||
*/ | ||
implicit class StringInterpolation(val sc: StringContext) extends AnyVal { | ||
def html(args: Any*): Html = interpolate(args, HtmlFormat) | ||
|
||
def xml(args: Any*): Xml = interpolate(args, XmlFormat) | ||
|
||
def js(args: Any*): JavaScript = interpolate(args, JavaScriptFormat) | ||
|
||
def interpolate[A <: Appendable[A]: ClassTag](args: Seq[Any], format: Format[A]): A = { | ||
StringContext.checkLengths(args, sc.parts) | ||
val array = Array.ofDim[Any](args.size + sc.parts.size) | ||
val strings = sc.parts.iterator | ||
val expressions = args.iterator | ||
array(0) = format.raw(strings.next()) | ||
var i = 1 | ||
while (strings.hasNext) { | ||
array(i) = expressions.next() | ||
array(i + 1) = format.raw(strings.next()) | ||
i += 2 | ||
} | ||
new BaseScalaTemplate[A, Format[A]](format)._display_(array) | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
api/shared/src/main/scala-3/play/twirl/api/BaseScalaTemplate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl.api | ||
|
||
import java.util.Optional | ||
import scala.collection.immutable | ||
import scala.jdk.CollectionConverters._ | ||
import scala.reflect.ClassTag | ||
|
||
case class BaseScalaTemplate[T <: Appendable[T], F <: Format[T]](format: F) { | ||
// The overloaded methods are here for speed. The compiled templates | ||
// can take advantage of them for a 12% performance boost | ||
def _display_(x: AnyVal): T = format.escape(x.toString) | ||
def _display_(x: String): T = if (x eq null) format.empty else format.escape(x) | ||
def _display_(x: Unit): T = format.empty | ||
def _display_(x: scala.xml.NodeSeq): T = if (x eq null) format.empty else format.raw(x.toString()) | ||
def _display_(x: T): T = if (x eq null) format.empty else x | ||
|
||
def _display_(o: Any)(implicit m: ClassTag[T]): T = { | ||
o match { | ||
case escaped if escaped != null && escaped.getClass == m.runtimeClass => escaped.asInstanceOf[T] | ||
case () => format.empty | ||
case None => format.empty | ||
case Some(v) => _display_(v) | ||
case key: Optional[_] => | ||
(if (key.isPresent) Some(key.get) else None) match { | ||
case None => format.empty | ||
case Some(v) => _display_(v) | ||
case null => format.empty | ||
} | ||
case xml: scala.xml.NodeSeq => format.raw(xml.toString()) | ||
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_)) | ||
case escapeds: TraversableOnce[_] => format.fill(escapeds.iterator.map(_display_).toList) | ||
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList) | ||
case escapeds: java.util.List[_] => | ||
format.fill(escapeds.asScala.map(_display_).toList) | ||
case string: String => format.escape(string) | ||
case v if v != null => format.escape(v.toString) | ||
case null => format.empty | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
api/shared/src/main/scala-3/play/twirl/api/TwirlHelperImports.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl.api | ||
|
||
import scala.language.implicitConversions | ||
|
||
/** | ||
* Imports for useful Twirl helpers. | ||
*/ | ||
object TwirlHelperImports { | ||
|
||
/** Allows Java collections to be used as if they were Scala collections. */ | ||
implicit def twirlJavaCollectionToScala[T](x: java.lang.Iterable[T]): Iterable[T] = { | ||
import scala.jdk.CollectionConverters._ | ||
x.asScala | ||
} | ||
|
||
/** Allows inline formatting of java.util.Date */ | ||
implicit class TwirlRichDate(date: java.util.Date) { | ||
def format(pattern: String): String = { | ||
new java.text.SimpleDateFormat(pattern).format(date) | ||
} | ||
} | ||
|
||
/** Adds a when method to Strings to control when they are rendered. */ | ||
implicit class TwirlRichString(string: String) { | ||
def when(predicate: => Boolean): String = { | ||
predicate match { | ||
case true => string | ||
case false => "" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl | ||
|
||
import scala.reflect.ClassTag | ||
|
||
package object api { | ||
|
||
/** | ||
* Brings the template engine as a | ||
* [[http://docs.scala-lang.org/overviews/core/string-interpolation.html string interpolator]]. | ||
* | ||
* Basic usage: | ||
* | ||
* {{{ | ||
* import play.twirl.api.StringInterpolation | ||
* | ||
* val name = "Martin" | ||
* val htmlFragment: Html = html"<div>Hello \$name</div>" | ||
* }}} | ||
* | ||
* Three interpolators are available: `html`, `xml` and `js`. | ||
*/ | ||
implicit class StringInterpolation(val sc: StringContext) extends AnyVal { | ||
def html(args: Any*): Html = interpolate(args, HtmlFormat) | ||
|
||
def xml(args: Any*): Xml = interpolate(args, XmlFormat) | ||
|
||
def js(args: Any*): JavaScript = interpolate(args, JavaScriptFormat) | ||
|
||
def interpolate[A <: Appendable[A]: ClassTag](args: Seq[Any], format: Format[A]): A = { | ||
StringContext.checkLengths(args, sc.parts) | ||
val array = Array.ofDim[Any](args.size + sc.parts.size) | ||
val strings = sc.parts.iterator | ||
val expressions = args.iterator | ||
array(0) = format.raw(strings.next()) | ||
var i = 1 | ||
while (strings.hasNext) { | ||
array(i) = expressions.next() | ||
array(i + 1) = format.raw(strings.next()) | ||
i += 2 | ||
} | ||
new BaseScalaTemplate[A, Format[A]](format)._display_(array) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.