aboutsummaryrefslogtreecommitdiffhomepage
path: root/scaffolder/src/Text/Edifact/Scaffolder/Commons/Text.hs
diff options
context:
space:
mode:
Diffstat (limited to 'scaffolder/src/Text/Edifact/Scaffolder/Commons/Text.hs')
-rw-r--r--scaffolder/src/Text/Edifact/Scaffolder/Commons/Text.hs91
1 files changed, 91 insertions, 0 deletions
diff --git a/scaffolder/src/Text/Edifact/Scaffolder/Commons/Text.hs b/scaffolder/src/Text/Edifact/Scaffolder/Commons/Text.hs
new file mode 100644
index 0000000..ef4e805
--- /dev/null
+++ b/scaffolder/src/Text/Edifact/Scaffolder/Commons/Text.hs
@@ -0,0 +1,91 @@
1{-# LANGUAGE DerivingStrategies #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE OverloadedStrings #-}
4
5module Text.Edifact.Scaffolder.Commons.Text
6 ( -- *
7 indent
8 , quote
9 , haskellList
10 , commaSeparated
11 -- *
12 , newline
13 -- *
14 , formatSpecification
15 -- *
16 , extensions
17 ) where
18
19
20import Text.Edifact.Scaffolder.Commons.Types (LanguageExtension (..))
21
22import Control.Category ((>>>))
23import Data.Char (isSpace)
24import Data.List (dropWhileEnd)
25import Data.String (IsString)
26import Data.Text (Text)
27import qualified Data.Text as T (all, dropWhileEnd,
28 null)
29import Formatting as F (mapf, sformat,
30 stext, string, (%))
31
32formatSpecification :: [Text] -> [Text]
33formatSpecification = cleanEmptyLines
34 >>> fmap quoteLine
35 >>> prependQuote
36
37prependQuote :: [Text] -> [Text]
38prependQuote ls =
39 [ "-- | Derived from this specification:"
40 , "--"
41 ] <> ls
42
43cleanEmptyLines :: [Text] -> [Text]
44cleanEmptyLines = dropWhile blank >>> dropWhileEnd blank
45
46blank :: Text -> Bool
47blank t = T.null t || T.all isSpace t
48
49quoteLine :: Text -> Text
50quoteLine = haskellQuote >>> cleanWhitespaces
51
52haskellQuote :: Text -> Text
53haskellQuote line = "-- > " <> line
54
55cleanWhitespaces :: Text -> Text
56cleanWhitespaces = T.dropWhileEnd (== ' ')
57
58indent :: Text -> Text
59indent t = " " <> t
60
61quote :: Text -> Text
62quote t = "'" <> t <> "'"
63
64haskellList :: [Text] -> [Text]
65haskellList =
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..]
71
72newtype CommaSeparated = CommaSeparated { getCommaSeparated :: Text } deriving newtype (IsString, Eq)
73
74instance Semigroup CommaSeparated where
75 t1 <> "" = t1
76 "" <> t2 = t2
77 t1 <> t2 = CommaSeparated (getCommaSeparated t1 <> ", " <> getCommaSeparated t2)
78
79instance Monoid CommaSeparated where
80 mempty = ""
81
82commaSeparated :: Foldable f => f Text -> Text
83commaSeparated = getCommaSeparated . foldMap CommaSeparated
84
85newline :: [Text]
86newline = [""]
87
88extensions :: [LanguageExtension] -> [Text]
89extensions =
90 let fExtension = "{-# LANGUAGE " % mapf getLanguageExtension F.string % " #-}"
91 in fmap (sformat fExtension)