diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-03-29 04:18:59 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-03-29 04:18:59 +0200 |
commit | 313e6a863be0931752aa21cf99f96195a6e2b8c6 (patch) | |
tree | 8c2f8adf65c95e31b1fd7b1ec7efeabca3a848d2 /build | |
parent | 29907c6c870933ec1031fa6499ea9994c96f7aa6 (diff) | |
download | dirty-haskell.org-313e6a863be0931752aa21cf99f96195a6e2b8c6.tar dirty-haskell.org-313e6a863be0931752aa21cf99f96195a6e2b8c6.tar.gz dirty-haskell.org-313e6a863be0931752aa21cf99f96195a6e2b8c6.tar.bz2 dirty-haskell.org-313e6a863be0931752aa21cf99f96195a6e2b8c6.tar.xz dirty-haskell.org-313e6a863be0931752aa21cf99f96195a6e2b8c6.zip |
Now generating RSS feeds for all lists
Diffstat (limited to 'build')
-rwxr-xr-x | build/generate-rss.hs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/build/generate-rss.hs b/build/generate-rss.hs new file mode 100755 index 0000000..18d2d6f --- /dev/null +++ b/build/generate-rss.hs | |||
@@ -0,0 +1,82 @@ | |||
1 | #!/usr/bin/env runhaskell | ||
2 | |||
3 | import System.Environment (getArgs) | ||
4 | import qualified Control.Monad.State as S | ||
5 | import Control.Monad (sequence) | ||
6 | |||
7 | import Text.Feed.Constructor | ||
8 | import Text.Feed.Util | ||
9 | import Text.Feed.Export | ||
10 | import Text.Feed.Types | ||
11 | |||
12 | import Text.Pandoc | ||
13 | import Text.Pandoc.Pretty | ||
14 | |||
15 | import qualified System.Time as T | ||
16 | import qualified System.Posix.Files as F | ||
17 | import qualified Text.XML.Light.Output as X | ||
18 | import qualified System.FilePath as FP | ||
19 | import System.Posix.Types | ||
20 | |||
21 | import Data.Maybe (fromMaybe) | ||
22 | import Data.List (dropWhileEnd, isSuffixOf) | ||
23 | |||
24 | type FeedState = (Feed, Maybe T.ClockTime) | ||
25 | |||
26 | feedKind = RSSKind $ Just "test" -- DEBUG | ||
27 | |||
28 | extract_title :: Pandoc -> String | ||
29 | extract_title d@(Pandoc m _) = do | ||
30 | title <- render Nothing $ cat $ map pretty (docTitle m) | ||
31 | return title | ||
32 | where pretty :: Inline -> Doc | ||
33 | pretty (Str s) = text s | ||
34 | pretty Space = space | ||
35 | pretty _ = empty | ||
36 | |||
37 | main :: IO () | ||
38 | main = do | ||
39 | (title:entries) <- getArgs | ||
40 | currentTime <- T.getClockTime | ||
41 | let feed = withFeedHome "http://dirty-haskell.org" $ withFeedTitle title $ newFeed feedKind | ||
42 | (populatedFeed, itemTime) <- S.execStateT (sequence $ map addItem' entries) (feed, Nothing) | ||
43 | let populatedFeed' = withFeedDate (toFeedDateString feedKind $ fromMaybe currentTime itemTime) populatedFeed | ||
44 | putStrLn $ X.ppElement $ xmlFeed populatedFeed' | ||
45 | |||
46 | addItem' :: FilePath -> S.StateT FeedState IO () | ||
47 | addItem' mdFile = do | ||
48 | url <- S.liftIO $ mkUrl mdFile | ||
49 | fileStatus <- S.liftIO $ F.getFileStatus mdFile | ||
50 | fileContents <- S.liftIO $ readFile mdFile | ||
51 | let title = extract_title $ document | ||
52 | contents = writeAsciiDoc def document | ||
53 | document = readMarkdown def fileContents | ||
54 | (feed, time) <- S.get | ||
55 | let fileTime = maximum ([F.modificationTime, F.statusChangeTime] <-> fileStatus) :: System.Posix.Types.EpochTime | ||
56 | fileTime' = T.TOD (toInteger $ fromEnum fileTime) 0 | ||
57 | let item = [ withItemTitle title | ||
58 | , withItemLink url | ||
59 | , withItemDate $ toFeedDateString feedKind fileTime' | ||
60 | , withItemDescription contents | ||
61 | ] <--> (newItem feedKind) :: Item | ||
62 | S.put (addItem item feed, if (Just fileTime' > time) then Just fileTime' else time) | ||
63 | |||
64 | mkUrl :: FilePath -> IO String | ||
65 | mkUrl link = do | ||
66 | status <- F.getFileStatus link | ||
67 | if F.isSymbolicLink status then | ||
68 | F.readSymbolicLink link >>= mkUrl | ||
69 | else | ||
70 | return $ mkUrl' link | ||
71 | mkUrl' :: FilePath -> String | ||
72 | mkUrl' path = (++) "http://dirty-haskell.org/posts/" $ switchExt ".md" ".html" $ FP.takeFileName path | ||
73 | |||
74 | switchExt prev after str = if ('.':prev) `isSuffixOf` str then (dropWhileEnd (/= '.') str) ++ after else str | ||
75 | |||
76 | (<->) :: [(a -> b)] -> a -> [b] | ||
77 | [] <-> _ = [] | ||
78 | (f:fs) <-> x = (f x:fs <-> x) | ||
79 | |||
80 | (<-->) :: [(a -> a)] -> a -> a | ||
81 | [] <--> x = x | ||
82 | (f:fs) <--> x = fs <--> (f x) | ||