Escaped carries allong it's escaped value

This commit is contained in:
Jasper Van der Jeugt 2011-01-02 12:49:43 +01:00
parent 2d1225104c
commit 220e4b484c
2 changed files with 11 additions and 7 deletions

View file

@ -21,14 +21,18 @@ readTemplate = Template . readTemplate'
readTemplate' [] = []
readTemplate' string
| "$$" `isPrefixOf` string =
EscapeCharacter : (readTemplate' $ drop 2 string)
let (key, rest) = readIdentifier $ drop 2 string
in Escaped key : readTemplate' rest
| "$" `isPrefixOf` string =
let (key, rest) = span isAlphaNum $ drop 1 string
let (key, rest) = readIdentifier $ drop 1 string
in Identifier key : readTemplate' rest
| otherwise =
let (chunk, rest) = break (== '$') string
in Chunk chunk : readTemplate' rest
-- Parse an identifier into (identifier, rest)
readIdentifier = span isAlphaNum
-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
-- "Page". When a key is not found, it is left as it is. You can specify
-- the characters used to replace escaped dollars (@$$@) here.
@ -40,7 +44,7 @@ applyTemplate template page =
substitute (Chunk chunk) = chunk
substitute (Identifier key) =
fromMaybe ('$' : key) $ M.lookup key $ toMap page
substitute (EscapeCharacter) = "$"
substitute (Escaped key) = '$' : key
-- | Apply a page as it's own template. This is often very useful to fill in
-- certain keys like @$root@ and @$url@.

View file

@ -29,16 +29,16 @@ instance Writable Template where
data TemplateElement
= Chunk String
| Identifier String
| EscapeCharacter
| Escaped String
deriving (Show, Eq, Typeable)
instance Binary TemplateElement where
put (Chunk string) = putWord8 0 >> put string
put (Identifier key) = putWord8 1 >> put key
put (EscapeCharacter) = putWord8 2
put (Escaped key) = putWord8 2 >> put key
get = getWord8 >>= \tag -> case tag of
0 -> Chunk <$> get
0 -> Chunk <$> get
1 -> Identifier <$> get
2 -> return EscapeCharacter
2 -> Escaped <$> get
_ -> error "Error reading cached template"