packages feed

nowdoc 0.1.0.4 → 0.1.1.0

raw patch · 9 files changed

+184/−12 lines, 9 filesdep +bytestringdep ~basebinary-added

Dependencies added: bytestring

Dependency ranges changed: base

Files

ChangeLog.md view
@@ -1,3 +1,26 @@ # Changelog for nowdoc  ## Unreleased changes++* add QuasiQuoter txtfile+* add QuasiQuoter binfile++### txtfile++QuasiQuoter txtfile incerts file contents as string without transformation.+It read file as text file (with default encoding on your system).++```hs+main :: IO ()+main = putStr [txtfile|foo.txt|]+```++### binfile++QuasiQuoter txtfile incerts file contents as string without transformation.+It read file as binary file.++```hs+main :: IO ()+main = putStr [binfile|foo.dat|]+```
README.md view
@@ -1,12 +1,20 @@ # nowdoc +Package nowdoc contain 3 QuasiQuotes.++* nowdoc: Simplest here document with only two transformation.+* txtfile: It incerts text file contents as string.+* binfile: It incerts binary file contents as string.++### QuasiQuoter nowdoc+ Simplest here document. Only two transformation.  * remove head newline if exist * remove one space from '|', space, space, ..., ']' -## short examples+### short examples  ```hs hello = [nowdoc|@@ -50,7 +58,7 @@ hello = "hello |]\nworld| ]\n! |  ]" ``` -## run perl+### run perl  ```hs {-# LANGUAGE QuasiQuotes #-}@@ -68,7 +76,7 @@ |]] ``` -## ASCII art+### ASCII art  ```hs {-# LANGUAGE QuasiQuotes #-}@@ -95,4 +103,24 @@       `4$$$c.$$$..e$$P"                   `^^^^^^^` |]+```++### QuasiQuoter txtfile++It incerts file contents as string without transformation.+It read file as text file (with default encoding on your system).++```hs+main :: IO ()+main = putStr [txtfile|foo.txt|]+```++### QuasiQuoter binfile++It incerts file contents as string without transformation.+It read file as binary file.++```hs+main :: IO ()+main = print [binfile|foo.dat|] ```
nowdoc.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 210f2844c035c43c8f37a0625f4be9e91610d28eaba8a46bec9f0880222815ae+-- hash: 5e9ef15efc4d8eb467e78125b912796263a7f0e16a41a1ee798c9890659fac4c  name:           nowdoc-version:        0.1.0.4+version:        0.1.1.0 synopsis:       Here document without variable expansion like PHP Nowdoc description:    Please see the README on GitHub at <https://github.com/YoshikuniJujo/nowdoc#readme> category:       Text@@ -21,11 +21,16 @@ extra-source-files:     ChangeLog.md     README.md+    samples/sample.dat+    samples/sample.txt     samples/sample0.hs     samples/sample1.hs     samples/sample2.hs     samples/sample3.hs     samples/sample4.hs+    samples/sample5.hs+    samples/sample6.hs+    samples/sample7.hs  source-repository head   type: git@@ -40,6 +45,7 @@       src   build-depends:       base >=4.7 && <5+    , bytestring     , template-haskell   default-language: Haskell2010 @@ -53,6 +59,7 @@   ghc-options: -threaded -rtsopts -with-rtsopts=-N   build-depends:       base >=4.7 && <5+    , bytestring     , nowdoc     , template-haskell   default-language: Haskell2010
+ samples/sample.dat view

binary file changed (absent → 256 bytes)

+ samples/sample.txt view
@@ -0,0 +1,1 @@+Hello, world!
+ samples/sample5.hs view
@@ -0,0 +1,11 @@+{-# LANGUAGE QuasiQuotes #-}++import Text.Nowdoc++main :: IO ()+main = putStr [nowdoc|+main :: IO ()+main = putStr [nowdoc|+Hello, world!+| ]+|]
+ samples/sample6.hs view
@@ -0,0 +1,7 @@+{-# LANGUAGE QuasiQuotes #-}++import Text.Nowdoc++main :: IO ()+main = putStr [txtfile|+	samples/sample.txt |]
+ samples/sample7.hs view
@@ -0,0 +1,6 @@+{-# LANGUAGE QuasiQuotes #-}++import Text.Nowdoc++main :: IO ()+main = print [binfile| samples/sample.dat |]
src/Text/Nowdoc.hs view
@@ -1,17 +1,59 @@+{- |++Module:		Text.Nowdoc+Description:	3 QuasiQuoter: nowdoc, txtfile, binfile+Copyright:	(c) Yoshikuni Jujo, 2018+License:	BSD-3-Clause+Maintainer:	PAF01143@nifty.ne.jp++With QuasiQuotes language extension.++-}+ {-# LANGUAGE LambdaCase #-} {-# OPTIONS_GHC -Wall -fno-warn-tabs #-} -module Text.Nowdoc (nowdoc) where+module Text.Nowdoc (nowdoc, txtfile, binfile) where -import Language.Haskell.TH (stringE)+import Data.List (dropWhileEnd)+import Data.Char (isSpace)+import Language.Haskell.TH (ExpQ, stringE, runIO)+import Language.Haskell.TH.Syntax (addDependentFile) import Language.Haskell.TH.Quote (QuasiQuoter(..)) +import qualified Data.ByteString.Char8 as BSC++{- |+Simplest here document. Only two transformation.++* remove head newline if exist+* remove one space from '|', space, space, ..., ']'++@+main :: IO ()+main = putStrLn [nowdoc|hello|]+@++@+main :: IO ()+main = putStr [nowdoc|+Hello, world!+|]+@++@+main :: IO ()+main = putStr [nowdoc|+main :: IO ()+main = putStr [nowdoc|+Hello, world!+| ]+|]+@+-}+ nowdoc :: QuasiQuoter-nowdoc = QuasiQuoter {-	quoteExp = stringE . unescape . \case '\n' : cs -> cs; cs -> cs,-	quotePat = const $ err "pattern",-	quoteType = const $ err "type",-	quoteDec = const $ err "declaration" }+nowdoc = qq { quoteExp = stringE . unescape . \case '\n' : cs -> cs; cs -> cs }  unescape :: String -> String unescape ('|' : cs) = case span (== ' ') cs of@@ -20,7 +62,54 @@ unescape (c : cs) = c : unescape cs unescape "" = "" +{- |+QuasiQuoter txtfile incerts file contents as string without transformation.+It read file as text file (with default encoding on your system).++@+main :: IO ()+main = putStr [txtfile|foo.txt|]+@+-}++txtfile :: QuasiQuoter+txtfile = qq { quoteExp = toQ readFile }++toQ :: (FilePath -> IO String) -> FilePath -> ExpQ+toQ rf fp_ = do+	cnt <- runIO $ rf fp+	addDependentFile fp+	stringE cnt+	where fp = dropBoth isSpace fp_++{- |+QuasiQuoter binfile incerts file contents as string without transformation.+It read file as binary file.++@+main :: IO ()+main = print [binfile|foo.dat|]+@+-}++binfile :: QuasiQuoter+binfile = qq { quoteExp = toQ $ (BSC.unpack <$>) . BSC.readFile }++-- ERROR MESSAGE++qq :: QuasiQuoter+qq = QuasiQuoter {+	quoteExp = undefined,+	quotePat = const $ err "pattern",+	quoteType = const $ err "type",+	quoteDec = const $ err "declaration" }+ err :: String -> a err ctx = error $ 	"You have used the `nowdoc' QuasiQoter in a " ++ ctx ++ 	" context; you must only use it in an expression context"++-- TOOLS++dropBoth :: (a -> Bool) -> [a] -> [a]+dropBoth = (.) <$> dropWhile <*> dropWhileEnd