<commit_before>function Twiddlers(tiddlyweb) {
    this.title = undefined;
    this.bag = undefined;
    this.currentUser = tiddlyweb.status.username;
    this.serverHost = tiddlyweb.status.server_host;
    this.headerTemplate = this._getTemplate('#tiddler-header-template');
    this.tiddlerTemplate = this._getTemplate('#tiddler-template');
    this.listTemplate = this._getTemplate('#related-list-template');
    this.followers = [];
}

Twiddlers.prototype.init = function () {
    this._setTitle();
    this._bindUIEvents();
    this._registerHandlebarsHelpers();
};

Twiddlers.prototype._getTemplate = function (id) {
    return Handlebars.compile($(id).html());
};

Twiddlers.prototype.getTwiddlers = function () {
    var context = this;
    this._getLocalTiddler(this.title).done(function (data) {
        context._displayTiddler(data);
        context._allSearch(context.title).done(context._displayRelated);
    });
};

Twiddlers.prototype._setTitle = function () {
    this.title = document.location.hash.substring(1);
};

Twiddlers.prototype._displayRelated = function (tiddlers) {
    var list = $('#relatedlist');
    list.empty();
    list.append(this.listTemplate({ tiddlers: this._filterOutOriginalTiddler(tiddlers, this.bag) }));
    list.find('.timeago').timeago();
};

Twiddlers.prototype._filterOutOriginalTiddler = function (tiddlers, originalBag) {
    //TODO: provide implementation for IE < 9?
    return tiddlers.filter(function (tiddler) {
        return tiddler.bag !== originalBag;
    });
};

Twiddlers.prototype._displayTiddler = function (tiddler) {
    $('header').html(this.headerTemplate({title: tiddler.title, user: this.currentUser }));
    $('#local').html(this.tiddlerTemplate({ html: tiddler.render }));
};

Twiddlers.prototype._getLocalTiddler = function (title) {
    var deferred = new $.Deferred();

    var context = this;
    var success = function (data) {
        context.bag = data.bag;
        deferred.resolve(data);
    };
    this._getTiddler(title, success);

    return deferred.promise();
};

Twiddlers.prototype.loadTiddler = function (uri, elem) {
    var success = function (data) {
        $(elem).hide().html(data.render).slideDown();
    };
    var match = 'tiddlyspace.com/';
    uri = uri.substring(uri.indexOf(match) + match.length) + '?render=1';
    this._doGET(uri, success, this._ajaxError);
};

Twiddlers.prototype._getTiddler = function (title, success) {
    this._doGET('/' + encodeURIComponent(title) + '?render=1', success, this._ajaxError);
};

// XXX: somewhat dupe from TwiddlersCount!
// Only do this if the currentUser is not GUEST
Twiddlers.prototype._getFollowers = function () {
    if (this.followers.length === 0) {
        var context = this;
        var success = function (data, status, xhr) {
            var followers = $.trim(data).split('\n');
            context.followers = $.map(followers, function (item) {
                return item.replace(/^@/, '');
            });
            context._followSearch(context.followers).done(this._displayRelated);
        };
        var url = '/search.txt?q=bag:' + this.currentUser +
            '_public%20tag:follow' +
            '%20_limit:999';
        this._doGET(url, success, this._ajaxError);
    }
    this._followSearch(this.followers).done(this._displayRelated);
};


Twiddlers.prototype._search = function (url, success) {
    this._doGET(url, success, this._ajaxError);
};

Twiddlers.prototype._allSearch = function (title) {
    var context = this;
    var deferred = new $.Deferred();
    var url = '/search.json?q=title:"' + encodeURIComponent(title) + '"';

    var success = function (data, status, xhr) {
        deferred.resolveWith(context, [data]);
    };
    this._search(url, success);

    return deferred.promise();
};

Twiddlers.prototype._followSearch = function (followers) {
    var context = this;
    var deferred = new $.Deferred();
    var url = '/search?q=title:"' + encodeURIComponent(this.title) +
        '"%20' + 'modifier:' + followers.join('%20OR%20modifier:');

    var success = function (data, status, xhr) {
        deferred.resolveWith(context, [data]);
    };
    this._search(url, success);

    return deferred.promise();
};

Twiddlers.prototype._doGET = function (url, success, error) {<commit_msg>DRYing up the two kinds of search<commit_after>function Twiddlers(tiddlyweb) {
    this.title = undefined;
    this.bag = undefined;
    this.currentUser = tiddlyweb.status.username;
    this.serverHost = tiddlyweb.status.server_host;
    this.headerTemplate = this._getTemplate('#tiddler-header-template');
    this.tiddlerTemplate = this._getTemplate('#tiddler-template');
    this.listTemplate = this._getTemplate('#related-list-template');
    this.followers = [];
}

Twiddlers.prototype.init = function () {
    this._setTitle();
    this._bindUIEvents();
    this._registerHandlebarsHelpers();
};

Twiddlers.prototype._getTemplate = function (id) {
    return Handlebars.compile($(id).html());
};

Twiddlers.prototype.getTwiddlers = function () {
    var context = this;
    this._getLocalTiddler(this.title).done(function (data) {
        context._displayTiddler(data);
        context._allSearch(context.title).done(context._displayRelated);
    });
};

Twiddlers.prototype._setTitle = function () {
    this.title = document.location.hash.substring(1);
};

Twiddlers.prototype._displayRelated = function (tiddlers) {
    var list = $('#relatedlist');
    list.empty();
    list.append(this.listTemplate({ tiddlers: this._filterOutOriginalTiddler(tiddlers, this.bag) }));
    list.find('.timeago').timeago();
};

Twiddlers.prototype._filterOutOriginalTiddler = function (tiddlers, originalBag) {
    //TODO: provide implementation for IE < 9?
    return tiddlers.filter(function (tiddler) {
        return tiddler.bag !== originalBag;
    });
};

Twiddlers.prototype._displayTiddler = function (tiddler) {
    $('header').html(this.headerTemplate({title: tiddler.title, user: this.currentUser }));
    $('#local').html(this.tiddlerTemplate({ html: tiddler.render }));
};

Twiddlers.prototype._getLocalTiddler = function (title) {
    var deferred = new $.Deferred();

    var context = this;
    var success = function (data) {
        context.bag = data.bag;
        deferred.resolve(data);
    };
    this._getTiddler(title, success);

    return deferred.promise();
};

Twiddlers.prototype.loadTiddler = function (uri, elem) {
    var success = function (data) {
        $(elem).hide().html(data.render).slideDown();
    };
    var match = 'tiddlyspace.com/';
    uri = uri.substring(uri.indexOf(match) + match.length) + '?render=1';
    this._doGET(uri, success, this._ajaxError);
};

Twiddlers.prototype._getTiddler = function (title, success) {
    this._doGET('/' + encodeURIComponent(title) + '?render=1', success, this._ajaxError);
};

// XXX: somewhat dupe from TwiddlersCount!
// Only do this if the currentUser is not GUEST
Twiddlers.prototype._getFollowers = function () {
    if (this.followers.length === 0) {
        var context = this;
        var success = function (data, status, xhr) {
            var followers = $.trim(data).split('\n');
            context.followers = $.map(followers, function (item) {
                return item.replace(/^@/, '');
            });
            context._followSearch(context.followers).done(this._displayRelated);
        };
        var url = '/search.txt?q=bag:' + this.currentUser +
            '_public%20tag:follow' +
            '%20_limit:999';
        this._doGET(url, success, this._ajaxError);
    }
    this._followSearch(this.followers).done(this._displayRelated);
};


Twiddlers.prototype._search = function (url) {
    var context = this;
    var deferred = new $.Deferred()

    var success = function (data, status, xhr) {
        deferred.resolveWith(context, [data]);
    };
    this._doGET(url, success, this._ajaxError);

    return deferred.promise();
};

Twiddlers.prototype._allSearch = function (title) {
    var url = '/search.json?q=title:"' + encodeURIComponent(title) + '"';
    return this._search(url);
};

Twiddlers.prototype._followSearch = function (followers) {
    var url = '/search?q=title:"' + encodeURIComponent(this.title) +
        '"%20' + 'modifier:' + followers.join('%20OR%20modifier:');
    return this._search(url);
};

Twiddlers.prototype._doGET = function (url, success, error) {
{'fraction_non_alphanumeric': 0.15247761194029852, 'fraction_numerical': 0.004298507462686567, 'mean_word_length': 3.50564819795589, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '466758', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2602, 'n_words': 472}