google-books-citation

Create Terminals wiki citation from google books URL

https://github.com/legalizeadulthood/google-books-citation

Science Score: 18.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (6.1%) to scientific vocabulary
Last synced: 10 months ago · JSON representation ·

Repository

Create Terminals wiki citation from google books URL

Basic Info
  • Host: GitHub
  • Owner: LegalizeAdulthood
  • Language: JavaScript
  • Default Branch: master
  • Size: 1.4 MB
Statistics
  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created almost 12 years ago · Last pushed almost 12 years ago
Metadata Files
Readme Citation

README.md

This is a simple nodejs script to take a google books URL and emit a google books citation template that is suitable for use in the Terminals Wiki. Example:

```

node citation.js "http://books.google.com/books?id=n5qBImUV6NQC&lpg=PA63-IA15&pg=PA63-IA15#v=onepage&q&f=false" {{Computerworld | id=n5qBImUV6NQC | page_prefix=PA63-IA | page=15 | title= | date=September 23, 1985 }} ```

Note that the URL must be quoted if copy/pasted from the browser as it will contain the query character ? which is a shell metacharacter.

The script fetches the URL and uses it to identify the publication name (Computerworld), the page number (15) and the date (September 23, 1985). It can't identify the appropriate article title, so it leaves that blank and this must be filled in manually.

On Windows, it can be handy to simply feed the output of the script into clip to get the output onto the clipboard. The provided batch file cite.bat is a convenience that runs node with the given url and pipes to clip.

Owner

  • Name: Richard Thomson
  • Login: LegalizeAdulthood
  • Kind: user
  • Location: Salt Lake City, UT

Citation (citation.js)

var cheerio = require('cheerio');
var http = require('http');
var moment = require('moment');
var request = require('request');
var util = require('util');

function starts_with(text, prefix) {
    return text.substring(0, prefix.length) === prefix;
}

function citation_text(citation) {
    var text = '{{' + citation.publication,
        parts = citation.url.replace(/^.*\?/, '').replace(/#.*$/, '').split('&');
    for (var i = 0; i < parts.length; ++i) {
        if (starts_with(parts[i], 'id=')) {
            text += '\n| ' + parts[i];
        } else if (starts_with(parts[i], 'pg=')) {
            pieces = parts[i].match(/pg=(.+[^0-9])([0-9]+)$/);
            if (pieces[1] !== 'PA') {
                text += '\n| page_prefix=' + pieces[1];
            }
            text += '\n| page=' + pieces[2];
        }
    }
    text += '\n| title=';
    text += '\n| date=' + citation.date;
    citation.text = text + '\n}}';
}

function citation_from_html(citation, callback) {
    var $ = cheerio.load(citation.html),
        publication = $('h1.gb-volume-title'),
        date = $('span', publication);
    citation.date = moment(date.text()).format('MMMM D, YYYY');
    date.empty();
    citation.publication = publication.text().trim();
    delete citation.html;
    citation_text(citation);
    callback(null, citation);
}

function citation_from_url(url, callback) {
    request(url,
        function(err, res, data) {
            if (err) {
                callback(err);
            } else {
                citation_from_html({ url: url, html: data }, callback)
            }
        });
}

function main(args) {
    citation_from_url(args[2], function(err, data) {
        console.log(data.text);
    });
}

main(process.argv);

GitHub Events

Total
Last Year