madlang- Randomized templating language DSL

Safe HaskellNone




Madlang Text Generation Library, EDSL, and Interpreted Language


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.


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


Parsers for .mad files

parseTok Source #


:: 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


Parse text given a context

import qualified Data.Text.IO as TIO

getParsed = do
    f <- TIO.readFile "template.mad"
    parseTok "filename.mad" [] [] f

parseTokM :: [Text] -> Parser (Context RandTok) Source #

Parse text as a token + context (aka a reader monad with all the other functions)

runFile Source #


:: [Text]

List of variables to substitute into the template

-> FilePath

Path to .mad file.

-> IO Text


Generate randomized text from a file containing a template

parseFile Source #


:: [Text]

variables to substitute into the template

-> FilePath


-> FilePath

filepath within folder

-> IO (Either (ParseError Char Dec) RandTok)

parsed RandTok

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.

data RandTok Source #

datatype for a token returning a random string


List [(Prob, RandTok)] 
Value Text 


Eq RandTok Source # 


(==) :: RandTok -> RandTok -> Bool #

(/=) :: RandTok -> RandTok -> Bool #

Show RandTok Source # 
Monoid RandTok Source #

Make RandTok a monoid so we can append them together nicely (since they do generate text).

(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


(==) :: Context a -> Context a -> Bool #

(/=) :: Context a -> Context a -> Bool #

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

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
:define something
    1.0 "hello"
    1.0 "goodbye"
    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.

madFile :: FilePath -> Q Exp Source #

Splice for embedding a '.mad' file, e.g.

demo :: IO T.Text
demo = run
    $(madFile "twitter-bot.mad")

Note that the embedded code cannot have any inclusions.