<commit_before>/*
 * grunt-gconfig
 * https://github.com/goliatone/grunt-gconfig
 *
 * Copyright (c) 2014 goliatone
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks

  grunt.registerMultiTask('gconfig', 'Your task description goes here.', function() {
    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      punctuation: '.',
      separator: ', '
    });

    // Iterate over all specified file groups.
    this.files.forEach(function(f) {
      // Concat specified files.
      var src = f.src.filter(function(filepath) {
        // Warn on and remove invalid source files (if nonull was set).
        if (!grunt.file.exists(filepath)) {
          grunt.log.warn('Source file "' + filepath + '" not found.');
          return false;
        } else {
          return true;
        }
      }).map(function(filepath) {
        // Read file source.
        return grunt.file.read(filepath);
      }).join(grunt.util.normalizelf(options.separator));

      // Handle options.
      src += options.punctuation;

      // Write the destination file.
      grunt.file.write(f.dest, src);

      // Print a success message.
      grunt.log.writeln('File "' + f.dest + '" created.');
    });
  });

};<commit_msg>Added initial gconfig grunt task.<commit_after>/*
 * grunt-gconfig
 * https://github.com/goliatone/grunt-gconfig
 *
 * Copyright (c) 2014 goliatone
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {
    var MetadataParser = require('../lib/metadata'),
        extend = require('../lib/utils').extend,
        path = require('path'),
        $ = require('cheerio');

    var desc = 'Process Gconfig metadata';

    grunt.registerMultiTask('gconfig', desc, function() {
        // Merge task-specific and/or target-specific options with these defaults.
        var options = this.options({
            punctuation: '.',
            separator: ', '
        });

        var cwd = process.cwd();
        
        grunt.log.writeln('\n', desc, 'total files:', this.files.length);
        grunt.log.writeln('--------------------------------------------');
        
        var metadata,
            data,
            parser = new MetadataParser(),
            filepath = this.data.gconfig,
            removeOriginalUnmatchedMeta = this.data.removeOriginalUnmatchedMeta,
            gconfig = grunt.file.readJSON(path.join(cwd, filepath));

        this.files.forEach(function(file) {
            var contents = file.src.filter(function(filepath) {
                // Remove nonexistent files (it's up to you to filter or warn here).
                if (grunt.file.exists(filepath)) return true;
                grunt.log.warn('Source file "' + filepath + '" not found.');
                return false;
            }).map(function(filepath) {
                // Read and return the file's source.
                var fileContent = grunt.file.read(filepath),
                parsedHTML = $.load(fileContent),
                defaults = parser.extractMetadata($('meta'));

                data = extend({}, defaults, gconfig);
                metadata = parser.buildMetadata(data);

                var htmlMeta = parsedHTML('meta');

                //TODO: We need to keep the meta that is not part of gconf
                if(htmlMeta.length){
                    grunt.log.writeln('- Building HTML metadata for ', filepath);

                    htmlMeta.each(function(i, el){
                        var item = $(this);
                        if(item.attr('name') && item.attr('name').indexOf(':')){
                            var name = item.attr('name');
                            if(metadata[name]){
                                grunt.log.writeln('  √ Update meta ', name, ' content ', item.attr('content'), ' to ', metadata[name].content);
                                item.attr('content', metadata[name].content);
                                delete metadata[name];
                            } else if(removeOriginalUnmatchedMeta){
                                grunt.log.writeln('  † Remove meta ', name);
                                item.remove();
                            }
                        }
                    });

                    Object.keys(metadata).forEach(function(name){
                        var content = metadata[name];
                        parsedHTML('meta').append(content.html);
                    });
                }
                else{
                    grunt.log.writeln('- Creating HTML metadata for ', filepath);
                    Object.keys(metadata).forEach(function(name){
                        if(name !== 'html') grunt.log.writeln('  √ Create metadata for', name);
                    });
                    parsedHTML('head').append($(metadata.html.join('\n')));
                }
                
                var outputHTML = parsedHTML.html();
                // Write joined contents to destination filepath.
                grunt.file.write(file.dest, outputHTML);
                // Print a success message.
                grunt.log.writeln('\n  + File "' + file.dest + '" created.\n');
            });
        });
    });
};
{'fraction_non_alphanumeric': 0.13610235490450584, 'fraction_numerical': 0.0014834044131281292, 'mean_word_length': 1.5771619684663163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3050259', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1365, 'n_words': 322}