Created
April 21, 2011 16:39
-
-
Save FooBarWidget/934945 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From c14a9b57c02a8301358c989951b5c1fa97a83130 Mon Sep 17 00:00:00 2001 | |
From: Hongli Lai (Phusion) <[email protected]> | |
Date: Wed, 6 Apr 2011 13:34:43 +0200 | |
Subject: [PATCH] Improve XHTML backend's table of contents generation latency. Instead of generating the TOC after window.onload, generate it every 500 msec until window.onload or until the DOM is ready. | |
--- | |
javascripts/asciidoc-xhtml11.js | 75 +++++++++++++++++++++++++++++++++++---- | |
xhtml11.conf | 4 +- | |
2 files changed, 70 insertions(+), 9 deletions(-) | |
diff --git a/javascripts/asciidoc-xhtml11.js b/javascripts/asciidoc-xhtml11.js | |
index 30d738a..2ad6c41 100644 | |
--- a/javascripts/asciidoc-xhtml11.js | |
+++ b/javascripts/asciidoc-xhtml11.js | |
@@ -59,6 +59,25 @@ toc: function (toclevels) { | |
} | |
var toc = document.getElementById("toc"); | |
+ if (!toc) { | |
+ return; | |
+ } | |
+ | |
+ // Delete existing TOC entries in case we're reloading the TOC. | |
+ var tocEntriesToRemove = []; | |
+ var i; | |
+ for (i = 0; i < toc.childNodes.length; i++) { | |
+ var entry = toc.childNodes[i]; | |
+ if (entry.nodeName == 'DIV' | |
+ && entry.getAttribute("class") | |
+ && entry.getAttribute("class").match(/^toclevel/)) | |
+ tocEntriesToRemove.push(entry); | |
+ } | |
+ for (i = 0; i < tocEntriesToRemove.length; i++) { | |
+ toc.removeChild(tocEntriesToRemove[i]); | |
+ } | |
+ | |
+ // Rebuild TOC entries. | |
var entries = tocEntries(document.getElementById("content"), toclevels); | |
for (var i = 0; i < entries.length; ++i) { | |
var entry = entries[i]; | |
@@ -86,24 +105,44 @@ toc: function (toclevels) { | |
*/ | |
footnotes: function () { | |
- var cont = document.getElementById("content"); | |
+ // Delete existing footnote entries in case we're reloading the footnodes. | |
+ var i; | |
var noteholder = document.getElementById("footnotes"); | |
+ if (!noteholder) { | |
+ return; | |
+ } | |
+ var entriesToRemove = []; | |
+ for (i = 0; i < noteholder.childNodes.length; i++) { | |
+ var entry = noteholder.childNodes[i]; | |
+ if (entry.nodeName == 'DIV' && entry.getAttribute("class") == "footnote") | |
+ entriesToRemove.push(entry); | |
+ } | |
+ for (i = 0; i < entriesToRemove.length; i++) { | |
+ noteholder.removeChild(entriesToRemove[i]); | |
+ } | |
+ | |
+ // Rebuild footnote entries. | |
+ var cont = document.getElementById("content"); | |
var spans = cont.getElementsByTagName("span"); | |
var refs = {}; | |
var n = 0; | |
for (i=0; i<spans.length; i++) { | |
if (spans[i].className == "footnote") { | |
n++; | |
- // Use [\s\S] in place of . so multi-line matches work. | |
- // Because JavaScript has no s (dotall) regex flag. | |
- note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1]; | |
+ var note = spans[i].getAttribute("data-note"); | |
+ if (!note) { | |
+ // Use [\s\S] in place of . so multi-line matches work. | |
+ // Because JavaScript has no s (dotall) regex flag. | |
+ note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1]; | |
+ spans[i].innerHTML = | |
+ "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n + | |
+ "' title='View footnote' class='footnote'>" + n + "</a>]"; | |
+ spans[i].setAttribute("data-note", note); | |
+ } | |
noteholder.innerHTML += | |
"<div class='footnote' id='_footnote_" + n + "'>" + | |
"<a href='#_footnoteref_" + n + "' title='Return to text'>" + | |
n + "</a>. " + note + "</div>"; | |
- spans[i].innerHTML = | |
- "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n + | |
- "' title='View footnote' class='footnote'>" + n + "</a>]"; | |
var id =spans[i].getAttribute("id"); | |
if (id != null) refs["#"+id] = n; | |
} | |
@@ -123,6 +162,28 @@ footnotes: function () { | |
} | |
} | |
} | |
+}, | |
+ | |
+install: function(toclevels) { | |
+ var timerId; | |
+ | |
+ function reinstall() { | |
+ asciidoc.footnotes(); | |
+ if (toclevels) { | |
+ asciidoc.toc(toclevels); | |
+ } | |
+ } | |
+ | |
+ function reinstallAndRemoveTimer() { | |
+ clearInterval(timerId); | |
+ reinstall(); | |
+ } | |
+ | |
+ timerId = setInterval(reinstall, 500); | |
+ if (document.addEventListener) | |
+ document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false); | |
+ else | |
+ window.onload = reinstallAndRemoveTimer; | |
} | |
} | |
diff --git a/xhtml11.conf b/xhtml11.conf | |
index 558fb65..073639a 100644 | |
--- a/xhtml11.conf | |
+++ b/xhtml11.conf | |
@@ -558,7 +558,7 @@ ifdef::linkcss[] | |
<script type="text/javascript"> | |
# Escape as CDATA to pass validators. | |
/*<![CDATA[*/ | |
-window.onload = function()\{asciidoc.footnotes();{toc? asciidoc.toc({toclevels});}\} | |
+asciidoc.install({toc?{toclevels}}); | |
/*]]>*/ | |
</script> | |
<script type="text/javascript" src="{scriptsdir=.}/asciidoc-xhtml11.js"></script> | |
@@ -567,8 +567,8 @@ ifndef::linkcss[] | |
<script type="text/javascript"> | |
# Escape as CDATA to pass validators. | |
/*<![CDATA[*/ | |
-window.onload = function()\{asciidoc.footnotes();{toc? asciidoc.toc({toclevels});}\} | |
include1::{scriptsdir=./javascripts}/asciidoc-xhtml11.js[] | |
+asciidoc.install({toc?{toclevels}}); | |
/*]]>*/ | |
</script> | |
endif::linkcss[] | |
-- | |
1.7.4.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment