highlight.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //===============================================================================================================
  2. // System : Color Syntax Highlighter
  3. // File : Highlight.js
  4. // Author : Eric Woodruff (Eric@EWoodruff.us)
  5. // Updated : 10/21/2012
  6. // Note : Copyright 2006-2012, Eric Woodruff, All rights reserved
  7. //
  8. // This contains the script to expand and collapse the regions in the syntax highlighted code.
  9. //
  10. // This is a customized version for the Sandcastle Help File Builder. It overrides the CopyCode() function
  11. // from the Hana, Prototype, and VS2005 presentation styles to remove the line numbering and collapsible
  12. // region elements. The VS2010 style does not currently use the CopyCode() function in here as it has its own
  13. // version for copying the code.
  14. //===============================================================================================================
  15. // Expand/collapse a region
  16. function HighlightExpandCollapse(showId, hideId)
  17. {
  18. var showSpan = document.getElementById(showId), hideSpan = document.getElementById(hideId);
  19. showSpan.style.display = "inline";
  20. hideSpan.style.display = "none";
  21. }
  22. // Copy the code from a colorized code block to the clipboard.
  23. function CopyCode(key)
  24. {
  25. var idx, line, block, htmlLines, lines, codeText, hasLineNos, hasRegions, clip, trans,
  26. copyObject, clipID;
  27. var reLineNo = /^\s*\d{1,4}/;
  28. var reRegion = /^\s*\d{1,4}\+.*?\d{1,4}-/;
  29. var reRegionText = /^\+.*?\-/;
  30. // Find the table row element containing the code
  31. var trElements = document.getElementsByTagName("tr");
  32. for(idx = 0; idx < trElements.length; idx++)
  33. if(key.parentNode.parentNode.parentNode == trElements[idx].parentNode)
  34. {
  35. block = trElements[idx].nextSibling;
  36. break;
  37. }
  38. if(block.innerText != undefined)
  39. codeText = block.innerText;
  40. else
  41. codeText = block.textContent;
  42. hasLineNos = block.innerHTML.indexOf("highlight-lineno");
  43. hasRegions = block.innerHTML.indexOf("highlight-collapsebox");
  44. htmlLines = block.innerHTML.split("\n");
  45. lines = codeText.split("\n");
  46. // Remove the line numbering and collapsible regions if present
  47. if(hasLineNos != -1 || hasRegions != -1)
  48. {
  49. codeText = "";
  50. for(idx = 0; idx < lines.length; idx++)
  51. {
  52. line = lines[idx];
  53. if(hasRegions && reRegion.test(line))
  54. line = line.replace(reRegion, "");
  55. else
  56. {
  57. line = line.replace(reLineNo, "");
  58. // Lines in expanded blocks have an extra space
  59. if(htmlLines[idx].indexOf("highlight-expanded") != -1 ||
  60. htmlLines[idx].indexOf("highlight-endblock") != -1)
  61. line = line.substr(1);
  62. }
  63. if(hasRegions && reRegionText.test(line))
  64. line = line.replace(reRegionText, "");
  65. codeText += line;
  66. // Not all browsers keep the line feed when split
  67. if(line[line.length - 1] != "\n")
  68. codeText += "\n";
  69. }
  70. }
  71. // IE or FireFox/Netscape?
  72. if(window.clipboardData)
  73. window.clipboardData.setData("Text", codeText);
  74. else
  75. if(window.netscape)
  76. {
  77. // Give unrestricted access to browser APIs using XPConnect
  78. try
  79. {
  80. netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  81. }
  82. catch(e)
  83. {
  84. alert("Universal Connect was refused, cannot copy to clipboard. Go to about:config and set " +
  85. "signed.applets.codebase_principal_support to true to enable clipboard support.");
  86. return;
  87. }
  88. // Creates an instance of nsIClipboard
  89. clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(
  90. Components.interfaces.nsIClipboard);
  91. // Creates an instance of nsITransferable
  92. if(clip)
  93. trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(
  94. Components.interfaces.nsITransferable);
  95. if(!trans)
  96. {
  97. alert("Copy to Clipboard is not supported by this browser");
  98. return;
  99. }
  100. // Register the data flavor
  101. trans.addDataFlavor("text/unicode");
  102. // Create object to hold the data
  103. copyObject = new Object();
  104. // Creates an instance of nsISupportsString
  105. copyObject = Components.classes["@mozilla.org/supports-string;1"].createInstance(
  106. Components.interfaces.nsISupportsString);
  107. // Assign the data to be copied
  108. copyObject.data = codeText;
  109. // Add data objects to transferable
  110. trans.setTransferData("text/unicode", copyObject, codeText.length * 2);
  111. clipID = Components.interfaces.nsIClipboard;
  112. if(!clipID)
  113. {
  114. alert("Copy to Clipboard is not supported by this browser");
  115. return;
  116. }
  117. // Transfer the data to the clipboard
  118. clip.setData(trans, null, clipID.kGlobalClipboard);
  119. }
  120. else
  121. alert("Copy to Clipboard is not supported by this browser");
  122. }