1 {-# LANGUAGE DerivingStrategies #-}
2 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
3 {-# LANGUAGE OverloadedStrings #-}
5 module Text.Edifact.Scaffolder.Commons.Text
20 import Text.Edifact.Scaffolder.Commons.Types (LanguageExtension (..))
22 import Control.Category ((>>>))
23 import Data.Char (isSpace)
24 import Data.List (dropWhileEnd)
25 import Data.String (IsString)
26 import Data.Text (Text)
27 import qualified Data.Text as T (all, dropWhileEnd,
29 import Formatting as F (mapf, sformat,
32 formatSpecification :: [Text] -> [Text]
33 formatSpecification = cleanEmptyLines
37 prependQuote :: [Text] -> [Text]
39 [ "-- | Derived from this specification:"
43 cleanEmptyLines :: [Text] -> [Text]
44 cleanEmptyLines = dropWhile blank >>> dropWhileEnd blank
47 blank t = T.null t || T.all isSpace t
49 quoteLine :: Text -> Text
50 quoteLine = haskellQuote >>> cleanWhitespaces
52 haskellQuote :: Text -> Text
53 haskellQuote line = "-- > " <> line
55 cleanWhitespaces :: Text -> Text
56 cleanWhitespaces = T.dropWhileEnd (== ' ')
58 indent :: Text -> Text
62 quote t = "'" <> t <> "'"
64 haskellList :: [Text] -> [Text]
66 let prefix :: Int -> Text -> Text
67 prefix 1 dep = sformat ("[ " % F.stext) dep
68 prefix _ dep = sformat (", " % F.stext) dep
69 suffix deps = deps <> ["]"]
70 in suffix . zipWith prefix [1..]
72 newtype CommaSeparated = CommaSeparated { getCommaSeparated :: Text } deriving newtype (IsString, Eq)
74 instance Semigroup CommaSeparated where
77 t1 <> t2 = CommaSeparated (getCommaSeparated t1 <> ", " <> getCommaSeparated t2)
79 instance Monoid CommaSeparated where
82 commaSeparated :: Foldable f => f Text -> Text
83 commaSeparated = getCommaSeparated . foldMap CommaSeparated
88 extensions :: [LanguageExtension] -> [Text]
90 let fExtension = "{-# LANGUAGE " % mapf getLanguageExtension F.string % " #-}"
91 in fmap (sformat fExtension)