Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redundant terms #3517

Merged

Conversation

Projects
None yet
3 participants
@fbennett
Copy link
Member

commented May 22, 2018

In reference to #3491, there are 253 styles in the repository with similarly redundant terms. This pull request fixes them all. Here is the node script used to apply the fix (installed in the top level of the repo and run from there, after fetching its dependencies from npm):

var fs = require('fs');
var xml = require('xmldom').DOMParser;
var xpath = require('xpath');
var enc = require('encode-decode');
var select = xpath.useNamespaces({"cs": "http://purl.org/net/xbiblio/csl"});

var ret = {};

function EntityFixer() {
    this.dat = {};
    var lurkers = {
        "<": "{LESS-THAN}",
        ">": "{GREATER-THAN}",
        " ": "{NO-BREAK-SPACE}"
    }
    this.memo = function(txt) {
        for (key in lurkers) {
            var rex = new RegExp(key, "g");
            txt = txt.replace(rex, lurkers[key]);
        }
        var m = txt.match(/(\&\#[0-9]+;)/g);
        if (m) {
            for (var memo of m) {
                this.dat[memo] = enc.htmlDecode(memo);
            }
        }
        return txt;
    }
    this.dememo = function(txt) {
        for (var key in this.dat) {
            var rex = new RegExp(this.dat[key], "g");
            txt = txt.replace(rex, key);
        }
        for (key in lurkers) {
            var rex = new RegExp(lurkers[key], "g");
            txt = txt.replace(rex, key);
        }
        return txt;
    }
}



for (var fn of fs.readdirSync('.')) {
    var writeToFile = false;
    var entityFixer = new EntityFixer();
    if (fn.slice(-4) !== ".csl") continue;
    var txt = fs.readFileSync(fn).toString();
    txt = entityFixer.memo(txt);
    var doc = new xml().parseFromString(txt);
    var termNodes = select("//cs:locale/cs:terms/cs:term", doc);
    for (var termNode of termNodes) {
        var single = select(".//cs:single", termNode)[0];
        var multiple = select(".//cs:multiple", termNode)[0];
        if (single) {
            single = single.textContent ? single.textContent : false;
            multiple = multiple.textContent ? multiple.textContent : false;
            if (single && single === multiple) {
	            for (var i=0,ilen=termNode.childNodes.length;i<ilen;i++) {
	                termNode.removeChild(termNode.childNodes[0]);
	            }
                var textNode = doc.createTextNode(single);
	            termNode.appendChild(textNode);
	            writeToFile = true;
            }
        }
    }
    if (writeToFile) {
        txt = entityFixer.dememo(doc.toString());
        fs.writeFileSync(fn, txt + "\n");
    }
}
@csl-bot

This comment has been minimized.

Copy link

commented May 22, 2018

Awesome! You just created a pull request to the Citation Styles Language styles repository. One of our human volunteers will try to get in touch soon (usually within a week). In the meantime, I will run some automated checks. You should be notified of the results in a few minutes.

If you haven't done so yet, please make sure your style validates and follows all our other Style Requirements.

To update this pull request, visit the "Files changed" tab above, and click on the pencil icon (see below) in the top-right corner of your style to start editing.

If you have any questions, please leave a comment and we'll get back to you. While we usually respond in English, feel free to write in whatever language you're most comfortable.

@csl-bot

This comment has been minimized.

Copy link

commented May 22, 2018

😃 Your submission passed all our automated tests.

@adam3smith adam3smith merged commit 9296c8c into citation-style-language:master May 22, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@adam3smith

This comment has been minimized.

Copy link
Member

commented May 22, 2018

woah, cool, thanks!

@fbennett fbennett deleted the fbennett:fix-redundant-terms branch May 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.