This commit is contained in:
Abhinav Sarkar 2012-08-04 23:58:01 +05:30
parent 36030e744e
commit 6e2bdf2f37
1 changed files with 6 additions and 4 deletions

View File

@ -26,7 +26,7 @@ data Elem = Elem {
elemChildren :: [Elem] elemChildren :: [Elem]
} deriving (Show) } deriving (Show)
indent n = concat . (replicate n) $ " " indent n = concat . replicate n $ " "
trimValue value = case value of trimValue value = case value of
Nothing -> Nothing Nothing -> Nothing
@ -47,7 +47,7 @@ line level = do
spaces spaces
tag <- many1 upper tag <- many1 upper
spaces spaces
value <- fmap trimValue $ optionMaybe $ manyTill (anyChar) newline value <- fmap trimValue $ optionMaybe $ manyTill anyChar newline
return $ Line level tag value id return $ Line level tag value id
-- parses an element -- parses an element
@ -60,7 +60,7 @@ element level = do
return $ Elem lineTag lineValue lineId children return $ Elem lineTag lineValue lineId children
-- parses a document -- parses a document
document = (element 0) `endBy` whitespaces document = element 0 `endBy` whitespaces
-- normalizes an element by merging values of CONC and CONT -- normalizes an element by merging values of CONC and CONT
-- elements with parent element value -- elements with parent element value
@ -88,7 +88,9 @@ elemToXml indentation Elem{..} =
++ "<" ++ elemTag ++ "<" ++ elemTag
++ maybe "" (\i -> " id=\"@" ++ i ++ "@\"") elemId ++ maybe "" (\i -> " id=\"@" ++ i ++ "@\"") elemId
++ case elemChildren of ++ case elemChildren of
[] -> ">" ++ normalizeValue elemValue ++ "</" ++ elemTag ++ ">" [] -> case normalizeValue elemValue of
"" -> " />"
text -> ">" ++ text ++ "</" ++ elemTag ++ ">"
_ -> maybe "" (\v -> " value=\"" ++ v ++ "\"") elemValue ++ ">\n" _ -> maybe "" (\v -> " value=\"" ++ v ++ "\"") elemValue ++ ">\n"
++ unlines (map (elemToXml (indentation + 1)) elemChildren) ++ unlines (map (elemToXml (indentation + 1)) elemChildren)
++ indent indentation ++ "</" ++ elemTag ++ ">" ++ indent indentation ++ "</" ++ elemTag ++ ">"