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

Node.js Support #23

Open
loretoparisi opened this issue Mar 23, 2016 · 2 comments
Open

Node.js Support #23

loretoparisi opened this issue Mar 23, 2016 · 2 comments

Comments

@loretoparisi
Copy link

It would be awesome to add node.js support for the ruby library.

@loretoparisi
Copy link
Author

I'm not sure if it would work (like fs access), but I was trying Opal so using at least

http://cdn.opalrb.org/opal/current/opal.min.js
http://cdn.opalrb.org/opal/current/opal-parser.min.js

getting (from http://opalrb.org/try/#)

/* Generated by Opal 0.9.0 */
(function(Opal) {
    Opal.dynamic_require_severity = "error";
    var OPAL_CONFIG = {
        method_missing: true,
        arity_check: false,
        freezing: true,
        tainting: true
    };

    function $rb_plus(lhs, rhs) {
        return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs + rhs : lhs['$+'](rhs);
    }

    function $rb_minus(lhs, rhs) {
        return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs - rhs : lhs['$-'](rhs);
    }
    var self = Opal.top,
        $scope = Opal,
        nil = Opal.nil,
        $breaker = Opal.breaker,
        $slice = Opal.slice,
        $klass = Opal.klass,
        $hash2 = Opal.hash2,
        $range = Opal.range;

    Opal.add_stubs(['$attr_accessor', '$open', '$gets', '$split', '$[]=', '$+', '$dirname', '$[]', '$sub', '$fetch', '$init_word_table', '$map!', '$to_proc', '$include?', '$ord', '$chop!', '$join', '$init_table', '$each', '$sprintf', '$first', '$unpack', '$<<', '$chomp', '$downcase!', '$capitalize!', '$to_i', '$tr!', '$to_sym', '$-', '$size', '$=~', '$!=', '$last', '$scan', '$alias_method']);
    return (function($base, $super) {
        function $Pinyin() {};
        var self = $Pinyin = $klass($base, $super, 'Pinyin', $Pinyin);

        var def = self.$$proto,
            $scope = self.$$scope;

        Opal.cdecl($scope, 'TONE_MARK', $hash2(["a", "o", "e", "i", "u", "v"], {
            "a": ["ā", "á", "ǎ", "à", "a"],
            "o": ["ō", "ó", "ǒ", "ò", "o"],
            "e": ["ē", "é", "ě", "è", "e"],
            "i": ["ī", "í", "ǐ", "ì", "i"],
            "u": ["ū", "ú", "ǔ", "ù", "u"],
            "v": ["ǖ", "ǘ", "ǚ", "ǜ", "ü"]
        }));

        return (function(self) {
            var $scope = self.$$scope,
                def = self.$$proto,
                TMP_6;

            self.$attr_accessor("table");
            self.$attr_accessor("ruby2");
            Opal.defn(self, '$init_table', function() {
                var $a, $b, TMP_1, self = this,
                    datfile = nil;
                if (self.table == null) self.table = nil;

                if ((($a = self.table) !== nil && (!$a.$$is_boolean || $a == true))) {
                    return nil
                };
                datfile = "pinyin-utf8.dat";
                self.table = $hash2([], {});
                return ($a = ($b = $scope.get('File')).$open, $a.$$p = (TMP_1 = function(file) {
                    var self = TMP_1.$$s || this,
                        $a, $b, $c, line = nil,
                        key = nil,
                        value = nil;
                    if (self.table == null) self.table = nil;
                    if (file == null) file = nil;
                    while ((($b = line = file.$gets()) !== nil && (!$b.$$is_boolean || $b == true))) {
                        $c = line.$split(" ", 2), $b = Opal.to_ary($c), key = ($b[0] == null ? nil : $b[0]), value = ($b[1] == null ? nil : $b[1]), $c;
                        self.table['$[]='](key, value);
                    }
                }, TMP_1.$$s = self, TMP_1), $a).call($b, $rb_plus($scope.get('File').$dirname("(file)"), "/../data/" + (datfile)));
            });
            Opal.defn(self, '$init_word_table', function() {
                var $a, $b, TMP_2, self = this;
                if (self.words_table == null) self.words_table = nil;

                if ((($a = self.words_table) !== nil && (!$a.$$is_boolean || $a == true))) {
                    return nil
                };
                self.words_table = $hash2([], {});
                if ((($a = $scope.get('ENV')['$[]']("WORDS_FILE")) !== nil && (!$a.$$is_boolean || $a == true))) {
                    return ($a = ($b = $scope.get('File')).$open, $a.$$p = (TMP_2 = function(file) {
                        var self = TMP_2.$$s || this,
                            $a, $b, $c, line = nil,
                            key = nil,
                            value = nil;
                        if (self.words_table == null) self.words_table = nil;
                        if (file == null) file = nil;
                        while ((($b = line = file.$gets()) !== nil && (!$b.$$is_boolean || $b == true))) {
                            $c = line.$sub("\n", "").$split("|", 2), $b = Opal.to_ary($c), key = ($b[0] == null ? nil : $b[0]), value = ($b[1] == null ? nil : $b[1]), $c;
                            self.words_table['$[]='](key, value);
                        }
                    }, TMP_2.$$s = self, TMP_2), $a).call($b, $scope.get('ENV')['$[]']("WORDS_FILE"))
                } else {
                    return nil
                };
            });
            Opal.defn(self, '$translate', TMP_6 = function(chars, options) {
                var $a, $b, $c, $d, TMP_3, $e, TMP_4, self = this,
                    $iter = TMP_6.$$p,
                    $yield = $iter || nil,
                    splitter = nil,
                    tonemarks = nil,
                    tone = nil,
                    camel = nil,
                    results = nil,
                    is_english = nil;
                if (self.words_table == null) self.words_table = nil;

                if (options == null) {
                    options = $hash2([], {})
                }
                TMP_6.$$p = null;
                splitter = options.$fetch("splitter", " ");
                tonemarks = options.$fetch("tonemarks", false);
                tone = options.$fetch("tone", ((($a = false) !== false && $a !== nil) ? $a : tonemarks));
                camel = options.$fetch("camelcase", false);
                self.$init_word_table();
                results = self.words_table['$[]'](chars);
                if (results !== false && results !== nil) {
                    results = results.$split();
                    ($a = ($b = results)['$map!'], $a.$$p = "downcase".$to_proc(), $a).call($b);
                    if (camel !== false && camel !== nil) {
                        ($a = ($c = results)['$map!'], $a.$$p = "capitalize".$to_proc(), $a).call($c)
                    };
                    if (tone !== false && tone !== nil) {} else {
                        ($a = ($d = results)['$map!'], $a.$$p = (TMP_3 = function(x) {
                            var self = TMP_3.$$s || this,
                                $a;
                            if (x == null) x = nil;
                            if ((($a = ($range(48, 57, false))['$include?'](x['$[]'](-1).$ord())) !== nil && (!$a.$$is_boolean || $a == true))) {
                                return x['$chop!']()
                            } else {
                                return x
                            }
                        }, TMP_3.$$s = self, TMP_3), $a).call($d)
                    };
                    return results.$join(splitter);
                };
                self.$init_table();
                results = [];
                is_english = false;
                ($a = ($e = chars.$scan(/./)).$each, $a.$$p = (TMP_4 = function(char$) {
                    var self = TMP_4.$$s || this,
                        $a, $b, TMP_5, key = nil,
                        pinyin = nil,
                        tone_index = nil;
                    if (self.ruby2 == null) self.ruby2 = nil;
                    if (self.table == null) self.table = nil;
                    if (char$ == null) char$ = nil;
                    key = (function() {
                        if ((($a = self.ruby2) !== nil && (!$a.$$is_boolean || $a == true))) {
                            return char$
                        } else {
                            return self.$sprintf("%X", char$.$unpack("U").$first())
                        };
                        return nil;
                    })();
                    if ((($a = self.table['$[]'](key)) !== nil && (!$a.$$is_boolean || $a == true))) {
                        if (is_english !== false && is_english !== nil) {
                            results['$<<'](splitter)
                        };
                        is_english = false;
                        pinyin = self.table['$[]'](key).$chomp().$split(" ", 2)['$[]'](0);
                        if ((($a = self.ruby2) !== nil && (!$a.$$is_boolean || $a == true))) {} else {
                            pinyin['$downcase!']()
                        };
                        if (tone !== false && tone !== nil) {} else {
                            pinyin['$chop!']()
                        };
                        if (camel !== false && camel !== nil) {
                            pinyin['$capitalize!']()
                        };
                        if (tonemarks !== false && tonemarks !== nil) {
                            tone_index = pinyin['$[]'](-1).$to_i();
                            pinyin = pinyin['$[]']($range(0, -1, true));
                            ($a = ($b = ["a", "o", "e", "i", "u", "v"]).$each, $a.$$p = (TMP_5 = function(v) {
                                var self = TMP_5.$$s || this,
                                    $a;
                                if (v == null) v = nil;
                                if ((($a = pinyin['$tr!'](v, $scope.get('TONE_MARK')['$[]'](v.$to_sym())['$[]']($rb_minus(tone_index, 1)))) !== nil && (!$a.$$is_boolean || $a == true))) {
                                    return ($breaker.$v = nil, $breaker)
                                } else {
                                    return nil
                                }
                            }, TMP_5.$$s = self, TMP_5), $a).call($b);
                        };
                        if (($yield !== nil)) {
                            return results['$<<']((((($a = Opal.yieldX($yield, [pinyin, results.$size()])) === $breaker) ? $breaker.$v : $a)))
                        } else {
                            results['$<<'](pinyin);
                            return results['$<<'](splitter);
                        };
                    } else {
                        if ((($a = char$['$=~'](/[a-zA-Z0-9]/)) !== nil && (!$a.$$is_boolean || $a == true))) {
                            results['$<<'](char$)
                        } else if ((($a = results.$last()['$!='](splitter)) !== nil && (!$a.$$is_boolean || $a == true))) {
                            results['$<<'](splitter)
                        };
                        return is_english = true;
                    };
                }, TMP_4.$$s = self, TMP_4), $a).call($e);
                return results.$join("").$chomp(splitter);
            });
            return self.$alias_method("t", "translate");
        })(Opal.get_singleton_class(self));
    })($scope.base, null)
})(Opal);

The source was a bit worst, so I have used a JavaScript Beautifier to make it more readable.

Also I'm not sure how Opal handles the file access here: datfile = "pinyin-utf8.dat";
The file compiles and run in JavaScript, so it seems to be formally correct. I have tried it in this JSFiddle

@loretoparisi
Copy link
Author

So I removed the dictionary, just to make a test and replacing with some key value lines so that in the init_table method

def init_table
      return if @table

      @table  = {}

      @table["鼸"] = "xian3"
      @table["鼺"] = "yan3"
      @table["鼻"] = "lei2"
      @table["鼼"] = "yao3"

    end

But when I try to compile in Opal I get some errors now:

unpack: undefined method `unpack' for "中":String
unpack: undefined method `unpack' for "":String
    at $.$new (http://opalrb.org/javascripts/try.js:6:8830)
    at String.Opal.defn.TMP_5 (http://opalrb.org/javascripts/try.js:5:20896)
    at String.n [as $unpack] (http://opalrb.org/javascripts/try.js:4:24433)
    at eval (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:95:46)
    at $a.$$p.TMP_3 (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:96:28)
    at E.yield1 (http://opalrb.org/javascripts/try.js:4:29046)
    at Array.e.defn.y [as $each] (http://opalrb.org/javascripts/try.js:8:28187)
    at $.Opal.defn.TMP_5 (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:134:46)
    at eval (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:140:42)
    at eval (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:141:3)

This error unpack: undefined methodunpack'seems to be related to theOpal/Ruby` versions.
Reported in opal/opal#1413

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant