lookup.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }
  11. }
  12. /**
  13. * Determine relative path for a given non-canonical title
  14. */
  15. function getStaticURL(text, depth) {
  16. var pdbk = getPDBK(text);
  17. if (pdbk == "") {
  18. return "";
  19. } else {
  20. var i;
  21. var path = getHashedDirectory(pdbk, depth) + "/" + getFriendlyName(pdbk) + ".html";
  22. if (!/(index\.html|\/)$/.exec(location)) {
  23. for (i = 0; i < depth; i++) {
  24. path = "../" + path;
  25. }
  26. } else {
  27. path = "articles/" + path;
  28. }
  29. return path;
  30. }
  31. }
  32. function getPDBK(text) {
  33. // Spaces to underscores
  34. text = text.replace(/ /g, "_");
  35. // Trim leading and trailing space
  36. text = text.replace(/^_+/g, "");
  37. text = text.replace(/_+$/g, "");
  38. // Capitalise first letter
  39. return ucfirst(text);
  40. }
  41. function getHashedDirectory(pdbk, depth) {
  42. // Find the first colon if there is one, use characters after it
  43. var dbk = pdbk.replace(/^[^:]*:_*(.*)$/, "$1");
  44. var i, c, dir = "";
  45. for (i=0; i < depth; i++) {
  46. if (i) {
  47. dir += "/";
  48. }
  49. if (i >= dbk.length) {
  50. dir += "_";
  51. } else {
  52. c = dbk.charAt(i);
  53. cc = dbk.charCodeAt(i);
  54. if (cc >= 128 || /[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/.exec(c)) {
  55. dir += c.toLowerCase();
  56. } else {
  57. dir += binl2hex([cc]).substr(0,2).toUpperCase();
  58. }
  59. }
  60. }
  61. return dir;
  62. }
  63. function ucfirst(s) {
  64. return s.charAt(0).toUpperCase() + s.substring(1, s.length);
  65. }
  66. function getFriendlyName(name) {
  67. // Replace illegal characters for Windows paths with underscores
  68. var friendlyName = name.replace(/[\/\\*?"<>|~]/g, "_");
  69. // Work out lower case form. We assume we're on a system with case-insensitive
  70. // filenames, so unless the case is of a special form, we have to disambiguate
  71. var lowerCase = ucfirst(name.toLowerCase());
  72. // Make it mostly unique
  73. if (lowerCase != friendlyName) {
  74. friendlyName += "_" + hex_md5(_to_utf8(name)).substring(0, 4);
  75. }
  76. // Handle colon specially by replacing it with tilde
  77. // Thus we reduce the number of paths with hashes appended
  78. friendlyName = friendlyName.replace(":", "~");
  79. return friendlyName;
  80. }