Skip to content

Commit

Permalink
Interpreter takes a charset
Browse files Browse the repository at this point in the history
  • Loading branch information
edadma committed May 25, 2018
1 parent 897d8ce commit 32300db
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 373 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Liquescent
[![Build Status](https://www.travis-ci.org/edadma/liquescent.svg?branch=master)](https://www.travis-ci.org/edadma/liquescent)
[![Coverage Status](https://coveralls.io/repos/github/edadma/liquescent/badge.svg?branch=master)](https://coveralls.io/github/edadma/liquescent?branch=master)
[![License](https://img.shields.io/badge/license-ISC-blue.svg)](https://github.com/edadma/liquescent/blob/master/LICENSE)
[![Version](https://img.shields.io/badge/latest_release-v0.2.3-orange.svg)](https://github.com/edadma/liquescent/releases/tag/v0.2.3)
[![Version](https://img.shields.io/badge/latest_release-v0.2.4-orange.svg)](https://github.com/edadma/liquescent/releases/tag/v0.2.4)

*Liquescent* is an implementation of the [Liquid](https://shopify.github.io/liquid/) templating language for the [Scala](http://scala-lang.org) programming language.

Expand Down Expand Up @@ -96,7 +96,7 @@ This program prints
This next example shows how to use *Liquescent* as an executable on the command line.

```bash
echo "{{ v | join: \", \" }}" | java -jar liquescent-0.2.3.jar -j "{v: [\"one\", \"two\", \"three\"]}" --
echo "{{ v | join: \", \" }}" | java -jar liquescent-0.2.4.jar -j "{v: [\"one\", \"two\", \"three\"]}" --
```

The above command prints
Expand All @@ -118,7 +118,7 @@ Use the following definition to use Liquescent in your Maven project:
<dependency>
<groupId>xyz.hyperreal</groupId>
<artifactId>liquescent</artifactId>
<version>0.2.3</version>
<version>0.2.4</version>
</dependency>
```

Expand All @@ -127,7 +127,7 @@ Add the following to your `build.sbt` file to use Liquescent in your SBT project
```sbt
resolvers += "Hyperreal Repository" at "https://dl.bintray.com/edadma/maven"

libraryDependencies += "xyz.hyperreal" %% "liquescent" % "0.2.3"
libraryDependencies += "xyz.hyperreal" %% "liquescent" % "0.2.4"
```

Building
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "liquescent"

version := "0.2.3"
version := "0.2.4"

scalaVersion := "2.12.6"

Expand Down
24 changes: 16 additions & 8 deletions src/main/scala/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package xyz.hyperreal.liquescent

import java.io.{ByteArrayOutputStream, File, PrintStream}
import java.nio.charset.Charset

import xyz.hyperreal.lia.Math
import xyz.hyperreal.numbers.BigDecimalMath
Expand All @@ -10,7 +11,7 @@ import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer


class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], settings: Map[Symbol, Any], assigns: Map[String, Any], context: AnyRef ) {
class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], settings: Map[Symbol, Any], assigns: Map[String, Any], context: AnyRef, charset: Charset ) {

val globals = new mutable.HashMap[String, Any] ++ assigns
val scopes = new ArrayBuffer[mutable.HashMap[String, Any]]
Expand Down Expand Up @@ -40,8 +41,10 @@ class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], setting

def capture( statement: StatementAST, locals: Map[String, Any] ) = {
val bytes = new ByteArrayOutputStream
val ps = new PrintStream( bytes, false, charset.name )

execute( statement, locals, new PrintStream(bytes) )
execute( statement, locals, ps )
ps.flush
bytes.toString
}

Expand All @@ -68,21 +71,26 @@ class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], setting
def include( input: File, locals: Map[String, Any], out: PrintStream ): Unit = include( io.Source.fromFile(input), locals, out )

def execute( op: StatementAST, locals: Map[String, Any], out: PrintStream ): Unit = {
def output( a: Any ) = {
out.print( a )
out.flush
}

op match {
// case LayoutStatementAST( _ ) =>
case TextOutputStatementAST( s ) => out.print( s )
case RawStatementAST( s, _, _ ) => out.print( s )
case TextOutputStatementAST( s ) => output( s )
case RawStatementAST( s, _, _ ) => output( s )
case CommentStatementAST( _, _, _) =>
case ExpressionOutputStatementAST( expr, _, _ ) =>
out.print( display(eval( expr, locals )) )
output( display(eval( expr, locals )) )
// eval( expr, locals ) match {
// case l: List[_] => l map display mkString
// case s => display( s )
// }
// )
case AssignStatementAST( name, expr, _, _ ) => setVar( name, eval(expr, locals) )
case IncrementStatementAST( name, _, _ ) =>
out.print( incdec get name match {
output( incdec get name match {
case None =>
incdec(name) = 0
0
Expand All @@ -93,7 +101,7 @@ class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], setting
res
} )
case DecrementStatementAST( name, _, _ ) =>
out.print( incdec get name match {
output( incdec get name match {
case None =>
incdec(name) = -1
-1
Expand Down Expand Up @@ -178,7 +186,7 @@ class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], setting
}

exitScope
case CycleStatementAST( items, _, _ ) => out.print( display(eval(items(getVar("#idx", locals).asInstanceOf[Int]%items.length), locals)) )
case CycleStatementAST( items, _, _ ) => output( display(eval(items(getVar("#idx", locals).asInstanceOf[Int]%items.length), locals)) )
case BreakStatementAST( _, _ ) => throw new BreakException
case ContinueStatementAST( _, _ ) => throw new ContinueException
}
Expand Down

0 comments on commit 32300db

Please sign in to comment.