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

Commit ad94ddf

Browse files
phadejalexbiehl
authored andcommitted
Grid Tables (#718)
* Add table examples * Add table types and adopt simple parser Simple parser is done by Giovanni Cappellotto (@potomak) in #577 It seems to support single fine full tables, so far from full RST-grid tables, but it's good start. Table type support row- and colspans, but obviously parser is lacking. Still TODO: - Latex backend. Should we use multirow package https://ctan.org/pkg/multirow?lang=en? - Hoogle backend: ? * Implement grid-tables * Refactor table parser * Add two ill-examples * Update CHANGES.md * Basic documentation for tables * Fix documentation example
1 parent 022b5ef commit ad94ddf

File tree

29 files changed

+996
-7
lines changed

29 files changed

+996
-7
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* to be released
44

5+
* Haddock now supports tables in documentation inspired by reSTs grid tables
6+
57
* A --reexport flag, which can be used to add extra modules to the
68
top-level module tree
79

doc/markup.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,26 @@ If the output format supports it, the mathematics will be rendered
10781078
inside the documentation. For example, the HTML backend will display
10791079
the mathematics via `MathJax <https://www.mathjax.org>`__.
10801080

1081+
Grid Tables
1082+
~~~~~~~~~~~
1083+
1084+
Inspired by reSTs grid tables Haddock supports a complete table representation via a grid-like "ASCII art". Grid tables are described with a visual grid made up of the characters "-", "=", "|", and "+". The hyphen ("-") is used for horizontal lines (row separators). The equals sign ("=") may be used to separate optional header rows from the table body. The vertical bar ("|") is used for vertical lines (column separators). The plus sign ("+") is used for intersections of horizontal and vertical lines. ::
1085+
1086+
-- | This is a grid table:
1087+
--
1088+
-- +------------------------+------------+----------+----------+
1089+
-- | Header row, column 1 | Header 2 | Header 3 | Header 4 |
1090+
-- | (header rows optional) | | | |
1091+
-- +========================+============+==========+==========+
1092+
-- | body row 1, column 1 | column 2 | column 3 | column 4 |
1093+
-- +------------------------+------------+----------+----------+
1094+
-- | body row 2 | Cells may span columns. |
1095+
-- +------------------------+------------+---------------------+
1096+
-- | body row 3 | Cells may | \[ |
1097+
-- +------------------------+ span rows. | f(n) = \sum_{i=1} |
1098+
-- | body row 4 | | \] |
1099+
-- +------------------------+------------+---------------------+
1100+
10811101
Anchors
10821102
~~~~~~~
10831103

haddock-api/resources/html/Classic.theme/xhaddock.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,20 @@ td.rdoc p {
392392
}
393393

394394

395+
.doc table {
396+
border-collapse: collapse;
397+
border-spacing: 0px;
398+
}
399+
400+
.doc th,
401+
.doc td {
402+
padding: 5px;
403+
border: 1px solid #ddd;
404+
}
405+
406+
.doc th {
407+
background-color: #f0f0f0;
408+
}
395409

396410
#footer {
397411
background-color: #000099;

haddock-api/resources/html/Ocean.std-theme/ocean.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,21 @@ div#style-menu-holder {
443443
margin-top: 0.8em;
444444
}
445445

446+
.doc table {
447+
border-collapse: collapse;
448+
border-spacing: 0px;
449+
}
450+
451+
.doc th,
452+
.doc td {
453+
padding: 5px;
454+
border: 1px solid #ddd;
455+
}
456+
457+
.doc th {
458+
background-color: #f0f0f0;
459+
}
460+
446461
.clearfix:after {
447462
clear: both;
448463
content: " ";

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ markupTag dflags = Markup {
328328
markupAName = const $ str "",
329329
markupProperty = box TagPre . str,
330330
markupExample = box TagPre . str . unlines . map exampleToString,
331-
markupHeader = \(Header l h) -> box (TagInline $ "h" ++ show l) h
331+
markupHeader = \(Header l h) -> box (TagInline $ "h" ++ show l) h,
332+
markupTable = \(Table _ _) -> str "TODO: table"
332333
}
333334

334335

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,8 @@ parLatexMarkup ppId = Markup {
11391139
markupAName = \_ _ -> empty,
11401140
markupProperty = \p _ -> quote $ verb $ text p,
11411141
markupExample = \e _ -> quote $ verb $ text $ unlines $ map exampleToString e,
1142-
markupHeader = \(Header l h) p -> header l (h p)
1142+
markupHeader = \(Header l h) p -> header l (h p),
1143+
markupTable = \(Table h b) p -> table h b p
11431144
}
11441145
where
11451146
header 1 d = text "\\section*" <> braces d
@@ -1148,6 +1149,8 @@ parLatexMarkup ppId = Markup {
11481149
| l > 0 && l <= 6 = text "\\subsubsection*" <> braces d
11491150
header l _ = error $ "impossible header level in LaTeX generation: " ++ show l
11501151

1152+
table _ _ _ = text "{TODO: Table}"
1153+
11511154
fixString Plain s = latexFilter s
11521155
fixString Verb s = s
11531156
fixString Mono s = latexMonoFilter s

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ parHtmlMarkup qual insertAnchors ppId = Markup {
7373
markupMathDisplay = \mathjax -> toHtml ("\\[" ++ mathjax ++ "\\]"),
7474
markupProperty = pre . toHtml,
7575
markupExample = examplesToHtml,
76-
markupHeader = \(Header l t) -> makeHeader l t
76+
markupHeader = \(Header l t) -> makeHeader l t,
77+
markupTable = \(Table h r) -> makeTable h r
7778
}
7879
where
7980
makeHeader :: Int -> Html -> Html
@@ -85,6 +86,22 @@ parHtmlMarkup qual insertAnchors ppId = Markup {
8586
makeHeader 6 mkup = h6 mkup
8687
makeHeader l _ = error $ "Somehow got a header level `" ++ show l ++ "' in DocMarkup!"
8788

89+
makeTable :: [TableRow Html] -> [TableRow Html] -> Html
90+
makeTable hs bs = table (concatHtml (hs' ++ bs'))
91+
where
92+
hs' | null hs = []
93+
| otherwise = [thead (concatHtml (map (makeTableRow th) hs))]
94+
95+
bs' = [tbody (concatHtml (map (makeTableRow td) bs))]
96+
97+
makeTableRow :: (Html -> Html) -> TableRow Html -> Html
98+
makeTableRow thr (TableRow cs) = tr (concatHtml (map (makeTableCell thr) cs))
99+
100+
makeTableCell :: (Html -> Html) -> TableCell Html -> Html
101+
makeTableCell thr (TableCell i j c) = thr c ! (i' ++ j')
102+
where
103+
i' = if i == 1 then [] else [ colspan i ]
104+
j' = if j == 1 then [] else [ rowspan j ]
88105

89106
examplesToHtml l = pre (concatHtml $ map exampleToHtml l) ! [theclass "screen"]
90107

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ rename dflags gre = rn
143143
DocEmpty -> pure (DocEmpty)
144144
DocString str -> pure (DocString str)
145145
DocHeader (Header l t) -> DocHeader . Header l <$> rn t
146+
DocTable t -> DocTable <$> traverse rn t
146147

147148
-- | Wrap an identifier that's out of scope (i.e. wasn't found in
148149
-- 'GlobalReaderEnv' during 'rename') in an appropriate doc. Currently

haddock-api/src/Haddock/InterfaceFile.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,32 @@ instance Binary a => Binary (Header a) where
464464
t <- get bh
465465
return (Header l t)
466466

467+
instance Binary a => Binary (Table a) where
468+
put_ bh (Table h b) = do
469+
put_ bh h
470+
put_ bh b
471+
get bh = do
472+
h <- get bh
473+
b <- get bh
474+
return (Table h b)
475+
476+
instance Binary a => Binary (TableRow a) where
477+
put_ bh (TableRow cs) = put_ bh cs
478+
get bh = do
479+
cs <- get bh
480+
return (TableRow cs)
481+
482+
instance Binary a => Binary (TableCell a) where
483+
put_ bh (TableCell i j c) = do
484+
put_ bh i
485+
put_ bh j
486+
put_ bh c
487+
get bh = do
488+
i <- get bh
489+
j <- get bh
490+
c <- get bh
491+
return (TableCell i j c)
492+
467493
instance Binary Meta where
468494
put_ bh Meta { _version = v } = put_ bh v
469495
get bh = (\v -> Meta { _version = v }) <$> get bh
@@ -547,6 +573,9 @@ instance (Binary mod, Binary id) => Binary (DocH mod id) where
547573
put_ bh (DocMathDisplay x) = do
548574
putByte bh 22
549575
put_ bh x
576+
put_ bh (DocTable x) = do
577+
putByte bh 23
578+
put_ bh x
550579

551580
get bh = do
552581
h <- getByte bh
@@ -620,6 +649,9 @@ instance (Binary mod, Binary id) => Binary (DocH mod id) where
620649
22 -> do
621650
x <- get bh
622651
return (DocMathDisplay x)
652+
23 -> do
653+
x <- get bh
654+
return (DocTable x)
623655
_ -> error "invalid binary data found in the interface file"
624656

625657

haddock-api/src/Haddock/Types.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ instance (NFData a, NFData mod)
471471
DocProperty a -> a `deepseq` ()
472472
DocExamples a -> a `deepseq` ()
473473
DocHeader a -> a `deepseq` ()
474+
DocTable a -> a `deepseq` ()
474475

475476
#if !MIN_VERSION_ghc(8,0,2)
476477
-- These were added to GHC itself in 8.0.2
@@ -491,6 +492,14 @@ instance NFData Picture where
491492
instance NFData Example where
492493
rnf (Example a b) = a `deepseq` b `deepseq` ()
493494

495+
instance NFData id => NFData (Table id) where
496+
rnf (Table h b) = h `deepseq` b `deepseq` ()
497+
498+
instance NFData id => NFData (TableRow id) where
499+
rnf (TableRow cs) = cs `deepseq` ()
500+
501+
instance NFData id => NFData (TableCell id) where
502+
rnf (TableCell i j c) = i `deepseq` j `deepseq` c `deepseq` ()
494503

495504
exampleToString :: Example -> String
496505
exampleToString (Example expression result) =

0 commit comments

Comments
 (0)