Implement title detection for articles

This commit is contained in:
Tissevert 2019-02-03 16:14:19 +01:00
parent c8a9a6c9eb
commit 7e4cde152c
1 changed files with 14 additions and 4 deletions

View File

@ -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)