neat-interpolation-0.3.2.1/0000755000000000000000000000000012742366137013670 5ustar0000000000000000neat-interpolation-0.3.2.1/LICENSE0000644000000000000000000000204112742366137014672 0ustar0000000000000000Copyright (c) 2013, Nikita Volkov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.neat-interpolation-0.3.2.1/neat-interpolation.cabal0000644000000000000000000000516412742366137020476 0ustar0000000000000000name: neat-interpolation version: 0.3.2.1 synopsis: A quasiquoter for neat and simple multiline text interpolation description: A quasiquoter for producing Text values with support for a simple interpolation of input values. It removes the excessive indentation from the input and accurately manages the indentation of all lines of the interpolated variables. category: String, QuasiQoutes license: MIT license-file: LICENSE copyright: (c) 2013, Nikita Volkov author: Nikita Volkov maintainer: Nikita Volkov homepage: https://github.com/nikita-volkov/neat-interpolation bug-reports: https://github.com/nikita-volkov/neat-interpolation/issues build-type: Simple cabal-version: >=1.10 source-repository head type: git location: git://github.com/nikita-volkov/neat-interpolation.git library hs-source-dirs: library exposed-modules: NeatInterpolation other-modules: NeatInterpolation.Parsing NeatInterpolation.String build-depends: text == 1.*, parsec >= 3 && < 4, template-haskell >= 2.8 && < 3, base-prelude < 2, base >= 4.6 && < 5 ghc-options: -funbox-strict-fields default-extensions: Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor, DeriveGeneric, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples default-language: Haskell2010 test-suite api-tests type: exitcode-stdio-1.0 hs-source-dirs: executables main-is: APITests.hs build-depends: neat-interpolation, HTF >= 0.11 && < 0.14, base-prelude default-extensions: Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor, DeriveGeneric, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples default-language: Haskell2010 neat-interpolation-0.3.2.1/Setup.hs0000644000000000000000000000005612742366137015325 0ustar0000000000000000import Distribution.Simple main = defaultMain neat-interpolation-0.3.2.1/executables/0000755000000000000000000000000012742366137016174 5ustar0000000000000000neat-interpolation-0.3.2.1/executables/APITests.hs0000644000000000000000000000226012742366137020164 0ustar0000000000000000{-# OPTIONS_GHC -F -pgmF htfpp #-} import BasePrelude import Test.Framework import NeatInterpolation main = htfMain $ htf_thisModulesTests test_demo = do assertEqual "function(){\n function(){\n {\n indented line\n indented line\n }\n }\n return {\n indented line\n indented line\n }\n}\n" (template a a) assertEqual "this_could_be_one_long_identifier\n" (escaped "one") where template a b = [text| function(){ function(){ $a } return $b } |] escaped name = [text|this_could_be_${name}_long_identifier|] a = "{\n indented line\n indented line\n}" test_dollar = do assertEqual "function(){\n function(){\n {\n indented line\n indented line\n }\n }\n return \"$b\"\n}\n" (template a a) assertEqual "this_could_be_$one$_long_identifier\n" (escaped "one") where template a b = [text| function(){ function(){ $a } return "$$b" } |] escaped name = [text|this_could_be_$$${name}$$_long_identifier|] a = "{\n indented line\n indented line\n}" neat-interpolation-0.3.2.1/library/0000755000000000000000000000000012742366137015334 5ustar0000000000000000neat-interpolation-0.3.2.1/library/NeatInterpolation.hs0000644000000000000000000000655712742366137021344 0ustar0000000000000000{-# OPTIONS_GHC -fno-warn-missing-fields #-} -- | -- 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 #-} -- > -- > import NeatInterpolation -- > import Data.Text (Text) -- > -- > f :: Text -> Text -> Text -- > f a b = -- > [text| -- > function(){ -- > function(){ -- > $a -- > } -- > return $b -- > } -- > |] -- -- Executing the following: -- -- > main = T.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 = T.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? -- -- If you need to separate variable placeholder from the following text to -- prevent treating the rest of line as variable name, use escaped variable: -- -- > f name = [text|this_could_be_${name}_long_identifier|] -- -- So -- -- > f "one" == "this_could_be_one_long_identifier" -- -- If you want to write something that looks like a variable but should be -- inserted as-is, escape it with another @$@: -- -- > f word = [text|$$my ${word} $${string}|] -- -- results in -- -- > f "funny" == "$my funny ${string}|] module NeatInterpolation (text) where import BasePrelude import Language.Haskell.TH import Language.Haskell.TH.Quote import NeatInterpolation.String import NeatInterpolation.Parsing import Data.Text (Text) import qualified Data.Text as T -- | -- The quasiquoter. text :: QuasiQuoter text = QuasiQuoter {quoteExp = quoteExprExp} -- | -- A function used internally by the quasiquoter. Just ignore it. indentQQPlaceholder :: Int -> Text -> Text indentQQPlaceholder indent text = case T.lines text of head:tail -> T.intercalate (T.pack "\n") $ head : map (T.replicate indent (T.singleton ' ') <>) tail [] -> text quoteExprExp :: String -> Q Exp quoteExprExp input = case parseLines $ normalizeQQInput input of Left e -> fail $ show e Right lines -> sigE (appE [|T.unlines|] $ listE $ map lineExp lines) [t|Text|] lineExp :: Line -> Q Exp lineExp (Line indent contents) = case contents of [] -> [| T.empty |] [x] -> toExp x xs -> appE [|T.concat|] $ listE $ map toExp xs where toExp = contentExp (fromIntegral indent) contentExp :: Integer -> LineContent -> Q Exp contentExp _ (LineContentText text) = appE [|T.pack|] (stringE text) contentExp indent (LineContentIdentifier name) = do valueName <- lookupValueName name case valueName of Just valueName -> do appE (appE (varE 'indentQQPlaceholder) $ litE $ integerL indent) (varE valueName) Nothing -> fail $ "Value `" ++ name ++ "` is not in scope" neat-interpolation-0.3.2.1/library/NeatInterpolation/0000755000000000000000000000000012742366137020773 5ustar0000000000000000neat-interpolation-0.3.2.1/library/NeatInterpolation/Parsing.hs0000644000000000000000000000234712742366137022740 0ustar0000000000000000module NeatInterpolation.Parsing where import BasePrelude hiding (try, (<|>), many) import Text.Parsec hiding (Line) data Line = Line {lineIndent :: Int, lineContents :: [LineContent]} deriving (Show) data LineContent = LineContentText [Char] | LineContentIdentifier [Char] deriving (Show) parseLines :: [Char] -> Either ParseError [Line] parseLines = parse lines "NeatInterpolation.Parsing.parseLines" where lines = sepBy line newline <* eof line = Line <$> countIndent <*> many content countIndent = fmap length $ try $ lookAhead $ many $ char ' ' content = try escapedDollar <|> try identifier <|> contentText identifier = fmap LineContentIdentifier $ char '$' *> (try identifier' <|> between (char '{') (char '}') identifier') escapedDollar = fmap LineContentText $ char '$' *> count 1 (char '$') identifier' = many1 (alphaNum <|> char '\'' <|> char '_') contentText = do text <- manyTill anyChar end if null text then fail "Empty text" else return $ LineContentText $ text where end = (void $ try $ lookAhead escapedDollar) <|> (void $ try $ lookAhead identifier) <|> (void $ try $ lookAhead newline) <|> eof neat-interpolation-0.3.2.1/library/NeatInterpolation/String.hs0000644000000000000000000000251212742366137022575 0ustar0000000000000000module NeatInterpolation.String where import BasePrelude normalizeQQInput :: [Char] -> [Char] normalizeQQInput = trim . unindent' . tabsToSpaces where unindent' :: [Char] -> [Char] unindent' s = case lines s of head:tail -> let unindentedHead = dropWhile (== ' ') head minimumTailIndent = minimumIndent . unlines $ tail unindentedTail = case minimumTailIndent of Just indent -> map (drop indent) tail Nothing -> tail in unlines $ unindentedHead : unindentedTail [] -> [] trim :: [Char] -> [Char] trim = dropWhileRev isSpace . dropWhile isSpace dropWhileRev :: (a -> Bool) -> [a] -> [a] dropWhileRev p = foldr (\x xs -> if p x && null xs then [] else x:xs) [] unindent :: [Char] -> [Char] unindent s = case minimumIndent s of Just indent -> unlines . map (drop indent) . lines $ s Nothing -> s tabsToSpaces :: [Char] -> [Char] tabsToSpaces ('\t':tail) = " " ++ tabsToSpaces tail tabsToSpaces (head:tail) = head : tabsToSpaces tail tabsToSpaces [] = [] minimumIndent :: [Char] -> Maybe Int minimumIndent = listToMaybe . sort . map lineIndent . filter (not . null . dropWhile isSpace) . lines -- | Amount of preceding spaces on first line lineIndent :: [Char] -> Int lineIndent = length . takeWhile (== ' ')