summaryrefslogtreecommitdiffstats
path: root/lib/parse
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-28 20:16:30 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-28 20:16:30 +0200
commit5f1bfc02805e074ab47bc8966410cf8042363c48 (patch)
tree74072e8dbb2963e3697846dbc59efbb214f76d6b /lib/parse
parentb715901f9203daacf705ce4e9db47b3d75980996 (diff)
downloadgitbook-5f1bfc02805e074ab47bc8966410cf8042363c48.zip
gitbook-5f1bfc02805e074ab47bc8966410cf8042363c48.tar.gz
gitbook-5f1bfc02805e074ab47bc8966410cf8042363c48.tar.bz2
Fix #163: Move logic of git infos extraction to lib
Diffstat (limited to 'lib/parse')
-rw-r--r--lib/parse/git.js56
-rw-r--r--lib/parse/index.js3
2 files changed, 58 insertions, 1 deletions
diff --git a/lib/parse/git.js b/lib/parse/git.js
new file mode 100644
index 0000000..4478f20
--- /dev/null
+++ b/lib/parse/git.js
@@ -0,0 +1,56 @@
+var Q = require('q');
+var _ = require('lodash');
+var cp = require('child_process');
+var url = require('url');
+
+// Get the remote of a given repo
+function gitURL(path) {
+ var d = Q.defer();
+
+ cp.exec("git config --get remote.origin.url", {
+ cwd: path,
+ env: process.env,
+ }, function(err, stdout, stderr) {
+ if(err) {
+ return d.reject(err);
+ }
+
+ return d.resolve(stdout);
+ });
+
+ return d.promise
+ .then(function(output) {
+ return output.replace(/(\r\n|\n|\r)/gm, "");
+ });
+}
+
+// Poorman's parsing
+// Parse a git URL to a github ID : username/reponame
+function githubID(_url) {
+ // Remove .git if it's in _url
+ var sliceEnd = _url.slice(-4) === '.git' ? -4 : _url.length;
+
+ // Detect HTTPS repos
+ var parsed = url.parse(_url);
+ if(parsed.protocol === 'https:' && parsed.host === 'github.com') {
+ return parsed.path.slice(1, sliceEnd);
+ }
+
+ // Detect SSH repos
+ if(_url.indexOf('git@') === 0) {
+ return _url.split(':', 2)[1].slice(0, sliceEnd);
+ }
+
+ // None found
+ return null;
+}
+
+function titleCase(str) {
+ return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
+}
+
+module.exports = {
+ url: gitURL,
+ githubID: githubID,
+ titleCase: titleCase
+};
diff --git a/lib/parse/index.js b/lib/parse/index.js
index 0ebb03a..bb7779f 100644
--- a/lib/parse/index.js
+++ b/lib/parse/index.js
@@ -5,5 +5,6 @@ module.exports = {
lex: require('./lex'),
progress: require('./progress'),
navigation: require('./navigation'),
- readme: require('./readme')
+ readme: require('./readme'),
+ git: require('./git')
};