Skip to content

Commit db8ae14

Browse files
committed
site: include highlighting for definitions
1 parent e234477 commit db8ae14

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

support/shake/app/Shake/Highlights.hs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE ViewPatterns #-}
23
module Shake.Highlights (renderHighlights) where
34

45
import Control.Monad.Trans
56

67
import qualified Data.Map.Strict as Map
8+
import qualified Data.Set as Set
79
import qualified Data.Text as Text
810
import Data.Traversable
911
import Data.Map.Strict (Map)
12+
import Data.Set (Set)
1013
import Data.Text (Text)
1114
import Data.Maybe
1215
import Data.Char
@@ -21,6 +24,37 @@ import Definitions
2124

2225
import Debug.Trace
2326

27+
-- | Make some 'Text' title-cased.
28+
titleCase :: Text -> Text
29+
titleCase txt =
30+
case Text.uncons txt of
31+
Just (c, cs) -> Text.cons (toUpper c) cs
32+
Nothing -> ""
33+
34+
-- | Construct a title for a highlighting block.
35+
-- The rules for titles are:
36+
-- 1. If the highlighting icon has an @aria-label@, then we use that as the root of the label.
37+
-- 2. If there is no @aria-label@, then we title-case the highlighting class name.
38+
-- 3. If the highlighting block has an @id@ attribute, then title case it, and concatenate it on.
39+
-- This is used to display the definition name in @definition@ blocks.
40+
highlightText
41+
:: Text
42+
-- ^ Highlighting class of the block.
43+
-> [TagTree Text]
44+
-- ^ The highlight SVG for the block.
45+
-> [Attribute Text]
46+
-- ^ Highlight block attributes.
47+
-> Text
48+
highlightText clz svg attr =
49+
fromMaybe (titleCase clz) ariaLabel <>
50+
maybe "" (": " <>) (lookup "id" attr)
51+
where
52+
ariaLabel :: Maybe Text
53+
ariaLabel =
54+
case svg of
55+
(TagBranch _ svgAttr _:_) -> lookup "aria-label" svgAttr
56+
_ -> Nothing
57+
2458
-- | Expands away @<div class="warning">@ and @<details warning>@. Any
2559
-- icon under @support/web/highlights@ is supported; the @definition@
2660
-- icon will additionally include the principal label being defined (the
@@ -40,17 +74,18 @@ renderHighlights input stream = do
4074
isHighlight (xs, "") = isIcon xs
4175
isHighlight _ = Nothing
4276

43-
readIcon icn = do
44-
tree <- parseTree . Text.pack <$> readFile' ("support/web/highlights" </> Text.unpack icn -<.> "svg")
45-
case tree of
46-
(TagBranch _ attr _:_) | Just label <- lookup "aria-label" attr -> pure (tree, label)
47-
_ -> pure (tree, Text.cons (toUpper (Text.head icn)) (Text.tail icn))
48-
iconSpan icn = do
49-
(icon, name') <- readIcon icn
50-
let name = TagText name'
77+
titleCase (Text.uncons -> Just (c, cs)) = Text.cons (toUpper c) cs
78+
titleCase _ = ""
79+
80+
readIcon icn =
81+
parseTree . Text.pack <$> readFile' ("support/web/highlights" </> Text.unpack icn -<.> "svg")
82+
83+
highlightSpan icn attr = do
84+
svg <- readIcon icn
85+
let text = TagText (highlightText icn svg attr)
5186
pure $ TagBranch "span" [("class", "highlight-header")]
52-
[ TagBranch "span" [("class", "highlight-icon")] icon
53-
, TagBranch "span" [("class", "highlight-text")] [TagLeaf name]
87+
[ TagBranch "span" [("class", "highlight-icon")] svg
88+
, TagBranch "span" [("class", "highlight-text")] [TagLeaf text]
5489
]
5590

5691
go :: TagTree Text -> Action (TagTree Text)
@@ -59,15 +94,15 @@ renderHighlights input stream = do
5994
let clzs = Text.words clz,
6095
[icn] <- mapMaybe isIcon clzs
6196
= do
62-
icon <- iconSpan icn
97+
icon <- highlightSpan icn attr
6398
children <- traverse go children
6499
pure $ TagBranch "div" (("class", "highlighted " <> clz):attr) $ icon:children
65100

66101
go t@(TagBranch "details" attr children)
67102
| Just (sattr, schild, rest) <- summary children
68103
, [icn] <- mapMaybe isHighlight attr
69104
= do
70-
icon <- iconSpan icn
105+
icon <- highlightSpan icn attr
71106
schild <- traverse go schild
72107
rest <- traverse go rest
73108
pure $ TagBranch "details" (("class", icn):attr) $

support/web/css/components/highlight.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ span.highlight-header {
4747
@include highlight(terminology);
4848
@include highlight(source);
4949
@include highlight(summary);
50+
@include highlight(definition);

support/web/css/theme.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ $theme: (
5252
text: (light: $cyan-700, dark: $cyan-600),
5353
),
5454

55+
definition: (
56+
bg: (light: $bluegray-600, dark: $bluegray-400),
57+
icon: (light: $bluegray-700, dark: $bluegray-300),
58+
text: (light: $bluegray-700, dark: $bluegray-300),
59+
),
60+
5561
commit: (
5662
bg: (light: $violet-500, dark: $violet-400),
5763
),

0 commit comments

Comments
 (0)