9c220dc8231c8e6bd6da0e4653823e161ac6ed6f
[dead/halcyon.git] / src / Twitter / Xml.hs
1 -- |Application-specific XML functions.
2 module Twitter.Xml
3 where
4
5 import Data.Char (chr)
6 import Test.HUnit
7 import Text.Regex (matchRegex, mkRegex, subRegex)
8 import Text.XML.HaXml
9
10 -- |Returns the 'CharData' contained within the given 'Content', or
11 -- 'Nothing' if no acceptable CharData was found. It will parse either
12 -- a 'CString' ('String') or 'CRef' (XML entity reference).
13 get_char_data :: Content i -> (Maybe CharData)
14 get_char_data (CString _ cd _) = Just cd
15 get_char_data (CRef ref _) = Just (verbatim ref) -- Entities.
16 get_char_data _ = Nothing
17
18
19 -- |A 'CFilter' returning all top-level <status> elements.
20 -- The name is due to the fact that if we retrieve more than
21 -- one status, they will be wrapped in a <statuses> tag, and
22 -- thus not be top-level.
23 single_status :: CFilter i
24 single_status = (tag "status")
25
26 -- |A 'CFilter' returning all <status> tags within <statuses>.
27 all_statuses :: CFilter i
28 all_statuses = (tag "statuses" /> tag "status")
29
30 -- |Finds the text of the <id> element contained within some other
31 -- content. Called unique_id here because status_id is used elsewhere.
32 unique_id :: CFilter i
33 unique_id = keep /> (tag "id") /> txt
34
35 -- |Finds the text of the <created_at> element contained within some
36 -- other element.
37 status_created_at :: CFilter i
38 status_created_at = keep /> (tag "created_at") /> txt
39
40 -- |Finds the text of the <text> element contained within some
41 -- other element.
42 status_text :: CFilter i
43 status_text = keep /> (tag "text") /> txt
44
45 -- |Finds the XML of the <user> element contained within some other
46 -- element.
47 status_user :: CFilter i
48 status_user = keep /> (tag "user")
49
50 -- |Finds the text of the <screen_name> element contained within some
51 -- other element.
52 user_screen_name :: CFilter i
53 user_screen_name = keep /> (tag "screen_name") /> txt
54
55 -- |A wrapper around the 'read' function which returns either Nothing
56 -- or (Just <the thing that could be read>).
57 maybe_read :: (Read a) => String -> Maybe a
58 maybe_read str =
59 case (reads str) of
60 [] -> Nothing
61 ((y,_):_) -> Just y
62
63 -- |Takes a unicode codepoint in decimal and returns it as a
64 -- one-character string.
65 entity_from_codepoint :: String -> String
66 entity_from_codepoint codepoint =
67 case (maybe_read codepoint) of
68 Nothing -> ""
69 Just num -> [(chr num)]
70
71
72 -- |A list of tuples whose first entry is a regular expression
73 -- matching XML entities, and whose second entry is the ASCII
74 -- character represented by that entity.
75 xml_entities :: [(String, String)]
76 xml_entities = [("[lr]dquo", "\""),
77 ("quot", "\""),
78 ("[mn]dash", "-"),
79 ("nbsp", " "),
80 ("amp", "&"),
81 ("lt", "<"),
82 ("gt", ">"),
83 ("hellip", "…")]
84
85 -- |Replace all of the XML entities in target.
86 replace_entities :: String -> String
87 replace_entities target =
88 unescape_numeric (unescape_recursive xml_entities target)
89
90 -- |Recursively unescape all numeric entities in the given String.
91 unescape_numeric :: String -> String
92 unescape_numeric target =
93 case match of
94 Nothing -> target
95 Just subexprs ->
96 case subexprs of
97 [] -> target
98 s1:_ ->
99 let this_entity_regex = mkRegex ("&#" ++ s1 ++ ";") in
100 let replacement = entity_from_codepoint s1 in
101 let new_target = subRegex this_entity_regex target replacement in
102 unescape_numeric new_target
103 where
104 from = "&#([0-9]+);"
105 match = matchRegex (mkRegex from) target
106
107
108
109 -- |The recursive function which does the real work for
110 -- 'replace_entities'.
111 unescape_recursive :: [(String, String)] -> String -> String
112 unescape_recursive [] target = target
113 unescape_recursive replacements target =
114 unescape_recursive (tail replacements) (subRegex (mkRegex from) target to)
115 where
116 replacement = (replacements !! 0)
117 from = "&" ++ (fst replacement) ++ ";"
118 to = (snd replacement)
119
120
121
122 xml_tests :: [Test]
123 xml_tests = [ test_replace_entities ]
124
125
126 test_replace_entities :: Test
127 test_replace_entities =
128 TestCase $ assertEqual "All entities are replaced correctly." expected_text actual_text
129 where
130 actual_text = (replace_entities "&quot;The moon is gay&#8230;&hellip;&quot; said &lt;insert the current president of the United States of America&gt;. &ldquo;It&#8217;s OK&mdash;&ndash;he&#8217;s not a real doctor.&rdquo;")
131 expected_text = "\"The moon is gay……\" said <insert the current president of the United States of America>. \"It’s OK--he’s not a real doctor.\""