{"size": 1635, "ext": "js", "max_stars_repo_path": "packages/slicer_cli_web-pv/web_client/parser/parse.js", "max_stars_repo_name": "ShenQianwithC/HistomicsTK", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/slicer_cli_web-pv/web_client/parser/parse.js", "max_issues_repo_name": "ShenQianwithC/HistomicsTK", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/slicer_cli_web-pv/web_client/parser/parse.js", "max_forks_repo_name": "ShenQianwithC/HistomicsTK", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.7118644068, "max_line_length": 97, "alphanum_fraction": 0.6079510703}
import _ from 'underscore';

import panel from './panel';

/**
 * This is a parser for Slicer's GUI Schema:
 *   https://www.slicer.org/slicerWiki/index.php/Slicer3:Execution_Model_Documentation#XML_Schema
 */

/**
 * Parse a Slicer GUI spec into a json object for rendering
 * the controlsPanel view.  This function parses into the following structure:
 *
 * * global metadata
 *   * panels[] -- each is rendered in a different panel
 *     * groups[] -- each is rendered together seperated by a horizontal line
 *       * parameters[] -- individual parameters
 *
 * @param {string|object} spec The slicer GUI spec content (accepts parsed or unparsed xml)
 * @param {object} [opts] When provided this object will used to provide information about
 *     the outputs of the spec.
 * @returns {object}
 */
function parse(spec, opts = {}) {
    var gui, $spec;

    if (_.isString(spec)) {
        spec = $.parseXML(spec);
    }

    $spec = $(spec).find('executable:first');

    // top level metadata
    gui = {
        title: $spec.find('executable > title').text(),
        description: $spec.find('executable > description').text()
    };

    // optional metadata
    _.each(
        ['version', 'documentation-url', 'license', 'contributor', 'acknowledgements'],
        function (key) {
            var val = $spec.find('executable > ' + key + ':first');
            if (val.length) {
                gui[key] = val.text();
            }
        }
    );

    gui.panels = _.filter(
        _.map($spec.find('executable > parameters'), (p) => panel(p, opts)),
        _.isObject
    );

    return gui;
}

export default parse;
