Skip to content

Commit f3d5eb0

Browse files
authored
Merge pull request #953 from daira/easier-math-in-rst
Various improvements in the rendering of math and references. rST ZIPs now support $...$ for inline math.
2 parents 399e638 + 96dafb2 commit f3d5eb0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2656
-2365
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*.save
2121
*.save.*
2222
*~
23+
*.html.temp
2324

2425
.Makefile.uptodate
2526
.zipfilelist.*

Makefile

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Dependencies: see zip-guide.rst and protocol/README.rst
22

3+
MARKDOWN_OPTION?=--pandoc
4+
35
.PHONY: all-zips all tag-release protocol all-protocol discard
46
all-zips: .Makefile.uptodate
57
echo "$(patsubst zips/%,%,$(sort $(wildcard zips/zip-*.rst) $(wildcard zips/zip-*.md)))" >.zipfilelist.new
@@ -31,30 +33,18 @@ all-specs: all-zips
3133
discard:
3234
git checkout -- 'rendered/*.html' 'README.rst' 'rendered/protocol/*.pdf'
3335

34-
.Makefile.uptodate: Makefile edithtml.sh
36+
.Makefile.uptodate: Makefile render.sh
3537
$(MAKE) clean
3638
touch .Makefile.uptodate
3739

38-
define PROCESSRST
39-
$(eval TITLE := $(shell echo '$(patsubst zips/%,%,$(basename $<))' | sed -E 's|zip-0{0,3}|ZIP |;s|draft-|Draft |')$(shell grep -E '^(\.\.)?\s*Title: ' $< |sed -E 's|.*Title||'))
40-
rst2html5 -v --title="$(TITLE)" $< >$@
41-
./edithtml.sh --rst $@
42-
endef
43-
44-
define PROCESSMD
45-
$(eval TITLE := $(shell echo '$(patsubst zips/%,%,$(basename $<))' | sed -E 's|zip-0{0,3}|ZIP |;s|draft-|Draft |')$(shell grep -E '^(\.\.)?\s*Title: ' $< |sed -E 's|.*Title||'))
46-
pandoc --from=markdown --to=html $< --output=$@
47-
./edithtml.sh --md $@ "${TITLE}"
48-
endef
49-
50-
rendered/index.html: README.rst edithtml.sh
51-
$(PROCESSRST)
40+
rendered/index.html: README.rst render.sh
41+
./render.sh --rst $< $@
5242

53-
rendered/%.html: zips/%.rst edithtml.sh
54-
$(PROCESSRST)
43+
rendered/%.html: zips/%.rst render.sh
44+
./render.sh --rst $< $@
5545

56-
rendered/%.html: zips/%.md edithtml.sh
57-
$(PROCESSMD)
46+
rendered/%.html: zips/%.md render.sh
47+
./render.sh $(MARKDOWN_OPTION) $< $@
5848

5949
README.rst: .zipfilelist.current .draftfilelist.current makeindex.sh README.template $(wildcard zips/zip-*.rst) $(wildcard zips/zip-*.md) $(wildcard zips/draft-*.rst) $(wildcard zips/draft-*.md)
6050
./makeindex.sh | cat README.template - >README.rst

edithtml.sh

Lines changed: 0 additions & 49 deletions
This file was deleted.

render.sh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
if ! ( ( [ "x$1" = "x--rst" ] || [ "x$1" = "x--pandoc" ] || [ "x$1" = "x--mmd" ] ) && [ $# -eq 3 ] ); then
6+
cat - <<EndOfUsage
7+
Usage: render.sh --rst|--pandoc|--mmd <inputfile> <htmlfile> <title>
8+
9+
--rst render reStructuredText using rst2html5
10+
--pandoc render Markdown using pandoc
11+
--mmd render Markdown using multimarkdown
12+
EndOfUsage
13+
exit
14+
fi
15+
16+
inputfile="$2"
17+
outputfile="$3"
18+
19+
if ! [ -f "${inputfile}" ]; then
20+
echo "File not found: ${inputfile}"
21+
exit
22+
fi
23+
24+
if [ "x$1" = "x--rst" ]; then
25+
filetype='.rst'
26+
else
27+
filetype='.md'
28+
fi
29+
title="$(basename -s ${filetype} ${inputfile} | sed -E 's|zip-0{0,3}|ZIP |; s|draft-|Draft |')$(grep -E '^(\.\.)?\s*Title: ' ${inputfile} |sed -E 's|.*Title||')"
30+
echo " ${title}"
31+
32+
Math1='<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+" crossorigin="anonymous">'
33+
Math2='<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-7zkQWkzuo3B5mTepMUcHkMB5jZaolc2xDwL6VFqjFALcbeS9Ggm/Yr2r3Dy4lfFg" crossorigin="anonymous"></script>'
34+
Math3='<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-43gviWU0YVjaDtb/GhzOouOXtZMP/7XUzwPTstBeZFe/+rCMvRwr4yROQP43s0Xk" crossorigin="anonymous" onload="renderMathInElement(document.body);"></script>'
35+
ViewAndStyle='<meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="css/style.css">'
36+
37+
cat <(
38+
if [ "x$1" = "x--rst" ]; then
39+
# These are basic regexps so \+ is needed, not +.
40+
# We use the Unicode 💲 character to move an escaped $ out of the way,
41+
# which is much easier than trying to handle escapes within a capture.
42+
43+
cat "${inputfile}" \
44+
| sed 's|[\][$]|💲|g;
45+
s|[$]\([^$]\+\)[$]\([.,:;!?)-]\)|:math:`\1\\!`\2|g;
46+
s|[$]\([^$]\+\)[$]|:math:`\1`|g;
47+
s|💲|$|g' \
48+
| rst2html5 -v --title="${title}" - \
49+
| sed "s|<script src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>|${Math1}\n ${Math2}\n ${Math3}|;
50+
s|</head>|${ViewAndStyle}</head>|"
51+
else
52+
if [ "x$1" = "x--pandoc" ]; then
53+
# Not actually MathJax. KaTeX is compatible if we use the right headers.
54+
pandoc --mathjax --from=markdown --to=html "${inputfile}" --output="${outputfile}.temp"
55+
else
56+
multimarkdown ${inputfile} -o "${outputfile}.temp"
57+
fi
58+
59+
# Both pandoc and multimarkdown just output the HTML body.
60+
echo "<!DOCTYPE html>"
61+
echo "<html>"
62+
echo "<head>"
63+
echo " <title>${title}</title>"
64+
echo " <meta charset=\"utf-8\" />"
65+
if grep -q -E 'class="math( inline)?"' "${outputfile}.temp"; then
66+
echo " ${Math1}"
67+
echo " ${Math2}"
68+
echo " ${Math3}"
69+
fi
70+
echo " ${ViewAndStyle}"
71+
echo "</head>"
72+
echo "<body>"
73+
cat "${outputfile}.temp"
74+
rm -f "${outputfile}.temp"
75+
echo "</body>"
76+
echo "</html>"
77+
fi
78+
) \
79+
| sed \
80+
's|<a href="[^":]*">Protocol Specification</a>|<span class="lightmode"><a href="https://zips.z.cash/protocol/protocol.pdf">Protocol Specification</a></span>|g;
81+
s|\s*<a href="[^":]*">(dark mode version)</a>|<span class="darkmode" style="display: none;"><a href="https://zips.z.cash/protocol/protocol-dark.pdf">Protocol Specification</a></span>|g;
82+
s|<a \(class=[^ ]* \)*href="\([^":]*\)\.rst\(\#[^"]*\)*">|<a \1href="\2\3">|g;
83+
s|<a \(class=[^ ]* \)*href="\([^":]*\)\.md\(\#[^"]*\)*">|<a \1href="\2\3">|g;
84+
s|&lt;\(https:[^&]*\)&gt;|\&lt;<a href="\1">\1</a>\&gt;|g;
85+
s|src="../rendered/|src="|g;
86+
s|<a href="rendered/|<a href="|g;
87+
s|<a \(class=[^ ]* \)*href="zips/|<a \1href="|g' \
88+
| perl -p0e \
89+
's|<section id="([^"]*)">\s*.?\s*<h([1-9])>([^<]*(?:<code>[^<]*</code>[^<]*)?)</h([1-9])>|<section id="\1"><h\2><span class="section-heading">\3</span><span class="section-anchor"> <a rel="bookmark" href="#\1"><img width="24" height="24" class="section-anchor" src="assets/images/section-anchor.png" alt=""></a></span></h\4>|g' \
90+
> "${outputfile}"

rendered/css/style.css

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -296,28 +296,18 @@ pre {
296296
font-size: 0.9375rem;
297297
}
298298

299-
span.math {
300-
transform: scale(1, 1.03);
301-
-moz-transform: scale(1, 1.03);
302-
-ms-transform: scale(1, 1.03);
303-
-webkit-transform: scale(1, 1.03);
304-
-o-transform: scale(1, 1.03);
305-
font-size: 0.97rem;
299+
.katex {
300+
font-size: 1.21em;
306301
}
307302

308303
div.math {
309-
transform: scale(1.3, 1.339);
310-
-moz-transform: scale(1.3, 1.339);
311-
-ms-transform: scale(1.3, 1.339);
312-
-webkit-transform: scale(1.3, 1.339);
313-
-o-transform: scale(1.3, 1.339);
304+
transform: scale(1.42, 1.42);
314305
display: block;
315306
overflow-x: auto;
316307
overflow-y: hidden;
317308
margin: 2.6rem 1rem 2.6rem 1rem;
318309
text-align: center;
319310
padding: 0;
320-
font-size: 0.97rem;
321311
}
322312

323313
a, a:visited {
@@ -344,12 +334,76 @@ span.section-heading:hover + span {
344334
opacity: 1;
345335
}
346336

347-
a.footnote_reference::before {
337+
a.footnote_reference::before, a.footnote-ref::before {
348338
content: "[";
349339
}
350-
a.footnote_reference::after {
340+
341+
a.footnote_reference::after, a.footnote-ref::after {
342+
content: "]";
343+
}
344+
345+
/* {{{ rst-specific */
346+
#references table, #references th, #references td {
347+
border: 0 none transparent;
348+
font-size: 1.125rem;
349+
}
350+
351+
#references table {
352+
margin-left: 0;
353+
margin-bottom: 0;
354+
}
355+
356+
#references th {
357+
padding-left: 0;
358+
width: 1.9em;
359+
text-align: right;
360+
}
361+
362+
#references th::before {
363+
content: "[";
364+
}
365+
366+
#references th::after {
351367
content: "]";
352368
}
369+
/* }}} rst-specific */
370+
371+
/* {{{ md-specific */
372+
a.footnote-ref sup {
373+
vertical-align: baseline;
374+
font-size: 100%;
375+
}
376+
377+
/* pandoc only, not multimarkdown */
378+
#footnotes ol {
379+
margin-top: -3ex;
380+
}
381+
382+
.footnotes li {
383+
margin-top: -0.8ex;
384+
margin-left: 1em;
385+
list-style-type: md-references;
386+
}
387+
388+
.footnotes ::marker {
389+
font-weight: 600;
390+
font-size: 1.125rem;
391+
}
392+
393+
.footnotes hr {
394+
display: none;
395+
}
396+
397+
.footnote-back::before {
398+
content: " ";
399+
}
400+
401+
@counter-style md-references {
402+
system: extends decimal;
403+
prefix: "[";
404+
suffix: "]     ";
405+
}
406+
/* }}} md-specific */
353407

354408
strong, b {
355409
font-family: 'robotomedium',Arial,Helvetica Neue,Helvetica,sans-serif;
@@ -361,7 +415,6 @@ hr {
361415
margin: 1.875rem 0;
362416
}
363417

364-
365418
table {
366419
border-collapse: collapse;
367420
border: 0 none transparent;
@@ -399,22 +452,6 @@ td:first-child {
399452
padding-bottom: 0.4rem;
400453
}
401454

402-
#references table, #references th, #references td {
403-
border: 0 none transparent;
404-
font-size: 1.125rem;
405-
}
406-
407-
#references table {
408-
margin-bottom: 0;
409-
}
410-
411-
#references th::before {
412-
content: "[";
413-
}
414-
#references th::after {
415-
content: "]";
416-
}
417-
418455
@media (max-width: 576px) {
419456
table:not(.footnote) {
420457
display: block;

0 commit comments

Comments
 (0)