{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Headroom.Header
( Header(..)
, addHeader
, containsHeader
, dropHeader
, headerSize
, replaceHeader
)
where
import Headroom.FileType ( FileType(..) )
import Headroom.Header.Impl
import qualified Headroom.Text as T
import Headroom.Types ( NewLine(..) )
import RIO
import qualified RIO.List as L
data =
{ Header -> FileType
hFileType :: FileType
, Header -> Text
hContent :: Text
}
deriving (Header -> Header -> Bool
(Header -> Header -> Bool)
-> (Header -> Header -> Bool) -> Eq Header
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Header -> Header -> Bool
$c/= :: Header -> Header -> Bool
== :: Header -> Header -> Bool
$c== :: Header -> Header -> Bool
Eq, Int -> Header -> ShowS
[Header] -> ShowS
Header -> String
(Int -> Header -> ShowS)
-> (Header -> String) -> ([Header] -> ShowS) -> Show Header
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Header] -> ShowS
$cshowList :: [Header] -> ShowS
show :: Header -> String
$cshow :: Header -> String
showsPrec :: Int -> Header -> ShowS
$cshowsPrec :: Int -> Header -> ShowS
Show)
addHeader :: Header
-> Text
-> Text
(Header fileType :: FileType
fileType content :: Text
content) input :: Text
input = Text
output
where
output :: Text
output = if Bool
containsHeader' then Text
input else Text
content Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
newLine Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
input
containsHeader' :: Bool
containsHeader' = FileType -> Text -> Bool
containsHeader FileType
fileType Text
input
newLine :: Text
newLine = NewLine -> Text
T.showNewLine (NewLine -> Text) -> NewLine -> Text
forall a b. (a -> b) -> a -> b
$ NewLine -> Maybe NewLine -> NewLine
forall a. a -> Maybe a -> a
fromMaybe NewLine
LF (Text -> Maybe NewLine
T.detectNewLine Text
input)
containsHeader :: FileType
-> Text
-> Bool
fileType :: FileType
fileType input :: Text
input = FileType -> Text -> Int
headerSize FileType
fileType Text
input Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
dropHeader :: FileType
-> Text
-> Text
fileType :: FileType
fileType input :: Text
input = NewLine -> [Text] -> Text
T.unlines' NewLine
newLine ([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
numLines ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ [Text]
lines'
where
numLines :: Int
numLines = FileType -> Text -> Int
headerSize FileType
fileType Text
input
(newLine :: NewLine
newLine, lines' :: [Text]
lines') = Text -> (NewLine, [Text])
T.lines' Text
input
headerSize :: FileType
-> Text
-> Int
= \case
CSS -> Text -> Int
headerSizeCSS
Haskell -> Text -> Int
headerSizeHaskell
HTML -> Text -> Int
headerSizeHTML
Java -> Text -> Int
headerSizeJava
JS -> Text -> Int
headerSizeJS
Scala -> Text -> Int
headerSizeScala
replaceHeader :: Header
-> Text
-> Text
h :: Header
h@(Header fileType :: FileType
fileType _) = Header -> Text -> Text
addHeader Header
h (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FileType -> Text -> Text
dropHeader FileType
fileType