1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE RecordWildCards #-}
3 {-# LANGUAGE ViewPatterns #-}
4 {-# OPTIONS_GHC -fno-warn-orphans #-}
5 -- | Render a TCT file in DTC.
6 module Language.TCT.DTC where
8 import Control.Applicative (Applicative(..))
9 import Control.Monad (Monad(..), forM_, mapM)
11 import Data.Eq (Eq(..))
12 import Data.Foldable (Foldable(..))
13 import Data.Function (($), (.), flip)
14 import Data.Functor ((<$>))
16 import Data.Maybe (Maybe(..))
17 import Data.Monoid (Monoid(..))
18 import Data.Ord (Ord(..))
19 import Data.Semigroup (Semigroup(..))
20 import Data.Sequence (ViewL(..))
21 import Data.String (String, IsString(..))
22 import Data.Text (Text)
23 import Prelude (Num(..), undefined)
24 import Text.Blaze ((!))
25 import Text.Blaze.Html (Html)
26 import Text.Show (Show(..))
27 import qualified Control.Monad.Trans.State as S
28 import qualified Data.List as L
29 import qualified Data.Sequence as Seq
30 import qualified Data.Text as Text
31 import qualified Text.Blaze as B
32 import qualified Text.Blaze.Internal as B
33 import qualified Data.Text.Lazy as TL
35 import Language.TCT.Tree
36 import Language.TCT.Token
37 import Language.TCT.Elem hiding (trac,dbg)
38 import qualified Language.TCT.Text as Write
39 import Text.Blaze.Utils
40 import Text.Blaze.DTC (DTC)
41 import qualified Text.Blaze.DTC as D
42 import qualified Text.Blaze.DTC.Attributes as DA
44 import Debug.Trace (trace)
45 trac :: String -> a -> a
48 dbg :: Show a => String -> a -> a
49 dbg m x = trac (m <> ": " <> show x) x
51 dtc :: Trees (Cell Key) (Cell Token) -> DTC
53 D.xmlModel "./schema/dtc.rnc"
54 D.xmlStylesheet "./xsl/document.html5.xsl"
55 D.html5Stylesheet "./xsl/document.html5.xsl"
56 D.atomStylesheet "./xsl/document.atom.xsl"
58 forM_ tct $ d_TreeCell []
60 d_TreeCell :: [Key] -> Tree (Cell Key) (Cell Token) -> DTC
61 d_TreeCell path (TreeN (unCell -> key@KeySection{}) ts) =
63 Tree0 (Cell _posTitle _ (TokenPlain title)) :< _ ->
64 D.section ! DA.name (attrValue title) $
66 Tree0 (Cell _posTitle _ title) :< _ ->
68 D.name $ d_Token (key:path) title
70 _ -> D.section d_content
72 d_content = forM_ (case Seq.viewl ts of {Tree0{} :< ts' -> ts'; _ -> ts}) $ d_TreeCell (key:path)
73 d_TreeCell path (Tree0 cell) = d_CellToken path cell
74 d_TreeCell path (TreeN cell@(unCell -> KeyColon{}) ts) =
75 foldr (\(n,v) -> B.AddCustomAttribute (B.Text n) (B.Text v))
76 (d_CellKey path cell children)
79 attrs :: [(Name,Text)]
84 TreeN (unCell -> KeyEqual n _wh) v ->
85 (n,TL.toStrict $ Write.text $ Write.treeRackUpLeft <$> v):acc
88 children = Seq.filter (\t ->
91 TreeN (unCell -> KeyEqual{}) _cs -> False
94 d_TreeCell path (TreeN cell ts) = d_CellKey path cell ts
96 d_CellKey :: [Key] -> Cell Key -> Trees (Cell Key) (Cell Token) -> DTC
97 d_CellKey path (Cell _pos _posEnd key) cells = do
99 KeyColon n _wh -> d_Key n
100 KeyGreat n _wh -> d_Key n
101 KeyEqual n _wh -> d_Key n
102 KeyBar n _wh -> d_Key n
104 B.toMarkup ("- "::Text)
105 forM_ cells $ d_TreeCell (key:path)
107 KeyLower name attrs -> do
108 B.Content $ "<"<>B.toMarkup name
110 forM_ cells $ d_TreeCell path
115 B.CustomParent (B.Text name) $
116 forM_ cells $ d_TreeCell (key:path)
118 d_CellToken :: [Key] -> Cell Token -> DTC
119 d_CellToken path (Cell _pos _posEnd tok) =
120 -- case L.find (\case KeySection{} -> True; _ -> False) $ dbg "d_CellToken: path:" path of
121 case dbg "d_CellToken: path" path of
124 TokenPair PairElem{} _t -> d_Token path tok
125 _ -> D.para $ d_Token path tok
126 _ -> d_Token path tok
128 d_Token :: [Key] -> Token -> DTC
129 d_Token path tok = go tok
131 -- indent = Text.replicate (columnPos pos - 1) " "
133 go (TokenPlain txt) = B.toMarkup txt
134 go (TokenTag v) = D.ref mempty ! DA.to (attrValue v)
135 go (TokenEscape c) = B.toMarkup c
136 go (TokenLink lnk) = D.eref (B.toMarkup lnk) ! DA.to (attrValue lnk)
137 go (TokenPair PairSlash t) = D.i $ go t
138 go (TokenPair PairBackquote t) = D.code $ go t
139 go (TokenPair PairFrenchquote t) = D.q $ go t
140 go (TokenPair PairHash (TokenPlain t)) =
141 D.ref mempty ! DA.to (attrValue t)
142 go (TokenPair (PairElem name attrs) t) =
145 Tokens ts | Seq.null ts ->
146 B.CustomLeaf (B.Text name) True mempty
147 _ -> B.CustomParent (B.Text name) $ go t
148 go (TokenPair p t) = do
149 let (o,c) = pairBorders p t
153 go (Tokens ts) = foldMap go ts
155 d_Attrs :: Attrs -> DTC -> DTC
156 d_Attrs = flip $ foldl' d_Attr
158 d_Attr :: DTC -> (Text,Attr) -> DTC
159 d_Attr acc (_,Attr{..}) =