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

Source hyperlinker #410

Merged
merged 109 commits into from
Jul 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
ce0237f
Create scaffolding for Haskell source parser module.
panhania Jun 4, 2015
e17f625
Implement function for tagging parsed chunks with source spans.
panhania Jun 4, 2015
413f7f3
Implement simple string chunking based on HsColour library.
panhania Jun 4, 2015
6fb8d5a
Create basic token classification method.
panhania Jun 4, 2015
57d4c9c
Adapt source span tagging to work with current whitespace handling.
panhania Jun 4, 2015
c7ecc59
Add record accessors to exports of hyperlinker parser module.
panhania Jun 4, 2015
7b60788
Make parser module export all types and associated accessors.
panhania Jun 5, 2015
5e904cb
Create simple HTML renderer for parsed source file.
panhania Jun 5, 2015
1a43f35
Add support for specifying the CSS file path in HTML source renderer.
panhania Jun 5, 2015
01a2e7c
Fix identifier recognition in Haskell source parser.
panhania Jun 5, 2015
ffd0e80
Fix comment recognition in Haskell source parser.
panhania Jun 5, 2015
e5bd5d3
Add support for recognizing compiler pragmas in source parser.
panhania Jun 5, 2015
d275f87
Create scaffolding of module for associating tokens with AST names.
panhania Jun 6, 2015
74de002
Implement utility method for extracting variable identifiers from AST.
panhania Jun 6, 2015
d06a2b5
Create simple mechanism for associating tokens with AST names.
panhania Jun 6, 2015
cb3ece1
Add dummy support for hyperlinking named tokens.
panhania Jun 6, 2015
7d43b8a
Fix span matcher bug causing wrong items being hyperlinked.
panhania Jun 6, 2015
9a51a6d
Constrain elements exported by hyperlinker modules.
panhania Jun 6, 2015
666af8d
Add support for type token recognition.
panhania Jun 7, 2015
7065693
Add support for binding token recognition.
panhania Jun 7, 2015
21984e4
Implement go-to-definition mechanism for local bindings.
panhania Jun 7, 2015
c84a3ef
Implement module export- and import-list item hyperlinking.
panhania Jun 8, 2015
fab61bb
Fix span matching to allow parenthesized operators hyperlinking.
panhania Jun 8, 2015
b31513d
Fix weird hyperlinking of parenthesized operators.
panhania Jun 8, 2015
162b02e
Add support for type declaration anchors.
panhania Jun 11, 2015
c678689
Add support for top-level function declaration anchors.
panhania Jun 11, 2015
1064953
Fix external anchors to contain HTML file extension.
panhania Jun 12, 2015
60db149
Refactor the way AST names are handled within detailed tokens.
panhania Jun 17, 2015
a85224a
Implement hyperlinking of imported module names.
panhania Jun 17, 2015
ebd60c5
Fix parsing of single line comments with broken up newlines.
panhania Jun 17, 2015
a7888ae
Fix bug with improper newline handling.
panhania Jun 17, 2015
45cc27f
Fix issues with escaped newlines in comments.
panhania Jun 18, 2015
61942ce
Add support for parsing C preprocessor macros.
panhania Jun 18, 2015
416c384
Add some documentation for parser module of source hyperlinker.
panhania Jun 21, 2015
937a601
Add some documentation for AST module of source hyperlinker.
panhania Jun 21, 2015
ce4b560
Add command line option for generating hyperlinked source.
panhania Jun 22, 2015
3eb96a6
Extend module interface with rich source token stream field.
panhania Jun 22, 2015
4190a05
Implement source tokenization during interface creation process.
panhania Jun 22, 2015
62d44cd
Create hyperlinker module and plug it into the Haddock pipeline.
panhania Jun 22, 2015
6f16398
Add support for providing custom CSS files for hyperlinked source.
panhania Jun 22, 2015
a6bd86a
Add support for fancy highlighting upon hovering over identifier.
panhania Jun 22, 2015
affd889
Make source hyperlinker generate output in apropriate directory.
panhania Jun 26, 2015
844c09d
Create module with hyperlinker utility functions.
panhania Jun 26, 2015
d58bcf2
Make external hyperlinks point to locations specified by source URLs.
panhania Jun 26, 2015
ab07020
Rewrite source generation to fixed links and directory structure.
panhania Jun 27, 2015
a6eb5a1
Add basic support for cross-package hyperlink generation.
panhania Jun 28, 2015
98cb99c
Disable generating hyperlinks for module references.
panhania Jun 28, 2015
4a6b6c1
Make Haddock generate source for all interfaces (also hidden ones).
panhania Jun 28, 2015
311b3cc
Prevent source parser from throwing exception when lexing fails.
panhania Jun 28, 2015
6cf5e45
Implement workaround for Chrome highlighting issues.
panhania Jun 28, 2015
5a86381
Make hyperlinker generate correct anchors for data constructors.
panhania Jun 29, 2015
46b1520
Make hyperlinker generate anchors for record field declarations.
panhania Jun 29, 2015
671e7dc
Fix issue with hyperlink highlight styling in Chrome browser.
panhania Jun 29, 2015
d6cfd26
Add support for hyperlinking constructor names in patters.
panhania Jun 29, 2015
a1d3cb1
Add support for hyperlinking field names in record patterns.
panhania Jun 29, 2015
7d26944
Add support for hyperlinking field names in record expressions.
panhania Jun 29, 2015
6bebd57
Make hyperlinker respect pretty-printer flag and add documentation.
panhania Jun 30, 2015
fe22eda
Unexpose hyperlinker modules in Cabal configuration.
panhania Jun 30, 2015
d44fc5b
Setup HSpec framework for Haddock API package.
panhania Jun 27, 2015
f3d1f3c
Add basic tests related to comment parsing.
panhania Jun 27, 2015
86ccbbf
Add tests related to parsing basic language constructs.
panhania Jun 28, 2015
2426a64
Add simple tests for do-notation parsing.
panhania Jun 28, 2015
5ddb425
Add very simple QuickCheck properties for source parser spec.
panhania Jun 28, 2015
5f13457
Create simple test runner for hyperlinker tests.
panhania Jun 30, 2015
3b6cbe3
Add test case for basic identifier hyperlinking.
panhania Jun 30, 2015
15ac1a8
Add test case for operator hyperlinking.
panhania Jun 30, 2015
95dfb7a
Add test case for constructor hyperlinking.
panhania Jun 30, 2015
354d329
Add test case for record expressions and patterns hyperlinking.
panhania Jun 30, 2015
9dfb3f8
Add test case for literal syntax highlighting.
panhania Jun 30, 2015
cddb709
Add hyperlinker test runner to .cabal and .gitignore files.
panhania Jun 30, 2015
beab75b
Adapt hyperlinker test runner to have the same interface as HTML one.
panhania Jun 30, 2015
5da9073
Fix hyperlinker test runner file paths and add pretty-printing option.
panhania Jun 30, 2015
40d0a05
Add reference files for hyperlinker test cases.
panhania Jun 30, 2015
395a9c3
Make hyperlinker test runner strip local links from generated source.
panhania Jun 30, 2015
7675698
Create simple script for accepting hyperlinker test case references.
panhania Jun 30, 2015
db51ad0
Re-accept hyperlinker test cases with local references stripped out.
panhania Jun 30, 2015
a229331
Fix bug with diffing wrong files in hyperlinker test runner.
panhania Jun 30, 2015
4b0b4a8
Remove unused dependencies in Haddock API spec configuration.
panhania Jul 1, 2015
b91ee2f
Add support for hyperlinking synonyms in patterns.
panhania Jul 1, 2015
dc2eed5
Create test case for hyperlinking @-patterns.
panhania Jul 1, 2015
dd781d1
Add support for hyperlinking universally quantified type variables.
panhania Jul 1, 2015
571944f
Create hyperlinker test case with quantified type variables.
panhania Jul 1, 2015
2b748bb
Add scoped type variables test for polymorphism test case.
panhania Jul 1, 2015
d6fcd46
Add record wildcards test for records hyperlinking test case.
panhania Jul 1, 2015
980664b
Document some functions in XHTML utlity module.
panhania Jul 1, 2015
868248d
Make hyperlinker render qualified names as one entity.
panhania Jul 1, 2015
8071c27
Add qualified name test for identifiers hyperlinking test case.
panhania Jul 1, 2015
0d0550c
Fix crash happening when hyperlinking type family declarations.
panhania Jul 2, 2015
5c01af0
Add support for anchoring data family constructor declarations.
panhania Jul 2, 2015
28e93ce
Improve support for hyperlinking type families.
panhania Jul 2, 2015
0ea2c4a
Add hyperlinker test case for checking type and type family declarati…
panhania Jul 2, 2015
aa6c6de
Fix issue with operators being recognized as preprocessor directives.
panhania Jul 2, 2015
257e045
Fix broken tests for parsing and hyperlinking hash operators.
panhania Jul 2, 2015
d761512
Add support for anchoring signatures in type class declarations.
panhania Jul 2, 2015
ef3b869
Make hyperlinker generate anchors only to top-level value bindings.
panhania Jul 2, 2015
29bb1ce
Create hyperlinker test case for type classes.
panhania Jul 2, 2015
9c156cd
Update docs with information about source hyperlinking.
panhania Jul 4, 2015
820381b
Update docs on using `--read-interface` option.
panhania Jul 4, 2015
a861470
Remove potentially dangerous record access in hyperlinker AST module.
panhania Jul 4, 2015
cab1191
Make Haddock generate warnings about potential misuse of hyperlinker.
panhania Jul 4, 2015
861c45b
Fix incorrect specification of source style option in doc file.
panhania Jul 4, 2015
99980dc
Refactor source path mapping to use modules as indices.
panhania Jul 5, 2015
5927bfd
Fix bug where not all module interfaces were added to source mapping.
panhania Jul 5, 2015
fcaa46b
Extract main hyperlinker types to separate module.
panhania Jul 6, 2015
1325460
Move source paths types to hyperlinker types module.
panhania Jul 6, 2015
bbd036a
Add support for hyperlinking modules in import lists.
panhania Jul 6, 2015
b6e9968
Add short documentation for hyperlinker source map type.
panhania Jul 6, 2015
0e1cad7
Fix bug with module name being hyperlinked to `Prelude`.
panhania Jul 6, 2015
d76c57b
Fix problem with spec build in Haddock API configuration.
panhania Jul 6, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/haddock-api/dist/
/haddock-library/dist/
/html-test/out/
/hypsrc-test/out/
/latex-test/out/

/doc/haddock
Expand Down
73 changes: 66 additions & 7 deletions doc/haddock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,31 +345,53 @@
<varlistentry>
<term>
<indexterm><primary><option>-i</option></primary></indexterm>
<option>-i</option> <replaceable>path</replaceable>,<replaceable>file</replaceable>
<option>-i</option> <replaceable>file</replaceable>
</term>
<term>
<indexterm><primary><option>-i</option></primary></indexterm>
<option>-i</option> <replaceable>docpath</replaceable>,<replaceable>file</replaceable>
</term>
<term>
<indexterm><primary><option>-i</option></primary></indexterm>
<option>-i</option> <replaceable>docpath</replaceable>,<replaceable>srcpath</replaceable>,<replaceable>file</replaceable>
</term>
<term>
<indexterm><primary><option>--read-interface</option></primary></indexterm>
<option>--read-interface</option>=<replaceable>file</replaceable>
</term>
<term>
<indexterm><primary><option>--read-interface</option></primary></indexterm>
<option>--read-interface</option>=<replaceable>path</replaceable>,<replaceable>file</replaceable>
<option>--read-interface</option>=<replaceable>docpath</replaceable>,<replaceable>file</replaceable>
</term>
<term>
<indexterm><primary><option>--read-interface</option></primary></indexterm>
<option>--read-interface</option>=<replaceable>docpath</replaceable>,<replaceable>srcpath</replaceable>,<replaceable>file</replaceable>
</term>
<listitem>
<para>Read the interface file in
<replaceable>file</replaceable>, which must have been
produced by running Haddock with the
<option>--dump-interface</option> option. The interface
describes a set of modules whose HTML documentation is
located in <replaceable>path</replaceable> (which may be a
relative pathname). The <replaceable>path</replaceable> is
optional, and defaults to <quote>.</quote>.</para>
located in <replaceable>docpath</replaceable> (which may be a
relative pathname). The <replaceable>docpath</replaceable> is
optional, and defaults to <quote>.</quote>. The
<replaceable>srcpath</replaceable> is optional but has no default
value.</para>

<para>This option allows Haddock to produce separate sets of
documentation with hyperlinks between them. The
<replaceable>path</replaceable> is used to direct hyperlinks
<replaceable>docpath</replaceable> is used to direct hyperlinks
to point to the right files; so make sure you don't move the
HTML files later or these links will break. Using a
relative <replaceable>path</replaceable> means that a
relative <replaceable>docpath</replaceable> means that a
documentation subtree can still be moved around without
breaking links.</para>

<para>Similarly to <replaceable>docpath</replaceable>, <replaceable>srcpath</replaceable> is used generate cross-package hyperlinks but
within sources rendered with <option>--hyperlinked-source</option>
option.</para>

<para>Multiple <option>--read-interface</option> options may
be given.</para>
</listitem>
Expand Down Expand Up @@ -528,6 +550,43 @@ $ pdflatex <replaceable>package</replaceable>.tex</screen>
</listitem>
</varlistentry>

<varlistentry>
<term>
<indexterm><primary><option>--hyperlinked-source</option></primary></indexterm>
<option>--hyperlinked-source</option>
</term>
<listitem>
<para>Generate hyperlinked source code (as HTML web page). All
rendered files will be put into
<filename class='directory'>src/</filename> subfolder of output
directory.</para>
<para>Usually, this should be used in combination with
<option>--html</option> option - generated documentation will then
contain references to appropriate code fragments. Previously, this
behaviour could be achieved by generating sources using external
tool and specifying <option>--source-base</option>,
<option>--source-module</option>, <option>--source-entity</option>
and related options. Note that these flags are ignored once
<option>--hyperlinked-source</option> is set.</para>
<para>In order to make cross-package source hyperlinking possible,
appropriate source paths have to be set up when providing
interface files using <option>--read-interface</option>
option.</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
<indexterm><primary><option>--source-css</option></primary></indexterm>
<option>--source-css=<replaceable>style</replaceable></option>
</term>
<listitem>
<para>Use custom CSS file for sources rendered by the
<option>--hyperlinked-source</option> option. If no custom style
file is provided, Haddock will use default one.</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
<indexterm><primary><option>-S</option></primary></indexterm>
Expand Down
29 changes: 29 additions & 0 deletions haddock-api/haddock-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ stability: experimental
data-dir:
resources
data-files:
html/solarized.css
html/frames.html
html/haddock-util.js
html/highlight.js
html/Classic.theme/haskell_icon.gif
html/Classic.theme/minus.gif
html/Classic.theme/plus.gif
Expand Down Expand Up @@ -79,6 +81,12 @@ library
Haddock.Backends.LaTeX
Haddock.Backends.HaddockDB
Haddock.Backends.Hoogle
Haddock.Backends.Hyperlinker
Haddock.Backends.Hyperlinker.Ast
Haddock.Backends.Hyperlinker.Parser
Haddock.Backends.Hyperlinker.Renderer
Haddock.Backends.Hyperlinker.Types
Haddock.Backends.Hyperlinker.Utils
Haddock.ModuleTree
Haddock.Types
Haddock.Doc
Expand All @@ -89,6 +97,27 @@ library
Haddock.Convert
Paths_haddock_api

test-suite spec
type: exitcode-stdio-1.0
default-language: Haskell2010
main-is: Spec.hs
ghc-options: -Wall

hs-source-dirs:
test
, src

other-modules:
Haddock.Backends.Hyperlinker.ParserSpec

build-depends:
base >= 4.3 && < 4.9
, containers
, ghc >= 7.10 && < 7.10.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think relaxing this upper bound would be good, perhaps to something like 7.10.*. Unless we have a reason to believe that it won't work with 7.10.2, there's no need to constrain it this tightly. In all likelyhood, this will come out after 7.10.2 is released anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just mindlessly copied it from the normal build dependencies. However, I don't think most of these are actually needed for spec so I can safely get rid of them.


, hspec
, QuickCheck == 2.*

source-repository head
type: git
location: https://github.com/haskell/haddock.git
27 changes: 27 additions & 0 deletions haddock-api/resources/html/highlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

var highlight = function (on) {
return function () {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var that = links[i];

if (this.href != that.href) {
continue;
}

if (on) {
that.classList.add("hover-highlight");
} else {
that.classList.remove("hover-highlight");
}
}
}
};

window.onload = function () {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
links[i].onmouseover = highlight(true);
links[i].onmouseout = highlight(false);
}
};
55 changes: 55 additions & 0 deletions haddock-api/resources/html/solarized.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
body {
background-color: #fdf6e3;
}

.hs-identifier {
color: #073642;
}

.hs-identifier.hs-var {
}

.hs-identifier.hs-type {
color: #5f5faf;
}

.hs-keyword {
color: #af005f;
}

.hs-string, .hs-char {
color: #cb4b16;
}

.hs-number {
color: #268bd2;
}

.hs-operator {
color: #d33682;
}

.hs-glyph, .hs-special {
color: #dc322f;
}

.hs-comment {
color: #8a8a8a;
}

.hs-pragma {
color: #2aa198;
}

.hs-cpp {
color: #859900;
}

a:link, a:visited {
text-decoration: none;
border-bottom: 1px solid #eee8d5;
}

a:hover, a.hover-highlight {
background-color: #eee8d5;
}
66 changes: 60 additions & 6 deletions haddock-api/src/Haddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Haddock.Backends.Xhtml
import Haddock.Backends.Xhtml.Themes (getThemes)
import Haddock.Backends.LaTeX
import Haddock.Backends.Hoogle
import Haddock.Backends.Hyperlinker
import Haddock.Interface
import Haddock.Parser
import Haddock.Types
Expand All @@ -45,6 +46,7 @@ import Data.List (isPrefixOf)
import Control.Exception
import Data.Maybe
import Data.IORef
import Data.Map (Map)
import qualified Data.Map as Map
import System.IO
import System.Exit
Expand Down Expand Up @@ -158,6 +160,7 @@ haddockWithGhc ghc args = handleTopExceptions $ do
_ -> return flags

unless (Flag_NoWarnings `elem` flags) $ do
hypSrcWarnings flags
forM_ (warnings args) $ \warning -> do
hPutStrLn stderr warning

Expand Down Expand Up @@ -226,13 +229,16 @@ renderStep dflags flags qual pkgs interfaces = do
let
ifaceFiles = map snd pkgs
installedIfaces = concatMap ifInstalledIfaces ifaceFiles
srcMap = Map.fromList [ (ifPackageKey if_, x) | ((_, Just x), if_) <- pkgs ]
render dflags flags qual interfaces installedIfaces srcMap
extSrcMap = Map.fromList $ do
((_, Just path), ifile) <- pkgs
iface <- ifInstalledIfaces ifile
return (instMod iface, path)
render dflags flags qual interfaces installedIfaces extSrcMap


-- | Render the interfaces with whatever backend is specified in the flags.
render :: DynFlags -> [Flag] -> QualOption -> [Interface] -> [InstalledInterface] -> SrcMap -> IO ()
render dflags flags qual ifaces installedIfaces srcMap = do
render :: DynFlags -> [Flag] -> QualOption -> [Interface] -> [InstalledInterface] -> Map Module FilePath -> IO ()
render dflags flags qual ifaces installedIfaces extSrcMap = do

let
title = fromMaybe "" (optTitle flags)
Expand All @@ -243,6 +249,7 @@ render dflags flags qual ifaces installedIfaces srcMap = do
opt_index_url = optIndexUrl flags
odir = outputDir flags
opt_latex_style = optLaTeXStyle flags
opt_source_css = optSourceCssFile flags

visibleIfaces = [ i | i <- ifaces, OptHide `notElem` ifaceOptions i ]

Expand All @@ -256,10 +263,25 @@ render dflags flags qual ifaces installedIfaces srcMap = do
pkgNameVer = modulePackageInfo dflags flags pkgMod

(srcBase, srcModule, srcEntity, srcLEntity) = sourceUrls flags
srcMap' = maybe srcMap (\path -> Map.insert pkgKey path srcMap) srcEntity

srcModule'
| Flag_HyperlinkedSource `elem` flags = Just hypSrcModuleUrlFormat
| otherwise = srcModule

srcMap = mkSrcMap $ Map.union
(Map.map SrcExternal extSrcMap)
(Map.fromList [ (ifaceMod iface, SrcLocal) | iface <- ifaces ])

pkgSrcMap = Map.mapKeys modulePackageKey extSrcMap
pkgSrcMap'
| Flag_HyperlinkedSource `elem` flags =
Map.insert pkgKey hypSrcModuleNameUrlFormat pkgSrcMap
| Just srcNameUrl <- srcEntity = Map.insert pkgKey srcNameUrl pkgSrcMap
| otherwise = pkgSrcMap

-- TODO: Get these from the interface files as with srcMap
srcLMap' = maybe Map.empty (\path -> Map.singleton pkgKey path) srcLEntity
sourceUrls' = (srcBase, srcModule, srcMap', srcLMap')
sourceUrls' = (srcBase, srcModule', pkgSrcMap', srcLMap')

libDir <- getHaddockLibDir flags
prologue <- getPrologue dflags flags
Expand Down Expand Up @@ -308,6 +330,9 @@ render dflags flags qual ifaces installedIfaces srcMap = do
ppLaTeX title pkgStr visibleIfaces odir (fmap _doc prologue) opt_latex_style
libDir

when (Flag_HyperlinkedSource `elem` flags) $ do
ppHyperlinkedSource odir libDir opt_source_css pretty srcMap ifaces

-- | From GHC 7.10, this function has a potential to crash with a
-- nasty message such as @expectJust getPackageDetails@ because
-- package name and versions can no longer reliably be extracted in
Expand Down Expand Up @@ -476,6 +501,35 @@ shortcutFlags flags = do
++ "Ported to use the GHC API by David Waern 2006-2008\n"


-- | Generate some warnings about potential misuse of @--hyperlinked-source@.
hypSrcWarnings :: [Flag] -> IO ()
hypSrcWarnings flags = do

when (hypSrc && any isSourceUrlFlag flags) $
hPutStrLn stderr $ concat
[ "Warning: "
, "--source-* options are ignored when "
, "--hyperlinked-source is enabled."
]

when (not hypSrc && any isSourceCssFlag flags) $
hPutStrLn stderr $ concat
[ "Warning: "
, "source CSS file is specified but "
, "--hyperlinked-source is disabled."
]

where
hypSrc = Flag_HyperlinkedSource `elem` flags
isSourceUrlFlag (Flag_SourceBaseURL _) = True
isSourceUrlFlag (Flag_SourceModuleURL _) = True
isSourceUrlFlag (Flag_SourceEntityURL _) = True
isSourceUrlFlag (Flag_SourceLEntityURL _) = True
isSourceUrlFlag _ = False
isSourceCssFlag (Flag_SourceCss _) = True
isSourceCssFlag _ = False


updateHTMLXRefs :: [(DocPaths, InterfaceFile)] -> IO ()
updateHTMLXRefs packages = do
writeIORef html_xrefs_ref (Map.fromList mapping)
Expand Down
Loading