-
Notifications
You must be signed in to change notification settings - Fork 4
/
chop.hs
95 lines (80 loc) · 2.6 KB
/
chop.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import System.Environment (getArgs)
import Data.List
import Data.List.Split
import Juicer.Freeze
import Juicer.Blend
splitBegin :: String -> String -> (String, String)
splitBegin begin input =
let parts = splitOn begin input
in (head parts, intercalate begin $ tail parts)
splitEnd :: String -> String -> (String, String)
splitEnd end input =
let parts = splitOn end input
in (intercalate end $ init parts, last parts)
trim :: String -> String -> String -> String
trim begin end input =
let (_,body,_) = splitBody begin end input
in body
trimBegin :: String -> String -> String
trimBegin begin input =
let (head,body) = splitBegin begin input
in body
trimEnd :: String -> String -> String
trimEnd end input =
let (body,tail) = splitEnd end input
in body
trimTag :: String -> String -> String
trimTag tag input = trim ("<"++tag++">") ("</"++tag++">") input
splitBody :: String -> String -> String -> (String, String, String)
splitBody begin end input = do
let (head,rest) = splitBegin begin input
let (body,tail) = splitEnd end rest
(head,body,tail)
splitTag :: String -> String -> [String]
splitTag tag input = do
let openTag = "<"++tag++">"
let closeTag = "</"++tag++">"
let items = splitOn openTag $ trimBegin openTag input
map (trimEnd closeTag) items
printItems :: [String] -> IO()
printItems [] = return ()
printItems (item:items) = do
printItem item
printItems items
printItem :: String -> IO()
printItem item = do
let title = trimTag "title" item
let desc = trimTag "description" item
putStrLn $ "Post Title: " ++ title
putStrLn $ "Post Description: " ++ desc
getPosts :: [String] -> [(String, String)]
getPosts = map getPost
getPost :: String -> (String, String)
getPost item = do
let title = trimTag "title" item
let desc = trimTag "description" item
(title, desc)
main :: IO ()
main = do
args <- getArgs
case args of
(rssname:frozenname:manifestname:[]) -> chopit rssname frozenname manifestname
otherwise -> putStrLn "chop [rss] [archive] [manifest]"
chopit :: String -> String -> String -> IO()
chopit rss frozen manifest = do
f <- readFile rss
let channel = trimTag "channel" f
let (header,_) = splitBegin "<item>" channel
let title = splitTag "title" header !! 0
let desc = trimTag "description" header
putStrLn $ "Feed Title: " ++ title
putStrLn $ "Feed Description: " ++ desc
putStrLn "------------------"
let items = splitTag "item" channel
putStrLn $ show $ length items
printItems items
let posts = getPosts items
let feed = package (title, desc) posts
freeze feed frozen
let mf = metafeed feed
metafreeze mf manifest