gonito/Handler/Extract.hs

59 lines
1.6 KiB
Haskell
Raw Normal View History

2015-09-04 15:10:47 +02:00
module Handler.Extract where
import Import
2018-05-19 07:17:54 +02:00
import qualified Data.Text as T
2015-09-04 15:10:47 +02:00
import Text.Pandoc
import Text.Pandoc.Walk (walk)
import Text.Pandoc.Shared (stringify)
import Data.Maybe
2017-09-22 14:23:03 +02:00
import System.IO (withFile, IOMode(..), readFile)
2015-09-04 15:10:47 +02:00
extractHeaders :: Block -> [String]
extractHeaders (Header 1 _ x) = [stringify x]
extractHeaders _ = []
extractFirstHeader :: Pandoc -> Maybe String
extractFirstHeader doc = case queryWith extractHeaders doc of
(s:_) -> Just s
[] -> Nothing
extractParas :: Block -> [String]
extractParas (Para x) = [stringify x]
extractParas _ = []
extractFirstPara :: Pandoc -> Maybe String
extractFirstPara doc = case queryWith extractParas doc of
(s:_) -> Just s
[] -> Nothing
readDoc :: String -> Pandoc
2018-05-19 07:17:54 +02:00
readDoc s = case runPure $ readMarkdown def (T.pack s) of
2015-09-04 15:10:47 +02:00
Right doc -> doc
Left err -> error (show err)
defaultTitle :: String
defaultTitle = "[???]"
defaultDescription :: String
defaultDescription = ""
readmeFile :: FilePath
readmeFile = "README.md"
2018-01-18 08:21:06 +01:00
imageFile :: FilePath
imageFile = ".seeme.png"
2015-09-04 15:10:47 +02:00
getTitleAndDescription :: String -> (String, String)
getTitleAndDescription contents = (title, description)
where title = fromMaybe defaultTitle $ extractFirstHeader doc
description = fromMaybe defaultDescription $ extractFirstPara doc
doc = readDoc contents
extractTitleAndDescription :: FilePath -> IO (String, String)
extractTitleAndDescription fp = do
2017-09-22 14:23:03 +02:00
contents <- System.IO.readFile fp
2015-09-04 15:10:47 +02:00
return $ getTitleAndDescription contents