commit 0909ced11bb47063499bcf80aa601668fa4a608c
parent 7e8ff47b5b135f88c3a0ef77779690fdfedb4c0b
Author: Matthew Flatt <mflatt@racket-lang.org>
Date: Wed, 28 May 2014 10:52:48 +0100
scriblib/figure: make figure hyperlinks show start of figure
... instead of putting the figure's caption at the top of the
browser window.
I can't decide whether I like the technique here, which is to use
JavaScript to move a figure caption's <a name=...> when the page is
loaded. On the one hand, fixing up HTML via load-time JavaScript code
seems ugly compared to generating the right HTML in the first place.
On the other hand, it simplifies the generatation of a
backend-independent Scribble document (i.e., don't try to generate
different target locations for HTML and Latex), and HTML-specific
behavior seems properly handled at the HTML/JavaScript level.
original commit: 2b33b8d9668988b67f6b68b9eb012eb6d8f417d5
Diffstat:
3 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/pkgs/scribble-pkgs/scribble-lib/scriblib/figure.js b/pkgs/scribble-pkgs/scribble-lib/scriblib/figure.js
@@ -0,0 +1,27 @@
+
+AddOnLoad(function () {
+ /* Lift figure targets to the start of the figure's blockquote,
+ so that clicking on a target reference shows the figure
+ content, instead of scrolling the figure caption to the
+ top of the page. */
+ var targets = document.getElementsByTagName("a");
+ for (var i = 0; i < targets.length; i++) {
+ var a = targets[i];
+ var n = a.attributes["x-target-lift"];
+ if (n) {
+ var s = n.value;
+ var p = a.parentNode;
+ while (p && (p.className != s)) {
+ p = p.parentNode;
+ }
+ if (p) {
+ var cs = p.children;
+ a.parentNode.removeChild(a);
+ if (cs.length > 0)
+ p.insertBefore(a, cs[0]);
+ else
+ p.appendChild(a);
+ }
+ }
+ }
+});
diff --git a/pkgs/scribble-pkgs/scribble-lib/scriblib/figure.rkt b/pkgs/scribble-pkgs/scribble-lib/scriblib/figure.rkt
@@ -47,6 +47,15 @@
(define centertext-style (make-style "Centertext" figure-style-extras))
+;; See "figure.js":
+(define figure-target-style
+ (make-style #f
+ (list
+ (make-attributes '((x-target-lift . "Figure")))
+ (make-js-addition
+ (path->main-collects-relative
+ (collection-file-path "figure.js" "scriblib"))))))
+
(define (make-figure-ref c s)
(element (style "FigureRef" (list* (command-extras (list s))
figure-style-extras))
@@ -100,6 +109,7 @@
(counter-target figures tag
"Figure"
(if continue? " (continued): " ": ")
+ #:target-style figure-target-style
#:continue? continue?))
(define (ref-proc initial)
diff --git a/pkgs/scribble-pkgs/scribble-lib/scriblib/private/counter.rkt b/pkgs/scribble-pkgs/scribble-lib/scriblib/private/counter.rkt
@@ -19,13 +19,14 @@
`(,(string->symbol (format "counter-~a" kind)) ,tag)
`(counter (,(counter-name counter) ,tag ,@kind))))
-(define (counter-target counter tag label
+(define (counter-target counter tag label
+ #:target-style [target-style #f]
#:continue? [continue? #f]
. content)
(let ([content (decode-content content)])
(define c
(make-target-element
- #f
+ target-style
(list
(make-collect-element
#f