Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Added title & description metadata extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
gseguin committed Jul 26, 2012
1 parent 4121fc0 commit 332705b
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions build/tasks/plugins.js
Expand Up @@ -7,7 +7,10 @@ module.exports = function( grunt ) {
grunt.registerTask( "plugins", function() {
var globalConfig = pluginConfig = grunt.config.get( 'global' ),
pluginConfig = globalConfig.plugins,
done = this.async(), filenameToPlugin;
done = this.async(),
filenameToPlugin,
escapeRegexString,
extractMetadata;

filenameToPlugin = function( filename ) {
var pathless = filename.replace( /js\//, "" ),
Expand All @@ -27,6 +30,48 @@ module.exports = function( grunt ) {
.replace( ".js", ".jquery.json" );
};

// Thank you Dojo
// summary: Regular expressions and Builder resources
escapeRegexString = function ( /*String*/str, /*String?*/except ) {
// summary:
// Adds escape sequences for special characters in regular expressions
// except:
// a String with special characters to be left unescaped

return str.replace( /([\.$?*|{}\(\)\[\]\\\/\+^])/g, function ( ch ) {
if ( except && except.indexOf( ch ) != -1 ) {
return ch;
}
return "\\" + ch;
} ); // String
};

extractMetadata = function( data ) {
var lines = data.split( "\n" ),
matches = lines.filter( function( line, index ) {
return ( /^.*\/\/>>\s*[^:]+:.*$/ ).test( line );
}),
metadata = {};
if ( matches && matches.length ) {
matches.forEach( function( meta ) {
var attr = meta.replace( /^.*\/\/>>\s*([^:]+):.*$/, "$1" ).trim(),
attrLabelRE = new RegExp( "^.*" + escapeRegexString( "//>>" + attr + ":") + "\\s*", "m" ),
value = meta.replace( attrLabelRE, "" ).trim(),
namespace, name,
indexOfDot = attr.indexOf( "." );
if ( indexOfDot > 0 ) { // if there is something before the dot
namespace = attr.split( "." )[0];
name = attr.substring( indexOfDot+1 );
metadata[ namespace ] = metadata[ namespace ] || {};
metadata[ namespace ][ name ] = value;
} else {
metadata[ attr ] = value;
}
});
}
return metadata;
};

requirejs.tools.useLib( function ( require ) {
require( [ 'parse' ], function ( parse ) {
// grab all the files that still use the original convention
Expand All @@ -44,16 +89,18 @@ module.exports = function( grunt ) {

var data = fs.readFileSync( filename, 'utf8' ),
tree = parse.findDependencies( filename, data ),
manifest, template;
manifest,
template,
metadata = extractMetadata( data );

template = grunt.file.read( pluginConfig.template );

manifest = filenameToPlugin( filename );

grunt.file.write( manifest, grunt.template.process( template, {
name: filename.replace(".json", ""),
title: "TODO",
desc: "TODO",
title: metadata.label || "Untitled",
desc: metadata.description || "",
version: globalConfig.ver.official
}));
});
Expand Down

0 comments on commit 332705b

Please sign in to comment.