{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
module Headroom.Ext.Haskell
(
extractModuleName
, extractVariables
, extractTemplateMeta
)
where
import Headroom.Configuration.Types ( CtHeaderConfig )
import Headroom.Data.Regex ( match
, re
)
import Headroom.Data.TextExtra ( fromLines
, toLines
)
import Headroom.Ext.Haskell.Haddock ( HaddockModuleHeader(..)
, extractFieldOffsets
, extractModuleHeader
)
import Headroom.Template ( Template(..) )
import Headroom.Types ( TemplateMeta(..) )
import Headroom.Variables ( mkVariables )
import Headroom.Variables.Types ( Variables(..) )
import RIO
import RIO.Lens ( ix )
import qualified RIO.List as L
extractModuleName :: Text
-> Maybe Text
extractModuleName = go . toLines
where
go [] = Nothing
go (x : xs) = maybe (go xs) (^? ix 1) (match [re|^module\s+(\S+)|] x)
extractVariables :: CtHeaderConfig
-> Maybe TemplateMeta
-> Maybe (Int, Int)
-> Text
-> Variables
extractVariables _ meta headerPos text = (mkVariables . catMaybes)
[ ("_haskell_module_copyright", ) <$> hmhCopyright
, ("_haskell_module_license", ) <$> hmhLicense
, ("_haskell_module_maintainer", ) <$> hmhMaintainer
, ("_haskell_module_name", ) <$> extractModuleName text
, ("_haskell_module_portability", ) <$> hmhPortability
, ("_haskell_module_stability", ) <$> hmhStability
, ("_haskell_module_longdesc", ) <$> hmhLongDesc
, ("_haskell_module_shortdesc", ) <$> hmhShortDesc
]
where
HaddockModuleHeader {..} = extractModuleHeader headerText meta
headerText = maybe "" (\(s, e) -> cut s e text) headerPos
cut s e = fromLines . L.take (e - s) . L.drop s . toLines
extractTemplateMeta :: (Template t)
=> t
-> TemplateMeta
extractTemplateMeta template = HaskellTemplateMeta offsets
where offsets = extractFieldOffsets template