1 {-# LANGUAGE OverloadedStrings #-}
3 module Text.Edifact.BundleReader.Commons where
5 import Text.Edifact.BundleReader.Configuration
6 import Text.Edifact.Scaffolder.Commons
8 import Control.Monad.Reader (ReaderT, runReaderT)
9 import Data.ByteString (ByteString)
10 import Data.Char (toLower)
11 import Data.List.Split (splitWhen)
12 import Data.Text as T (isInfixOf, lines,
16 import Data.Text.Encoding as TE (decodeLatin1,
18 import Data.Text.IO as TIO (writeFile)
20 import Text.Parsec (Parsec, runParser)
22 type BundleReader = ReaderT BundleReaderEnv IO
24 decodeContent :: ByteString -> Text
25 decodeContent content = either (const $ cleanupAsciiArt $ decodeLatin1 content) id (decodeUtf8' content)
27 splitFileByDash :: Int -> Text -> [Text]
29 let separator = T.replicate n "-"
30 isNotEmpty = not . T.null . T.strip
32 filter isNotEmpty . fmap T.unlines . splitWhen (separator `T.isInfixOf`) . T.lines
34 runBundleReader :: BundleReader () -> BundleReaderEnv -> IO ()
35 runBundleReader = runReaderT
37 getOutputFile :: Format String (a -> String) -> FilePath -> a -> BundleReader FilePath
38 getOutputFile f d c = do
39 home <- getSpecificationHome
40 pure (formatToString (string % "/" % string % "/" % f) home d c)
42 getSpecificationHome :: BundleReader FilePath
43 getSpecificationHome = do
45 rev <- getTargetRevision
46 pure (home </> formatToString fRevision rev)
48 toFile :: Text -> FilePath -> BundleReader ()
49 toFile specification outputFile = liftIO (TIO.writeFile outputFile specification)
51 parseOrFail :: (Monoid u) => Text -> Parsec String u a -> BundleReader a
52 parseOrFail specification parser = either (error . (\a -> show specification <> show a)) pure (runParser parser mempty "" (T.unpack specification))
54 toFileWithParser :: (Monoid a, Monoid u) => Text -> FilePath -> Parsec String u a -> BundleReader a
55 toFileWithParser specification outputFile parser = do
56 liftIO (TIO.writeFile outputFile specification)
57 either (error . show) pure (runParser parser mempty "" (T.unpack specification))
59 lower :: Format r (String -> r)
60 lower = mapf (fmap toLower) string
62 fRevision :: Format r (Revision -> r)
63 fRevision = mapf getRevision string
65 fRevisionLower :: Format r (Revision -> r)
66 fRevisionLower = mapf getRevision lower
68 fMessageCodeLower :: Format r (MessageCode -> r)
69 fMessageCodeLower = mapf getMessageCode lower
71 fSegmentCodeLower :: Format r (SegmentCode -> r)
72 fSegmentCodeLower = mapf getSegmentCode lower
74 fCompositeCodeLower :: Format r (CompositeCode -> r)
75 fCompositeCodeLower = mapf getCompositeCode lower
77 fSimpleCodeLower :: Format r (SimpleCode -> r)
78 fSimpleCodeLower = mapf getSimpleCode lower
80 -- This might not be the proper way to do it...
81 -- Use Data.Text.Encoding.decodeUtf8With instead?
82 cleanupAsciiArt :: Text -> Text