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 <screen_name> element contained within some
52 user_screen_name :: CFilter i
53 user_screen_name = keep /> (tag "screen_name") /> txt
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
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
69 Just num -> [(chr num)]
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", "\""),
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)
90 -- |Recursively unescape all numeric entities in the given String.
91 unescape_numeric :: String -> String
92 unescape_numeric target =
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
105 match = matchRegex (mkRegex from) target
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)
116 replacement = (replacements !! 0)
117 from = "&" ++ (fst replacement) ++ ";"
118 to = (snd replacement)
123 xml_tests = [ test_replace_entities ]
126 test_replace_entities :: Test
127 test_replace_entities =
128 TestCase $ assertEqual "All entities are replaced correctly." expected_text actual_text
130 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.”")
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.\""