Skip to content

Strip the dynamically generated ID from syntaxhighligher, respect data-linenum #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,21 @@ grunt.registerHelper("syntax-highlight", function( options, callback ) {
// receives the innerHTML of a <code> element and if the first character
// is an encoded left angle bracket, we'll "conclude" the "language" is html
function crudeHTMLcheck ( input ) {
return input.indexOf("&lt;") === 0 ? "html" : "";
return input.trim().indexOf("&lt;") === 0 ? "html" : "";
}

// when parsing the class attribute, make sure a class matches an actually
// highlightable language, instead of being presentational (e.g. 'example')
function getLanguageFromClass( str ) {
str = str || "";
var classes = str.split(" "),
c = classes.length;
while (--c) {
if ( nsh.getLanguage( classes[c] ) ) {
return classes[c];
}
}
return "";
}

var html = options.file ? grunt.file.read( options.file ) : options.cmd.stdout,
Expand All @@ -110,10 +124,13 @@ grunt.registerHelper("syntax-highlight", function( options, callback ) {
highlight.each( function( index, el ) {
var $t = $(this),
code = $t.html(),
lang = $t.attr("data-lang") || $t.attr("class") || crudeHTMLcheck( code ),
lang = $t.attr("data-lang") || getLanguageFromClass( $t.attr("class") ) || crudeHTMLcheck( code ),
linenum = $t.attr("data-linenum") || 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If data-linenum is not specified, line numbers shouldn't be generated. If its present, but has no value, default to 1. If present, and has a value, use that.

brush = nsh.getLanguage( lang ) || nsh.getLanguage( "js" ),
highlighted = nsh.highlight( code, brush );
$t.parent().replaceWith( highlighted );
highlighted = nsh.highlight( code, brush, {
"first-line": linenum
});
$t.parent().replaceWith( $(highlighted).removeAttr("id") );
});
}
catch ( excp ) {
Expand Down