packages feed

text-format-heavy 0.1.3.0 → 0.1.4.0

raw patch · 3 files changed

+43/−5 lines, 3 files

Files

Data/Text/Format/Heavy/Instances.hs view
@@ -3,6 +3,9 @@ module Data.Text.Format.Heavy.Instances   (-- * Utility data types    Single (..), Several (..), Shown (..),+   -- * Combinators+   DefaultValue (..), ThenCheck (..),+   withDefault, optional,    -- * Generic formatters    genericIntFormat, genericFloatFormat   ) where@@ -12,6 +15,7 @@ import Data.Default import Data.Word import Data.Int+import Data.Maybe import qualified Data.Map as M import qualified Data.Text as T import qualified Data.Text.Encoding as TE@@ -271,4 +275,26 @@  instance Formatable x => VarContainer (M.Map TL.Text x) where   lookupVar name pairs = Variable `fmap` M.lookup name pairs++-- | Variable container which contains fixed value for any variable name.+data DefaultValue = DefaultValue Variable++instance VarContainer DefaultValue where+  lookupVar _ (DefaultValue var) = Just var++-- | Combiled variable container, which uses parameters from @c1@,+-- and if variable is not found there it will check in @c2@.+data ThenCheck c1 c2 = ThenCheck c1 c2++instance (VarContainer c1, VarContainer c2) => VarContainer (ThenCheck c1 c2) where+  lookupVar name (ThenCheck c1 c2) =+    case lookupVar name c1 of+      Just result -> Just result+      Nothing -> lookupVar name c2++withDefault :: VarContainer c => c -> Variable -> ThenCheck c DefaultValue+withDefault c value = c `ThenCheck` DefaultValue value++optional :: VarContainer c => c -> ThenCheck c DefaultValue+optional c = c `withDefault` (Variable TL.empty) 
README.md view
@@ -20,6 +20,17 @@ parsed from lazy Text, or can be entered as string literals, since `Format` implements `IsString`. +There are two syntaxes of formatting strings defined by this package:++* Default Python-like syntax, which is generally described as "anything in+  braces is a variable substitution". `instance IsString Format` uses this+  syntax.+* Alternative Shell-like syntax, which is generally described as "anything+  after dollar sign is a variable substitution".++It is possible to implement custom syntaxes of format strings: you just need to+parse instances of `Format` type from some sort of strings.+ The `format` function takes a `Format` specification and a container with variables. Container types are generalized by `VarContainer` type class. Standard container implementations include:@@ -37,9 +48,9 @@ integers, floats and strings, python-like syntax is used. Standard set of variable types includes: - * Integers (`Int` and `Integer`, others can be easily added);+ * Integers (`Int`, `Integer`, `Int8..64`, `Word8..64`, others can be easily added);  * Floats (`Float` and `Double`);- * Strings (`String`, lazy and strict `Text`);+ * Strings (`String`, lazy and strict `ByteString`, lazy and strict `Text`);  * Booleans;  * Time/date values from Data.Time.  * Any instance of `Show` type class can be used by packing it into `Shown`@@ -47,9 +58,10 @@  One can implement custom variable types. -For examples, please refer to [GitHub-wiki](https://github.com/portnov/text-format-heavy/wiki) and `examples/`+For examples, please refer to [GitHub wiki][1] and `examples/` directory in this repo. There are also some examples in haddock documentation.  License: BSD3.++[1]: https://github.com/portnov/text-format-heavy/wiki 
text-format-heavy.cabal view
@@ -1,5 +1,5 @@ name:                text-format-heavy-version:             0.1.3.0+version:             0.1.4.0 synopsis:            Full-weight string formatting library, analog of Python's string.format description:         This package contains full-featured string formatting function, similar to                      Python's string.format. Features include: