1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE StrictData #-}
4 {-# OPTIONS_GHC -Wno-missing-signatures #-}
5 module Textphile.CLI.Source where
7 import Control.Applicative (Applicative(..), Alternative(..))
8 import Control.Monad (Monad(..), forM_, unless, when)
9 import Control.Monad.Trans.Except (runExcept)
10 import Control.Monad.Trans.Maybe (MaybeT(..))
11 import Control.Monad.Trans.State.Strict (runState)
13 import Data.Either (Either(..))
14 import Data.Eq (Eq(..))
15 import Data.Foldable as Foldable (Foldable(..))
16 import Data.Function (($), (.), id, flip)
17 import Data.Functor ((<$>))
18 import Data.Maybe (Maybe(..), fromMaybe)
19 import Data.Ord (Ord(..))
20 import Data.Proxy (Proxy(..))
21 import Data.Semigroup (Semigroup(..))
22 import Data.Text (Text)
23 import Prelude (error)
24 import Symantic.CLI as CLI
25 import System.FilePath ((-<.>))
26 import System.IO (IO, FilePath)
27 import Text.Show (Show(..))
28 import qualified Data.ByteString as BS
29 import qualified Data.List as List
30 import qualified Data.Text as Text
31 import qualified Data.Text.Lazy as TL
32 import qualified Data.Text.Lazy.IO as TL
33 import qualified Data.TreeSeq.Strict as Tree
34 import qualified Paths_textphile as Textphile
35 import qualified Symantic.Document as Doc
36 import qualified System.Directory as IO
37 import qualified System.Environment as Env
38 import qualified System.FilePath as FP
39 import qualified System.FilePath as FilePath
40 import qualified System.IO as IO
41 import qualified Text.Blaze.DTC as Blaze.DTC
42 import qualified Text.Blaze.HTML5 as Blaze.HTML5
43 import qualified Text.Blaze.Renderer.Utf8 as Blaze
44 import qualified Text.Blaze.Utils as Blaze
45 import qualified Text.Megaparsec as P
46 import qualified Textphile.DTC.Read.TCT as DTC
47 import qualified Textphile.DTC.Sym as DTC
48 import qualified Textphile.DTC.Write.HTML5 as DTC
49 import qualified Textphile.DTC.Write.XML as DTC
50 import qualified Textphile.TCT as TCT
51 import qualified Textphile.TCT.Write.HTML5 as TCT
52 import qualified Textphile.TCT.Write.Plain as TCT
53 import qualified Textphile.TCT.Write.XML as TCT
54 import qualified Textphile.Utils as FS
56 import Textphile.CLI.Lang
57 import Textphile.CLI.Utils
59 data Cfg_Source = Cfg_Source
60 { cfg_source_dump_tct :: Bool
64 helps l10n_help_command_source $
85 helps l10n_help_opt_input $
86 var @FP.FilePath "INPUT"
89 cfg_global@Cfg_Global{..}
90 cfg_source@Cfg_Source{..} =
94 run_source_tct cfg_source_input cfg_source_output = do
95 TCT.readTCT cfg_source_input >>= \case
96 Left err -> error $ show err
98 when cfg_source_dump_tct $
99 FS.writeFile (cfg_source_output-<.>"tct.dump") $
100 TL.pack $ Tree.prettyTrees tct
102 run_source_plain cfg_source_input = do
103 let cfg_source_output = cfg_source_input FilePath.<.>"txt"
104 tct <- run_source_tct cfg_source_input cfg_source_output
105 FS.writeFile cfg_source_output $
107 run_source_html5 cfg_source_input = do
108 let cfg_source_output = cfg_source_input FilePath.<.>"html5"
109 tct <- run_source_tct cfg_source_input cfg_source_output
110 FS.withFile cfg_source_output IO.WriteMode $ \h ->
111 Blaze.renderMarkupToByteStringIO (BS.hPutStr h) $