string-interpolate-0.3.1.0: Haskell string/text/bytestring interpolation that just works
Copyright(c) William Yao 2019-2021
LicenseBSD-3
Maintainerwilliamyaoh@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Data.String.Interpolate

Description

This module provides three quasiquoters, i, __i, and iii, which:

  • handle all of String/Text/ByteString, both strict and lazy
  • can interpolate into anything that implements IsString
  • can interpolate anything that implements Show
  • are Unicode aware
  • are fast
  • handle multiline strings

i leaves newlines and whitespace intact as they are in the source code. __i strips leading indentation and surrounding blank lines, while leaving linebreaks intact. iii collapses newlines/whitespace into single spaces, putting all the output on a single line.

As an example,

{-# LANGUAGE OverloadedStrings #-}

import Data.Text
import Data.String.Interpolate ( i )

λ> age = 33 :: Int
λ> name = "Tatiana" :: Text
λ> [i|{"name": "#{name}", "age": #{age}}|] :: String
>>> "{\"name\": \"Tatiana\", \"age\": 33}"

λ> [i|
Name: #{name}
Age: #{age}
|] :: String
>>> "\nName: Tatiana\nAge: 33\n"

There are also variants of __i and iii which have different behavior for surrounding newlines.

See the README at https://gitlab.com/williamyaoh/string-interpolate/blob/master/README.md for more details and examples.

Synopsis

Basic interpolators

i :: QuasiQuoter Source #

The basic, no-frills interpolator. Will interpolate anything you wrap in #{}, and otherwise leaves what you write alone.

__i :: QuasiQuoter Source #

An interpolator that handles indentation. Will interpolate anything you wrap in #{}, remove leading indentation, and remove any blank lines before and after the content.

If the contained interpolation uses both tabs and spaces for indentation, __i will assume the indentation type it finds in the first nonblank line, ignoring indentation of the other type. Please don't use mixed indentation.

Note that only indentation you actually write in source code will be stripped; __i does not touch any lines or whitespace inserted by interpolations themselves.

There is no extra performance penalty for using __i.

iii :: QuasiQuoter Source #

An interpolator that strips excess whitespace. Will collapse any sequences of multiple spaces or whitespace into a single space, putting the output onto a single line with surrounding whitespace removed.

Note that only whitespace you actually write in source code will be collapsed; iii does not touch any lines or whitespace inserted by interpolations themselves.

There is no extra performance penalty for using iii.

Interpolator variants for newline handling

__i'E :: QuasiQuoter Source #

Like __i, but leaves any surrounding newlines intact.

The way to remember which is which is to look at the suffix character; the multiple horizontal lines of the capital E suggests multiple textual lines.

__i'L :: QuasiQuoter Source #

Like __i, but collapses any surrounding newlines into a single newline.

The way to remember which is which is to look at the suffix character; the single horizontal line of the capital L suggests that it leaves only a single newline.

iii'E :: QuasiQuoter Source #

Like iii, but leaves any surrounding newlines intact.

The way to remember which is which is to look at the suffix character; the multiple horizontal lines of the capital E suggests multiple textual lines.

iii'L :: QuasiQuoter Source #

Like iii, but collapses any surrounding newlines into a single newline.

The way to remember which is which is to look at the suffix character; the single horizontal line of the capital L suggests that it leaves only a single newline.