Oops, need to quote that define call.
[mediawiki.git] / skins / htmldump / lookup.js
blob5fd8d019a1311a87bfc7241573390ad20655a9e2
1 /**
2  * "Go" function for static HTML dump
3  */
4 function goToStatic(depth) {
5         var url = getStaticURL(document.getElementById("searchInput").value, depth);
6         if (url != "") {
7                 location = url;
8         } else {
9                 alert("Invalid title");
10         }
13 /**
14  * Determine relative path for a given non-canonical title
15  */
16 function getStaticURL(text, depth) {
17         var pdbk = getPDBK(text);
18         if (pdbk == "") {
19                 return "";
20         } else {
21                 var i;
22                 var path = getHashedDirectory(pdbk, depth) + "/" + getFriendlyName(pdbk) + ".html";
23                 if (!/(index\.html|\/)$/.exec(location)) {
24                         for (i = 0; i < depth; i++) {
25                                 path = "../" + path;
26                         }
27                 }
28                 return path;
29         }
32 function getPDBK(text) {
33         // Spaces to underscores
34         text = text.replace(/ /g, "_");
36         // Trim leading and trailing space
37         text = text.replace(/^_+/g, "");
38         text = text.replace(/_+$/g, "");
40         // Capitalise first letter
41         return ucfirst(text);
44 function getHashedDirectory(pdbk, depth) {
45         // Find the first colon if there is one, use characters after it
46         var dbk = pdbk.replace(/^[^:]*:_*(.*)$/, "$1");
47         var i, c, dir = "";
49         for (i=0; i < depth; i++) {
50                 if (i) {
51                         dir += "/";
52                 }
53                 if (i >= dbk.length) {
54                         dir += "_";
55                 } else {
56                         c = dbk.charAt(i);
57                         cc = dbk.charCodeAt(i);
58                         
59                         if (cc >= 128 || /[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/.exec(c)) {
60                                 dir += c.toLowerCase();
61                         } else {
62                                 dir += binl2hex([cc]).substr(0,2).toUpperCase();
63                         }
64                 }
65         }
66         return dir;
69 function ucfirst(s) {
70         return s.charAt(0).toUpperCase() + s.substring(1, s.length);
73 function getFriendlyName(name) {
74         // Replace illegal characters for Windows paths with underscores
75         var friendlyName = name.replace(/[\/\\*?"<>|~]/g, "_");
77         // Work out lower case form. We assume we're on a system with case-insensitive
78         // filenames, so unless the case is of a special form, we have to disambiguate
79         var lowerCase = ucfirst(name.toLowerCase());
81         // Make it mostly unique
82         if (lowerCase != friendlyName) {
83                 friendlyName += "_" + hex_md5(_to_utf8(name)).substring(0, 4);
84         }
85         // Handle colon specially by replacing it with tilde
86         // Thus we reduce the number of paths with hashes appended
87         friendlyName = friendlyName.replace(":", "~");
89         return friendlyName;