Implement title detection for articles

This commit is contained in:
Tissevert 2019-02-03 16:14:19 +01:00
parent c8a9a6c9eb
commit 7e4cde152c

View file

@ -5,7 +5,7 @@ module Article (
, at , at
) where ) where
import Control.Monad.State (evalState, state) import Control.Monad.State (evalState, modify, state)
import Data.Text (Text, pack) import Data.Text (Text, pack)
import System.FilePath ((</>)) import System.FilePath ((</>))
import System.Posix.Types (FileID) import System.Posix.Types (FileID)
@ -18,11 +18,21 @@ data Article = Article {
, preview :: String , 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 :: Int -> String -> (String, String)
parseBegining linesCount = evalState (do parseBegining linesCount = evalState (do
first <- state $ splitAt 1 theTitle <- state getTitle
second <- state $ splitAt linesCount modify $ dropWhile $ not . null
return (unlines first, unlines second) modify $ dropWhile null
thePreview <- state $ splitAt linesCount
return (theTitle, unlines thePreview)
) . lines ) . lines
at :: Int -> FilePath -> IO (FileID, Article) at :: Int -> FilePath -> IO (FileID, Article)