0 | module Language.XML.Attribute
 1 |
 2 | import Data.List
 3 | import Data.String.Parser
 4 |
 5 | import public Language.XML.Name
 6 |
 7 | public export
 8 | record Attribute where
 9 |     constructor MkAttribute
10 |     name : QName
11 |     value : String
12 |
13 | %name Attribute attr
14 |
15 | public export
16 | lookup : (name : QName) -> List Attribute -> Maybe String
17 | lookup name attrs = lookup name $ map (\attr => (attr.name, attr.value)) attrs
18 |
19 | export
20 | Show Attribute where
21 |     show attr = "\{show attr.name}=\{show attr.value}"
22 |
23 | export
24 | quotedString : Parser String
25 | quotedString = do
26 |     quote <- satisfy (\c => c == '"' || c == '\'')
27 |     content <- map pack $ many $ satisfy (/= quote)
28 |     ignore $ char quote
29 |     pure content
30 |
31 | export
32 | attribute : Parser Attribute
33 | attribute = (do
34 |     name <- qName
35 |     spaces
36 |     ignore $ string "="
37 |     spaces
38 |     value <- quotedString
39 |     pure $ MkAttribute name value
40 |   ) <?> "XML attribute"
41 |
42 | export
43 | exactAttribute : QName -> Parser String
44 | exactAttribute expectedName = do
45 |     MkAttribute name value <- attribute
46 |     if name == expectedName
47 |         then pure value
48 |         else fail "Unexpected attribute: \{show name}"
49 |