1 -- |Application-specific XML functions.
7 import Text.Regex (matchRegex, mkRegex, subRegex)
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
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")
26 -- |A 'CFilter' returning all <status> tags within <statuses>.
27 all_statuses :: CFilter i
28 all_statuses = (tag "statuses" /> tag "status")
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
35 -- |Finds the text of the <created_at> element contained within some
37 status_created_at :: CFilter i
38 status_created_at = keep /> (tag "created_at") /> txt
40 -- |Finds the text of the <text> element contained within some
42 status_text :: CFilter i
43 status_text = keep /> (tag "text") /> txt
45 -- |Finds the XML of the <user> element contained within some other
47 status_user :: CFilter i
48 status_user = keep /> (tag "user")
50 -- | Finds the text of the <retweeted> element contained within some
52 status_retweeted :: CFilter i
53 status_retweeted = keep /> (tag "retweeted") /> txt
55 -- | Finds the text of the <in_reply_to_status_id> element contained
56 -- within some other element.
57 status_reply_to_status_id :: CFilter i
58 status_reply_to_status_id = keep /> (tag "in_reply_to_status_id") /> txt
60 -- |Finds the text of the <screen_name> element contained within some
62 user_screen_name :: CFilter i
63 user_screen_name = keep /> (tag "screen_name") /> txt
65 -- |A wrapper around the 'read' function which returns either Nothing
66 -- or (Just <the thing that could be read>).
67 maybe_read :: (Read a) => String -> Maybe a
73 -- |Takes a unicode codepoint in decimal and returns it as a
74 -- one-character string.
75 entity_from_codepoint :: String -> String
76 entity_from_codepoint codepoint =
77 case (maybe_read codepoint) of
79 Just num -> [(chr num)]
82 -- | A list of tuples whose first entry is a regular expression
83 -- matching XML entities, and whose second entry is the ASCII
84 -- character represented by that entity.
86 -- For some reason, ampersands are escaped twice in the status
87 -- text. Rather than unescape everything twice, we just stick "amp"
89 xml_entities :: [(String, String)]
90 xml_entities = [("[lr]dquo", "\""),
100 -- |Replace all of the XML entities in target.
101 replace_entities :: String -> String
102 replace_entities target =
103 unescape_numeric (unescape_recursive xml_entities target)
105 -- |Recursively unescape all numeric entities in the given String.
106 unescape_numeric :: String -> String
107 unescape_numeric target =
114 let this_entity_regex = mkRegex ("&#" ++ s1 ++ ";") in
115 let replacement = entity_from_codepoint s1 in
116 let new_target = subRegex this_entity_regex target replacement in
117 unescape_numeric new_target
120 match = matchRegex (mkRegex from) target
124 -- |The recursive function which does the real work for
125 -- 'replace_entities'.
126 unescape_recursive :: [(String, String)] -> String -> String
127 unescape_recursive [] target = target
128 unescape_recursive replacements target =
129 unescape_recursive (tail replacements) (subRegex (mkRegex from) target to)
131 replacement = (replacements !! 0)
132 from = "&" ++ (fst replacement) ++ ";"
133 to = (snd replacement)
138 xml_tests = [ test_replace_entities, test_double_unescape ]
141 test_replace_entities :: Test
142 test_replace_entities =
143 TestCase $ assertEqual "All entities are replaced correctly." expected_text actual_text
145 actual_text = (replace_entities ""The moon is gay……" said <insert the current president of the United States of America>. “It’s OK—–he’s not a real doctor.”")
146 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.\""
149 test_double_unescape :: Test
150 test_double_unescape =
151 TestCase $ assertEqual "The status text is unescaped twice." expected_text actual_text
153 actual_text = (replace_entities "As a kid, I'd pull a girl's hair to let her know I liked her, but now that I'm older &amp; wiser I simply hit her with my car.")
154 expected_text = "As a kid, I'd pull a girl's hair to let her know I liked her, but now that I'm older & wiser I simply hit her with my car."