Skip to content

Commit

Permalink
version 1.8, added line highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Andi Dittrich committed Nov 10, 2013
1 parent c89aea1 commit 6d7f361
Show file tree
Hide file tree
Showing 22 changed files with 360 additions and 75 deletions.
29 changes: 27 additions & 2 deletions Build/EnlighterJS.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: EnlighterJS
description: Syntax Highlighter for MooTools - based on the famous Lighter.js
license: MIT-style X11 License
version: 1.7.1
build: 97924d21c8ffba8e80364f64e2a7a4ab/October 5 2013
version: 1.8
build: 44fcaedaaa33f2910e4f7d44aac52557/November 10 2013
authors:
- Andi Dittrich (author of EnlighterJS fork)
Expand Down Expand Up @@ -110,6 +110,11 @@ ol.EnlighterJSRendered li.lastline {
padding-bottom: 5px;
}

/* special line highlight color */
ol.EnlighterJSRendered li.specialline {
background-color: #F4F8FC;
}

ol.EnlighterJSRendered li.hoverEnabled:hover {
border-top: 1px solid #eee;
border-bottom: 1px solid #eee;
Expand Down Expand Up @@ -177,6 +182,11 @@ ol.gitEnlighterJS li.hoverEnabled:hover{
border: none;
}

/* special line highlight color */
ol.gitEnlighterJS li.specialline {
background-color: #fffff2;
}

/* non leading zero */
ol.gitEnlighterJS {
list-style-type: decimal;
Expand Down Expand Up @@ -241,6 +251,12 @@ ol.mochaEnlighterJS li.hoverEnabled:hover{
border: none;
border-left: 2px solid #939393;
}

/* special line highlight color */
ol.mochaEnlighterJS li.specialline {
background-color: #423F43;
}

pre.mochaEnlighterJS {
background-color: #2D2522;
}
Expand Down Expand Up @@ -348,6 +364,10 @@ ol.tuttiEnlighterJS li.hoverEnabled:hover{
background-color: #F4F8FC;
border: none;
}
/* special line highlight color */
ol.tuttiEnlighterJS li.specialline {
background-color: #F4F8FC;
}

/** Symbol styles */
.tuttiEnlighterJS .de1 { color: #6eb13f; }
Expand Down Expand Up @@ -418,6 +438,11 @@ ol.twilightEnlighterJS li.hoverEnabled:hover{
border-left: 1px solid #939393;
}

/* special line highlight color */
ol.twilightEnlighterJS li.specialline {
background-color: #202021;
}

/** Symbol styles */
.twilightEnlighterJS .de1 { color: #fff; }
.twilightEnlighterJS .de2 { color: #fff; }
Expand Down
132 changes: 119 additions & 13 deletions Build/EnlighterJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: EnlighterJS
description: Syntax Highlighter for MooTools - based on the famous Lighter.js
license: MIT-style X11 License
version: 1.7.1
build: 6f3c1bcb2c530f0ea1e1f77a7f24a634/October 5 2013
version: 1.8
build: e44079501117bd7e1443d6d34b8383e7/November 10 2013
authors:
- Andi Dittrich (author of EnlighterJS fork)
Expand Down Expand Up @@ -118,11 +118,14 @@ var EnlighterJS = new Class({
// get theme name - use options as fallback
var themeName = (this.options.forceTheme ? null : this.codeblock.get('data-enlighter-theme')) || this.options.theme;

// special lines to highlight ?
var specialLines = new EnlighterJS.SpecialLineHighlighter(this.codeblock.get('data-enlighter-highlight'));

// Load language parser
language = new EnlighterJS.Language[languageName](code, {});

// compile tokens -> generate output
var output = this.compiler.compile(language, themeName.toLowerCase());
var output = this.compiler.compile(language, themeName.toLowerCase(), specialLines);

// grab content into specific container or after original code block ?
if (this.container) {
Expand Down Expand Up @@ -177,6 +180,76 @@ var EnlighterJS = new Class({
}
});

/*
---
name: Special Line Highlighter
description: Highlights special lines
license: MIT-style X11 License
authors:
- Andi Dittrich
requires:
- Core/1.4.5
provides: [EnlighterJS.SpecialLineHighlighter]
...
*/

EnlighterJS.SpecialLineHighlighter = new Class({

// storage of line numbers to highlight
specialLines: {},

/**
* @constructs
* @param {String} html attribute content "highlight" - scheme 4,5,6,10-12,19
*/
initialize : function(lineNumberString){
// special lines given ?
if (lineNumberString == null || lineNumberString.length == 0){
return;
}

// split attribute string into segments
var segments = lineNumberString.split(',');

// iterate over segments
segments.each(function(item, index){
// pattern xxxx-yyyy
var parts = item.match(/([0-9]+)-([0-9]+)/);

// single line or line-range
if (parts!=null){
// 2 items required
var start = parts[1].toInt();
var stop = parts[2].toInt();

// valid range ?
if (stop > start){
// add lines to storage
for (var i=start;i<=stop;i++){
this.specialLines['l' + i] = true;
}
}
}else{
// add line to storage
this.specialLines['l' + item.toInt()] = true;
}
}.bind(this));
},

/**
* Check if the given linenumber is a special line
* @param Integer lineNumber
* @returns {Boolean}
*/
isSpecialLine: function(lineNumber){
return (this.specialLines['l' + lineNumber] || false);
}

});
/*
---
description: Compiles an array of Tokens into an Element.
Expand Down Expand Up @@ -220,10 +293,12 @@ EnlighterJS.Compiler = new Class({
* language The Language used when parsing.
* @param {String}
* theme The Theme to use.
* @param {SpecialLineHighlighter}
* lines to highlight
* @return {Element} The generated Element.
*/
compile : function(language, theme){
var container = this._compile(language, theme);
compile : function(language, theme, specialLines){
var container = this._compile(language, theme, specialLines);

// set class and id attributes.
container.addClass(theme + 'EnlighterJS');
Expand All @@ -241,7 +316,7 @@ EnlighterJS.Compiler = new Class({
/**
* Extending classes must override this method and return a highlighted Element using the language and theme that were passed in.
*/
_compile : function(language, theme){
_compile : function(language, theme, specialLines){
throw new Error('Extending classes must override the _compile method.');
}
});
Expand Down Expand Up @@ -714,7 +789,18 @@ EnlighterJS.Compiler.Inline = new Class({
this.parent(options);
},

_compile : function(language, theme){
/**
* Compiles an array of tokens into a highlighted element using a language and a theme.
*
* @param {Language}
* language The Language used when parsing.
* @param {String}
* theme The Theme to use.
* @param {SpecialLineHighlighter}
* lines to highlight
* @return {Element} The generated Element.
*/
_compile : function(language, theme, specialLines){
// create output container element
var container = new Element(this.options.containerTag);

Expand Down Expand Up @@ -763,14 +849,29 @@ EnlighterJS.Compiler.List = new Class({
this.parent(options);
},


_compile : function(language, theme){
/**
* Compiles an array of tokens into a highlighted element using a language and a theme.
*
* @param {Language}
* language The Language used when parsing.
* @param {String}
* theme The Theme to use.
* @param {SpecialLineHighlighter}
* lines to highlight
* @return {Element} The generated Element.
*/
_compile : function(language, theme, specialLines){
// create new outer container element
var container = new Element(this.options.containerTag);

// current line element
var currentLine = new Element('li');
// line number count
var lineCounter = 1;

// current line element
var currentLine = new Element('li', {
'class': (specialLines.isSpecialLine(lineCounter) ? 'specialline' : '')
});

// generate output based on ordered list of tokens
language.getTokens().each(function(token, index){
// get classname
Expand All @@ -792,9 +893,14 @@ EnlighterJS.Compiler.List = new Class({
// grab old line into output container
container.grab(currentLine);

// create new line
currentLine = new Element('li');
// new line
lineCounter++;

// create new line, add special line classes
currentLine = new Element('li', {
'class': (specialLines.isSpecialLine(lineCounter) ? 'specialline' : '')
});

// create new token-element
currentLine.grab(new Element('span', {
'class': className,
Expand Down
Loading

0 comments on commit 6d7f361

Please sign in to comment.