<commit_before>// Copyright 2002-2015, University of Colorado Boulder

/**
 * This grunt task generates a file [sim-name].xml for use by the website when automatically deploying sims
 * and translations.
 *
 * @author <NAME>
 * @author <NAME>
 */

/* jslint node: true */ // allows "process" to pass lint instead of getting an undefined lint error

/**
 * @param grunt the grunt instance
 * @param sim the sim name, defaults to the current directory
 */
module.exports = function( grunt, sim ) {
  'use strict';

  if ( !sim ) {
    var directory = process.cwd();
    var directoryComponents = directory.split( '/' );
    sim = directoryComponents[ directoryComponents.length - 1 ];
  }

  grunt.file.defaultEncoding = 'utf8';

  var rootdir = '../babel/' + sim;
  var englishStringsFile = sim + '-strings_en.json';
  var filenames = [ englishStringsFile ];

  grunt.file.recurse( rootdir, function( abspath, rootdir, subdir, filename ) {
    filenames.push( filename );
  } );

  var getLocaleFromName = function( filename ) {
    var firstUnderscoreIndex = filename.indexOf( '_' );
    var periodIndex = filename.indexOf( '.' );
    return filename.substring( firstUnderscoreIndex + 1, periodIndex );
  };

  // grab the title from sim/package.json
  var packageJSON = grunt.file.readJSON( '../' + sim + '/package.json' );
  var simTitleKey = packageJSON.simTitleStringKey;

  simTitleKey = simTitleKey.split( '/' )[ 1 ];

  // create xml, making a simulation tag for each language
  var finalXML = '<?xml version="1.0" encoding="utf-8" ?>\n' +
                 '<project name="molecules-and-light">\n' +
                 '<simulations>\n';

  for ( var j = 0; j < filenames.length; j++ ) {
    var filename = filenames[ j ];
    var locale = getLocaleFromName( filename );
    var languageJSON = grunt.file.readJSON( ( locale === 'en' ) ? englishStringsFile : '../babel' + '/' + sim + '/' + filename );

    if ( languageJSON[ simTitleKey ] ) {
      finalXML = finalXML.concat( '<simulation name="' + sim + '" locale="' + locale + '">\n' +
                                  '<title><![CDATA[' + languageJSON[ simTitleKey ].value + ']]></title>\n' +
                                  '</simulation>\n' );
    }
  }

  finalXML = finalXML.concat( '</simulations>\n' + '</project>' );
<commit_msg>refactor in createXML, see #139<commit_after>// Copyright 2002-2015, University of Colorado Boulder

/**
 * This grunt task generates a file [sim-name].xml for use by the website when automatically deploying sims
 * and translations.
 *
 * @author <NAME>
 * @author <NAME>
 */

/* jslint node: true */ // allows "process" to pass lint instead of getting an undefined lint error

/**
 * @param grunt the grunt instance
 * @param sim the sim name, defaults to the current directory
 */
module.exports = function( grunt, sim ) {
  'use strict';

  if ( !sim ) {
    var directory = process.cwd();
    var directoryComponents = directory.split( '/' );
    sim = directoryComponents[ directoryComponents.length - 1 ];
  }

  grunt.file.defaultEncoding = 'utf8';

  var rootdir = '../babel/' + sim;
  var englishStringsFile = sim + '-strings_en.json';
  var stringFiles = [ { name: englishStringsFile, locale: 'en' } ];

  grunt.file.recurse( rootdir, function( abspath, rootdir, subdir, filename ) {
    var firstUnderscoreIndex = filename.indexOf( '_' );
    var periodIndex = filename.indexOf( '.' );
    var locale = filename.substring( firstUnderscoreIndex + 1, periodIndex );
    stringFiles.push( { name: filename, locale: locale } );
  } );

  console.log( stringFiles );

  // grab the title from sim/package.json
  var packageJSON = grunt.file.readJSON( '../' + sim + '/package.json' );
  var simTitleKey = packageJSON.simTitleStringKey;

  simTitleKey = simTitleKey.split( '/' )[ 1 ];

  // create xml, making a simulation tag for each language
  var finalXML = '<?xml version="1.0" encoding="utf-8" ?>\n' +
                 '<project name="molecules-and-light">\n' +
                 '<simulations>\n';

  for ( var j = 0; j < stringFiles.length; j++ ) {
    var stringFile = stringFiles[ j ];
    var languageJSON = grunt.file.readJSON( ( stringFile.locale === 'en' ) ? englishStringsFile : '../babel' + '/' + sim + '/' + stringFile.name );

    if ( languageJSON[ simTitleKey ] ) {
      finalXML = finalXML.concat( '<simulation name="' + sim + '" locale="' + stringFile.locale + '">\n' +
                                  '<title><![CDATA[' + languageJSON[ simTitleKey ].value + ']]></title>\n' +
                                  '</simulation>\n' );
    }
  }

  finalXML = finalXML.concat( '</simulations>\n' + '</project>' );

{'fraction_non_alphanumeric': 0.15225361224929912, 'fraction_numerical': 0.007547983610092733, 'mean_word_length': 2.858569051580699, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 2, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3060361', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1381, 'n_words': 391}