{-# 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
= [Text] -> Maybe Text
go ([Text] -> Maybe Text) -> (Text -> [Text]) -> Text -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
toLines
where
go :: [Text] -> Maybe Text
go [] = Maybe Text
forall a. Maybe a
Nothing
go (Text
x : [Text]
xs) = Maybe Text -> ([Text] -> Maybe Text) -> Maybe [Text] -> Maybe Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Text] -> Maybe Text
go [Text]
xs) ([Text] -> Getting (First Text) [Text] Text -> Maybe Text
forall s a. s -> Getting (First a) s a -> Maybe a
^? Index [Text] -> Traversal' [Text] (IxValue [Text])
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index [Text]
1) (Regex -> Text -> Maybe [Text]
match [re|^module\s+(\S+)|] Text
x)
extractVariables :: CtHeaderConfig
-> Maybe TemplateMeta
-> Maybe (Int, Int)
-> Text
-> Variables
CtHeaderConfig
_ Maybe TemplateMeta
meta Maybe (Int, Int)
headerPos Text
text = ([(Text, Text)] -> Variables
mkVariables ([(Text, Text)] -> Variables)
-> ([Maybe (Text, Text)] -> [(Text, Text)])
-> [Maybe (Text, Text)]
-> Variables
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe (Text, Text)] -> [(Text, Text)]
forall a. [Maybe a] -> [a]
catMaybes)
[ (Text
"_haskell_module_copyright", ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
hmhCopyright
, (Text
"_haskell_module_license", ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
hmhLicense
, (Text
"_haskell_module_maintainer", ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
hmhMaintainer
, (Text
"_haskell_module_name", ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Text
extractModuleName Text
text
, (Text
"_haskell_module_portability", ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
hmhPortability
, (Text
"_haskell_module_stability", ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
hmhStability
, (Text
"_haskell_module_longdesc", ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
hmhLongDesc
, (Text
"_haskell_module_shortdesc", ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
hmhShortDesc
]
where
HaddockModuleHeader {Maybe Text
hmhLongDesc :: HaddockModuleHeader -> Maybe Text
hmhShortDesc :: HaddockModuleHeader -> Maybe Text
hmhStability :: HaddockModuleHeader -> Maybe Text
hmhPortability :: HaddockModuleHeader -> Maybe Text
hmhMaintainer :: HaddockModuleHeader -> Maybe Text
hmhLicense :: HaddockModuleHeader -> Maybe Text
hmhCopyright :: HaddockModuleHeader -> Maybe Text
hmhShortDesc :: Maybe Text
hmhLongDesc :: Maybe Text
hmhStability :: Maybe Text
hmhPortability :: Maybe Text
hmhMaintainer :: Maybe Text
hmhLicense :: Maybe Text
hmhCopyright :: Maybe Text
..} = Text -> Maybe TemplateMeta -> HaddockModuleHeader
extractModuleHeader Text
headerText Maybe TemplateMeta
meta
headerText :: Text
headerText = Text -> ((Int, Int) -> Text) -> Maybe (Int, Int) -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (\(Int
s, Int
e) -> Int -> Int -> Text -> Text
cut Int
s Int
e Text
text) Maybe (Int, Int)
headerPos
cut :: Int -> Int -> Text -> Text
cut Int
s Int
e = [Text] -> Text
fromLines ([Text] -> Text) -> (Text -> [Text]) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Text] -> [Text]
forall a. Int -> [a] -> [a]
L.take (Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
s) ([Text] -> [Text]) -> (Text -> [Text]) -> Text -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Text] -> [Text]
forall a. Int -> [a] -> [a]
L.drop Int
s ([Text] -> [Text]) -> (Text -> [Text]) -> Text -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
toLines
extractTemplateMeta :: (Template t)
=> t
-> TemplateMeta
t
template = HaddockFieldOffsets -> TemplateMeta
HaskellTemplateMeta HaddockFieldOffsets
offsets
where offsets :: HaddockFieldOffsets
offsets = t -> HaddockFieldOffsets
forall t. Template t => t -> HaddockFieldOffsets
extractFieldOffsets t
template