1 {-# LANGUAGE DuplicateRecordFields #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE TypeApplications #-}
8 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 module Language.DTC.Write.HTML5 where
11 import Control.Applicative (Applicative(..))
12 import Control.Category
15 import Data.Char (Char)
16 import Data.Default.Class (Default(..))
17 import Data.Foldable (Foldable(..), concat)
18 import Data.Function (($), const, flip, on)
19 import Data.Functor (Functor(..), (<$>))
20 import Data.Functor.Compose (Compose(..))
22 import Data.Map.Strict (Map)
23 import Data.Maybe (Maybe(..), maybe, mapMaybe, fromJust, maybeToList)
24 import Data.Monoid (Monoid(..))
25 import Data.Ord (Ord(..))
26 import Data.Semigroup (Semigroup(..))
27 import Data.String (String, IsString(..))
28 import Data.Text (Text)
29 import Data.TreeSeq.Strict (Tree(..), tree0)
30 import Data.Tuple (snd)
31 import System.FilePath (FilePath)
32 import Text.Blaze ((!))
33 import Text.Blaze.Html (Html)
34 import Text.Show (Show(..))
35 import qualified Control.Monad.Trans.State as S
36 import qualified Data.Char as Char
37 import qualified Data.List as List
38 import qualified Data.Map.Strict as Map
39 import qualified Data.Sequence as Seq
40 import qualified Data.Strict.Maybe as Strict
41 import qualified Data.Text as Text
42 import qualified Data.Text.Lazy as TL
43 import qualified Data.TreeMap.Strict as TreeMap
44 import qualified Data.TreeSeq.Strict.Zipper as Tree
45 import qualified Text.Blaze.Html5 as H
46 import qualified Text.Blaze.Html5.Attributes as HA
47 import qualified Text.Blaze.Internal as H
49 import Text.Blaze.Utils
50 import Data.Locale hiding (Index)
51 import qualified Data.Locale as Locale
53 import Language.DTC.Write.XML ()
54 import Language.DTC.Write.Plain (Plain, Plainify(..))
55 import qualified Language.DTC.Write.Plain as Plain
56 import qualified Language.DTC.Document as DTC
57 import qualified Language.DTC.Anchor as Anchor
59 (<&>) :: Functor f => f a -> (a -> b) -> f b
64 type Html5 = StateMarkup State ()
65 instance IsString Html5 where
71 { state_styles :: Map FilePath CSS
72 , state_scripts :: Map FilePath Script
73 , state_indexs :: Map DTC.Pos (DTC.Terms, Anchor.Irefs)
74 , state_rrefs :: Anchor.Rrefs
75 , state_figures :: Map TL.Text (Map DTC.Pos (Maybe DTC.Title))
76 , state_references :: Map DTC.Ident DTC.About
77 , state_notes :: Anchor.Notes
78 , state_plainify :: Plain.State
80 instance Default State where
82 { state_styles = mempty
83 , state_scripts = mempty
84 , state_indexs = mempty
85 , state_rrefs = mempty
86 , state_figures = mempty
87 , state_references = mempty
88 , state_notes = mempty
89 , state_plainify = def
97 { keys_index :: Map DTC.Pos DTC.Terms
98 , keys_figure :: Map TL.Text (Map DTC.Pos (Maybe DTC.Title))
99 , keys_reference :: Map DTC.Ident DTC.About
101 instance Default Keys where
102 def = Keys mempty mempty mempty
106 keys :: a -> S.State Keys ()
107 instance KeysOf DTC.Body where
109 instance KeysOf (Tree DTC.BodyNode) where
115 S.modify $ \s -> s{keys_index=
116 Map.insert pos terms $ keys_index s}
118 S.modify $ \s -> s{keys_figure=
120 type_ (Map.singleton pos mayTitle) $
122 DTC.References{..} ->
123 S.modify $ \s -> s{keys_reference=
126 (DTC.id (r::DTC.Reference))
127 (DTC.about (r::DTC.Reference)))
130 DTC.ToC{} -> return ()
131 DTC.ToF{} -> return ()
132 DTC.Block{} -> return ()
134 -- * Class 'Html5ify'
135 class Html5ify a where
136 html5ify :: a -> Html5
137 instance Html5ify H.Markup where
138 html5ify = Compose . return
139 instance Html5ify Char where
140 html5ify = html5ify . H.toMarkup
141 instance Html5ify Text where
142 html5ify = html5ify . H.toMarkup
143 instance Html5ify TL.Text where
144 html5ify = html5ify . H.toMarkup
145 instance Html5ify String where
146 html5ify = html5ify . H.toMarkup
147 instance Html5ify DTC.Title where
148 html5ify (DTC.Title t) = html5ify t
149 instance Html5ify DTC.Para where
150 html5ify = mapM_ html5ify
151 instance Html5ify DTC.Ident where
152 html5ify (DTC.Ident i) = html5ify i
153 instance Html5ify Int where
154 html5ify = html5ify . show
155 instance Html5ify DTC.Nat where
156 html5ify (DTC.Nat n) = html5ify n
157 instance Html5ify DTC.Nat1 where
158 html5ify (DTC.Nat1 n) = html5ify n
161 Localize ls Plain Plain.L10n =>
163 LocaleIn ls -> DTC.Document -> Html
164 document locale DTC.Document{..} = do
165 let Keys{..} = keys body `S.execState` def
166 let (body',state_rrefs,state_notes,state_indexs) =
167 let irefs = foldMap Anchor.irefsOfTerms keys_index in
168 let (body0, Anchor.State{state_irefs, state_rrefs=rrefs, state_notes=notes}) =
169 Anchor.anchorify body `S.runState`
170 def{Anchor.state_irefs=irefs} in
171 (body0,rrefs,notes,) $
172 (<$> keys_index) $ \terms ->
174 TreeMap.intersection const state_irefs $
175 Anchor.irefsOfTerms terms
176 let state_plainify = def
177 { Plain.state_localize = Locale.localize locale }
178 let (html5Body, State{state_styles,state_scripts}) =
183 , state_figures = keys_figure
184 , state_references = keys_reference
189 H.html ! HA.lang (attrify $ countryCode locale) $ do
191 H.meta ! HA.httpEquiv "Content-Type"
192 ! HA.content "text/html; charset=UTF-8"
193 whenSome (DTC.titles $ DTC.about (head :: DTC.Head)) $ \ts ->
195 H.toMarkup $ Plain.text state_plainify $ List.head ts
196 forM_ (DTC.links $ DTC.about (head :: DTC.Head)) $ \DTC.Link{rel, href} ->
197 H.link ! HA.rel (attrify rel)
198 ! HA.href (attrify href)
199 H.meta ! HA.name "generator"
200 ! HA.content "https://hackage.haskell.org/package/hdoc"
202 (`mapMaybe` toList body) $ \case
203 Tree k@DTC.Section{} _ -> Just k
205 forM_ chapters $ \case
207 H.link ! HA.rel "Chapter"
208 ! HA.title (attrify $ plainify title)
209 ! HA.href ("#"<>attrify pos)
211 H.link ! HA.rel "stylesheet"
212 ! HA.type_ "text/css"
213 ! HA.href "style/dtc-html5.css"
214 forM_ state_styles $ \style ->
215 H.style ! HA.type_ "text/css" $
217 forM_ state_scripts $ \script ->
218 H.script ! HA.type_ "application/javascript" $
223 -- * Type 'BodyCursor'
224 -- | Cursor to navigate within a 'DTC.Body' according to many axis (like in XSLT).
225 type BodyCursor = Tree.Zipper DTC.BodyNode
226 instance Html5ify DTC.Body where
228 forM_ (Tree.zippers body) $ \z ->
229 forM_ (Tree.axis_repeat Tree.axis_following_sibling_nearest `Tree.runAxis` z) $
231 instance Html5ify BodyCursor
233 case Tree.current z of
237 H.section ! HA.class_ "section"
238 ! HA.id (attrify pos) $$ do
239 forM_ aliases html5ify
240 html5CommonAttrs attrs $
241 H.table ! HA.class_ "section-header" $$
244 H.td ! HA.class_ "section-number" $$ do
245 html5SectionNumber $ DTC.posAncestors pos
246 H.td ! HA.class_ "section-title" $$ do
247 (case List.length $ DTC.posAncestors pos of
256 forM_ (Tree.axis_child `Tree.runAxis` z) $
258 notes <- liftStateMarkup $ S.gets state_notes
259 case Map.lookup pos notes of
262 H.aside ! HA.class_ "notes" $$ do
266 forM_ ns $ \(num,para) ->
268 H.td ! HA.class_ "note-ref" $$ do
269 H.a ! HA.class_ "note-number"
270 ! HA.id ("note."<>attrify num)
271 ! HA.href ("#note."<>attrify num) $$ do
274 H.a ! HA.href ("#note-ref."<>attrify num) $$ do
278 DTC.Block b -> html5ify b
280 H.nav ! HA.class_ "toc"
281 ! HA.id (attrify pos) $$ do
282 H.span ! HA.class_ "toc-name" $$
283 H.a ! HA.href (attrify pos) $$
284 html5ify Plain.L10n_Table_of_Contents
286 forM_ (Tree.axis_following_sibling `Tree.runAxis` z) $
289 H.nav ! HA.class_ "tof"
290 ! HA.id (attrify pos) $$
291 H.table ! HA.class_ "tof" $$
295 html5CommonAttrs attrs $
296 H.div ! HA.class_ ("figure " <> attrify ("figure-"<>type_))
297 ! HA.id (attrify pos) $$ do
298 H.table ! HA.class_ "figure-caption" $$
302 then H.a ! HA.href ("#"<>attrify pos) $$ mempty
304 H.td ! HA.class_ "figure-number" $$ do
305 H.a ! HA.href ("#"<>attrify pos) $$ do
307 html5ify $ DTC.posAncestors pos
308 forM_ mayTitle $ \title ->
309 H.td ! HA.class_ "figure-title" $$ do
310 unless (TL.null type_) $
311 html5ify $ Plain.L10n_Colon
313 H.div ! HA.class_ "figure-content" $$ do
316 (allTerms,refsByTerm) <- liftStateMarkup $ S.gets $ (Map.! pos) . state_indexs
317 let chars = Anchor.termsByChar allTerms
318 H.div ! HA.class_ "index"
319 ! HA.id (attrify pos) $$ do
320 H.nav ! HA.class_ "index-nav" $$ do
321 forM_ (Map.keys chars) $ \char ->
322 H.a ! HA.href ("#"<>(attrify pos <> "." <> attrify char)) $$
324 H.dl ! HA.class_ "index-chars" $$
325 forM_ (Map.toList chars) $ \(char,terms) -> do
327 let i = attrify pos <> "." <> attrify char in
329 ! HA.href ("#"<>i) $$
332 H.dl ! HA.class_ "index-term" $$ do
333 forM_ terms $ \aliases -> do
335 H.ul ! HA.class_ "index-aliases" $$
336 forM_ (List.take 1 aliases) $ \term ->
337 H.li ! HA.id (attrifyIref term) $$
341 List.sortBy (compare `on` DTC.section . snd) $
342 (`foldMap` aliases) $ \words ->
344 path <- Anchor.pathFromWords words
345 Strict.maybe Nothing (Just . ((words,) <$>) . List.reverse) $
346 TreeMap.lookup path refsByTerm in
348 (<$> anchs) $ \(term,DTC.Anchor{..}) ->
349 H.a ! HA.class_ "index-iref"
350 ! HA.href ("#"<>attrifyIrefCount term count) $$
351 html5ify $ DTC.posAncestors section
352 DTC.References{..} ->
353 html5CommonAttrs attrs $
354 H.div ! HA.class_ "references"
355 ! HA.id (attrify pos) $$ do
358 instance Html5ify DTC.Words where
359 html5ify = html5ify . Anchor.plainifyWords
360 instance Html5ify DTC.Alias where
361 html5ify DTC.Alias{id=id_, ..} = do
362 H.a ! HA.class_ "alias"
363 ! HA.id (attrify id_) $$
366 cleanPara :: DTC.Para -> DTC.Para
369 Tree DTC.Iref{} ls -> cleanPara ls
370 Tree DTC.Note{} _ -> mempty
371 Tree n ts -> pure $ Tree n $ cleanPara ts
373 html5ifyToC :: Maybe DTC.Nat -> BodyCursor -> Html5
374 html5ifyToC depth z =
375 case Tree.current z of
376 Tree DTC.Section{..} _ts -> do
378 H.table ! HA.class_ "toc-entry" $$
381 H.td ! HA.class_ "section-number" $$
382 html5SectionRef $ DTC.posAncestors pos
383 H.td ! HA.class_ "section-title" $$
384 html5ify $ cleanPara $ DTC.unTitle title
385 when (maybe True (> DTC.Nat 1) depth && not (null sections)) $
388 html5ifyToC (depth >>= DTC.predNat)
394 `Tree.axis_filter_current` \case
395 Tree DTC.Section{} _ -> True
398 html5ifyToF :: [TL.Text] -> Html5
399 html5ifyToF types = do
400 figsByType <- liftStateMarkup $ S.gets state_figures
402 Map.foldMapWithKey (\ty -> ((ty,) <$>)) $
406 Map.intersection figsByType $
407 Map.fromList [(ty,()) | ty <- types]
408 forM_ (Map.toList figs) $ \(pos, (type_, title)) ->
410 H.td ! HA.class_ "figure-number" $$
411 H.a ! HA.href ("#"<>attrify pos) $$ do
413 html5ify $ DTC.posAncestors pos
415 H.td ! HA.class_ "figure-title" $$
416 html5ify $ cleanPara $ DTC.unTitle ti
418 instance Html5ify [DTC.Block] where
419 html5ify = mapM_ html5ify
420 instance Html5ify DTC.Block where
423 html5CommonAttrs attrs $
424 H.p ! HA.class_ "para"
425 ! HA.id (attrify pos) $$ do
428 html5CommonAttrs attrs $
429 H.ol ! HA.class_ "ol"
430 ! HA.id (attrify pos) $$ do
431 forM_ items $ \item ->
432 H.li $$ html5ify item
434 html5CommonAttrs attrs $
435 H.ul ! HA.class_ "ul"
436 ! HA.id (attrify pos) $$ do
437 forM_ items $ \item ->
438 H.li $$ html5ify item
440 html5CommonAttrs attrs $
441 H.pre ! HA.class_ ("artwork " <> attrify ("artwork-"<>type_))
442 ! HA.id (attrify pos) $$ do
445 html5CommonAttrs attrs $
446 H.div ! HA.class_ ("quote " <> attrify ("quote-"<>type_))
447 ! HA.id (attrify pos) $$ do
450 html5ify $ H.Comment (H.String $ TL.unpack t) ()
451 instance Html5ify DTC.Lines where
452 html5ify (Tree n ls) =
454 DTC.BR -> html5ify H.br
455 DTC.Plain t -> html5ify t
456 DTC.B -> H.strong $$ html5ify ls
457 DTC.Code -> H.code $$ html5ify ls
458 DTC.Del -> H.del $$ html5ify ls
460 i <- liftStateMarkup $ do
461 i <- S.gets $ Plain.state_italic . state_plainify
464 (state_plainify s){Plain.state_italic=
467 H.em ! HA.class_ (if i then "even" else "odd") $$
472 (state_plainify s){Plain.state_italic=i}}
473 DTC.Sub -> H.sub $$ html5ify ls
474 DTC.Sup -> H.sup $$ html5ify ls
475 DTC.SC -> H.span ! HA.class_ "smallcaps" $$ html5ify ls
476 DTC.U -> H.span ! HA.class_ "underline" $$ html5ify ls
481 H.sup ! HA.class_ "note-number" $$
482 H.a ! HA.class_ "note-ref"
483 ! HA.id ("note-ref."<>attrify num)
484 ! HA.href ("#note."<>attrify num) $$
487 depth <- liftStateMarkup $ do
488 depth <- S.gets $ Plain.state_quote . state_plainify
489 S.modify $ \s -> s{state_plainify=
490 (state_plainify s){Plain.state_quote=
493 H.span ! HA.class_ "q" $$ do
494 html5ify $ Plain.L10n_QuoteOpen depth
495 html5ify $ Tree DTC.I ls
496 html5ify $ Plain.L10n_QuoteClose depth
500 (state_plainify s){Plain.state_quote = depth}}
502 H.a ! HA.class_ "eref"
503 ! HA.href (attrify href) $$
505 then html5ify $ DTC.unURL href
509 Nothing -> html5ify ls
510 Just DTC.Anchor{..} ->
511 H.span ! HA.class_ "iref"
512 ! HA.id (attrifyIrefCount term count) $$
515 H.a ! HA.class_ "ref"
516 ! HA.href ("#"<>attrify to) $$
521 refs <- liftStateMarkup $ S.gets state_references
522 case Map.lookup to refs of
525 H.span ! HA.class_ "rref-broken" $$
528 Just DTC.About{..} -> do
530 forM_ (List.take 1 titles) $ \(DTC.Title title) -> do
531 html5ify $ Tree DTC.Q $
534 Just u -> pure $ Tree (DTC.Eref u) title
537 H.a ! HA.class_ "rref"
538 ! HA.href ("#rref."<>attrify to)
539 ! HA.id ("rref."<>attrify to<>maybe "" (\DTC.Anchor{..} -> "."<>attrify count) anchor) $$
542 instance Html5ify DTC.URL where
543 html5ify (DTC.URL url) =
544 H.a ! HA.class_ "eref"
545 ! HA.href (attrify url) $$
547 instance Html5ify DTC.Date where
548 html5ify = html5ify . Plain.L10n_Date
549 instance Html5ify DTC.About where
550 html5ify DTC.About{..} =
551 html5CommasDot $ concat $
553 , html5Entity <$> authors
554 , html5ify <$> maybeToList date
555 , html5Entity <$> maybeToList editor
556 , html5Serie <$> series
559 html5Titles :: [DTC.Title] -> [Html5]
560 html5Titles ts | null ts = []
561 html5Titles ts = [html5Title $ fold $ List.intersperse t $ toList ts]
562 where t = DTC.Title $ Seq.singleton $ tree0 $ DTC.Plain " — "
563 html5Title (DTC.Title title) =
564 html5ify $ Tree DTC.Q $
567 Just u -> pure $ Tree (DTC.Eref u) title
568 html5SerieHref href DTC.Serie{..} = do
569 sp <- liftStateMarkup $ S.gets state_plainify
571 Tree DTC.Eref{href} $
573 [ tree0 $ DTC.Plain $ name
574 , tree0 $ DTC.Plain $ Plain.text sp Plain.L10n_Colon
575 , tree0 $ DTC.Plain key
577 html5Serie s@DTC.Serie{name="RFC", key} | TL.all Char.isDigit key =
578 html5SerieHref (DTC.URL $ "https://tools.ietf.org/html/rfc"<>key) s
579 html5Serie s@DTC.Serie{name="DOI", key} =
580 html5SerieHref (DTC.URL $ "https://dx.doi.org/"<>key) s
581 html5Serie DTC.Serie{..} = do
583 html5ify Plain.L10n_Colon
585 html5Entity DTC.Entity{url=mu, ..} = do
586 html5ify @DTC.Lines $
588 _ | not (TL.null email) ->
589 Tree (DTC.Eref $ DTC.URL $ "mailto:"<>email) $
590 pure $ tree0 $ DTC.Plain name
593 pure $ tree0 $ DTC.Plain name
594 _ -> tree0 $ DTC.Plain name
599 instance Html5ify DTC.Reference where
600 html5ify DTC.Reference{id=id_, ..} =
602 H.td ! HA.class_ "reference-key" $$
603 html5ify @DTC.Lines $ Tree DTC.Rref{anchor=Nothing, to=id_} Seq.empty
604 H.td ! HA.class_ "reference-content" $$ do
606 rrefs <- liftStateMarkup $ S.gets state_rrefs
607 case Map.lookup id_ rrefs of
610 H.span ! HA.class_ "reference-rrefs" $$
612 (<$> List.reverse anchs) $ \DTC.Anchor{..} ->
613 H.a ! HA.class_ "reference-rref"
614 ! HA.href ("#rref."<>attrify id_<>"."<>attrify count) $$
615 html5ify $ DTC.posAncestors section
616 instance Html5ify DTC.PosPath where
624 Text.intercalate "." $
625 Text.pack . show . snd <$> as
626 instance Html5ify Plain where
628 sp <- liftStateMarkup $ S.gets state_plainify
629 let (t,sp') = Plain.runPlain p sp
631 liftStateMarkup $ S.modify $ \s -> s{state_plainify=sp'}
633 html5CommasDot :: [Html5] -> Html5
634 html5CommasDot [] = pure ()
635 html5CommasDot hs = do
636 sequence_ $ List.intersperse ", " hs
639 html5CommonAttrs :: DTC.CommonAttrs -> Html5 -> Html5
640 html5CommonAttrs DTC.CommonAttrs{id=id_, ..} =
641 Compose . (addClass . addId <$>) . getCompose
646 _ -> H.AddCustomAttribute "class" $
647 H.String $ TL.unpack $ TL.unwords classes
648 addId = maybe id (\(DTC.Ident i) ->
649 H.AddCustomAttribute "id" (H.String $ TL.unpack i)) id_
651 html5SectionNumber :: DTC.PosPath -> Html5
652 html5SectionNumber = go mempty
654 go :: DTC.PosPath -> DTC.PosPath -> Html5
656 case Seq.viewl next of
657 Seq.EmptyL -> pure ()
658 a@(_n,rank) Seq.:< as -> do
659 H.a ! HA.href ("#"<>attrify (prev Seq.|>a)) $$
661 when (not (null as) || null prev) $ do
665 html5SectionRef :: DTC.PosPath -> Html5
667 H.a ! HA.href ("#"<>attrify as) $$
672 instance Attrify DTC.Anchor where
673 attrify DTC.Anchor{..} =
675 <> "." <> attrify count
676 instance Attrify Plain where
678 let (t,_) = Plain.runPlain p def in
680 instance Attrify DTC.PosPath where
681 attrify = attrify . plainify
682 instance Attrify DTC.Pos where
683 attrify = attrify . DTC.posAncestors
685 attrifyIref :: DTC.Words -> H.AttributeValue
687 "iref" <> "." <> attrify (Anchor.plainifyWords term)
688 attrifyIrefCount :: DTC.Words -> DTC.Nat1 -> H.AttributeValue
689 attrifyIrefCount term count =
691 <> "." <> attrify (Anchor.plainifyWords term)
692 <> "." <> attrify count
695 instance Html5ify Plain.L10n where
696 html5ify = html5ify . plainify
697 instance Localize ls Plain Plain.L10n => Localize ls Html5 Plain.L10n where
698 localize loc a = html5ify (Locale.localize loc a::Plain)
699 instance LocalizeIn FR Html5 Plain.L10n where
700 localizeIn loc = html5ify @Plain . localizeIn loc
701 instance LocalizeIn EN Html5 Plain.L10n where
702 localizeIn loc = html5ify @Plain . localizeIn loc