Skip to content

Commit

Permalink
Add textNL quasiquote that normalizes newlines
Browse files Browse the repository at this point in the history
For nikita-volkov#14.

Missing:

- full testing.
- more complete docs.
- using textNL in examples?
  • Loading branch information
Blaisorblade committed Aug 13, 2016
1 parent 90b918e commit a49dede
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 11 additions & 5 deletions library/NeatInterpolation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
-- results in
--
-- > f "funny" == "$my funny ${string}|]
module NeatInterpolation (text) where
module NeatInterpolation (text, textNL) where

import BasePrelude

Expand All @@ -96,8 +96,14 @@ import qualified Data.Text as T
-- |
-- The quasiquoter.
text :: QuasiQuoter
text = QuasiQuoter {quoteExp = quoteExprExp}
text = QuasiQuoter {quoteExp = quoteExprExp False}

-- |
-- A variant of the quasiquoter, insensitive to source line end style (Unix or
-- Windows). That is, newlines in your file are parsed as \n, irrespective of
-- how the source file was saved or checked out.
textNL :: QuasiQuoter
textNL = QuasiQuoter {quoteExp = quoteExprExp True}
-- |
-- A function used internally by the quasiquoter. Just ignore it.
indentQQPlaceholder :: Int -> Text -> Text
Expand All @@ -107,9 +113,9 @@ indentQQPlaceholder indent text = case T.lines text of
[] -> text


quoteExprExp :: String -> Q Exp
quoteExprExp input =
case parseLines $ normalizeQQInput input of
quoteExprExp :: Bool -> String -> Q Exp
quoteExprExp normalizeNL input =
case parseLines $ normalizeQQInput normalizeNL input of
Left e -> fail $ show e
Right lines -> sigE (appE [|T.unlines|] $ listE $ map lineExp lines)
[t|Text|]
Expand Down
10 changes: 8 additions & 2 deletions library/NeatInterpolation/String.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ module NeatInterpolation.String where
import BasePrelude


normalizeQQInput :: [Char] -> [Char]
normalizeQQInput = trim . unindent' . tabsToSpaces
normalizeQQInput :: Bool -> [Char] -> [Char]
normalizeQQInput normalizeNL =
if normalizeNL then
filter (/= '\r') . body
else
body
where
body :: [Char] -> [Char]
body = trim . unindent' . tabsToSpaces
unindent' :: [Char] -> [Char]
unindent' s =
case lines s of
Expand Down

0 comments on commit a49dede

Please sign in to comment.