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 Language.DTC.Document as DTC hiding (Plain)
37 import qualified Language.DTC.Document as DTC
40 type Plain = S.State State TLB.Builder
42 runPlain :: Plain -> State -> (TL.Text, State)
44 let (b,s') = S.runState p s in
45 (TLB.toLazyText b, s')
47 text :: Plainify a => State -> a -> TL.Text
48 text st a = fst $ runPlain (plainify a) st
50 instance IsString Plain where
51 fromString = return . fromString
52 instance Semigroup Plain where
54 instance Monoid Plain where
61 { state_localize :: L10n -> Plain
62 , state_italic :: Bool
65 instance Default State where
67 { state_localize = plainify . show
68 , state_italic = False
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
82 instance Plainify Para where
84 ParaItem{..} -> plainify item
85 ParaItems{..} -> plainify items
87 instance Plainify DTC.Plain where
88 plainify = foldMap plainify
89 instance Plainify (Tree PlainNode) where
90 plainify (Tree n ls) =
93 PlainText txt -> plainify txt
94 PlainGroup -> plainify ls
95 PlainB -> "*"<>plainify ls<>"*"
96 PlainCode -> "`"<>plainify ls<>"`"
97 PlainDel -> "-"<>plainify ls<>"-"
98 PlainI -> "/"<>plainify ls<>"/"
99 PlainNote{..} -> "" -- TODO: to be coded, with a switch on/off in State
102 plainify L10n_QuoteOpen{..} <>
104 plainify L10n_QuoteClose{..}
105 PlainSC -> plainify ls
106 PlainSub -> plainify ls
107 PlainSup -> plainify ls
108 PlainU -> "_"<>plainify ls<>"_"
109 PlainEref{..} -> plainify ls
110 PlainIref{..} -> plainify ls
111 PlainRef{..} -> plainify ls
112 PlainRref{..} -> plainify ls
113 instance Plainify Title where
114 plainify (Title t) = plainify t
115 instance Plainify PosPath where
118 snd . foldl' (\(nParent,acc) (n,c) ->
120 (if TL.null acc then acc else acc <> ".") <>
122 then TL.pack (show c)
123 else TL.pack (show n)<>TL.pack (show c))
127 instance Plainify XmlName where
128 plainify = plainify . show
129 instance Plainify Int where
130 plainify = plainify . show
131 instance Plainify Nat where
132 plainify (Nat n) = plainify n
133 instance Plainify Nat1 where
134 plainify (Nat1 n) = plainify n
138 = L10n_Table_of_Contents
140 | L10n_QuoteOpen {depth :: Nat}
141 | L10n_QuoteClose {depth :: Nat}
144 instance Plainify L10n where
146 loc <- S.gets state_localize
148 instance LocalizeIn FR Plain L10n where
150 L10n_Table_of_Contents -> "Sommaire"
152 L10n_QuoteOpen{..} ->
153 case unNat depth `mod` 3 of
157 L10n_QuoteClose{..} ->
158 case unNat depth `mod` 3 of
162 L10n_Date Date{..} ->
164 List.intersperse " " $
166 [ maybe [] (pure . plainify) day
179 9 -> pure "septembre"
181 11 -> pure "novembre"
182 12 -> pure "décembre"
186 instance LocalizeIn EN Plain L10n where
188 L10n_Table_of_Contents -> "Summary"
190 L10n_QuoteOpen{..} ->
191 case unNat depth `mod` 3 of
195 L10n_QuoteClose{..} ->
196 case unNat depth `mod` 3 of
200 L10n_Date Date{..} ->
202 List.intersperse " " $
204 [ maybe [] (pure . plainify) day
217 9 -> pure "September"
219 11 -> pure "November"
220 12 -> pure "December"