1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE ExistentialQuantification #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE MultiParamTypeClasses #-}
6 {-# LANGUAGE OverloadedStrings #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 module Textphile.DTC.Write.Plain where
11 import Control.Applicative (Applicative(..), liftA2)
12 import Control.Category
14 import Data.Default.Class (Default(..))
15 import Data.Foldable (Foldable(..), concat)
16 import Data.Function (($))
18 import Data.Maybe (Maybe(..), maybe)
19 import Data.Monoid (Monoid(..))
20 import Data.Semigroup (Semigroup(..))
21 import Data.Text (Text)
22 import Data.TreeSeq.Strict (Tree(..))
23 import Data.String (String, IsString(..))
25 import Text.Show (Show(..))
26 import qualified Control.Monad.Trans.Reader as R
27 import qualified Data.List as List
28 import qualified Data.Text.Lazy as TL
29 import qualified Data.Text.Lazy.Builder as TLB
30 import qualified Symantic.XML as XML
32 import Data.Locale hiding (Index)
34 import Textphile.DTC.Write.XML ()
35 import Textphile.DTC.Document as DTC hiding (Plain)
36 import qualified Textphile.DTC.Document as DTC
39 type Plain = R.Reader Reader TLB.Builder
41 runPlain :: Plain -> Reader -> TL.Text
42 runPlain p ro = TLB.toLazyText $ R.runReader p ro
44 text :: Plainify a => Reader -> a -> TL.Text
45 text ro a = runPlain (plainify a) ro
47 instance IsString Plain where
48 fromString = return . fromString
49 instance Semigroup Plain where
51 instance Monoid Plain where
56 data Reader = Reader -- TODO: could be a Reader
57 { reader_l10n :: Loqualization (L10n Plain)
60 instance Default Reader where
62 { reader_l10n = Loqualization EN_US
63 , reader_quote = Nat 0
67 class Plainify a where
68 plainify :: a -> Plain
69 instance Plainify String where
70 plainify = return . TLB.fromString
71 instance Plainify Text where
72 plainify = return . TLB.fromText
73 instance Plainify TL.Text where
74 plainify = return . TLB.fromLazyText
76 instance Plainify Para where
78 ParaItem{..} -> plainify item
79 ParaItems{..} -> plainify items
81 instance Plainify DTC.Plain where
82 plainify = foldMap plainify
83 instance Plainify (Tree PlainNode) where
84 plainify (Tree n ls) =
87 PlainText txt -> plainify txt
88 PlainGroup -> plainify ls
89 PlainB -> "*"<>plainify ls<>"*"
90 PlainCode -> "`"<>plainify ls<>"`"
91 PlainDel -> "-"<>plainify ls<>"-"
92 PlainI -> "/"<>plainify ls<>"/"
93 PlainNote{..} -> "" -- TODO: to be coded, with a switch on/off in Reader
95 Reader{reader_l10n=Loqualization loc} <- R.ask
96 l10n_Quote (plainify ls) loc
97 PlainSC -> plainify ls
98 PlainSpan{..} -> plainify ls
99 PlainSub -> plainify ls
100 PlainSup -> plainify ls
101 PlainU -> "_"<>plainify ls<>"_"
102 PlainEref{..} -> plainify ls
103 PlainIref{..} -> plainify ls
104 PlainAt{..} -> (if at_back then "~" else mempty)<>"@"<>plainify ls<>"@"
105 PlainTag{..} -> (if tag_back then "~" else mempty)<>"#"<>plainify ls<>"#"
107 (if null ls then mempty else "("<>plainify ls<> ")") <>
108 "["<>plainify (unIdent ref_ident)<>"]"
110 (if null ls then mempty else "("<>plainify ls<> ")") <>
111 maybe "@" (\at -> "@"<>plainify (unIdent at)<>"@") pageRef_at <>
112 "["<>plainify pageRef_path<>"]"
113 instance Plainify Title where
114 plainify (Title t) = plainify t
115 instance Plainify XML.QName where
116 plainify = plainify . show
117 instance Plainify Int where
118 plainify = plainify . show
119 instance Plainify Nat where
120 plainify (Nat n) = plainify n
121 instance Plainify Nat1 where
122 plainify (Nat1 n) = plainify n
125 class L10n msg lang where
126 l10n_Colon :: FullLocale lang -> msg
127 l10n_Table_of_Contents :: FullLocale lang -> msg
128 l10n_Quote :: msg -> FullLocale lang -> msg
129 l10n_Date :: Date -> FullLocale lang -> msg
131 instance L10n TL.Text FR where
132 l10n_Colon _loc = " : "
133 l10n_Table_of_Contents _loc = "Sommaire"
134 l10n_Quote msg _loc = "« "<>msg<>" »"
135 l10n_Date Date{..} _loc =
138 List.intersperse " " $
140 [ maybe [] (pure . show) date_day
153 9 -> pure "septembre"
155 11 -> pure "novembre"
156 12 -> pure "décembre"
160 instance L10n TL.Text EN where
161 l10n_Colon _loc = ": "
162 l10n_Table_of_Contents _loc = "Table of Contents"
163 l10n_Quote msg _loc = "“"<>msg<>"”"
164 l10n_Date Date{..} _loc =
167 List.intersperse " " $
169 [ maybe [] (pure . show) date_day
182 9 -> pure "September"
184 11 -> pure "November"
185 12 -> pure "December"
190 instance L10n Plain FR where
191 l10n_Colon loc = plainify (l10n_Colon loc::TL.Text)
192 l10n_Table_of_Contents loc = plainify (l10n_Table_of_Contents loc::TL.Text)
193 l10n_Quote msg _loc = do
194 depth <- R.asks reader_quote
196 case unNat depth `mod` 3 of
200 m <- R.local (\ro -> ro{reader_quote=succNat depth}) msg
202 l10n_Date date loc = plainify (l10n_Date date loc::TL.Text)
203 instance L10n Plain EN where
204 l10n_Colon loc = plainify (l10n_Colon loc::TL.Text)
205 l10n_Table_of_Contents loc = plainify (l10n_Table_of_Contents loc::TL.Text)
206 l10n_Quote msg _loc = do
207 depth <- R.asks reader_quote
209 case unNat depth `mod` 3 of
213 m <- R.local (\s -> s{reader_quote=succNat depth}) msg
215 l10n_Date date loc = plainify (l10n_Date date loc::TL.Text)
218 -- ** Type 'L10nPlain'
219 -- | Useful newtype to just use 'plainify', instead of 'state_l10n'.
220 newtype L10nPlain = L10nPlain (forall l. L10n l Plain => FullLocale l -> Plain)
221 instance Plainify L10nPlain where
222 plainify (L10nPlain l10n) = do
223 State{state_l10n} <- S.get