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 html5CommonAttrs attrs $
240 H.table ! HA.class_ "section-header" $$
243 H.td ! HA.class_ "section-number" $$ do
244 html5SectionNumber $ DTC.posAncestors pos
245 H.td ! HA.class_ "section-title" $$ do
246 (case List.length $ DTC.posAncestors pos of
255 forM_ (Tree.axis_child `Tree.runAxis` z) $
257 notes <- liftStateMarkup $ S.gets state_notes
258 case Map.lookup pos notes of
261 H.aside ! HA.class_ "notes" $$ do
265 forM_ ns $ \(num,para) ->
267 H.td ! HA.class_ "note-ref" $$ do
268 H.a ! HA.class_ "note-number"
269 ! HA.id ("note."<>attrify num)
270 ! HA.href ("#note."<>attrify num) $$ do
273 H.a ! HA.href ("#note-ref."<>attrify num) $$ do
277 DTC.Block b -> html5ify b
279 H.nav ! HA.class_ "toc"
280 ! HA.id (attrify pos) $$ do
281 H.span ! HA.class_ "toc-name" $$
282 H.a ! HA.href (attrify pos) $$
283 html5ify Plain.L10n_Table_of_Contents
285 forM_ (Tree.axis_following_sibling `Tree.runAxis` z) $
288 H.nav ! HA.class_ "tof"
289 ! HA.id (attrify pos) $$
290 H.table ! HA.class_ "tof" $$
294 html5CommonAttrs attrs $
295 H.div ! HA.class_ ("figure " <> attrify ("figure-"<>type_))
296 ! HA.id (attrify pos) $$ do
297 H.table ! HA.class_ "figure-caption" $$
301 then H.a ! HA.href ("#"<>attrify pos) $$ mempty
303 H.td ! HA.class_ "figure-number" $$ do
304 H.a ! HA.href ("#"<>attrify pos) $$ do
306 html5ify $ DTC.posAncestors pos
307 forM_ mayTitle $ \title ->
308 H.td ! HA.class_ "figure-title" $$ do
309 unless (TL.null type_) $
310 html5ify $ Plain.L10n_Colon
312 H.div ! HA.class_ "figure-content" $$ do
315 (allTerms,refsByTerm) <- liftStateMarkup $ S.gets $ (Map.! pos) . state_indexs
316 let chars = Anchor.termsByChar allTerms
317 H.div ! HA.class_ "index"
318 ! HA.id (attrify pos) $$ do
319 H.nav ! HA.class_ "index-nav" $$ do
320 forM_ (Map.keys chars) $ \char ->
321 H.a ! HA.href ("#"<>(attrify pos <> "." <> attrify char)) $$
323 H.dl ! HA.class_ "index-chars" $$
324 forM_ (Map.toList chars) $ \(char,terms) -> do
326 let i = attrify pos <> "." <> attrify char in
328 ! HA.href ("#"<>i) $$
331 H.dl ! HA.class_ "index-term" $$ do
332 forM_ terms $ \aliases -> do
334 H.ul ! HA.class_ "index-aliases" $$
335 forM_ (List.take 1 aliases) $ \term ->
336 H.li ! HA.id (attrifyIref term) $$
340 List.sortBy (compare `on` DTC.section . snd) $
341 (`foldMap` aliases) $ \words ->
343 path <- Anchor.pathFromWords words
344 Strict.maybe Nothing (Just . ((words,) <$>) . List.reverse) $
345 TreeMap.lookup path refsByTerm in
347 (<$> anchs) $ \(term,DTC.Anchor{..}) ->
348 H.a ! HA.class_ "index-iref"
349 ! HA.href ("#"<>attrifyIrefCount term count) $$
350 html5ify $ DTC.posAncestors section
351 DTC.References{..} ->
352 html5CommonAttrs attrs $
353 H.div ! HA.class_ "references"
354 ! HA.id (attrify pos) $$ do
357 instance Html5ify DTC.Words where
358 html5ify = html5ify . Anchor.plainifyWords
360 cleanPara :: DTC.Para -> DTC.Para
363 Tree DTC.Iref{} ls -> cleanPara ls
364 Tree DTC.Note{} _ -> mempty
365 Tree n ts -> pure $ Tree n $ cleanPara ts
367 html5ifyToC :: Maybe DTC.Nat -> BodyCursor -> Html5
368 html5ifyToC depth z =
369 case Tree.current z of
370 Tree DTC.Section{..} _ts -> do
372 H.table ! HA.class_ "toc-entry" $$
375 H.td ! HA.class_ "section-number" $$
376 html5SectionRef $ DTC.posAncestors pos
377 H.td ! HA.class_ "section-title" $$
378 html5ify $ cleanPara $ DTC.unTitle title
379 when (maybe True (> DTC.Nat 1) depth && not (null sections)) $
382 html5ifyToC (depth >>= DTC.predNat)
388 `Tree.axis_filter_current` \case
389 Tree DTC.Section{} _ -> True
392 html5ifyToF :: [TL.Text] -> Html5
393 html5ifyToF types = do
394 figsByType <- liftStateMarkup $ S.gets state_figures
396 Map.foldMapWithKey (\ty -> ((ty,) <$>)) $
400 Map.intersection figsByType $
401 Map.fromList [(ty,()) | ty <- types]
402 forM_ (Map.toList figs) $ \(pos, (type_, title)) ->
404 H.td ! HA.class_ "figure-number" $$
405 H.a ! HA.href ("#"<>attrify pos) $$ do
407 html5ify $ DTC.posAncestors pos
409 H.td ! HA.class_ "figure-title" $$
410 html5ify $ cleanPara $ DTC.unTitle ti
412 instance Html5ify [DTC.Block] where
413 html5ify = mapM_ html5ify
414 instance Html5ify DTC.Block where
417 html5CommonAttrs attrs $
418 H.p ! HA.class_ "para"
419 ! HA.id (attrify pos) $$ do
422 html5CommonAttrs attrs $
423 H.ol ! HA.class_ "ol"
424 ! HA.id (attrify pos) $$ do
425 forM_ items $ \item ->
426 H.li $$ html5ify item
428 html5CommonAttrs attrs $
429 H.ul ! HA.class_ "ul"
430 ! HA.id (attrify pos) $$ do
431 forM_ items $ \item ->
432 H.li $$ html5ify item
434 html5CommonAttrs attrs $
435 H.pre ! HA.class_ ("artwork " <> attrify ("artwork-"<>type_))
436 ! HA.id (attrify pos) $$ do
439 html5CommonAttrs attrs $
440 H.div ! HA.class_ ("quote " <> attrify ("quote-"<>type_))
441 ! HA.id (attrify pos) $$ do
444 html5ify $ H.Comment (H.String $ TL.unpack t) ()
445 instance Html5ify DTC.Lines where
446 html5ify (Tree n ls) =
448 DTC.BR -> html5ify H.br
449 DTC.Plain t -> html5ify t
450 DTC.B -> H.strong $$ html5ify ls
451 DTC.Code -> H.code $$ html5ify ls
452 DTC.Del -> H.del $$ html5ify ls
454 i <- liftStateMarkup $ do
455 i <- S.gets $ Plain.state_italic . state_plainify
458 (state_plainify s){Plain.state_italic=
461 H.em ! HA.class_ (if i then "even" else "odd") $$
466 (state_plainify s){Plain.state_italic=i}}
467 DTC.Sub -> H.sub $$ html5ify ls
468 DTC.Sup -> H.sup $$ html5ify ls
469 DTC.SC -> H.span ! HA.class_ "smallcaps" $$ html5ify ls
470 DTC.U -> H.span ! HA.class_ "underline" $$ html5ify ls
475 H.sup ! HA.class_ "note-number" $$
476 H.a ! HA.class_ "note-ref"
477 ! HA.id ("note-ref."<>attrify num)
478 ! HA.href ("#note."<>attrify num) $$
481 depth <- liftStateMarkup $ do
482 depth <- S.gets $ Plain.state_quote . state_plainify
483 S.modify $ \s -> s{state_plainify=
484 (state_plainify s){Plain.state_quote=
487 H.span ! HA.class_ "q" $$ do
488 html5ify $ Plain.L10n_QuoteOpen depth
489 html5ify $ Tree DTC.I ls
490 html5ify $ Plain.L10n_QuoteClose depth
494 (state_plainify s){Plain.state_quote = depth}}
496 H.a ! HA.class_ "eref"
497 ! HA.href (attrify href) $$
499 then html5ify $ DTC.unURL href
503 Nothing -> html5ify ls
504 Just DTC.Anchor{..} ->
505 H.span ! HA.class_ "iref"
506 ! HA.id (attrifyIrefCount term count) $$
509 H.a ! HA.class_ "ref"
510 ! HA.href ("#"<>attrify to) $$
515 refs <- liftStateMarkup $ S.gets state_references
516 case Map.lookup to refs of
519 H.span ! HA.class_ "rref-broken" $$
522 Just DTC.About{..} -> do
524 forM_ (List.take 1 titles) $ \(DTC.Title title) -> do
525 html5ify $ Tree DTC.Q $
528 Just u -> pure $ Tree (DTC.Eref u) title
531 H.a ! HA.class_ "rref"
532 ! HA.href ("#rref."<>attrify to)
533 ! HA.id ("rref."<>attrify to<>maybe "" (\DTC.Anchor{..} -> "."<>attrify count) anchor) $$
536 instance Html5ify DTC.URL where
537 html5ify (DTC.URL url) =
538 H.a ! HA.class_ "eref"
539 ! HA.href (attrify url) $$
541 instance Html5ify DTC.Date where
542 html5ify = html5ify . Plain.L10n_Date
543 instance Html5ify DTC.About where
544 html5ify DTC.About{..} =
545 html5CommasDot $ concat $
547 , html5Entity <$> authors
548 , html5ify <$> maybeToList date
549 , html5Entity <$> maybeToList editor
550 , html5Serie <$> series
553 html5Titles :: [DTC.Title] -> [Html5]
554 html5Titles ts | null ts = []
555 html5Titles ts = [html5Title $ fold $ List.intersperse t $ toList ts]
556 where t = DTC.Title $ Seq.singleton $ tree0 $ DTC.Plain " — "
557 html5Title (DTC.Title title) =
558 html5ify $ Tree DTC.Q $
561 Just u -> pure $ Tree (DTC.Eref u) title
562 html5SerieHref href DTC.Serie{..} = do
563 sp <- liftStateMarkup $ S.gets state_plainify
565 Tree DTC.Eref{href} $
567 [ tree0 $ DTC.Plain $ name
568 , tree0 $ DTC.Plain $ Plain.text sp Plain.L10n_Colon
569 , tree0 $ DTC.Plain key
571 html5Serie s@DTC.Serie{name="RFC", key} | TL.all Char.isDigit key =
572 html5SerieHref (DTC.URL $ "https://tools.ietf.org/html/rfc"<>key) s
573 html5Serie s@DTC.Serie{name="DOI", key} =
574 html5SerieHref (DTC.URL $ "https://dx.doi.org/"<>key) s
575 html5Serie DTC.Serie{..} = do
577 html5ify Plain.L10n_Colon
579 html5Entity DTC.Entity{url=mu, ..} = do
580 html5ify @DTC.Lines $
582 _ | not (TL.null email) ->
583 Tree (DTC.Eref $ DTC.URL $ "mailto:"<>email) $
584 pure $ tree0 $ DTC.Plain name
587 pure $ tree0 $ DTC.Plain name
588 _ -> tree0 $ DTC.Plain name
593 instance Html5ify DTC.Reference where
594 html5ify DTC.Reference{id=id_, ..} =
596 H.td ! HA.class_ "reference-key" $$
597 html5ify @DTC.Lines $ Tree DTC.Rref{anchor=Nothing, to=id_} Seq.empty
598 H.td ! HA.class_ "reference-content" $$ do
600 rrefs <- liftStateMarkup $ S.gets state_rrefs
601 case Map.lookup id_ rrefs of
604 H.span ! HA.class_ "reference-rrefs" $$
606 (<$> List.reverse anchs) $ \DTC.Anchor{..} ->
607 H.a ! HA.class_ "reference-rref"
608 ! HA.href ("#rref."<>attrify id_<>"."<>attrify count) $$
609 html5ify $ DTC.posAncestors section
610 instance Html5ify DTC.PosPath where
618 Text.intercalate "." $
619 Text.pack . show . snd <$> as
620 instance Html5ify Plain where
622 sp <- liftStateMarkup $ S.gets state_plainify
623 let (t,sp') = Plain.runPlain p sp
625 liftStateMarkup $ S.modify $ \s -> s{state_plainify=sp'}
627 html5CommasDot :: [Html5] -> Html5
628 html5CommasDot [] = pure ()
629 html5CommasDot hs = do
630 sequence_ $ List.intersperse ", " hs
633 html5CommonAttrs :: DTC.CommonAttrs -> Html5 -> Html5
634 html5CommonAttrs DTC.CommonAttrs{id=id_, ..} =
635 Compose . (addClass . addId <$>) . getCompose
640 _ -> H.AddCustomAttribute "class" $
641 H.String $ TL.unpack $ TL.unwords classes
642 addId = maybe id (\(DTC.Ident i) ->
643 H.AddCustomAttribute "id" (H.String $ TL.unpack i)) id_
645 html5SectionNumber :: DTC.PosPath -> Html5
646 html5SectionNumber = go mempty
648 go :: DTC.PosPath -> DTC.PosPath -> Html5
650 case Seq.viewl next of
651 Seq.EmptyL -> pure ()
652 a@(_n,rank) Seq.:< as -> do
653 H.a ! HA.href ("#"<>attrify (prev Seq.|>a)) $$
655 when (not (null as) || null prev) $ do
659 html5SectionRef :: DTC.PosPath -> Html5
661 H.a ! HA.href ("#"<>attrify as) $$
666 instance Attrify DTC.Anchor where
667 attrify DTC.Anchor{..} =
669 <> "." <> attrify count
670 instance Attrify Plain where
672 let (t,_) = Plain.runPlain p def in
674 instance Attrify DTC.PosPath where
675 attrify = attrify . plainify
676 instance Attrify DTC.Pos where
677 attrify = attrify . DTC.posAncestors
679 attrifyIref :: DTC.Words -> H.AttributeValue
681 "iref" <> "." <> attrify (Anchor.plainifyWords term)
682 attrifyIrefCount :: DTC.Words -> DTC.Nat1 -> H.AttributeValue
683 attrifyIrefCount term count =
685 <> "." <> attrify (Anchor.plainifyWords term)
686 <> "." <> attrify count
689 instance Html5ify Plain.L10n where
690 html5ify = html5ify . plainify
691 instance Localize ls Plain Plain.L10n => Localize ls Html5 Plain.L10n where
692 localize loc a = html5ify (Locale.localize loc a::Plain)
693 instance LocalizeIn FR Html5 Plain.L10n where
694 localizeIn loc = html5ify @Plain . localizeIn loc
695 instance LocalizeIn EN Html5 Plain.L10n where
696 localizeIn loc = html5ify @Plain . localizeIn loc