edadma / backslash   0.4

ISC License Website GitHub

a string templating language written in Scala Native

Scala versions: 2.12

Backslash

Build Status Build status Coverage Status License Version

Backslash is a string templating language written in Scala. Backslash looks like TeX with the default delimiters (which can be changed), but it's not TeX. Backslash behaves like any other macro/templating language: it copies input text to output. Backslash is an attempt to create a dryer templating language that still allows you to copy HTML (or whatever you're using it for) verbatim. So, although Backslash is somewhat inspired by TeX, it shares very little in common with it except less typing. If your HTML doesn't containing any scripting then the "TeXish" delimiters (\, {, }) are usually fine. However, as in Mustache, delimiters can be changed anywhere in the input stream (except between command arguments).

Examples

Templating

Here's a typical Backslash template with looping and conditionals.

Template

<h3>Products</h3>

<ul>
  \for products {
    <li>\name&emsp;$\price&emsp;
      \if inStock {
        <a href="#">Buy It!</a>
      } \else {
        Out of stock.
      }
    </li>
  }
</ul>

Data

{
  "products": [
    {
      "name": "RCA 32\u2033 ROKU SMART TV",
      "price": 207.00,
      "inStock": true
    },
    {
      "name": "LG 55UK6300",
      "price": 1098.00,
      "inStock": false
    }
  ]
}

Output

<h3>Products</h3>

<ul>

    <li>RCA 32″ ROKU SMART TV&emsp; $207.00&emsp;

        <a href="#">Buy It!</a>

    </li>

    <li>LG 55UK6300&emsp; $1098.00&emsp;

        Out of stock.

    </li>

</ul>

Library

This example program shows how to create a custom command to output an HTML unordered list, and also demonstrates a Backslash \for loop.

import scala.util.parsing.input.Position

import io.github.edadma.backslash._


object Example extends App {

  val input =
    """
      |<h2>Vaudeville Acts</h2>
      |<ol>
      |  \for \in act acts {
      |    <li>
      |      <h3>\act.name</h3>
      |      \list \act.members
      |    </li>
      |  }
      |</ol>
    """.trim.stripMargin
  val acts =
    List(
      Map(
        "name" -> "Three Stooges",
        "members" -> List( "Larry", "Moe", "Curly" )
      ),
      Map(
        "name" -> "Andrews Sisters",
        "members" -> List( "LaVerne", "Maxine", "Patty" )
      ),
      Map(
        "name" -> "Abbott and Costello",
        "members" -> List( "William (Bud) Abbott", "Lou Costello" )
      )
    )
  val customCommand =
    new Command( "list", 1 ) {
      def apply( pos: Position, rendered: Renderer, args: List[Any], context: AnyRef ) = {
        val list = args.head.asInstanceOf[List[String]]

        s"<ul>${list map (item => s"<li>$item</li>") mkString}</ul>"
      }
    }

  val parser = new Parser( Command.standard ++ Map("list" -> customCommand) )
  val renderer = new Renderer( parser, Map() )

  renderer.render( parser.parse(io.Source.fromString(input)), Map("acts" -> acts), Console.out )
}

This program prints

<h2>Vaudeville Acts</h2>
<ol>

    <li>
      <h3>Three Stooges</h3>
      <ul><li>Larry</li><li>Moe</li><li>Curly</li></ul></li>

    <li>
      <h3>Andrews Sisters</h3>
      <ul><li>LaVerne</li><li>Maxine</li><li>Patty</li></ul></li>

    <li>
      <h3>Abbott and Costello</h3>
      <ul><li>William (Bud) Abbott</li><li>Lou Costello</li></ul></li>

</ol>

Executable

This next example shows how to use Backslash as an executable from the command line.

echo "testing \join \v \", \"" | java -jar backslash-0.4.23.jar -j "{v: [\"one\", \"two\", \"three\"]}" --

The above command prints

testing one, two, three

Usage

Library

Use the following definition to use Backslash in your Maven project:

<repository>
  <id>hyperreal</id>
  <url>https://dl.bintray.com/edadma/maven</url>
</repository>

<dependency>
  <groupId>io.github.edadma</groupId>
  <artifactId>backslash</artifactId>
  <version>0.4.23</version>
</dependency>

Add the following to your build.sbt file to use Backslash in your SBT project:

resolvers += "Hyperreal Repository" at "https://dl.bintray.com/edadma/maven"

libraryDependencies += "io.github.edadma" %% "backslash" % "0.4.23"

Executable

An executable can be downloaded from here. You do not need the Scala library for it to work because the JAR already contains all dependencies. You just need Java 11+ installed.

Run it as a normal Java executable JAR with the command java -jar backslash-0.4.23.jar <template> in the folder where you downloaded the file, where template is the name of the template file to be rendered.

Building

Requirements

  • Java 11
  • SBT 1.2.8+
  • Scala 2.13.0+

Clone and Assemble Executable

git clone git://github.com/edadma/backslash.git
cd backslash
sbt assembly

The command sbt assembly also runs all the unit tests.

License

ISC © 2019 Edward A. Maxedon, Sr.