Skip to content

Commit 5cde36a

Browse files
Replace highlight.js with static highlighting using Pandoc
Co-authored-by: tauomicronmu <[email protected]>
1 parent c0e1b7b commit 5cde36a

File tree

5 files changed

+57
-24
lines changed

5 files changed

+57
-24
lines changed

message-index/css/highlight.css

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
1-
pre code.hljs {
2-
display: block;
3-
overflow-x: auto;
4-
padding: 1em
5-
}
6-
7-
code.hljs {
8-
padding: 3px 5px
9-
}
1+
/* This is based on the output of running Pandoc with the `--standalone` flag and
2+
* `--highlight-style pygments`, with hardcoded colors replaced with CSS
3+
* variables.
4+
*
5+
* The names referred to in the comments are from: https://docs.kde.org/trunk5/en/kate/katepart/highlight.html
6+
* (See header "Available Default Styles".)
7+
*/
8+
code span.al { color: var(); font-weight: bold; } /* Alert */
9+
code span.an { color: var(--code-comment-color); font-weight: bold; font-style: italic; } /* Annotation */
10+
code span.at { color: var(--code-pragma-color); } /* Attribute */
11+
code span.bn { color: var(--code-literal-color); } /* BaseN */
12+
code span.bu { color: var(--code-name-color); } /* BuiltIn */
13+
code span.cf { color: var(--code-kw-color); font-weight: bold; } /* ControlFlow */
14+
code span.ch { color: var(--code-literal-color); } /* Char */
15+
code span.cn { color: var(--code-symbol-color); } /* Constant */
16+
code span.co { color: var(--code-comment-color); font-style: italic; } /* Comment */
17+
code span.cv { color: var(--code-comment-color); font-weight: bold; font-style: italic; } /* CommentVar */
18+
code span.do { color: var(--code-comment-color); font-style: italic; } /* Documentation */
19+
code span.dt { color: var(--code-constructor-color); } /* DataType */
20+
code span.dv { color: var(--code-literal-color); } /* DecVal */
21+
code span.er { color: var(); font-weight: bold; } /* Error */
22+
code span.ex { } /* Extension */
23+
code span.fl { color: var(--code-literal-color); } /* Float */
24+
code span.fu { color: var(--code-name-color); } /* Function */
25+
code span.im { color: var(--code-kw-color); font-weight: bold; } /* Import */
26+
code span.in { color: var(); font-weight: bold; font-style: italic; } /* Information */
27+
code span.kw { color: var(--code-kw-color); font-weight: bold; } /* Keyword */
28+
code span.op { color: var(--code-symbol-color); } /* Operator */
29+
code span.ot { color: var(--code-color); } /* Other */
30+
code span.pp { color: var(--code-comment-color); } /* Preprocessor */
31+
code span.sc { color: var(--code-literal-color); } /* SpecialChar */
32+
code span.ss { color: var(--code-string-color); } /* SpecialString */
33+
code span.st { color: var(--code-string-color); } /* String */
34+
code span.va { color: var(--code-attr-color); } /* Variable */
35+
code span.vs { color: var(--code-string-color); } /* VerbatimString */
36+
code span.wa { color: var(); font-weight: bold; font-style: italic; } /* Warning */
1037

38+
/*
1139
.hljs {
1240
color: var(--code-color);
1341
background: var(--code-bg-color);
@@ -65,3 +93,5 @@ code.hljs {
6593
.hljs-link {
6694
text-decoration: underline
6795
}
96+
97+
*/

message-index/message-index.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ executable site
1212
, microlens ^>= 0.4.12
1313
, binary ^>= 0.8.8
1414
, aeson ^>= 2.0.3 || ^>= 2.1
15+
, pandoc ^>= 3.1.3
1516
, pandoc-types ^>= 1.22 || ^>= 1.23
1617
, containers ^>= 0.6
1718
, text ^>= 1.2 || ^>= 2.0

message-index/site.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import Hakyll
2222
import Lens.Micro (_1, _2, _3)
2323
import Lens.Micro.Extras (view)
2424
import System.FilePath
25-
import Text.Pandoc.Definition (Meta (..), MetaValue (..), Pandoc (..))
25+
import qualified Text.Pandoc as Pandoc
26+
import qualified Text.Pandoc.Definition as Pandoc
2627

2728
main :: IO ()
2829
main = hakyll $ do
@@ -76,13 +77,9 @@ main = hakyll $ do
7677
( mconcat
7778
[ indexlessUrlField "url",
7879
field "name" (pure . view _1 . itemBody),
79-
-- Set the language that highlight.js should use for syntax highlighting
80-
field "language" $ \(itemBody -> (filename, _, _)) ->
81-
pure $ case dropWhile (== '.') $ takeExtension filename of
82-
"hs" -> "haskell"
83-
other -> other,
84-
field "before" (maybe (pure "<not present>") (fmap itemBody . load . itemIdentifier) . view _2 . itemBody),
85-
field "after" (maybe (pure "<not present>") (fmap itemBody . load . itemIdentifier) . view _3 . itemBody)
80+
-- TODO: pick the right language
81+
field "before" (maybe (pure "<not present>") (fmap (T.unpack . highlightHaskell . T.pack) . fmap itemBody . load . itemIdentifier) . view _2 . itemBody),
82+
field "after" (maybe (pure "<not present>") (fmap (T.unpack . highlightHaskell . T.pack) . fmap itemBody . load . itemIdentifier) . view _3 . itemBody)
8683
]
8784
)
8885
(return files),
@@ -287,3 +284,13 @@ indexless url
287284
where
288285
lru = reverse url
289286
toDrop = "index.html"
287+
288+
highlightHaskell :: T.Text -> T.Text
289+
highlightHaskell code =
290+
let writerOptions = Pandoc.def
291+
-- We make a fake Pandoc document that's just the code embedded in a code block.
292+
document =
293+
Pandoc.Pandoc mempty [Pandoc.CodeBlock ("", ["haskell"], []) code]
294+
in case Pandoc.runPure $ Pandoc.writeHtml5String writerOptions document of
295+
Left err -> error $ "Unexpected Pandoc error: " ++ show err
296+
Right html -> html

message-index/templates/default.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<meta name="robots" content="noindex">
99
<title>$title$ — Haskell Error Index</title>
1010
<link rel="stylesheet" href="/css/highlight.css">
11-
<script src="/js/highlight.min.js"></script>
1211
<link rel="stylesheet" href="/css/default.css" />
1312
<link rel="stylesheet" href="/css/theme.css" />
1413
</head>
@@ -35,10 +34,6 @@ <h1>$title$</h1>
3534
<a href="http://jaspervdj.be/hakyll" target="_blank" rel="noopener noreferrer">Hakyll</a>
3635
</p>
3736
</footer>
38-
39-
<script>document.querySelectorAll('code.language-haskell').forEach(el => {
40-
hljs.highlightElement(el);
41-
});</script>
4237
</body>
4338

4439
</html>

message-index/templates/example.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<div class="example-inner">
77
<div class="example-title">Before</div>
88
<!-- keep next line as is, i.e., on one line, or the code will not format properly -->
9-
<pre class="example-pre"><code class="language-$language$">$before$</code></pre>
9+
<pre>$before$</pre>
1010
</div>
1111
</div>
1212
<div class="example">
1313
<div class="example-inner">
1414
<div class="example-title">After</div>
1515
<!-- keep next line as is, i.e., on one line, or the code will not format propertly -->
16-
<pre class="example-pre"><code class="language-$language$">$after$</code></pre>
16+
<pre>$after$</pre>
1717
</div>
1818
</div>
1919
</div>

0 commit comments

Comments
 (0)