module Text.LaTeX.Result (
     -- * Result Type
     Result
   , toResult
   , fromResult
     -- * Special Characters
   , resCharsStr
     -- * Manipulating Results
   , pfinal 
   , bfinal
   , comm
   , ks
   , bs
   , sep
   , ds
   ) where
import Data.Monoid

import Data.DString (DString)
import Data.String.ToString (toString)
import GHC.Exts (fromString)

-- Result Type (MUST be a Monoid) --

type Result = DString

toResult :: String -> Result
toResult = fromString

fromResult :: Result -> String
fromResult = toString

------------------------------------

inStart :: Result -> (Result -> Result)
inStart = mappend

inEnd :: Result -> (Result -> Result)
inEnd = flip mappend

inBoth :: Result -> Result -> (Result -> Result)
inBoth r0 r1 x = mconcat [r0,x,r1]

--

pfinal :: Result -> Result
pfinal = inEnd $ toResult "\n\n"

bfinal :: Result -> Result
bfinal = inEnd $ toResult "{}"

comm :: Result -> Result
comm = inStart $ toResult "\\"

ks :: Result -> Result
ks = inBoth (toResult "{") (toResult "}")

bs :: Result -> Result
bs = inBoth (toResult "[") (toResult "]")

sep :: Result -> Result
sep = inBoth (toResult "|") (toResult "|")

ds :: Result -> Result
ds = inBoth (toResult "$") (toResult "$")

resCharsStr_ :: [(Char,String)]
resCharsStr_ = 
              [ ('#',"\\#") 
              , ('$',"\\$")
              , ('%',"\\%")
              , ('^',"\\^{}")
              , ('&',"\\&")
              , ('_',"\\_")
              , ('{',"\\{")
              , ('}',"\\}")
              , ('~',"\\~{}")
              , ('\\',"\\textbackslash") ]

resCharsStr :: [(Char,Result)]
resCharsStr = map (\(x,y) -> (x,toResult y)) resCharsStr_