Skip to content

Commit

Permalink
add static highlighter highlight method
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing committed Oct 11, 2013
1 parent a2244c6 commit 51dc39b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
10 changes: 1 addition & 9 deletions doc/site/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,6 @@ function highlight() {
var m = el.className.match(/language-(\w+)|(javascript)/);
if (!m) return
var mode = "ace/mode/" + (m[1] || m[2]);
var theme = "ace/theme/xcode";
var data = dom.getInnerText(el).trim();

highlighter.render(data, mode, theme, 1, true, function (highlighted) {
dom.importCssString(highlighted.css, "ace_highlight");
el.innerHTML = highlighted.html;
});
highlighter.highlight(el, {mode: mode, theme: "ace/theme/xcode"})
});


}
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<link href="./doc/site/style.css" rel="stylesheet" type="text/css" />
<link href="./doc/site/images/favicon.ico" rel="icon" type="image/x-icon" />

<script src="./build/src-min-noconflict/ace.js"></script>
<script src="./build/src-min-noconflict/ext-static_highlight.js"></script>
<script src="./build/src-min/ace.js"></script>
<script src="./build/src-min/ext-static_highlight.js"></script>
</head>
<body>
<a href="http://github.com/ajaxorg/ace">
Expand Down
54 changes: 47 additions & 7 deletions lib/ace/ext/static_highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var EditSession = require("../edit_session").EditSession;
var TextLayer = require("../layer/text").Text;
var baseStyles = require("../requirejs/text!./static.css");
var config = require("../config");
var dom = require("../lib/dom");
/**
* Transforms a given input code snippet into HTML using the given mode
*
Expand Down Expand Up @@ -86,13 +87,13 @@ exports.render = function(input, mode, theme, lineStart, disableGutter, callback
return waiting || done();
};

/* Transforms a given input code snippet into HTML using the given mode
*
* @param {string} input Code snippet
* @param {mode} mode Mode loaded from /ace/mode (use 'ServerSideHiglighter.getMode')
* @param {string} r Code snippet
* @returns {object} An object containing: html, css
*/
/*
* Transforms a given input code snippet into HTML using the given mode
* @param {string} input Code snippet
* @param {mode} mode Mode loaded from /ace/mode (use 'ServerSideHiglighter.getMode')
* @param {string} r Code snippet
* @returns {object} An object containing: html, css
*/

exports.renderSync = function(input, mode, theme, lineStart, disableGutter) {
lineStart = parseInt(lineStart || 1, 10);
Expand Down Expand Up @@ -136,4 +137,43 @@ exports.renderSync = function(input, mode, theme, lineStart, disableGutter) {
};
};



exports.highlight = function(el, opts) {
var m = el.className.match(/lang-(\w+)/);
var mode = opts.mode || m && ("ace/mode/" + m[1]);
if (!mode)
return false;
var theme = opts.theme || "ace/theme/textmate";

var data = "";
var nodes = [];

if (el.firstElementChild) {
var textLen = 0;
for (var i = 0; i < el.childNodes.length; i++) {
var ch = el.childNodes[i];
if (ch.nodeType == 3) {
textLen += ch.data.length;
data += ch.data;
} else {
nodes.push(textLen, ch);
}
}
} else {
data = dom.getInnerText(el);
}

exports.render(data, mode, theme, 1, true, function (highlighted) {
dom.importCssString(highlighted.css, "ace_highlight");
el.innerHTML = highlighted.html;
var container = el.firstChild.firstChild
for (var i = 0; i < nodes.length; i += 2) {
var pos = highlighted.session.doc.indexToPosition(nodes[i])
var node = nodes[i + 1];
var lineEl = container.children[pos.row];
lineEl && lineEl.appendChild(nodes[i+1]);
}
});
};
});

0 comments on commit 51dc39b

Please sign in to comment.