neat-interpolation-0.2.0: A quasiquoter for neat and simple multiline text interpolation

Safe HaskellNone

NeatInterpolation

Description

NeatInterpolation provides a quasiquoter for producing strings with a simple interpolation of input values. It removes the excessive indentation from the input and accurately manages the indentation of all lines of interpolated variables. But enough words, the code shows it better.

Consider the following declaration:

 {-# LANGUAGE QuasiQuotes, OverloadedStrings #-}
 
 import NeatInterpolation
 
 f :: String -> String -> String
 f a b = 
   [string|
     function(){
       function(){
         $a
       }
       return $b
     }
   |]

Executing the following:

 main = putStrLn $ f "1" "2"

will produce this (notice the reduced indentation compared to how it was declared):

 function(){
   function(){
     1
   }
   return 2
 }

Now let's test it with multiline string parameters:

 main = putStrLn $ f 
   "{\n  indented line\n  indented line\n}" 
   "{\n  indented line\n  indented line\n}" 

We get

 function(){
   function(){
     {
       indented line
       indented line
     }
   }
   return {
     indented line
     indented line
   }
 }

See how it neatly preserved the indentation levels of lines the variable placeholders were at?

Synopsis

Documentation

string :: QuasiQuoterSource

The quasiquoter.

indentQQPlaceholder :: Int -> String -> StringSource

A function used internally by quasiquoter. Just ignore it.