1 {-# LANGUAGE DuplicateRecordFields #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# OPTIONS_GHC -fno-warn-orphans #-}
8 module Language.DTC.Write.Plain where
10 import Control.Applicative (Applicative(..), liftA2)
11 import Control.Category
14 import Data.Default.Class (Default(..))
15 import Data.Eq (Eq(..))
16 import Data.Foldable (Foldable(..), concat)
17 import Data.Function (($))
19 import Data.Maybe (Maybe(..), maybe)
20 import Data.Monoid (Monoid(..))
21 import Data.Semigroup (Semigroup(..))
22 import Data.Text (Text)
23 import Data.TreeSeq.Strict (Tree(..))
24 import Data.Tuple (fst, snd)
25 import Data.String (String, IsString(..))
27 import Text.Show (Show(..))
28 import qualified Control.Monad.Trans.State as S
29 import qualified Data.List as List
30 import qualified Data.Text.Lazy as TL
31 import qualified Data.Text.Lazy.Builder as TLB
33 import Data.Locale hiding (Index)
35 import Language.DTC.Write.XML ()
36 import qualified Language.DTC.Document as DTC
39 type Plain = S.State State TLB.Builder
41 runPlain :: Plain -> State -> (TL.Text, State)
43 let (b,s') = S.runState p s in
44 (TLB.toLazyText b, s')
46 text :: Plainify a => State -> a -> TL.Text
47 text st a = fst $ runPlain (plainify a) st
49 instance IsString Plain where
50 fromString = return . fromString
51 instance Semigroup Plain where
53 instance Monoid Plain where
60 { state_localize :: L10n -> Plain
61 , state_italic :: Bool
62 , state_quote :: DTC.Nat
64 instance Default State where
66 { state_localize = plainify . show
67 , state_italic = False
68 , state_quote = DTC.Nat 0
73 class Plainify a where
74 plainify :: a -> Plain
75 instance Plainify String where
76 plainify = return . TLB.fromString
77 instance Plainify Text where
78 plainify = return . TLB.fromText
79 instance Plainify TL.Text where
80 plainify = return . TLB.fromLazyText
81 instance Plainify DTC.Para where
82 plainify = foldMap plainify
83 instance Plainify DTC.Lines where
84 plainify (Tree n ls) =
87 DTC.Plain p -> plainify p
88 DTC.B -> "*"<>plainify ls<>"*"
89 DTC.Code -> "`"<>plainify ls<>"`"
90 DTC.Del -> "-"<>plainify ls<>"-"
91 DTC.I -> "/"<>plainify ls<>"/"
94 let depth = DTC.Nat 0 in
95 plainify L10n_QuoteOpen{..} <>
97 plainify L10n_QuoteClose{..}
99 DTC.Sub -> plainify ls
100 DTC.Sup -> plainify ls
101 DTC.U -> "_"<>plainify ls<>"_"
102 DTC.Eref{..} -> plainify ls
103 DTC.Iref{..} -> plainify ls
104 DTC.Ref{..} -> plainify ls
105 DTC.Rref{..} -> plainify ls
106 instance Plainify DTC.Title where
107 plainify (DTC.Title t) = plainify t
108 instance Plainify DTC.PosPath where
111 snd . foldl' (\(nParent,acc) (n,c) ->
113 (if TL.null acc then acc else acc <> ".") <>
115 then TL.pack (show c)
116 else TL.pack (show n)<>TL.pack (show c))
120 instance Plainify DTC.XmlName where
121 plainify = plainify . show
122 instance Plainify Int where
123 plainify = plainify . show
124 instance Plainify DTC.Nat where
125 plainify (DTC.Nat n) = plainify n
126 instance Plainify DTC.Nat1 where
127 plainify (DTC.Nat1 n) = plainify n
131 = L10n_Table_of_Contents
133 | L10n_QuoteOpen {depth :: DTC.Nat}
134 | L10n_QuoteClose {depth :: DTC.Nat}
137 instance Plainify L10n where
139 loc <- S.gets state_localize
141 instance LocalizeIn FR Plain L10n where
143 L10n_Table_of_Contents -> "Sommaire"
145 L10n_QuoteOpen{..} ->
146 case DTC.unNat depth `mod` 3 of
150 L10n_QuoteClose{..} ->
151 case DTC.unNat depth `mod` 3 of
155 L10n_Date DTC.Date{..} ->
157 List.intersperse " " $
159 [ maybe [] (pure . plainify) day
172 9 -> pure "septembre"
174 11 -> pure "novembre"
175 12 -> pure "décembre"
179 instance LocalizeIn EN Plain L10n where
181 L10n_Table_of_Contents -> "Summary"
183 L10n_QuoteOpen{..} ->
184 case DTC.unNat depth `mod` 3 of
188 L10n_QuoteClose{..} ->
189 case DTC.unNat depth `mod` 3 of
193 L10n_Date DTC.Date{..} ->
195 List.intersperse " " $
197 [ maybe [] (pure . plainify) day
210 9 -> pure "September"
212 11 -> pure "November"
213 12 -> pure "December"