Skip to content

Commit

Permalink
code 'include' tag
Browse files Browse the repository at this point in the history
  • Loading branch information
edadma committed May 5, 2018
1 parent 47303db commit 011c988
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 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_snapshot_4-orange.svg)](https://github.com/edadma/liquescent/releases/tag/v0.2_snapshot_4)
[![Version](https://img.shields.io/badge/latest_release-v0.2_snapshot_5-orange.svg)](https://github.com/edadma/liquescent/releases/tag/v0.2_snapshot_5)

*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 @@ -52,7 +52,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_snapshot_4.jar -j "{v: [\"one\", \"two\", \"three\"]}" --
echo "{{ v | join: \", \" }}" | java -jar liquescent-0.2_snapshot_5.jar -j "{v: [\"one\", \"two\", \"three\"]}" --
```

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

Expand All @@ -83,7 +83,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_snapshot_4"
libraryDependencies += "xyz.hyperreal" %% "liquescent" % "0.2_snapshot_5"
```

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_snapshot_4"
version := "0.2_snapshot_5"

scalaVersion := "2.12.6"

Expand Down
1 change: 1 addition & 0 deletions htdocs/snippets/test.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a {{ test }} snippet
1 change: 1 addition & 0 deletions src/main/scala/AST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ case object ContinueStatementAST extends StatementAST
case class CycleStatementAST( items: Vector[ExpressionAST] ) extends StatementAST
case class CustomTagStatementAST( name: String, args: List[ExpressionAST] ) extends StatementAST
case class LayoutStatementAST( layout: Option[String] ) extends StatementAST
case class IncludeStatementAST( name: String, vars: List[(String, ExpressionAST)] ) extends StatementAST

case class ForGenerator( name: String, expr: ExpressionAST, parameters: List[ForParameter] )

Expand Down
7 changes: 5 additions & 2 deletions src/main/scala/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], setting
execute( parse.statement, locals, out )
}

def include( input: File, locals: Map[String, Any], out: PrintStream ) =
execute( LiquescentParser.parse(io.Source.fromFile(input)).statement, locals, out )
def include( input: io.Source, locals: Map[String, Any], out: PrintStream ) = execute( LiquescentParser.parse(input).statement, locals, out )

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 = {
op match {
Expand Down Expand Up @@ -136,6 +137,8 @@ class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], setting
case Some( (_, thenStatement) ) => execute( thenStatement, locals, out )
}
case CaptureStatementAST( name, body ) => setVar( name, capture(body, locals) )
case IncludeStatementAST( name, args ) =>
include( docroot(s"snippets/$name.liquid", settings), locals ++ (args map {case (k, v) => (k, eval(v, locals))}), out )
case ForStatementAST( name, expr, parameters, body ) =>
var list =
eval( expr, locals ) match {
Expand Down
21 changes: 20 additions & 1 deletion src/main/scala/LiquescentParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,13 @@ object LiquescentParser {

advance
_parseBlock
case TagElement( "increment", s ) =>
case TagElement( "include", s ) =>
val parser = new ElementParser

advance
block += parser( parser.includeTag, s )
_parseBlock
case TagElement( "increment", s ) =>
val parser = new ElementParser

advance
Expand Down Expand Up @@ -400,6 +406,19 @@ class ElementParser extends RegexParsers with PackratParsers {

lazy val incrementTag: PackratParser[IncrementStatementAST] = tagStart ~> "increment" ~> ident <~ tagEnd ^^ IncrementStatementAST

lazy val includeTag: PackratParser[IncludeStatementAST] =
(tagStart ~> "include" ~> string) ~ ("with" ~> expression <~ tagEnd) ^^ {
case f ~ v => IncludeStatementAST( f, List((f, v)) ) } |
(tagStart ~> "include" ~> string) ~ (opt("," ~> rep1sep(includeArgument, ",")) <~ tagEnd) ^^ {
case f ~ None => IncludeStatementAST( f, Nil )
case f ~ Some( a ) => IncludeStatementAST( f, a )
}

lazy val includeArgument: PackratParser[(String, ExpressionAST)] =
(ident <~ ":") ~ expression ^^ {
case k ~ v => (k, v)
}

lazy val layoutTag: PackratParser[LayoutStatementAST] =
tagStart ~> "layout" ~> string <~ tagEnd ^^ (s => LayoutStatementAST( Some(s) )) |
tagStart ~> "layout" ~> "none" <~ tagEnd ^^^ LayoutStatementAST( None )
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ object Main extends App {

def usage {
"""
|liquescent v0.2_snapshot_4
|liquescent v0.2_snapshot_5
|
|Usage: java -jar liquescent-0.2_snapshot_4.jar <options> <liquid template>
|Usage: java -jar liquescent-0.2_snapshot_5.jar <options> <liquid template>
|
|Options: --help display this help and exit
| -s <name> <string> assign <string> to variable <name>
Expand Down
3 changes: 2 additions & 1 deletion src/test/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ object Main extends Testing with App {
val res =
test(
"""
|{{ 'theme.css' | asset_url | stylesheet_tag }}
|{% assign test = 'cute' %}
|{% include 'test' %}
""".trim.stripMargin, false
)

Expand Down

0 comments on commit 011c988

Please sign in to comment.