<commit_before>/*
 * static-api-docs
 *
 *
 * Copyright (c) 2015 <NAME>
 * Licensed under the Apache license.
 */

'use strict';

var fs = require('fs');
var nunjucks = require('nunjucks');
var lodash = require('lodash');
var SwaggerParser = require('swagger-parser');
var jsonRefs = require('json-refs');

var YAML = require('yamljs');

nunjucks.configure({ autoescape: true });

module.exports = function (grunt) {


  grunt.registerMultiTask('static_api_docs', '', function () {

    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({});



    if(!this.data.hasOwnProperty('src')){
      grunt.log.warn('No "src" property in target: "' + this.target + '".');
      return false;
    }
    if(!this.data.hasOwnProperty('dest')){
      grunt.log.warn('No "dest" property in target: "' + this.target + '".');
      return false;
    }
    if (!grunt.file.exists(this.data.src)) {
      grunt.log.warn('Source file "' + this.data.src + '" not found.');
      return false;
    }

    // Swagger JSON source
    var swaggerYmlFile = process.cwd() + '/' + this.data.src;
    //var swaggerYML = require(swaggerYmlFile);

    SwaggerParser.validate(swaggerYmlFile, function(err, api) {
      if (err) {
        grunt.fail.warn(err)
      }
      else {
        grunt.log.write(swaggerYmlFile + " is valid Swagger Spec.")
      }
    });

    var swaggerJSON = YAML.load(process.cwd() + '/' + this.data.src);
    var apiModel = jsonRefs.resolveLocalRefs(swaggerJSON).resolved;

    var fileName = options.filename ||  "api-doc";
    var suppressMD = options.suppressMD || false;
    var suppressHTML = options.suppressHTML || false;
    var markdownOutputFile = this.data.dest + '/' + fileName + ".md";
    var htmlOutputFile = this.data.dest + '/' + fileName + ".html";

    // Parse the Swagger schema and reform to our liking
    lodash.forIn(apiModel.paths, function (path) {

      lodash.forIn(path, function (verb) {

        lodash.forIn(verb.responses, function (response) {

          response.schemaArr = [];

          recurvsiveFlatten(undefined, response.schema, response.schemaArr, -1);

        });
      });
    });


    if(!suppressMD) {
      grunt.file.write(markdownOutputFile, nunjucks.render(__dirname  + '/../templates/snippet.md', apiModel));
    }
    if(!suppressHTML) {
      grunt.file.write(htmlOutputFile, nunjucks.render(__dirname +  '/../templates/shell.html', apiModel));
    }<commit_msg>Digest and reformat "body" parameters.<commit_after>/*
 * static-api-docs
 *
 *
 * Copyright (c) 2015 <NAME>
 * Licensed under the Apache license.
 */

'use strict';

var fs = require('fs');
var nunjucks = require('nunjucks');
var lodash = require('lodash');
var SwaggerParser = require('swagger-parser');
var jsonRefs = require('json-refs');

var YAML = require('yamljs');

nunjucks.configure({ autoescape: true });

module.exports = function (grunt) {


  grunt.registerMultiTask('static_api_docs', '', function () {

    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({});

    if(!this.data.hasOwnProperty('src')){
      grunt.log.warn('No "src" property in target: "' + this.target + '".');
      return false;
    }
    if(!this.data.hasOwnProperty('dest')){
      grunt.log.warn('No "dest" property in target: "' + this.target + '".');
      return false;
    }
    if (!grunt.file.exists(this.data.src)) {
      grunt.log.warn('Source file "' + this.data.src + '" not found.');
      return false;
    }

    // Swagger JSON source
    var swaggerYmlFile = process.cwd() + '/' + this.data.src;
    //var swaggerYML = require(swaggerYmlFile);

    SwaggerParser.validate(swaggerYmlFile, function(err, api) {
      if (err) {
        grunt.fail.fatal(err)
      }
      else {
        grunt.log.write(swaggerYmlFile + " is valid Swagger Spec.")
      }
    });

    var swaggerJSON = YAML.load(process.cwd() + '/' + this.data.src);
    var apiModel = jsonRefs.resolveLocalRefs(swaggerJSON).resolved;

    var fileName = options.filename ||  "api-doc";
    var suppressMD = options.suppressMD || false;
    var suppressHTML = options.suppressHTML || false;
    var markdownOutputFile = this.data.dest + '/' + fileName + ".md";
    var htmlOutputFile = this.data.dest + '/' + fileName + ".html";

    // Parse the Swagger schema and reform to our liking
    lodash.forIn(apiModel.paths, function (path) {

      lodash.forIn(path, function (verb) {


        if(verb.parameters.filter(function (parameter) { return parameter.in === 'body'; }).length > 1){
          grunt.fail.fatal('More than one "body" parameter.  Combine all body parameters into one with name "body" and type "body".')
        };

        var bodyPars = [];
        var bodyIndex = null;
        verb.parameters.forEach(function(parameter, index){

          if(parameter.in === 'body') {

            bodyIndex = index;
            lodash.forIn(parameter.schema.properties, function(property, key){

              bodyPars.push({ name: key, in: 'body', description: property.description, required: property.required, type: property.type});

            });
          }

        });

        if(bodyIndex !== null) {
          verb.parameters.splice(bodyIndex);
          bodyPars.forEach(function(parameter){
            verb.parameters.push(parameter);
          });
        }

        lodash.forIn(verb.responses, function (response) {
          response.schemaArr = [];
          recurvsiveFlatten(undefined, response.schema, response.schemaArr, -1);
        });
      });
    });


    if(!suppressMD) {
      grunt.file.write(markdownOutputFile, nunjucks.render(__dirname  + '/../templates/snippet.md', apiModel));
    }
    if(!suppressHTML) {
      grunt.file.write(htmlOutputFile, nunjucks.render(__dirname +  '/../templates/shell.html', apiModel));
    }
{'fraction_non_alphanumeric': 0.15389853592100783, 'fraction_numerical': 0.0018726591760299626, 'mean_word_length': 2.8174139051332032, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, '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': '2817286', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1815, 'n_words': 380}