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

Commit c31c156

Browse files
Iñaki García EtxebarriaKleidukos
authored andcommitted
Add support for labeled module references
Support a markdown-style way of annotating module references. For instance -- | [label]("Module.Name#anchor") will create a link that points to the same place as the module reference "Module.Name#anchor" but the text displayed on the link will be "label".
1 parent a2f9f29 commit c31c156

File tree

14 files changed

+198
-49
lines changed

14 files changed

+198
-49
lines changed

doc/markup.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,11 @@ is valid before turning it into a link but unlike with identifiers,
982982
whether the module is in scope isn't checked and will always be turned
983983
into a link.
984984

985+
It is also possible to specify alternate text for the generated link
986+
using syntax analogous to that used for URLs: ::
987+
988+
-- | This is a reference to [the main module]("Module.Main").
989+
985990
Itemized and Enumerated Lists
986991
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
987992

haddock-api/src/Haddock/Backends/Hoogle.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ markupTag dflags = Markup {
325325
markupAppend = (++),
326326
markupIdentifier = box (TagInline "a") . str . out dflags,
327327
markupIdentifierUnchecked = box (TagInline "a") . str . showWrapped (out dflags . snd),
328-
markupModule = box (TagInline "a") . str,
328+
markupModule = \(ModLink m label) -> box (TagInline "a") (fromMaybe (str m) label),
329329
markupWarning = box (TagInline "i"),
330330
markupEmphasis = box (TagInline "i"),
331331
markupBold = box (TagInline "b"),

haddock-api/src/Haddock/Backends/LaTeX.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,12 @@ latexMarkup = Markup
12101210
, markupAppend = \l r v -> l v . r v
12111211
, markupIdentifier = \i v -> inlineElem (markupId v (fmap occName i))
12121212
, markupIdentifierUnchecked = \i v -> inlineElem (markupId v (fmap snd i))
1213-
, markupModule = \m _ -> inlineElem (let (mdl,_ref) = break (=='#') m in (tt (text mdl)))
1213+
, markupModule =
1214+
\(ModLink m mLabel) v ->
1215+
case mLabel of
1216+
Just lbl -> inlineElem . tt $ lbl v empty
1217+
Nothing -> inlineElem (let (mdl,_ref) = break (=='#') m
1218+
in (tt (text mdl)))
12141219
, markupWarning = \p v -> p v
12151220
, markupEmphasis = \p v -> inlineElem (emph (p v empty))
12161221
, markupBold = \p v -> inlineElem (bold (p v empty))

haddock-api/src/Haddock/Backends/Xhtml/DocMarkup.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ parHtmlMarkup qual insertAnchors ppId = Markup {
4444
markupAppend = (+++),
4545
markupIdentifier = thecode . ppId insertAnchors,
4646
markupIdentifierUnchecked = thecode . ppUncheckedLink qual,
47-
markupModule = \m -> let (mdl,ref) = break (=='#') m
48-
-- Accomodate for old style
49-
-- foo\#bar anchors
50-
mdl' = case reverse mdl of
51-
'\\':_ -> init mdl
52-
_ -> mdl
53-
in ppModuleRef (mkModuleName mdl') ref,
47+
markupModule = \(ModLink m lbl) ->
48+
let (mdl,ref) = break (=='#') m
49+
-- Accomodate for old style
50+
-- foo\#bar anchors
51+
mdl' = case reverse mdl of
52+
'\\':_ -> init mdl
53+
_ -> mdl
54+
in ppModuleRef lbl (mkModuleName mdl') ref,
5455
markupWarning = thediv ! [theclass "warning"],
5556
markupEmphasis = emphasize,
5657
markupBold = strong,

haddock-api/src/Haddock/Backends/Xhtml/Names.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,12 @@ ppModule mdl = anchor ! [href (moduleUrl mdl)]
186186
<< toHtml (moduleString mdl)
187187

188188

189-
ppModuleRef :: ModuleName -> String -> Html
190-
ppModuleRef mdl ref = anchor ! [href (moduleHtmlFile' mdl ++ ref)]
191-
<< toHtml (moduleNameString mdl)
189+
ppModuleRef :: Maybe Html -> ModuleName -> String -> Html
190+
ppModuleRef Nothing mdl ref = anchor ! [href (moduleHtmlFile' mdl ++ ref)]
191+
<< toHtml (moduleNameString mdl)
192+
ppModuleRef (Just lbl) mdl ref = anchor ! [href (moduleHtmlFile' mdl ++ ref)]
193+
<< lbl
194+
192195
-- NB: The ref parameter already includes the '#'.
193196
-- This function is only called from markupModule expanding a
194197
-- DocModule, which doesn't seem to be ever be used.

haddock-api/src/Haddock/Interface/Json.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ jsonDoc (DocIdentifierUnchecked modName) = jsonObject
9898
, ("modName", jsonString (showModName modName))
9999
]
100100

101-
jsonDoc (DocModule s) = jsonObject
101+
jsonDoc (DocModule (ModLink m _l)) = jsonObject
102102
[ ("tag", jsonString "DocModule")
103-
, ("string", jsonString s)
103+
, ("string", jsonString m)
104104
]
105105

106106
jsonDoc (DocWarning x) = jsonObject

haddock-api/src/Haddock/Interface/LexParseRn.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ rename dflags gre = rn
148148
DocDefList list -> DocDefList <$> traverse (\(a, b) -> (,) <$> rn a <*> rn b) list
149149
DocCodeBlock doc -> DocCodeBlock <$> rn doc
150150
DocIdentifierUnchecked x -> pure (DocIdentifierUnchecked x)
151-
DocModule str -> pure (DocModule str)
151+
DocModule (ModLink m l) -> DocModule . ModLink m <$> traverse rn l
152152
DocHyperlink (Hyperlink u l) -> DocHyperlink . Hyperlink u <$> traverse rn l
153153
DocPic str -> pure (DocPic str)
154154
DocMathInline str -> pure (DocMathInline str)

haddock-api/src/Haddock/InterfaceFile.hs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import GHC.Types.Unique.FM
4646
import GHC.Types.Unique.Supply
4747
import GHC.Types.Unique
4848

49+
import Documentation.Haddock.Parser (parseModLink)
50+
4951

5052
data InterfaceFile = InterfaceFile {
5153
ifLinkEnv :: LinkEnv,
@@ -69,6 +71,18 @@ ifUnitId if_ =
6971
binaryInterfaceMagic :: Word32
7072
binaryInterfaceMagic = 0xD0Cface
7173

74+
-- Note [The DocModule story]
75+
--
76+
-- Breaking changes to the DocH type result in Haddock being unable to read
77+
-- existing interfaces. This is especially painful for interfaces shipped
78+
-- with GHC distributions since there is no easy way to regenerate them!
79+
--
80+
-- PR #1315 introduced a breaking change to the DocModule constructor. To
81+
-- maintain backward compatibility we
82+
--
83+
-- Parse the old DocModule constructor format (tag 5) and parse the contained
84+
-- string into a proper ModLink structure. When writing interfaces we exclusively
85+
-- use the new DocModule format (tag 24)
7286

7387
-- IMPORTANT: Since datatypes in the GHC API might change between major
7488
-- versions, and because we store GHC datatypes in our interface files, we need
@@ -84,10 +98,10 @@ binaryInterfaceMagic = 0xD0Cface
8498
--
8599
binaryInterfaceVersion :: Word16
86100
#if MIN_VERSION_ghc(9,0,0) && !MIN_VERSION_ghc(9,1,0)
87-
binaryInterfaceVersion = 37
101+
binaryInterfaceVersion = 38
88102

89103
binaryInterfaceVersionCompatibility :: [Word16]
90-
binaryInterfaceVersionCompatibility = [binaryInterfaceVersion]
104+
binaryInterfaceVersionCompatibility = [37, binaryInterfaceVersion]
91105
#else
92106
#error Unsupported GHC version
93107
#endif
@@ -444,6 +458,15 @@ instance Binary a => Binary (Hyperlink a) where
444458
label <- get bh
445459
return (Hyperlink url label)
446460

461+
instance Binary a => Binary (ModLink a) where
462+
put_ bh (ModLink m label) = do
463+
put_ bh m
464+
put_ bh label
465+
get bh = do
466+
m <- get bh
467+
label <- get bh
468+
return (ModLink m label)
469+
447470
instance Binary Picture where
448471
put_ bh (Picture uri title) = do
449472
put_ bh uri
@@ -522,9 +545,6 @@ instance (Binary mod, Binary id) => Binary (DocH mod id) where
522545
put_ bh (DocIdentifier ae) = do
523546
putByte bh 4
524547
put_ bh ae
525-
put_ bh (DocModule af) = do
526-
putByte bh 5
527-
put_ bh af
528548
put_ bh (DocEmphasis ag) = do
529549
putByte bh 6
530550
put_ bh ag
@@ -579,6 +599,10 @@ instance (Binary mod, Binary id) => Binary (DocH mod id) where
579599
put_ bh (DocTable x) = do
580600
putByte bh 23
581601
put_ bh x
602+
-- See note [The DocModule story]
603+
put_ bh (DocModule af) = do
604+
putByte bh 24
605+
put_ bh af
582606

583607
get bh = do
584608
h <- getByte bh
@@ -598,9 +622,10 @@ instance (Binary mod, Binary id) => Binary (DocH mod id) where
598622
4 -> do
599623
ae <- get bh
600624
return (DocIdentifier ae)
625+
-- See note [The DocModule story]
601626
5 -> do
602627
af <- get bh
603-
return (DocModule af)
628+
return (parseModLink af)
604629
6 -> do
605630
ag <- get bh
606631
return (DocEmphasis ag)
@@ -655,6 +680,10 @@ instance (Binary mod, Binary id) => Binary (DocH mod id) where
655680
23 -> do
656681
x <- get bh
657682
return (DocTable x)
683+
-- See note [The DocModule story]
684+
24 -> do
685+
af <- get bh
686+
return (DocModule af)
658687
_ -> error "invalid binary data found in the interface file"
659688

660689

haddock-api/src/Haddock/Types.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ instance NFData id => NFData (Header id) where
501501
instance NFData id => NFData (Hyperlink id) where
502502
rnf (Hyperlink a b) = a `deepseq` b `deepseq` ()
503503

504+
instance NFData id => NFData (ModLink id) where
505+
rnf (ModLink a b) = a `deepseq` b `deepseq` ()
506+
504507
instance NFData Picture where
505508
rnf (Picture a b) = a `deepseq` b `deepseq` ()
506509

haddock-library/fixtures/Fixtures.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ instance ToExpr id => ToExpr (Header id)
149149
deriving instance Generic (Hyperlink id)
150150
instance ToExpr id => ToExpr (Hyperlink id)
151151

152+
deriving instance Generic (ModLink id)
153+
instance ToExpr id => ToExpr (ModLink id)
154+
152155
deriving instance Generic Picture
153156
instance ToExpr Picture
154157

0 commit comments

Comments
 (0)