packages feed

Spintax 0.3.6 → 0.3.6.1

raw patch · 2 files changed

+29/−28 lines, 2 filesdep ~extradep ~mtldep ~textPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: extra, mtl, text

API changes (from Hackage documentation)

Files

Spintax.cabal view
@@ -1,5 +1,5 @@ name:                Spintax-version:             0.3.6+version:             0.3.6.1 synopsis:            Random text generation based on spintax description:         Random text generation based on spintax with nested alternatives and empty options. homepage:            https://github.com/MichelBoucey/spintax@@ -7,12 +7,12 @@ license-file:        LICENSE author:              Michel Boucey maintainer:          michel.boucey@gmail.com-copyright:           (c) 2016-2021 - Michel Boucey+copyright:           (c) 2016-2024 - Michel Boucey category:            Text build-type:          Simple cabal-version:       >=1.10 -Tested-With: GHC ==8.4.4 || ==8.6.5 || ==8.8.4 || ==8.10.1 || ==8.10.4 || ==9.0.1+Tested-With: GHC ==8.8.4 || ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.3 || ==9.8.2  library   hs-source-dirs:    src@@ -20,9 +20,9 @@   build-depends:     attoparsec   >= 0.12.1.6 && < 0.15                    , base         >= 4.7 && < 5                    , extra        >= 1.4.3 && < 1.8-                   , mtl          >= 2.2.1 && < 2.3+                   , mtl          >= 2.2.1 && < 2.4                    , mwc-random   >= 0.13.3.2 && < 0.16-                   , text         >= 1.2.2 && < 1.3+                   , text         >=1.1 && < 2.2   default-language:  Haskell2010   GHC-Options:       -Wall 
src/Text/Spintax.hs view
@@ -1,19 +1,18 @@+{-# LANGUAGE FlexibleContexts  #-} {-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE FlexibleContexts #-}  module Text.Spintax (spintax) where  import           Control.Applicative  ((<|>))-import           Control.Monad.Reader (runReaderT, ask)+import           Control.Monad.Reader (ask, runReaderT) import           Data.Attoparsec.Text import qualified Data.List.Extra      as E-import           Data.Monoid          ((<>)) import qualified Data.Text            as T import           System.Random.MWC  -- | Generate random texts based on a spinning syntax template, with nested alternatives and empty options. ----- >λ> spintax "{{Oh my God|Awesome}, {a|the}|A|The} {quick {and dirty |||}||}{brown |pink |grey |}{fox|flea|elephant} jumps over {the|a} {lazy |smelly |sleepy |}{dog|cat|whale}{.|!|...}"+-- >λ> spintax "{{Oh my God|Awesome|Can't believe that}, {a|the}|A|The} {quick {and dirty |||}||}{brown |pink |grey |black |yellow }{fox|flea|elephant|panther|bear} jumps over {the|a} {lazy |smelly |sleepy |grouchy }{dog|cat|whale}{|||, that's {really |||}amazing}{.|!|...}" -- > Right "Awesome, the quick pink fox jumps over a sleepy whale." -- spintax :: T.Text -> IO (Either String T.Text)@@ -22,53 +21,55 @@   where     spin t = go T.empty [] t (0::Int)       where+         go o as i l           | l < 0  = parseFail           | l == 0 =             case parse spinSyntax i of               Done r m  ->                 case m of-                  "{" -> go o as r (l+1)+                  "{"                      -> go o as r (l+1)                   n | n == "}" || n == "|" -> parseFail-                  _   -> go (o <> m) as r l-              Partial _ -> return $ Right $ o <> i+                  _                        -> go (o <> m) as r l+              Partial _ -> pure (Right $ o <> i)               Fail {}   -> parseFail           | l == 1 =             case parse spinSyntax i of               Done r m ->                 case m of-                  "{" -> go o (add as m) r (l+1)+                  "{" -> go o (addAlter m) r (l+1)                   "}" -> do-                    a <- spin =<< randAlter as =<< ask+                    a <- spin =<< randAlter =<< ask                     case a of-                      Left _   -> parseFail                       Right t' -> go (o <> t') [] r (l-1)+                      Left _   -> parseFail                   "|" ->                     if E.null as                       then go o ["",""] r l                       else go o (E.snoc as "") r l-                  _   -> go o (add as m) r l+                  _   -> go o (addAlter m) r l               Partial _ -> parseFail               Fail {} -> parseFail           | l > 1 =             case parse spinSyntax i of               Done r m ->                 case m of-                  "{" -> go o (add as m) r (l+1)-                  "}" -> go o (add as m) r (l-1)-                  _   -> go o (add as m) r l+                  "{" -> go o (addAlter m) r (l+1)+                  "}" -> go o (addAlter m) r (l-1)+                  _   -> go o (addAlter m) r l               Partial _ -> parseFail               Fail {}   -> parseFail           where-            add _l _t =-              case E.unsnoc _l of-                Just (xs,x) -> E.snoc xs $ x <> _t-                Nothing     -> [_t]-            randAlter _as _g =-              (\r -> (!!) as (r-1)) <$> uniformR (1,E.length _as) _g-        go _ _ _ _ = parseFail -        parseFail = fail msg-        msg = "Spintax template parsing failure"+            addAlter n =+                case E.unsnoc as of+                  Just (xs,x) -> E.snoc xs (x <> n)+                  Nothing     -> pure n+            randAlter g =+              (\r -> (!!) as (r-1)) <$> uniformR (1,E.length as) g+        go _ _ _ _ = parseFail++        parseFail = fail "Spintax template parsing failure"+         spinSyntax =           openBrace <|> closeBrace <|> pipe <|> content             where