Funktionale Programmierung: HTML Seiten erzeugen (2) |
1module Main
2where
3
4import Text.XML.HXT.Arrow
5import System.Environment
6
7main :: IO ()
8main
9 = do
10 [src, dst] <- getArgs
11 runX ( readDocument
12 [ (a_parse_html, v_1)
13 , (a_issue_warnings, v_0)
14 ] src
15 >>>
16 processDocument
17 >>>
18 writeDocument
19 [ (a_indent, v_1)
20 ] dst
21 )
22 return ()
23
24processDocument :: ArrowXml a => a XmlTree XmlTree
25processDocument
26 = root [] [getChildren
27 >>>
28 isElem
29 >>>
30 genPage
31 ]
32 >>>
33 addXHtmlDoctypeTransitional
34
35genPage :: ArrowXml a => a XmlTree XmlTree
36genPage
37 = eelem "html"
38 += ( eelem "head"
39 += ( eelem "title"
40 += txt "Content of haskell.org"
41 )
42 )
43 += ( eelem "body"
44 += sattr "class" "haskell"
45 += ( eelem "h1"
46 += txt "Content of haskell.org"
47 )
48 += ( eelem "pre"
49 += formatBodyText
50 )
51 )
52
53formatBodyText :: ArrowXml a => a XmlTree XmlTree
54formatBodyText
55 = xshow getBodyText
56 >>>
57 arr ( words
58 >>>
59 trimLines 8
60 >>>
61 map unwords
62 >>>
63 unlines
64 )
65 >>>
66 mkText
67 where
68 trimLines n [] = []
69 trimLines n xs = take n xs :
70 trimLines n (drop n xs)
71
72getBodyText :: ArrowXml a => a XmlTree XmlTree
73getBodyText
74 = hasName "html"
75 />
76 hasName "body"
77 //>
78 ( hasName "div"
79 >>>
80 hasAttrValue "id" (== "content")
81 )
82 //>
83 ( hasName "td"
84 >>>
85 hasAttrValue "valign" (== "top")
86 >>>
87 hasAttrValue "width" (== "75%")
88 )
89 />
90 deep isText
|
ghc -e ":main haskell.org -" extractPureBodyText.hs
|
ghc -e ":main haskell.org content.org" extractPureBodyText.hs
|
|
Letzte Änderung: 27.03.2015 | © Prof. Dr. Uwe Schmidt |