Skip to content

Instantly share code, notes, and snippets.

@christianwish
Last active May 18, 2019 07:50
Show Gist options
  • Save christianwish/e954e05603016e504e6ec814dde77a6a to your computer and use it in GitHub Desktop.
Save christianwish/e954e05603016e504e6ec814dde77a6a to your computer and use it in GitHub Desktop.
xml haskell
import System.Environment
import Data.List
type AttrName = String
type AttrVal = String
newtype TagName = TagName String deriving (Show)
data Attr = Attr AttrName AttrVal
listToStr :: (Show a) => [a] -> String
listToStr = (intercalate " ") . (map show)
instance Show Attr where
show (Attr k v) = k ++ "=\"" ++ v ++ "\""
data Tag =
Fragment [Tag]
| Tag TagName [Attr] [Tag]
_IDENT = " "
instance Show Tag where
show (Tag (TagName a) attrs children) = openTag ++ identChild ++ closeTag
where attrStr = listToStr attrs
openTag = "<" ++ a ++ " " ++ attrStr ++ ">" ++ maybeNl
maybeNl = if length childStr == 0 then "" else "\n"
closeTag = "</" ++ a ++ ">\n"
identChild = unlines $ map (_IDENT ++) (lines childStr)
childStr = foldl (++) "" childrenStrs
childrenStrs = map show children
main = do
-- [path] <- getArgs
-- c <- readFile path
-- let a = "beginning"
-- result = a ++ c
-- putStrLn result
putStrLn $ show $ Tag (TagName "text") [Attr "witdh" "300", Attr "witdh" "300"] [
Tag (TagName "animation") [Attr "attr" "x", Attr "witdh" "300"] [],
Tag (TagName "animation") [Attr "attr" "x", Attr "witdh" "300"] [],
Tag (TagName "animation") [Attr "attr" "x", Attr "witdh" "300"] [
Tag (TagName "animation") [Attr "attr" "x", Attr "witdh" "300"] [],
Tag (TagName "animation") [Attr "attr" "x", Attr "witdh" "300"] [],
Tag (TagName "animation") [Attr "attr" "x", Attr "witdh" "300"] []
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment