File: highlight.js

package info (click to toggle)
db5.3 5.3.28%2Bdfsg1-0.5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 158,360 kB
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,326; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (187 lines) | stat: -rw-r--r-- 6,267 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//=============================================================================
// System  : Color Syntax Highlighter
// File    : Highlight.js
// Author  : Eric Woodruff  (Eric@EWoodruff.us)
// Updated : 11/13/2007
// Note    : Copyright 2006, Eric Woodruff, All rights reserved
//
// This contains the script to expand and collapse the regions in the
// syntax highlighted code.
//
//=============================================================================

// Expand/collapse a region
function HighlightExpandCollapse(showId, hideId)
{
    var showSpan = document.getElementById(showId),
        hideSpan = document.getElementById(hideId);

    showSpan.style.display = "inline";
    hideSpan.style.display = "none";
}

// Copy the code if Enter or Space is hit with the image focused
function CopyColorizedCodeCheckKey(titleDiv, eventObj)
{
    if(eventObj != undefined && (eventObj.keyCode == 13 ||
      eventObj.keyCode == 32))
        CopyColorizedCode(titleDiv);
}

// Change the icon as the mouse moves in and out of the Copy Code link
// There should be an image with the same name but an "_h" suffix just
// before the extension.
function CopyCodeChangeIcon(linkSpan)
{
    var image = linkSpan.firstChild.src;
    var pos = image.lastIndexOf(".");

    if(linkSpan.className == "highlight-copycode")
    {
        linkSpan.className = "highlight-copycode_h";
        linkSpan.firstChild.src = image.substr(0, pos) + "_h" +
            image.substr(pos);
    }
    else
    {
        linkSpan.className = "highlight-copycode";
        linkSpan.firstChild.src = image.substr(0, pos - 2) + image.substr(pos);
    }
}

// Copy the code from a colorized code block to the clipboard.
function CopyColorizedCode(titleDiv)
{
    var preTag, idx, line, block, htmlLines, lines, codeText, hasLineNos,
        hasRegions, clip, trans, copyObject, clipID;
    var reLineNo = /^\s*\d{1,4}/;
    var reRegion = /^\s*\d{1,4}\+.*?\d{1,4}-/;
    var reRegionText = /^\+.*?\-/;

    // Find the <pre> tag containing the code.  It should be in the next
    // element or one of its children.
    block = titleDiv.nextSibling;

    while(block.nodeName == "#text")
        block = block.nextSibling;

    while(block.tagName != "PRE")
    {
        block = block.firstChild;

        while(block.nodeName == "#text")
            block = block.nextSibling;
    }

    if(block.innerText != undefined)
        codeText = block.innerText;
    else
        codeText = block.textContent;

    hasLineNos = block.innerHTML.indexOf("highlight-lineno");
    hasRegions = block.innerHTML.indexOf("highlight-collapsebox");
    htmlLines = block.innerHTML.split("\n");
    lines = codeText.split("\n");

    // Remove the line numbering and collapsible regions if present
    if(hasLineNos != -1 || hasRegions != -1)
    {
        codeText = "";

        for(idx = 0; idx < lines.length; idx++)
        {
            line = lines[idx];

            if(hasRegions && reRegion.test(line))
                line = line.replace(reRegion, "");
            else
            {
                line = line.replace(reLineNo, "");

                // Lines in expanded blocks have an extra space
                if(htmlLines[idx].indexOf("highlight-expanded") != -1 ||
                  htmlLines[idx].indexOf("highlight-endblock") != -1)
                    line = line.substr(1);
            }

            if(hasRegions && reRegionText.test(line))
                line = line.replace(reRegionText, "");

            codeText += line;

            // Not all browsers keep the line feed when split
            if(line[line.length - 1] != "\n")
                codeText += "\n";
        }
    }

    // IE or FireFox/Netscape?
    if(window.clipboardData)
        window.clipboardData.setData("Text", codeText);
    else
        if(window.netscape)
        {
            // Give unrestricted access to browser APIs using XPConnect
            try
            {
                netscape.security.PrivilegeManager.enablePrivilege(
                    "UniversalXPConnect");
            }
            catch(e)
            {
                alert("Universal Connect was refused, cannot copy to " +
                    "clipboard.  Go to about:config and set " +
                    "signed.applets.codebase_principal_support to true to " +
                    "enable clipboard support.");
                return;
            }

            // Creates an instance of nsIClipboard
            clip = Components.classes[
                "@mozilla.org/widget/clipboard;1"].createInstance(
                Components.interfaces.nsIClipboard);

            // Creates an instance of nsITransferable
            if(clip)
                trans = Components.classes[
                    "@mozilla.org/widget/transferable;1"].createInstance(
                    Components.interfaces.nsITransferable);

            if(!trans)
            {
                alert("Copy to Clipboard is not supported by this browser");
                return;
            }

            // Register the data flavor
            trans.addDataFlavor("text/unicode");

            // Create object to hold the data
            copyObject = new Object();

            // Creates an instance of nsISupportsString
            copyObject = Components.classes[
                "@mozilla.org/supports-string;1"].createInstance(
                Components.interfaces.nsISupportsString);

            // Assign the data to be copied
            copyObject.data = codeText;

            // Add data objects to transferable
            trans.setTransferData("text/unicode", copyObject,
                codeText.length * 2);

            clipID = Components.interfaces.nsIClipboard;

            if(!clipID)
            {
                alert("Copy to Clipboard is not supported by this browser");
                return;
            }

            // Transfer the data to the clipboard
            clip.setData(trans, null, clipID.kGlobalClipboard);
        }
        else
            alert("Copy to Clipboard is not supported by this browser");
}