Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
base url option
Browse files Browse the repository at this point in the history
New `Flag_BaseURL` which configures from where static files are loaded
(--base-url).  If given and not equal "." static files are not coppied,
as this indicates that they are not read from the the directory where
we'd copy them.  The default value is ".".
  • Loading branch information
coot committed Dec 28, 2020
1 parent 6e74824 commit e24478e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
26 changes: 18 additions & 8 deletions haddock-api/src/Haddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import Haddock.GhcUtils (modifySessionDynFlags, setOutputDir)
import Control.Monad hiding (forM_)
import Data.Foldable (forM_, foldl')
import Data.Traversable (for)
import Data.List (isPrefixOf, nub)
import Data.List (find, isPrefixOf, nub)
import Control.Exception
import Data.Maybe
import Data.IORef
Expand Down Expand Up @@ -282,6 +282,7 @@ render dflags unit_state flags sinceQual qual ifaces installedIfaces extSrcMap =
unicode = Flag_UseUnicode `elem` flags
pretty = Flag_PrettyHtml `elem` flags
opt_wiki_urls = wikiUrls flags
opt_base_url = baseUrl flags
opt_contents_url = optContentsUrl flags
opt_index_url = optIndexUrl flags
odir = outputDir flags
Expand Down Expand Up @@ -363,27 +364,35 @@ render dflags unit_state flags sinceQual qual ifaces installedIfaces extSrcMap =
themes <- getThemes libDir flags >>= either bye return

let withQuickjump = Flag_QuickJumpIndex `elem` flags
withBaseURL = isJust
. find (\flag -> case flag of
Flag_BaseURL base_url ->
base_url /= "." && base_url /= "./"
_ -> False
)
$ flags

when (Flag_GenIndex `elem` flags) $ do
withTiming dflags' "ppHtmlIndex" (const ()) $ do
_ <- {-# SCC ppHtmlIndex #-}
ppHtmlIndex odir title pkgStr
themes opt_mathjax opt_contents_url sourceUrls' opt_wiki_urls
allVisibleIfaces pretty
opt_base_url allVisibleIfaces pretty
return ()

copyHtmlBits odir libDir themes withQuickjump
unless withBaseURL $
copyHtmlBits odir libDir themes withQuickjump

when (Flag_GenContents `elem` flags) $ do
withTiming dflags' "ppHtmlContents" (const ()) $ do
_ <- {-# SCC ppHtmlContents #-}
ppHtmlContents unit_state odir title pkgStr
themes opt_mathjax opt_index_url sourceUrls' opt_wiki_urls
allVisibleIfaces True prologue pretty
opt_base_url allVisibleIfaces True prologue pretty
sincePkg (makeContentsQual qual)
_ <- when withQuickjump $
ppJsonIndex odir sourceUrls' opt_wiki_urls
unicode opt_contents_url qual
unicode Nothing qual
ifaces
(nub $ map fst installedIfaces)

Expand All @@ -395,12 +404,13 @@ render dflags unit_state flags sinceQual qual ifaces installedIfaces extSrcMap =
_ <- {-# SCC ppHtml #-}
ppHtml unit_state title pkgStr visibleIfaces reexportedIfaces odir
prologue
themes opt_mathjax sourceUrls' opt_wiki_urls
themes opt_mathjax sourceUrls' opt_wiki_urls opt_base_url
opt_contents_url opt_index_url unicode sincePkg qual
pretty withQuickjump
return ()
copyHtmlBits odir libDir themes withQuickjump
writeHaddockMeta odir withQuickjump
unless withBaseURL $ do
copyHtmlBits odir libDir themes withQuickjump
writeHaddockMeta odir withQuickjump

-- TODO: we throw away Meta for both Hoogle and LaTeX right now,
-- might want to fix that if/when these two get some work on them
Expand Down
37 changes: 23 additions & 14 deletions haddock-api/src/Haddock/Backends/Xhtml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ ppHtml :: UnitState
-> Maybe String -- ^ The mathjax URL (--mathjax)
-> SourceURLs -- ^ The source URL (--source)
-> WikiURLs -- ^ The wiki URL (--wiki)
-> BaseURL -- ^ The base URL (--base-url)
-> Maybe String -- ^ The contents URL (--use-contents)
-> Maybe String -- ^ The index URL (--use-index)
-> Bool -- ^ Whether to use unicode in output (--use-unicode)
Expand All @@ -85,7 +86,7 @@ ppHtml :: UnitState

ppHtml state doctitle maybe_package ifaces reexported_ifaces odir prologue
themes maybe_mathjax_url maybe_source_url maybe_wiki_url
maybe_contents_url maybe_index_url unicode
maybe_base_url maybe_contents_url maybe_index_url unicode
pkg qual debug withQuickjump = do
let
visible_ifaces = filter visible ifaces
Expand All @@ -94,21 +95,21 @@ ppHtml state doctitle maybe_package ifaces reexported_ifaces odir prologue
when (isNothing maybe_contents_url) $
ppHtmlContents state odir doctitle maybe_package
themes maybe_mathjax_url maybe_index_url maybe_source_url maybe_wiki_url
(map toInstalledIface visible_ifaces ++ reexported_ifaces)
maybe_base_url (map toInstalledIface visible_ifaces ++ reexported_ifaces)
False -- we don't want to display the packages in a single-package contents
prologue debug pkg (makeContentsQual qual)

when (isNothing maybe_index_url) $ do
ppHtmlIndex odir doctitle maybe_package
themes maybe_mathjax_url maybe_contents_url maybe_source_url maybe_wiki_url
(map toInstalledIface visible_ifaces ++ reexported_ifaces) debug
maybe_base_url (map toInstalledIface visible_ifaces ++ reexported_ifaces) debug

when withQuickjump $
ppJsonIndex odir maybe_source_url maybe_wiki_url unicode pkg qual
visible_ifaces []

mapM_ (ppHtmlModule odir doctitle themes
maybe_mathjax_url maybe_source_url maybe_wiki_url
maybe_mathjax_url maybe_source_url maybe_wiki_url maybe_base_url
maybe_contents_url maybe_index_url unicode pkg qual debug) visible_ifaces


Expand All @@ -131,10 +132,16 @@ headHtml docTitle themes mathjax_url =
[ meta ! [ httpequiv "Content-Type", content "text/html; charset=UTF-8"]
, meta ! [ XHtml.name "viewport", content "width=device-width, initial-scale=1"]
, thetitle << docTitle
, styleSheet themes
, thelink ! [ rel "stylesheet", thetype "text/css", href quickJumpCssFile] << noHtml
, styleSheet base_url themes
, thelink ! [ rel "stylesheet"
, thetype "text/css"
, href (withBaseURL base_url quickJumpCssFile) ]
<< noHtml
, thelink ! [ rel "stylesheet", thetype "text/css", href fontUrl] << noHtml
, script ! [src haddockJsFile, emptyAttr "async", thetype "text/javascript"] << noHtml
, script ! [ src (withBaseURL base_url haddockJsFile)
, emptyAttr "async"
, thetype "text/javascript" ]
<< noHtml
, script ! [thetype "text/x-mathjax-config"] << primHtml mjConf
, script ! [src mjUrl, thetype "text/javascript"] << noHtml
]
Expand Down Expand Up @@ -273,14 +280,15 @@ ppHtmlContents
-> Maybe String
-> SourceURLs
-> WikiURLs
-> BaseURL
-> [InstalledInterface] -> Bool -> Maybe (MDoc GHC.RdrName)
-> Bool
-> Maybe Package -- ^ Current package
-> Qualification -- ^ How to qualify names
-> IO ()
ppHtmlContents state odir doctitle _maybe_package
themes mathjax_url maybe_index_url
maybe_source_url maybe_wiki_url ifaces showPkgs prologue debug pkg qual = do
maybe_source_url maybe_wiki_url maybe_base_url ifaces showPkgs prologue debug pkg qual = do
let tree = mkModuleTree state showPkgs
[(instMod iface, toInstalledDescription iface)
| iface <- ifaces
Expand All @@ -290,7 +298,7 @@ ppHtmlContents state odir doctitle _maybe_package
| iface <- ifaces
, instIsSig iface]
html =
headHtml doctitle themes mathjax_url +++
headHtml doctitle themes mathjax_url maybe_base_url +++
bodyHtml doctitle Nothing
maybe_source_url maybe_wiki_url
Nothing maybe_index_url << [
Expand Down Expand Up @@ -478,11 +486,12 @@ ppHtmlIndex :: FilePath
-> Maybe String
-> SourceURLs
-> WikiURLs
-> BaseURL
-> [InstalledInterface]
-> Bool
-> IO ()
ppHtmlIndex odir doctitle _maybe_package themes
maybe_mathjax_url maybe_contents_url maybe_source_url maybe_wiki_url ifaces debug = do
maybe_mathjax_url maybe_contents_url maybe_source_url maybe_wiki_url maybe_base_url ifaces debug = do
let html = indexPage split_indices Nothing
(if split_indices then [] else index)

Expand All @@ -498,7 +507,7 @@ ppHtmlIndex odir doctitle _maybe_package themes

where
indexPage showLetters ch items =
headHtml (doctitle ++ " (" ++ indexName ch ++ ")") themes maybe_mathjax_url +++
headHtml (doctitle ++ " (" ++ indexName ch ++ ")") themes maybe_mathjax_url maybe_base_url +++
bodyHtml doctitle Nothing
maybe_source_url maybe_wiki_url
maybe_contents_url Nothing << [
Expand Down Expand Up @@ -598,11 +607,11 @@ ppHtmlIndex odir doctitle _maybe_package themes

ppHtmlModule
:: FilePath -> String -> Themes
-> Maybe String -> SourceURLs -> WikiURLs
-> Maybe String -> SourceURLs -> WikiURLs -> BaseURL
-> Maybe String -> Maybe String -> Bool -> Maybe Package -> QualOption
-> Bool -> Interface -> IO ()
ppHtmlModule odir doctitle themes
maybe_mathjax_url maybe_source_url maybe_wiki_url
maybe_mathjax_url maybe_source_url maybe_wiki_url maybe_base_url
maybe_contents_url maybe_index_url unicode pkg qual debug iface = do
let
mdl = ifaceMod iface
Expand All @@ -620,7 +629,7 @@ ppHtmlModule odir doctitle themes
= toHtml mdl_str
real_qual = makeModuleQual qual aliases mdl
html =
headHtml mdl_str_annot themes maybe_mathjax_url +++
headHtml mdl_str_annot themes maybe_mathjax_url maybe_base_url +++
bodyHtml doctitle (Just iface)
maybe_source_url maybe_wiki_url
maybe_contents_url maybe_index_url << [
Expand Down
7 changes: 4 additions & 3 deletions haddock-api/src/Haddock/Backends/Xhtml/Themes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Haddock.Backends.Xhtml.Themes (
where

import Haddock.Options
import Haddock.Backends.Xhtml.Types ( BaseURL, withBaseURL )

import Control.Monad (liftM)
import Data.Char (toLower)
Expand Down Expand Up @@ -176,13 +177,13 @@ cssFiles :: Themes -> [String]
cssFiles ts = nub $ concatMap themeFiles ts


styleSheet :: Themes -> Html
styleSheet ts = toHtml $ zipWith mkLink rels ts
styleSheet :: BaseURL -> Themes -> Html
styleSheet base_url ts = toHtml $ zipWith mkLink rels ts
where
rels = "stylesheet" : repeat "alternate stylesheet"
mkLink aRel t =
thelink
! [ href (themeHref t), rel aRel, thetype "text/css",
! [ href (withBaseURL base_url (themeHref t)), rel aRel, thetype "text/css",
XHtml.title (themeName t)
]
<< noHtml
Expand Down
12 changes: 12 additions & 0 deletions haddock-api/src/Haddock/Backends/Xhtml/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
-----------------------------------------------------------------------------
module Haddock.Backends.Xhtml.Types (
SourceURLs, WikiURLs,
BaseURL,
withBaseURL,
LinksInfo,
Splice,
Unicode,
Expand All @@ -20,12 +22,22 @@ module Haddock.Backends.Xhtml.Types (

import Data.Map
import GHC
import qualified System.FilePath as FilePath


-- the base, module and entity URLs for the source code and wiki links.
type SourceURLs = (Maybe FilePath, Maybe FilePath, Map Unit FilePath, Map Unit FilePath)
type WikiURLs = (Maybe FilePath, Maybe FilePath, Maybe FilePath)

-- | base url for loading js, json, css resources. The default is "."
--
type BaseURL = Maybe String

-- TODO: we shouldn't use 'FilePath.</>'
withBaseURL :: BaseURL -> String -> String
withBaseURL Nothing uri = uri
withBaseURL (Just baseUrl) uri = baseUrl FilePath.</> uri


-- The URL for source and wiki links
type LinksInfo = (SourceURLs, WikiURLs)
Expand Down
7 changes: 7 additions & 0 deletions haddock-api/src/Haddock/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Haddock.Options (
optSourceCssFile,
sourceUrls,
wikiUrls,
baseUrl,
optDumpInterfaceFile,
optShowInterfaceFile,
optLaTeXStyle,
Expand Down Expand Up @@ -72,6 +73,7 @@ data Flag
| Flag_SourceEntityURL String
| Flag_SourceLEntityURL String
| Flag_WikiBaseURL String
| Flag_BaseURL String
| Flag_WikiModuleURL String
| Flag_WikiEntityURL String
| Flag_LaTeX
Expand Down Expand Up @@ -155,6 +157,8 @@ options backwardsCompat =
"URL for a source code link for each entity.\nUsed if name links are unavailable, eg. for TH splices.",
Option [] ["comments-base"] (ReqArg Flag_WikiBaseURL "URL")
"URL for a comments link on the contents\nand index pages",
Option [] ["base-url"] (ReqArg Flag_BaseURL "URL")
"Base URL for statis assets (eg. css, javascript, json files etc.).\nWhen given statis assets will not be copied.",
Option [] ["comments-module"] (ReqArg Flag_WikiModuleURL "URL")
"URL for a comments link for each module\n(using the %{MODULE} var)",
Option [] ["comments-entity"] (ReqArg Flag_WikiEntityURL "URL")
Expand Down Expand Up @@ -295,6 +299,9 @@ wikiUrls flags =
,optLast [str | Flag_WikiEntityURL str <- flags])


baseUrl :: [Flag] -> Maybe String
baseUrl flags = optLast [str | Flag_BaseURL str <- flags]

optDumpInterfaceFile :: [Flag] -> Maybe FilePath
optDumpInterfaceFile flags = optLast [ str | Flag_DumpInterface str <- flags ]

Expand Down

0 comments on commit e24478e

Please sign in to comment.