<commit_before>module.exports.save = save;
module.exports.load = load;
module.exports.loadRaw = loadRaw;

var config = require('../config.js')(location.hostname);
var githubBase = config.GithubAPI ? config.GithubAPI + '/api/v3': 'https://api.github.com';

function save(context, callback) {
    var source = context.data.get('source'),
        meta = context.data.get('meta'),
        newpath = context.data.get('newpath'),
        name = (meta && meta.name) || 'map.geojson',
        map = context.data.get('map');

    if (navigator.appVersion.indexOf('MSIE 9') !== -1 || !window.XMLHttpRequest) {
        return alert('Sorry, saving and sharing is not supported in IE9 and lower. ' +
            'Please use a modern browser to enjoy the full featureset of geojson.io');
    }

    if (!localStorage.github_token) {
        return alert('You need to log in with GitHub to commit changes');
    }

    context.repo.details(onrepo);

    function onrepo(err, repo) {
        var commitMessage,
            endpoint,
            method = 'POST',
            files = {};

        if (!err && repo.permissions.push) {
            commitMessage = context.commitMessage || prompt('Commit message:');
            if (!commitMessage) return;

            endpoint = source.url;
            method = 'PUT';
            var data = {
                message: commitMessage,
                branch: meta.branch,
                content: btoa(encodeURIComponent(JSON.stringify(map, null, 2)).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1);
            };

            // creating a file
            if (newpath) {
                data.path = newpath;
                context.data.set({ newpath: null });
            }

            // updating a file
            if (source.sha) {
                data.sha = source.sha;
            }
        } else {
            endpoint = githubBase + '/gists';
            files[name] = { content: JSON.stringify(map, null, 2) };
            data = { files: files };
        }

        context.user.signXHR(d3.json(endpoint))
            .on('load', function(data) {
                callback(null, data);
            })
            .on('error', function(err) {
                var message,
                    url = /(http:\/\/\S*)/g;

                try {
                    message = JSON.parse(err.responseText).message
                        .replace(url, '<a href="$&">$&</a>');
                } catch(e) {
                    message = 'Sorry, an error occurred.';<commit_msg>Fix copy-paste error in he function to tackle the unicode problem when saving a document.<commit_after>module.exports.save = save;
module.exports.load = load;
module.exports.loadRaw = loadRaw;

var config = require('../config.js')(location.hostname);
var githubBase = config.GithubAPI ? config.GithubAPI + '/api/v3': 'https://api.github.com';

function save(context, callback) {
    var source = context.data.get('source'),
        meta = context.data.get('meta'),
        newpath = context.data.get('newpath'),
        name = (meta && meta.name) || 'map.geojson',
        map = context.data.get('map');

    if (navigator.appVersion.indexOf('MSIE 9') !== -1 || !window.XMLHttpRequest) {
        return alert('Sorry, saving and sharing is not supported in IE9 and lower. ' +
            'Please use a modern browser to enjoy the full featureset of geojson.io');
    }

    if (!localStorage.github_token) {
        return alert('You need to log in with GitHub to commit changes');
    }

    context.repo.details(onrepo);

    function onrepo(err, repo) {
        var commitMessage,
            endpoint,
            method = 'POST',
            files = {};

        if (!err && repo.permissions.push) {
            commitMessage = context.commitMessage || prompt('Commit message:');
            if (!commitMessage) return;

            endpoint = source.url;
            method = 'PUT';
            var data = {
                message: commitMessage,
                branch: meta.branch,
                content: btoa(encodeURIComponent(JSON.stringify(map, null, 2)).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); }))
            };

            // creating a file
            if (newpath) {
                data.path = newpath;
                context.data.set({ newpath: null });
            }

            // updating a file
            if (source.sha) {
                data.sha = source.sha;
            }
        } else {
            endpoint = githubBase + '/gists';
            files[name] = { content: JSON.stringify(map, null, 2) };
            data = { files: files };
        }

        context.user.signXHR(d3.json(endpoint))
            .on('load', function(data) {
                callback(null, data);
            })
            .on('error', function(err) {
                var message,
                    url = /(http:\/\/\S*)/g;

                try {
                    message = JSON.parse(err.responseText).message
                        .replace(url, '<a href="$&">$&</a>');
                } catch(e) {
                    message = 'Sorry, an error occurred.';
{'fraction_non_alphanumeric': 0.13918918918918918, 'fraction_numerical': 0.005019305019305019, 'mean_word_length': 1.712565445026178, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7420989', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1413, 'n_words': 341}