Skip to content
Newer
Older
100644 73 lines (65 sloc) 2.11 KB
b5b636d @snoyberg Scaffolded site
snoyberg authored
1 module Import
2 ( module Prelude
3 , module Foundation
4 , (<>)
5 , Text
6 , module Data.Monoid
7 , module Control.Applicative
411c389 @snoyberg Initial Import.Content, no Markdown support yet
snoyberg authored
8 , module Import.Content
bf3cd5c @snoyberg Styling
snoyberg authored
9 , module Settings.StaticFiles
10 , getNewestBlog
5cf5af4 @snoyberg Blog feed
snoyberg authored
11 , getBlogList
bf3cd5c @snoyberg Styling
snoyberg authored
12 , prettyDay
7919f8c @snoyberg Wrap book and blog in IORef
snoyberg authored
13 , loadBook
14 , loadBlog
b5b636d @snoyberg Scaffolded site
snoyberg authored
15 ) where
16
17 import Prelude hiding (writeFile, readFile)
18 import Foundation
19 import Data.Monoid (Monoid (mappend, mempty, mconcat))
20 import Control.Applicative ((<$>), (<*>), pure)
21 import Data.Text (Text)
411c389 @snoyberg Initial Import.Content, no Markdown support yet
snoyberg authored
22 import Import.Content
bf3cd5c @snoyberg Styling
snoyberg authored
23 import Settings.StaticFiles
24 import Data.Maybe (listToMaybe)
25 import Data.List (sortBy)
26 import Data.Ord (comparing)
27 import qualified Data.Map as Map
28 import Data.Time
29 import System.Locale (defaultTimeLocale)
7919f8c @snoyberg Wrap book and blog in IORef
snoyberg authored
30 import qualified Book
31 import qualified Filesystem.Path.CurrentOS as F
32 import qualified Data.Yaml
33 import Settings (bookRoot, blogRoot)
34 import Data.IORef (readIORef)
b5b636d @snoyberg Scaffolded site
snoyberg authored
35
36 infixr 5 <>
37 (<>) :: Monoid m => m -> m -> m
411c389 @snoyberg Initial Import.Content, no Markdown support yet
snoyberg authored
38 (<>) = mappend
bf3cd5c @snoyberg Styling
snoyberg authored
39
5cf5af4 @snoyberg Blog feed
snoyberg authored
40 getBlogList :: Handler [(Route YesodWeb, Post)]
41 getBlogList = do
7919f8c @snoyberg Wrap book and blog in IORef
snoyberg authored
42 iblog <- ywBlog <$> getYesod
43 Blog blog <- liftIO $ readIORef iblog
5cf5af4 @snoyberg Blog feed
snoyberg authored
44 return $ concatMap go' $ concatMap go $ reverse $ Map.toList blog
45 where
46 go :: (a, Map.Map b c) -> [(a, b, c)]
47 go (a, m) = do
48 (b, c) <- reverse $ Map.toList m
49 return (a, b, c)
50 go' :: (Year, Month, [(Slug, Post)]) -> [(Route YesodWeb, Post)]
51 go' (y, m, ps) = do
52 (s, p) <- ps
53 return (BlogPostR y m s, p)
54
bf3cd5c @snoyberg Styling
snoyberg authored
55 getNewestBlog :: Handler (Route YesodWeb, Post)
56 getNewestBlog = do
7919f8c @snoyberg Wrap book and blog in IORef
snoyberg authored
57 iblog <- ywBlog <$> getYesod
58 Blog blog <- liftIO $ readIORef iblog
bf3cd5c @snoyberg Styling
snoyberg authored
59 maybe notFound return $ listToMaybe $ do
60 (year, x) <- take 1 $ reverse $ sortBy (comparing fst) $ Map.toList blog
61 (month, y) <- take 1 $ reverse $ sortBy (comparing fst) $ Map.toList x
62 (slug, post) <- take 1 y
63 return (BlogPostR year month slug, post)
64
65 prettyDay :: UTCTime -> String
66 prettyDay = formatTime defaultTimeLocale "%B %e, %Y"
7919f8c @snoyberg Wrap book and blog in IORef
snoyberg authored
67
68 loadBook :: IO Book.Book
6f517c1 @snoyberg Simplified XML based book
snoyberg authored
69 loadBook = Book.loadBook $ bookRoot F.</> "yesod-web-framework-book.ditamap"
7919f8c @snoyberg Wrap book and blog in IORef
snoyberg authored
70
71 loadBlog :: IO (Maybe Blog)
72 loadBlog = Data.Yaml.decodeFile $ F.encodeString $ blogRoot F.</> "posts.yaml"
Something went wrong with that request. Please try again.