| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Text.Madlibs
Contents
Description
Madlang Text Generation Library, EDSL, and Interpreted Language
Purpose
Madlang is a text-genrating Domain-Specific Language (DSL). It is similar in purpose to tracery, but it is written in Haskell and therefore offers more flexibility.
Example
In file example.mad:
:define gambling
1.0 "heads"
1.0 "tails"
:return
1.0 "The result of the coin flip was: " gambling
$ madlang run example.mad tails
- parseTok :: FilePath -> [(Key, RandTok)] -> [Text] -> Text -> Either (ParseError Char Dec) RandTok
- parseTokM :: [Text] -> Parser (Context RandTok)
- runFile :: [Text] -> FilePath -> IO Text
- parseFile :: [Text] -> FilePath -> FilePath -> IO (Either (ParseError Char Dec) RandTok)
- makeTree :: [Text] -> FilePath -> FilePath -> IO (Either (ParseError Char Dec) RandTok)
- run :: MonadRandom m => RandTok -> m Text
- runText :: MonadRandom m => [Text] -> String -> Text -> m Text
- data RandTok
- type Context a = State [(Key, RandTok)] a
- data SemanticError
- runMadlang :: IO ()
- madlang :: QuasiQuoter
- madFile :: FilePath -> Q Exp
Parsers for .mad files
Arguments
| :: FilePath | File name to use for parse errors |
| -> [(Key, RandTok)] | Context, i.e. other random data paired with a key. |
| -> [Text] | list of variables to substitute into the template |
| -> Text | Actaul text to parse |
| -> Either (ParseError Char Dec) RandTok | Result |
Parse text given a context
import qualified Data.Text.IO as TIO
getParsed = do
f <- TIO.readFile "template.mad"
parseTok "filename.mad" [] [] fparseTokM :: [Text] -> Parser (Context RandTok) Source #
Parse text as a token + context (aka a reader monad with all the other functions)
Arguments
| :: [Text] | List of variables to substitute into the template |
| -> FilePath | Path to |
| -> IO Text | Result |
Generate randomized text from a file containing a template
Arguments
| :: [Text] | variables to substitute into the template |
| -> FilePath | folder |
| -> FilePath | filepath within folder |
| -> IO (Either (ParseError Char Dec) RandTok) | parsed |
Parse a template file into the RandTok data type
makeTree :: [Text] -> FilePath -> FilePath -> IO (Either (ParseError Char Dec) RandTok) Source #
Parse a template into a RandTok suitable to be displayed as a tree
Functions and constructs for the RandTok data type
run :: MonadRandom m => RandTok -> m Text Source #
Generate randomized text from a RandTok
getText :: IO T.Text
getText = do
let exampleTok = List [(1.0,List [(0.5,Value "heads"),(0.5,Value "tails")])]
run exampleTok
runText :: MonadRandom m => [Text] -> String -> Text -> m Text Source #
Run based on text input, with nothing linked.
datatype for a token returning a random string
Instances
| Eq RandTok Source # | |
| Show RandTok Source # | |
| Monoid RandTok Source # | Make (Value "Hello") <> (List [(0.5," you"), (0.5, " me")]) (List [(0.5,"Hello you"), (0.5, "Hello me")]) |
| Eq a => Eq (Context a) Source # | Compare inside the state monad using only the underlying objects |
Types associated with the parser
type Context a = State [(Key, RandTok)] a Source #
State monad providing context, i.e. function we've already called before
data SemanticError Source #
Datatype for a semantic error
Constructors
| NoReturn | |
| CircularFunctionCalls Text Text | |
| InsufficientArgs Int Int | |
| DoubleDefinition Text | |
| NoContext Text |
Instances
| Show SemanticError Source # | display a |
| Exception SemanticError Source # | Derived via our show instance; |
Command-line executable
runMadlang :: IO () Source #
Main program action
Example Usage:
$ madlang run example.mad some text generated
Template Haskell EDSL
madlang :: QuasiQuoter Source #
QuasiQuoter for an EDSL, e.g.
demoQQ :: T.Text
demoQQ = run
[madlang|
:define something
1.0 "hello"
1.0 "goodbye"
:return
1.0 something|]
Note that this is in general much faster than running interpreted code, though inclusions
do not work in the QuasiQuoter or in spliced expressions.