heredoc-0.2.0.0/0000755000000000000000000000000012142035255011445 5ustar0000000000000000heredoc-0.2.0.0/heredoc.cabal0000644000000000000000000000117412142035255014045 0ustar0000000000000000name: heredoc version: 0.2.0.0 synopsis: multi-line string / here document using QuasiQuotes description: multi-line string / here document using QuasiQuotes license: PublicDomain license-file: LICENSE.txt author: James H. Fisher maintainer: jameshfisher@gmail.com homepage: http://hackage.haskell.org/package/heredoc category: Text build-type: Simple cabal-version: >=1.8 library exposed-modules: Text.Heredoc build-depends: base >= 4 && < 5, template-haskell >= 2.5 hs-source-dirs: src heredoc-0.2.0.0/Setup.hs0000644000000000000000000000005612142035255013102 0ustar0000000000000000import Distribution.Simple main = defaultMain heredoc-0.2.0.0/LICENSE.txt0000644000000000000000000000227312142035255013274 0ustar0000000000000000This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. 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 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. For more information, please refer to heredoc-0.2.0.0/src/0000755000000000000000000000000012142035255012234 5ustar0000000000000000heredoc-0.2.0.0/src/Text/0000755000000000000000000000000012142035255013160 5ustar0000000000000000heredoc-0.2.0.0/src/Text/Heredoc.hs0000644000000000000000000000647512142035255015101 0ustar0000000000000000module Text.Heredoc (here, there, str) where import Data.Maybe (fromMaybe) import Data.List (intercalate) import Language.Haskell.TH ( litE , stringL ) import Language.Haskell.TH.Quote ( QuasiQuoter ( QuasiQuoter , quoteExp , quotePat , quoteType , quoteDec ) , quoteFile ) data Ctx = Exp | Pat | Type | Dec qq :: String -> Ctx -> QuasiQuoter qq qqName correctCtx = QuasiQuoter { quoteExp = const $ error $ errorString Exp , quotePat = const $ error $ errorString Pat , quoteType = const $ error $ errorString Type , quoteDec = const $ error $ errorString Dec } where errorString ctx = "You have used the `" ++ qqName ++ "` QuasiQuoter " ++ "in " ++ ctxName ctx ++ " context; " ++ "you must only use it in " ++ ctxName correctCtx ++ " context" ctxName c = case c of Exp -> "an expression" Pat -> "a pattern" Type -> "a type" Dec -> "a declaration" toUnix :: String -> String toUnix cs = case cs of '\r':'\n' : cs -> '\n' : toUnix cs '\r' : cs -> '\n' : toUnix cs c : cs -> c : toUnix cs [] -> [] {-| Create a string-literal expression from the string being quoted. Newline literals are normalized to UNIX newlines (one '\n' character). -} here :: QuasiQuoter here = (qq "here" Exp) { quoteExp = litE . stringL . toUnix } {-| Create a string-literal expression from the contents of the file at the filepath being quoted. Newline literals are normalized to UNIX newlines (one '\n' character). -} there :: QuasiQuoter there = quoteFile here {-| Create a multi-line string literal whose left edge is demarcated by the "pipe" character ('|'). For example, >famousQuote = [str|Any dictator would admire the > |uniformity and obedience of the U.S. media. > | > | -- Noam Chomsky > |] is functionally equivalent to >famousQuote = "Any dictator would admire the\n" ++ > "uniformity and obedience of the U.S. media.\n" ++ > "\n" ++ > " -- Noam Chomsky\n" If desired, you can have a ragged left-edge, so >myHtml = [str| > | > |

My home page

> | > | > |] is functionally equivalent to >myHtml = "\n" ++ > "\n" ++ > "

My home page

\n" ++ > "\n" ++ > "\n" -} str :: QuasiQuoter str = (qq "str" Exp) { quoteExp = litE . stringL . intercalate "\n" . unPipe . lines . toUnix } where unPipe ls = case ls of [] -> [] l : ls -> l : case splitLast ls of Nothing -> [] Just (middles, last) -> map removePipe middles ++ [fromMaybe "" (tryRemovePipe last)] where removePipe cs = case tryRemovePipe cs of Nothing -> error "no pipe character found in line '" ++ cs ++ "'" Just cs -> cs tryRemovePipe cs = case dropWhile (/='|') cs of [] -> Nothing c:cs -> Just cs splitLast :: [a] -> Maybe ([a], a) splitLast xs = case reverse xs of [] -> Nothing l:i -> Just (reverse i, l)