Some error handling and link relativizing

This commit is contained in:
Chris Done 2014-03-08 08:14:07 +01:00
parent 251d058c01
commit 34ffb10c37
4 changed files with 33 additions and 10 deletions

View file

@ -8,3 +8,4 @@
/news NewsR GET /news NewsR GET
/report/#Int ReportR GET /report/#Int ReportR GET
/wiki/#Text WikiR GET /wiki/#Text WikiR GET
/wiki/ WikiHomeR GET

View file

@ -1,3 +1,4 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
-- | Wiki page controller. -- | Wiki page controller.
@ -8,34 +9,44 @@ import HL.Foundation
import HL.View.Wiki import HL.View.Wiki
import Blaze (renderHtml) import Blaze (renderHtml)
import Control.Exception.Lifted (catch)
import Data.Conduit import Data.Conduit
import Data.Conduit.Binary import Data.Conduit.Binary
import qualified Data.Conduit.List as CL import qualified Data.Conduit.List as CL
import Data.List (isPrefixOf)
import Data.Maybe import Data.Maybe
import Data.Monoid import Data.Monoid
import Data.Text (Text,unpack) import Data.Text (Text,unpack,pack)
import Network.HTTP.Conduit import Network.HTTP.Conduit
import Prelude hiding (readFile) import Prelude hiding (readFile,catch)
import Text.Pandoc.Definition
import Text.Pandoc.Readers.MediaWiki import Text.Pandoc.Readers.MediaWiki
import Text.Pandoc.Walk
import Text.Pandoc.Writers.HTML import Text.Pandoc.Writers.HTML
import Text.XML import Text.XML
import Text.XML.Cursor import Text.XML.Cursor
-- | Wiki home (no page specified).
getWikiHomeR :: Handler Html
getWikiHomeR =
redirect (WikiR "HaskellWiki:Community")
-- | Wiki controller. -- | Wiki controller.
getWikiR :: Text -> Handler Html getWikiR :: Text -> Handler Html
getWikiR name = getWikiR name =
do html <- liftIO (getWikiPageHtml name) do url <- getUrlRender
html <- liftIO (getWikiPageHtml url name)
blaze (wikiV html) blaze (wikiV html)
-- | Get the MediaWiki markup of a wiki page and then convert it to -- | Get the MediaWiki markup of a wiki page and then convert it to
-- HTML. -- HTML.
getWikiPageHtml :: Text -> IO (Either Text (Text,Html)) getWikiPageHtml url article =
getWikiPageHtml article =
do request <- parseUrl ("http://www.haskell.org/haskellwiki/Special:Export/" <> unpack article) do request <- parseUrl ("http://www.haskell.org/haskellwiki/Special:Export/" <> unpack article)
withManager $ \manager -> do withManager $ \manager -> do
response <- http request manager response <- http request manager
doc <- responseBody response $$+- sinkDoc def doc <- catch (fmap Just (responseBody response $$+- sinkDoc def))
case parse doc of (\(e::UnresolvedEntityException) -> return Nothing)
case doc >>= parse of
Nothing -> return (Left "Unable to parse XML from haskell.org.") Nothing -> return (Left "Unable to parse XML from haskell.org.")
Just (title,text) -> return (Right (title,writeHtml def text)) Just (title,text) -> return (Right (title,writeHtml def text))
where where
@ -43,7 +54,10 @@ getWikiPageHtml article =
do let cursor = fromDocument doc do let cursor = fromDocument doc
title <- listToMaybe (getTitle cursor) title <- listToMaybe (getTitle cursor)
text <- listToMaybe (getText cursor) text <- listToMaybe (getText cursor)
return (title,readMediaWiki def (unpack text)) return (title,walk' (relativize url)
(readMediaWiki def (unpack text)))
where walk' :: (Inline -> Inline) -> Pandoc -> Pandoc
walk' = walk
name n = name n =
Name {nameLocalName = n Name {nameLocalName = n
,nameNamespace = Just "http://www.mediawiki.org/xml/export-0.6/" ,nameNamespace = Just "http://www.mediawiki.org/xml/export-0.6/"
@ -64,3 +78,10 @@ getWikiPageHtml article =
element (name "title") >>= element (name "title") >>=
descendant >>= descendant >>=
content content
relativize url = walk links
where links asis@(Link is (ref,title))
| isPrefixOf "http://" ref || isPrefixOf "https://" ref = asis
| otherwise = Link is (unpack (url (WikiR (pack ref))),title)
links x = x

View file

@ -70,7 +70,7 @@ navigation cur url =
,(CommunityR,"Community") ,(CommunityR,"Community")
,(DocumentationR,"Documentation") ,(DocumentationR,"Documentation")
,(NewsR,"News") ,(NewsR,"News")
,(WikiR "","Wiki")])) ,(WikiHomeR,"Wiki")]))
where item route title = li theclass (a [href (url route)] title) where item route title = li theclass (a [href (url route)] title)
where theclass where theclass
| Just route == cur = [class_ "active"] | Just route == cur = [class_ "active"]

View file

@ -16,7 +16,8 @@ import Data.Text (Text)
wikiV :: Either Text (Text,Html) -> Blaze App wikiV :: Either Text (Text,Html) -> Blaze App
wikiV result = wikiV result =
template template
[(WikiR "","Wiki")] ([(WikiHomeR,"Wiki")] ++
[(WikiR name,name) | Right (name,_) <- [result]])
(\_ -> (\_ ->
container container
(row (row