1
1
{-# LANGUAGE CPP #-}
2
2
{-# LANGUAGE OverloadedStrings #-}
3
+ {-# LANGUAGE TypeApplications #-}
3
4
{-
4
5
Copyright (C) 2009 John MacFarlane <[email protected] >,
5
6
Anton van Straaten <[email protected] >
@@ -76,7 +77,6 @@ import Control.Monad.Except (throwError)
76
77
import Data.Foldable (traverse_ )
77
78
import Data.List (stripPrefix )
78
79
import Data.Maybe (isNothing , mapMaybe )
79
- import Data.Semigroup ((<>) )
80
80
import Network.Gitit.Cache (lookupCache , cacheContents )
81
81
import Network.Gitit.Framework hiding (uriPath )
82
82
import Network.Gitit.Layout
@@ -93,9 +93,6 @@ import qualified Text.Pandoc.Builder as B
93
93
import Text.HTML.SanitizeXSS (sanitizeBalance )
94
94
import Skylighting hiding (Context )
95
95
import Text.Pandoc hiding (MathML , WebTeX , MathJax )
96
- import Text.XHtml hiding ( (</>) , dir , method , password , rev )
97
- import Text.XHtml.Strict (stringToHtmlString )
98
- import Text.Blaze.Html.Renderer.String as Blaze ( renderHtml )
99
96
import URI.ByteString (Query (Query ), URIRef (uriPath ), laxURIParserOptions ,
100
97
parseURI , uriQuery )
101
98
import qualified Data.Text as T
@@ -104,6 +101,12 @@ import qualified Data.ByteString.Char8 as SC (pack, unpack)
104
101
import qualified Data.ByteString.Lazy as L (toChunks , fromChunks )
105
102
import qualified Data.FileStore as FS
106
103
import qualified Text.Pandoc as Pandoc
104
+ import Data.String (IsString (fromString ))
105
+ import Text.Blaze.Html.Renderer.String as Blaze ( renderHtml )
106
+ import Text.Blaze.Html5 hiding (u , s , contents , source , html , title , map )
107
+ import Text.Blaze.Html5.Attributes hiding (id )
108
+ import qualified Text.Blaze.Html5 as Html5
109
+ import qualified Text.Blaze.Html5.Attributes as Html5.Attr
107
110
108
111
--
109
112
-- ContentTransformer runners
@@ -195,7 +198,7 @@ preview = runPageTransformer $
195
198
contentsToPage >>=
196
199
pageToWikiPandoc >>=
197
200
pandocToHtml >>=
198
- return . toResponse . renderHtmlFragment
201
+ return . toResponse . renderHtml
199
202
200
203
-- | Applies pre-commit plugins to raw page source, possibly
201
204
-- modifying it.
@@ -332,6 +335,8 @@ pageToPandoc page' = do
332
335
, ctxMeta = pageMeta page' }
333
336
either (liftIO . E. throwIO) return $ readerFor (pageFormat page') (pageLHS page') (pageText page')
334
337
338
+ data WasRedirect = WasRedirect | WasNoRedirect
339
+
335
340
-- | Detects if the page is a redirect page and handles accordingly. The exact
336
341
-- behaviour is as follows:
337
342
--
@@ -374,56 +379,51 @@ handleRedirects page = case lookup "redirect" (pageMeta page) of
374
379
where
375
380
addMessage message = modifyContext $ \ context -> context
376
381
{ ctxLayout = (ctxLayout context)
377
- { pgMessages = pgMessages (ctxLayout context) ++ [message]
382
+ { pgMessages = pgMessages (ctxLayout context) ++ [renderHtml message]
378
383
}
379
384
}
380
385
redirectedFrom source = do
381
386
(url, html) <- processSource source
382
- return $ concat
383
- [ " Redirected from <a href=\" "
384
- , url
385
- , " ?redirect=no\" title=\" Go to original page\" >"
386
- , html
387
- , " </a>"
387
+ return $ mconcat
388
+ [ " Redirected from " ,
389
+ a ! href (url WasNoRedirect ) ! title " Go to original page" $ html
388
390
]
389
391
doubleRedirect source destination = do
390
392
(url, html) <- processSource source
391
393
(url', html') <- processDestination destination
392
- return $ concat
393
- [ " This page normally redirects to <a href=\" "
394
- , url'
395
- , " \" title=\" Continue to destination\" >"
396
- , html'
397
- , " </a>, but as you were already redirected from <a href=\" "
398
- , url
399
- , " ?redirect=no\" title=\" Go to original page\" >"
400
- , html
401
- , " </a>"
394
+ return $ mconcat
395
+ [ " This page normally redirects to "
396
+ , a ! href (fromString $ url' WasRedirect ) ! title " Continue to destination" $ html'
397
+ , " , but as you were already redirected from "
398
+ , a ! href (url WasNoRedirect ) ! title " Go to original page" $ html
402
399
, " , this was stopped to prevent a double-redirect."
403
400
]
404
401
cancelledRedirect destination = do
405
402
(url', html') <- processDestination destination
406
- return $ concat
407
- [ " This page redirects to <a href=\" "
408
- , url'
409
- , " \" title=\" Continue to destination\" >"
410
- , html'
411
- , " </a>."
403
+ return $ mconcat
404
+ [ " This page redirects to "
405
+ , a ! href (fromString $ url' WasRedirect ) ! title " Continue to destination" $ html'
412
406
]
413
407
processSource source = do
414
408
base' <- getWikiBase
415
- let url = stringToHtmlString $ base' ++ urlForPage source
416
- let html = stringToHtmlString source
409
+ let url redir = fromString @ AttributeValue $
410
+ base' ++ urlForPage source ++ case redir of
411
+ WasNoRedirect -> " ?redirect=no"
412
+ WasRedirect -> " "
413
+ let html = fromString @ Html source
417
414
return (url, html)
418
415
processDestination destination = do
419
416
base' <- getWikiBase
420
417
let (page', fragment) = break (== ' #' ) destination
421
- let url = stringToHtmlString $ concat
418
+ let url redir = concat
422
419
[ base'
423
420
, urlForPage page'
424
421
, fragment
425
- ]
426
- let html = stringToHtmlString page'
422
+
423
+ ] ++ case redir of
424
+ WasNoRedirect -> " ?redirect=no"
425
+ WasRedirect -> " "
426
+ let html = fromString @ Html page'
427
427
return (url, html)
428
428
getSource = do
429
429
cfg <- lift getConfig
@@ -461,26 +461,25 @@ handleRedirects page = case lookup "redirect" (pageMeta page) of
461
461
, urlForPage (pageName page)
462
462
, " ?redirect=yes"
463
463
]
464
- lift $ seeOther url' $ withBody $ concat
465
- [ " <!doctype html><html><head><title>307 Redirect"
466
- , " </title></head><body><p>You are being <a href=\" "
467
- , stringToHtmlString url'
468
- , " \" >redirected</a>.</body></p></html>"
464
+ lift $ seeOther url' $ withBody $ renderHtml $ docTypeHtml $ mconcat
465
+ [ Html5. head $ Html5. title " 307 Redirect"
466
+ , Html5. body $ p $ mconcat [
467
+ " You are being" ,
468
+ a ! href (fromString url') $ " redirected."
469
+ ]
469
470
]
470
471
Just True -> fmap Left $ do
471
472
(url', html') <- processDestination destination
472
- lift $ ok $ withBody $ concat
473
- [ " <!doctype html><html><head><title>Redirecting to "
474
- , html'
475
- , " </title><meta http-equiv=\" refresh\" content=\" 0; url="
476
- , url'
477
- , " \" /><script type=\" text/javascript\" >window.location=\" "
478
- , url'
479
- , " \" </script></head><body><p>Redirecting to <a href=\" "
480
- , url'
481
- , " \" >"
482
- , html'
483
- , " </a>...</p></body></html>"
473
+ lift $ ok $ withBody $ renderHtml $ docTypeHtml $ mconcat
474
+ [ Html5. head $ mconcat
475
+ [ Html5. title $ " Redirecting to" <> html'
476
+ , meta ! httpEquiv " refresh" ! content (fromString $ " 0; url=" <> url' WasRedirect )
477
+ , script ! type_ " text/javascript" $ fromString $ " window.location=\" " <> url' WasRedirect <> " \" "
478
+ ],
479
+ Html5. body $ p $ mconcat
480
+ [ " Redirecting to "
481
+ , a ! href (fromString $ url' WasRedirect ) $ html'
482
+ ]
484
483
]
485
484
Just False -> do
486
485
cancelledRedirect destination >>= addMessage
@@ -505,7 +504,7 @@ pandocToHtml pandocContents = do
505
504
case res of
506
505
Right t -> return t
507
506
Left e -> throwError $ PandocTemplateError $ T. pack e
508
- return $ primHtml $ T. unpack .
507
+ return $ preEscapedToHtml @ T. Text .
509
508
(if xssSanitize cfg then sanitizeBalance else id ) $
510
509
either E. throw id . runPure $ writeHtml5String def{
511
510
writerTemplate = Just compiledTemplate
@@ -538,8 +537,7 @@ highlightSource (Just source) = do
538
537
, traceOutput = False } l
539
538
$ T. pack $ filter (/= ' \r ' ) source of
540
539
Left e -> fail (show e)
541
- Right r -> return $ primHtml $ Blaze. renderHtml
542
- $ formatHtmlBlock formatOpts r
540
+ Right r -> return $ formatHtmlBlock formatOpts r
543
541
544
542
--
545
543
-- Plugin combinators
@@ -603,11 +601,11 @@ wikiDivify :: Html -> ContentTransformer Html
603
601
wikiDivify c = do
604
602
categories <- liftM ctxCategories get
605
603
base' <- lift getWikiBase
606
- let categoryLink ctg = li (anchor ! [ href $ base' ++ " /_category/" ++ ctg] << ctg)
604
+ let categoryLink ctg = li (a ! href (fromString $ base' ++ " /_category/" ++ ctg) $ fromString ctg)
607
605
let htmlCategories = if null categories
608
- then noHtml
609
- else thediv ! [identifier " categoryList" ] << ulist << map categoryLink categories
610
- return $ thediv ! [identifier " wikipage" ] << [c, htmlCategories]
606
+ then mempty
607
+ else Html5. div ! Html5.Attr. id " categoryList" $ ul $ foldMap categoryLink categories
608
+ return $ Html5. div ! Html5.Attr. id " wikipage" $ c <> htmlCategories
611
609
612
610
-- | Adds page title to a Pandoc document.
613
611
addPageTitleToPandoc :: String -> Pandoc -> ContentTransformer Pandoc
0 commit comments