Browse Source

Merge pull request #7 from catlog/master

add tran cli tool to search for dictionary
Juanito Fatas 11 năm trước cách đây
mục cha
commit
7002c61ee5
5 tập tin đã thay đổi với 130 bổ sung0 xóa
  1. 29 0
      bin/tran
  2. 1 0
      dict.textile
  3. 15 0
      index.js
  4. 53 0
      lib/search.js
  5. 32 0
      package.json

+ 29 - 0
bin/tran

@@ -0,0 +1,29 @@
+#! /usr/bin/env node
+
+var program = require('commander');
+var search = require('../lib/search.js');
+
+
+/**
+ * CLI
+ */
+
+program
+  .version(require('../package.json').version)
+  .usage('<command> [options]');
+
+program
+  .command('search <name>')
+  .description('Search for translations')
+  .action(search);
+
+// TODO fork -> add -> pull request work flow
+
+//program
+  //.command('create')
+  //.description('Create a new abbreviation')
+  //.action(create);
+
+program.parse(process.argv);
+
+if (!program.args[0]) program.help();

+ 1 - 0
dict.textile

@@ -802,6 +802,7 @@ h2. S
 
 | *英文* | *译法 1* | 译法 2 | 译法 3 |
 | SPOT, Single Point of Truth | 真理的单点性 | | |
+| sprite | 精灵图 | | |
 | SNA, System Network Architecture | 系统网络体系 | | |
 | superfluous | 多余的 | | |
 | single-segment | 单段的 | | |

+ 15 - 0
index.js

@@ -0,0 +1,15 @@
+// search, auth, fork, commit, pull req
+var Client = require('github');
+var client = new Client({
+  version: '3.0.0'
+});
+client.repos.getContent({
+  user: 'JuanitoFatas',
+  repo: 'Computer-Science-Glossary',
+  path: 'dict.textile',
+  ref: 'master'
+}, function(err, res) {
+  if (!err) {
+    console.log(new Buffer(res.content, 'base64').toString('utf8'));
+  }
+});

+ 53 - 0
lib/search.js

@@ -0,0 +1,53 @@
+var fs = require('fs');
+var Fuse = require('fuse.js');
+var client = new (require('github'))({
+  version: '3.0.0'
+});
+require('colors');
+
+var notEmpty = function(str) { return !!str.trim(); };
+var trim = function(str) { return str.trim(); };
+
+var fetch = function(callback) {
+  // TODO cache
+  client.repos.getContent({
+    user: 'JuanitoFatas',
+    repo: 'Computer-Science-Glossary',
+    path: 'dict.textile',
+    ref: 'master'
+  }, function(err, res) {
+    callback(err, new Buffer(res.content, 'base64').toString('utf8'));
+  });
+};
+
+var search = function(content, key) {
+  var reg = /h2\. ([A-Z])([^]*?)(?=h2\. [A-Z])/g;
+  var match,
+      table = [];
+  while (match = reg.exec(content)) {
+    match[2].split('\n')
+      .filter(notEmpty)
+      .slice(1)
+      .forEach(function(entry) {
+        var words = entry.split('|').filter(notEmpty).map(trim);
+        table.push({origin: words[0], translations: words.slice(1)});
+      });
+  }
+  var fuse = new Fuse(table, {keys: ['origin']});
+  return fuse.search(key).slice(0, 3);
+};
+
+module.exports = function(key) {
+  fetch(function(err, content) {
+    if (err) throw err;
+
+    var results = search(content, key);
+    console.log('Fuzzy match including: '.grey);
+    results.forEach(function(entry) {
+      console.log(entry.origin.green);
+      entry.translations.forEach(function(translation) {
+        console.log('  ' + translation.blue);
+      });
+    });
+  });
+};

+ 32 - 0
package.json

@@ -0,0 +1,32 @@
+{
+  "name": "tran",
+  "version": "0.0.1",
+  "description": "translation tool for Computer-Science-Glossary",
+  "main": "index.js",
+  "bin": {
+    "tran": "./bin/tran"
+  },
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/JuanitoFatas/Computer-Science-Glossary.git"
+  },
+  "keywords": [
+    "glossary",
+    "dictionary",
+    "catlog"
+  ],
+  "author": "CatTail",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/JuanitoFatas/Computer-Science-Glossary/issues"
+  },
+  "dependencies": {
+    "github": "~0.1.12",
+    "fuse.js": "0.0.0",
+    "commander": "~2.1.0",
+    "colors": "~0.6.2"
+  }
+}