diff --git a/src/Article.hs b/src/Article.hs index 3b8c9b3..40ef9a3 100644 --- a/src/Article.hs +++ b/src/Article.hs @@ -5,7 +5,7 @@ module Article ( , at ) where -import Control.Monad.State (evalState, state) +import Control.Monad.State (evalState, modify, state) import Data.Text (Text, pack) import System.FilePath (()) import System.Posix.Types (FileID) @@ -18,11 +18,21 @@ data Article = Article { , preview :: String } +getTitle :: [String] -> (String, [String]) +getTitle [] = ("", []) +getTitle (('#':' ':aTitle):rest) = (aTitle, rest) +getTitle (a:b:l) + | length a == length b && (all (== '#') b || all (== '=') b) = (a, b:l) + | otherwise = getTitle (b:l) +getTitle (_:rest) = getTitle rest + parseBegining :: Int -> String -> (String, String) parseBegining linesCount = evalState (do - first <- state $ splitAt 1 - second <- state $ splitAt linesCount - return (unlines first, unlines second) + theTitle <- state getTitle + modify $ dropWhile $ not . null + modify $ dropWhile null + thePreview <- state $ splitAt linesCount + return (theTitle, unlines thePreview) ) . lines at :: Int -> FilePath -> IO (FileID, Article)