-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-post.hs
executable file
·43 lines (34 loc) · 1.04 KB
/
new-post.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
#!/usr/bin/env stack
{- stack
script
--resolver=lts-13.8
--package base
--package text
--package time
-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Time.Clock
import Data.Time.Calendar
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import System.Environment (getArgs)
type Y = Integer
type M = Int
type D = Int
getDate :: IO (Y,M,D)
getDate = (toGregorian . utctDay) <$> getCurrentTime
mkDateString :: Y -> M -> D -> String
mkDateString y m d = year ++ "-" ++ month ++ "-" ++ day
where
year = show y
month = if m > 9 then show m else "0" ++ show m
day = if d > 9 then show d else "0" ++ show d
main = do
title <- (T.pack . head) <$> getArgs
tmpl <- TIO.readFile "templates/new-post.markdown"
(y,m,d) <- getDate
let date = T.pack $ mkDateString y m d
content = T.replace "DATE" date $
T.replace "TITLE" title tmpl
filepath = T.unpack $ "posts/" <> date <> "-" <> (T.replace " " "-" title) <> ".markdown"
TIO.writeFile filepath content