From 90c7c4e0cba943327d3a6de7a28bab95dd34c73d Mon Sep 17 00:00:00 2001 From: Stefan Dresselhaus Date: Mon, 6 Feb 2017 15:45:50 +0100 Subject: [PATCH] added functionality --- app/Main.hs | 4 +++- md2mail.cabal | 7 +++++-- src/Lib.hs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index de1c1ab..b4a07b7 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -3,4 +3,6 @@ module Main where import Lib main :: IO () -main = someFunc +main = do + md <- readFile "test.md" + sequence_ $ sequence . fmap print <$> getMails md diff --git a/md2mail.cabal b/md2mail.cabal index 03644fc..dc0ccad 100644 --- a/md2mail.cabal +++ b/md2mail.cabal @@ -1,7 +1,7 @@ name: md2mail version: 0.1.0.0 --- synopsis: --- description: +synopsis: small tool to read a markdown-file and send out mails +description: small tool to read a markdown-file and send out mails homepage: https://github.com/Drezil/md2mail#readme license: BSD3 license-file: LICENSE @@ -17,6 +17,9 @@ library hs-source-dirs: src exposed-modules: Lib build-depends: base >= 4.7 && < 5 + , time + , pandoc + , pandoc-types default-language: Haskell2010 executable md2mail diff --git a/src/Lib.hs b/src/Lib.hs index d36ff27..3180f1e 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -1,6 +1,51 @@ module Lib - ( someFunc + ( getMails + , Mail ) where -someFunc :: IO () -someFunc = putStrLn "someFunc" +import Text.Pandoc +import Text.Pandoc.Error (PandocError(..)) +import Text.Pandoc.Definition +import Data.Time.Calendar (Day, fromGregorian) +import Data.Time.Format (parseTimeM, defaultTimeLocale) +import Data.Monoid +import Data.Maybe +import Debug.Trace + +data Mail = Mail + { title :: String + , content :: String + , to :: Maybe String --Alternate Adress to send to + } deriving (Eq, Show) + +mkTestmail :: (Show a) => a -> (Day,Mail) +mkTestmail s = (fromGregorian 1999 1 1, Mail "Test" (show s) Nothing) + +getMails :: String -> Either PandocError [(Day, Mail)] +getMails markdown = do + (Pandoc meta document) <- readMarkdown def markdown + return $ filter isBullet document >>= mkMail + + +isBullet :: Block -> Bool +isBullet (BulletList _) = True +isBullet _ = False + +mkMail :: Block -> [(Day, Mail)] +mkMail (BulletList blocks) = catMaybes $ blToMail <$> blocks +mkMail _ = [] + +blToMail :: [Block] -> Maybe (Day, Mail) +blToMail (Para (Str dat:Space:tit):CodeBlock ca cont:_) = trace (show ca) dayMailPair + where + dayofmail d = parseTimeM True defaultTimeLocale "%d.%m." d :: Maybe Day + titleofmail = writePlain def (Pandoc (Meta mempty) [Para tit]) + contentofmail (_,a,_) = if "mail" `elem` a then Just cont else Nothing + completeMail :: Maybe Mail + completeMail = Mail <$> pure titleofmail + <*> contentofmail ca + <*> pure Nothing + dayMailPair :: Maybe (Day, Mail) + dayMailPair = (,) <$> dayofmail dat + <*> completeMail +blToMail _ = Nothing -- Just $ mkTestmail a