]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Xml.hs
Add " unescaping.
[dead/halcyon.git] / src / Twitter / Xml.hs
1 -- |Application-specific XML functions.
2 module Twitter.Xml
3 where
4
5 import Data.Maybe
6 import Test.HUnit
7 import Text.Regex (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 -> (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 <status> tags within <statuses>.
20 all_statuses :: CFilter
21 all_statuses = (tag "statuses" /> tag "status")
22
23 -- |Finds the text of the <id> element contained within some other
24 -- content. Called unique_id here because status_id is used elsewhere.
25 unique_id :: CFilter
26 unique_id = keep /> (tag "id") /> txt
27
28 -- |Finds the text of the <created_at> element contained within some
29 -- other element.
30 status_created_at :: CFilter
31 status_created_at = keep /> (tag "created_at") /> txt
32
33 -- |Finds the text of the <text> element contained within some
34 -- other element.
35 status_text :: CFilter
36 status_text = keep /> (tag "text") /> txt
37
38 -- |Finds the XML of the <user> element contained within some other
39 -- element.
40 status_user :: CFilter
41 status_user = keep /> (tag "user")
42
43 -- |Finds the text of the <screen_name> element contained within some
44 -- other element.
45 user_screen_name :: CFilter
46 user_screen_name = keep /> (tag "screen_name") /> txt
47
48
49 -- |A list of tuples whose first entry is a regular expression
50 -- matching XML entities, and whose second entry is the ASCII
51 -- character represented by that entity.
52 xml_entities :: [(String, String)]
53 xml_entities = [("[lr]dquo", "\""),
54 ("quot", "\""),
55 ("[mn]dash", "-"),
56 ("nbsp", " "),
57 ("#8217", "'"),
58 ("amp", "&"),
59 ("lt", "<"),
60 ("gt", ">")]
61
62 -- |Replace all of the XML entities in target.
63 replace_entities :: String -> String
64 replace_entities target = unescape_recursive xml_entities target
65
66 -- |The recursive function which does the real work for
67 -- 'replace_entities'.
68 unescape_recursive :: [(String, String)] -> String -> String
69 unescape_recursive [] target = target
70 unescape_recursive replacements target =
71 unescape_recursive (tail replacements) (subRegex (mkRegex from) target to)
72 where
73 replacement = (replacements !! 0)
74 from = "&" ++ (fst replacement) ++ ";"
75 to = (snd replacement)
76
77
78
79 xml_tests :: [Test]
80 xml_tests = [ test_replace_entities ]
81
82
83 test_replace_entities :: Test
84 test_replace_entities =
85 TestCase $ assertEqual "All entities are replaced correctly." expected_text actual_text
86 where
87 actual_text = (replace_entities "&quot;The moon is gay,&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;")
88 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.\""