4 | 4 |
|
5 | 5 |
module Main where
|
6 | 6 |
|
| 7 |
import Atom
|
7 | 8 |
import qualified Lektor as L
|
8 | |
import qualified Atom as A
|
9 | 9 |
|
10 | |
import Network.HTTP
|
11 | |
import System.Directory
|
12 | |
import System.Environment
|
13 | |
import Text.Atom.Feed.Import (elementFeed)
|
14 | |
import Text.XML.Light.Input (parseXMLDoc)
|
15 | |
|
16 | |
die :: String -> a
|
17 | |
die = error
|
| 10 |
import Control.Error (ExceptT(..), headErr, fmapLT, runScript, (??))
|
| 11 |
import Control.Monad.Trans.Class (lift)
|
| 12 |
import Network.HTTP (simpleHTTP, getRequest, rspBody)
|
| 13 |
import System.Environment (getArgs)
|
| 14 |
import Text.Atom.Feed.Import (elementFeed)
|
| 15 |
import Text.XML.Light.Input (parseXMLDoc)
|
18 | 16 |
|
19 | 17 |
usage :: String
|
20 | 18 |
usage = "Usage: lektor-rss [feed url]"
|
21 | 19 |
|
22 | |
lektorSetup :: IO ()
|
23 | |
lektorSetup = do
|
24 | |
dir <- getEnv "LEKTORDIR"
|
25 | |
setCurrentDirectory dir
|
26 | |
mapM_ (createDirectoryIfMissing True)
|
27 | |
[ "src", "tmp", "new", "cur" ]
|
28 | |
|
29 | 20 |
main :: IO ()
|
30 | |
main = do
|
31 | |
lektorSetup
|
32 | |
getArgs >>= \case
|
33 | |
[] -> putStrLn usage
|
34 | |
(url:_) -> do
|
35 | |
simpleHTTP (getRequest url) >>= \case
|
36 | |
Left _ -> die "Unable to fetch document"
|
37 | |
Right r -> writeAsAtom (rspBody r)
|
38 | |
|
39 | |
writeAsAtom :: String -> IO ()
|
40 | |
writeAsAtom s = case parseXMLDoc s of
|
41 | |
Nothing -> die "Unable to parse XML document"
|
42 | |
Just xml -> case elementFeed xml of
|
43 | |
Nothing -> die "XML document not an Atom feed"
|
44 | |
Just atom -> L.writeAll (A.atomToLektor atom)
|
| 21 |
main = runScript $ do
|
| 22 |
-- check the args for the url
|
| 23 |
url <- ExceptT (fmap (headErr usage) getArgs)
|
| 24 |
-- GET the resource
|
| 25 |
resp <- fmapLT show (ExceptT (simpleHTTP (getRequest url)))
|
| 26 |
-- parse the XML
|
| 27 |
xml <- parseXMLDoc (rspBody resp) ?? "Unable to parse XML document"
|
| 28 |
-- decode the Atom format
|
| 29 |
atom <- elementFeed xml ?? "XML document not an Atom feed"
|
| 30 |
-- write the Lektor dir
|
| 31 |
lift (L.writeAll (atomToLektor atom))
|