1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ViewPatterns #-}
4 module Hdoc.TCT.Write.HTML5 where
6 import Control.Monad (Monad(..), forM_, mapM_)
8 import Data.Char (Char)
9 import Data.Default.Class (Default(..))
10 import Data.Eq (Eq(..))
11 import Data.Foldable (Foldable(..))
12 import Data.Function (($), (.))
13 import Data.Functor.Compose (Compose(..))
14 import Data.List.NonEmpty (NonEmpty(..))
15 import Data.Maybe (Maybe(..))
16 import Data.Monoid (Monoid(..))
17 import Data.Ord (Ord(..), Ordering(..))
18 import Data.Semigroup (Semigroup(..))
19 import Data.Sequence (ViewL(..))
20 import Data.String (String, IsString(..))
21 import Prelude (Num(..), undefined, error)
22 import Text.Blaze ((!))
23 import Text.Blaze.Html (Html)
24 import Text.Show (Show(..))
25 import qualified Control.Monad.Trans.State as S
26 import qualified Data.List as List
27 import qualified Data.Sequence as Seq
28 import qualified Data.Text.Lazy as TL
29 import qualified Text.Blaze.Html5 as H
30 import qualified Text.Blaze.Html5.Attributes as HA
31 import qualified Text.Blaze.Internal as Blaze
33 -- import Hdoc.TCT.Debug
36 import Control.Monad.Utils
37 import Text.Blaze.Utils
38 import qualified Hdoc.TCT.Write.Plain as Plain
40 writeHTML5 :: Trees (Cell Node) -> Html
45 H.meta ! HA.httpEquiv "Content-Type"
46 ! HA.content "text/html; charset=UTF-8"
47 whenJust (titleFrom body) $ \t ->
49 H.toMarkup $ Plain.text def t
50 -- link ! rel "Chapter" ! title "SomeTitle">
51 H.link ! HA.rel "stylesheet"
53 ! HA.href "style/tct-html5.css"
54 let (html5Body, State{}) =
58 H.a ! HA.id "line-1" $ return ()
61 titleFrom :: Roots -> Maybe Root
64 Tree (unCell -> NodeHeader HeaderSection{}) _ts -> True
67 Tree (unCell -> NodeHeader (HeaderSection _lvl))
68 (Seq.viewl -> title:<_) -> Just title
72 type Html5 = ComposeState State Blaze.MarkupM ()
74 instance IsString Html5 where
75 fromString = mapM_ html5ify
77 html5 :: H.ToMarkup a => a -> Html5
78 html5 = Compose . return . H.toMarkup
84 , state_indent :: Html5
85 , state_italic :: Bool
86 , state_ext_html :: String
87 } -- deriving (Eq, Show)
88 instance Default State where
92 , state_italic = False
93 , state_ext_html = ".html"
95 -- instance Pretty State
98 class Html5ify a where
99 html5ify :: a -> Html5
100 instance Html5ify () where
102 instance Html5ify Char where
105 s@State{state_pos=Pos line _col, ..} <- liftComposeState S.get
106 liftComposeState $ S.put s{state_pos=Pos (line + 1) 1}
108 H.a ! HA.id ("line-"<>attrify (line + 1)) $$ return ()
111 liftComposeState $ S.modify' $ \s@State{state_pos=Pos line col} ->
112 s{state_pos=Pos line (col + 1)}
114 instance Html5ify String where
115 html5ify = mapM_ html5ify
116 instance Html5ify TL.Text where
120 let (h,ts) = TL.span (/='\n') t in
123 liftComposeState $ S.modify' $ \s@State{state_pos=Pos line col} ->
124 s{state_pos=Pos line $ col + int (TL.length h)}
128 -- NOTE: useless to increment the pos_column for h,
129 -- since the following '\n' will reset the pos_column.
132 instance Html5ify Pos where
133 html5ify new@(Pos lineNew colNew) = do
135 { state_pos=old@(Pos lineOld colOld)
137 } <- liftComposeState S.get
138 case lineOld`compare`lineNew of
140 forM_ [lineOld+1..lineNew] $ \lnum -> do
142 H.a ! HA.id ("line-"<>attrify lnum) $$ return ()
143 liftComposeState $ S.put s{state_pos=Pos lineNew 1}
145 Pos _lineMid colMid <- liftComposeState $ S.gets state_pos
146 html5 $ List.replicate (colNew - colMid) ' '
147 liftComposeState $ S.put s{state_pos=new}
148 EQ | colOld <= colNew -> do
149 liftComposeState $ S.put s{state_pos=new}
150 html5 $ List.replicate (colNew - colOld) ' '
151 _ -> error $ "html5ify: non-ascending Pos:"
152 <> "\n old: " <> show old
153 <> "\n new: " <> show new
154 instance Html5ify Roots where
155 html5ify = mapM_ html5ify
156 instance Html5ify Root where
157 html5ify (Tree (Cell (Span{span_begin=bp}:|_) nod) ts) = do
160 ----------------------
161 NodeLower name attrs -> do
162 H.span ! HA.class_ (mconcat ["header header-lower"," header-name-",attrify name]) $$ do
163 H.span ! HA.class_ "header-mark" $$ html5ify '<'
164 H.span ! HA.class_ "header-name" $$ html5ify name
167 ----------------------
170 HeaderGreat n wh -> html5HeaderRepeated "" "" n wh ">" "" "great"
171 HeaderBar n wh -> html5HeaderRepeated "" "" n wh "|" "" "bar"
172 HeaderColon n wh -> html5Header "" "" n wh ":" "" "colon"
173 HeaderEqual n wh -> html5Header "" "" n wh "=" "" "equal"
174 HeaderDot n -> html5Header "" "" n "" "." "" "dot"
175 HeaderDash -> html5Header "" "" "" "" "-" " " "dash"
176 HeaderDashDash -> html5Header "" "" "" "" "--" " " "dashdash"
177 HeaderBrackets n -> html5Header "[" "" n "" "]" "" "dashdash"
178 HeaderSection lvl -> do
180 H.span ! HA.class_ "section-title" $$ do
181 H.span ! HA.class_ "section-mark" $$ do
182 html5ify $ List.replicate lvl '#'
184 title :< _ -> h lvl $$ html5ify title
197 h n | n > 6 = H.span ! HA.class_ ("h h"<>attrify n)
199 HeaderDotSlash file -> do
200 ext <- liftComposeState $ S.gets state_ext_html
204 H.a ! HA.class_ "header-dotslash"
205 ! HA.href (attrify $ file<>ext) $$
208 html5Head :: Name -> White -> Name -> White -> TL.Text -> White -> H.AttributeValue -> Html5
209 html5Head markBegin whmb name whn markEnd whme cl = do
210 H.span ! HA.class_ (mconcat $ ["header header-",cl] <>
211 if TL.null name then [] else [" header-name-",attrify name]) $$ do
212 when (markBegin/="") $
213 H.span ! HA.class_ "header-mark" $$ html5ify markBegin
216 H.span ! HA.class_ "header-name" $$ html5ify name
219 H.span ! HA.class_ "header-mark" $$ html5ify markEnd
221 html5Header markBegin whmb name whn markEnd whme cl = do
222 html5Head markBegin whmb name whn markEnd whme cl
223 H.span ! HA.class_ "header-value" $$
225 html5HeaderRepeated :: Name -> White -> Name -> White -> TL.Text -> White -> H.AttributeValue -> Html5
226 html5HeaderRepeated markBegin whmb name whn markEnd whme cl = do
227 State{state_indent} <- liftComposeState S.get
228 liftComposeState $ S.modify' $ \s ->
231 Pos _lineMid colMid <- liftComposeState $ S.gets state_pos
232 html5ify $ List.replicate (pos_column bp - colMid) ' '
233 html5Head markBegin whmb name whn markEnd whme cl
235 r <- html5Header markBegin whmb name whn markEnd whme cl
236 liftComposeState $ S.modify' $ \s -> s{state_indent}
238 ----------------------
240 State{state_indent} <- liftComposeState S.get
241 liftComposeState $ S.modify' $ \s ->
244 Pos _lineMid colMid <- liftComposeState $ S.gets state_pos
245 html5ify $ List.replicate (pos_column bp - colMid) ' '
248 liftComposeState $ S.modify' $ \s -> s{state_indent}
250 ----------------------
252 State{state_indent} <- liftComposeState S.get
253 liftComposeState $ S.modify' $ \s ->
256 Pos _lineMid colMid <- liftComposeState $ S.gets state_pos
257 html5ify $ List.replicate (pos_column bp - colMid) ' '
260 liftComposeState $ S.modify' $ \s -> s{state_indent}
262 ----------------------
263 NodeToken t -> html5ify t <> html5ify ts
264 ----------------------
267 PairElem name attrs -> do
268 H.span ! HA.class_ ("pair-PairElem" <> " pair-elem-"<>attrify name) $$ do
269 H.span ! HA.class_ "pair-open" $$ o
270 unless (null ts) $ do
271 H.span ! HA.class_ "pair-content" $$ html5ify ts
272 H.span ! HA.class_ "pair-close" $$ c
275 H.span ! HA.class_ "elem-name" $$
280 ( "<"<>html5Name<>html5ify attrs<>"/>"
283 ( "<"<>html5Name<>html5ify attrs<>">"
284 , "</"<>html5Name<>">" )
286 H.span ! HA.class_ ("pair-"<>fromString (show pair)) $$ do
287 let (o,c) = pairBorders pair ts
288 H.span ! HA.class_ "pair-open" $$ html5ify o
289 H.span ! HA.class_ "pair-content" $$ em $ html5ify ts
290 H.span ! HA.class_ "pair-close" $$ html5ify c
296 || p == PairFrenchquote
297 || p == PairDoublequote -> do
298 State{..} <- liftComposeState $ S.get
299 liftComposeState $ S.modify' $ \s -> s{state_italic = not state_italic}
300 r <- H.em ! HA.class_ (if state_italic then "even" else "odd") $$ h
301 liftComposeState $ S.modify' $ \s -> s{state_italic}
304 instance Html5ify Token where
307 TokenText t -> html5ify t
309 H.span ! HA.class_ "tag" $$ do
310 H.span ! HA.class_ "tag-open" $$
313 TokenEscape c -> html5ify ['\\', c]
315 H.a ! HA.href (attrify l) $$
317 instance Html5ify ElemAttrs where
318 html5ify = mapM_ html5ify
319 instance Html5ify (White,ElemAttr) where
320 html5ify (elemAttr_white,ElemAttr{..}) = do
321 html5ify elemAttr_white
322 H.span ! HA.class_ "attr-name" $$
323 html5ify elemAttr_name
324 html5ify elemAttr_open
325 H.span ! HA.class_ "attr-value" $$
326 html5ify elemAttr_value
327 html5ify elemAttr_close