2012-07-20 47 views
3

我想使用CoffeeScript(CS)和CS編譯器,但不必安裝Node.js.沒有node.js的CoffeeScript編譯器?

我看到CS的核心編譯器是用JavaScript編寫的,所以不需要安裝Node.js,但是我找不到任何用Python/Java或任何其他語言編寫的編譯器,只有Node.js 。

我錯過了什麼嗎?

我需要在Python中編寫自己的實現嗎? ;)

感謝您的幫助! :)

回答

2

你可以在python中使用http://code.google.com/p/pyv8/運行javascript。

您將運行javascript code for CoffeeScript compiler,然後用此編譯CoffeeScript。

+0

是的,我想過,即使有http://packages.python.org/watchdog/quickstart.html更新,但我想知道我沒有做一些已經存在的東西:/ – 2012-07-20 08:25:20

+0

@CyrilN。當然如果你能找到一個本地python cs編譯器會更好。使用js,你可以更快地獲得最新的更新。 – Esailija 2012-07-20 08:25:48

1

如果你不想安裝node.js,你可以在你的web瀏覽器中編譯它們。 如何做到這一點的粗略細節in the docs

如果你不想使用你的瀏覽器,你可以在任何能夠運行javascript的腳本中運行該腳本, Java中的Rhino,python中的pyv8等。

1

您可以使用類似於:http://hovet.biz/blog/?p=27 這是一個基於Python2的編譯器,使用CoffeScript編譯器和Qt WebKit的Js版本。請注意,不涉及Qt GUI,它只是文本。

而這裏的來源,要求:

import sys, os, glob 
import time, htmllib 
from os.path import splitext 
from PyQt5.QtCore import QUrl 
from PyQt5.QtWebKitWidgets import QWebPage 
from PyQt5.QtWidgets import QApplication 

coffee_script_js = os.path.expanduser('~/src/coffee-monitor/coffee-script.js') 

assert os.path.exists(coffee_script_js) 

sleep_time = 0.05 

class CoffeeMonitor(QWebPage): 
    def __init__(self, src_dir): 
     self._cs_src = None 
     self._div_start = '<div id="cssrc">' 
     self._app = QApplication(sys.argv) 
     QWebPage.__init__(self) 
     self.loadFinished.connect(self.load_finished) 

     self._cs_src_dir = src_dir 

     if not self.prep_compile_file(): 
      exit() 

     self._app.exec_() 

    def prep_compile_file(self): 
     while 1: 
      try: 
       src = self.get_cs_src() 

       if src: 
        self.mainFrame().load(QUrl(src)) 
        return True 

       time.sleep(sleep_time) 

      except KeyboardInterrupt: 
       break 

     return False 

    def create_cs_html(self, f): 
     f_html = f + '.html' 

     src = open(f).read() 

     fil = open(f_html, 'w') 
     fil.write('''<html> 
<body> 
'''+ self._div_start +''' </div> 
<script type="text/coffeescript"> 
''') 

     fil.write('try\n') 
     #fil.write(" src = CoffeeScript.compile '" + src.replace('\n', '\\n').replace("'", "\\'") + "', bare: on\n") 
     fil.write(" src = CoffeeScript.compile '" + src.replace('\n', '\\n').replace("'", "\\'") + "', bare: off\n") 

     fil.write(''' document.getElementById("cssrc").textContent = src 
catch {location, message} 
    if location? 
    message = "'''+f+''': Error on line #{location.first_line + 1}: #{message}" 
    document.getElementById("cssrc").textContent = message 

</script> 
<script src="''' + coffee_script_js + '''"></script> 
</body> 
</html> 
''') 
     fil.close() 

     return 'file://' + f_html 

    def get_cs_src(self): 
     #files = [] 
     #for exts in [ 'coffee', 'cof', 'coffe', 'cofee', 'cofe', 'kaffe' ]: 
     # files += glob.glob(self._cs_src_dir + '/*.' + exts) 
     files = glob.glob(self._cs_src_dir + '/*.coffee') 

     self._cs_src = None 

     for f in files: 
      name,ext = splitext(f) 
      js = name + '.js' 
      if not os.path.exists(js): 
       self._cs_src = f 
       return self.create_cs_html(f) 

      c_time_cof = os.stat(f).st_ctime 
      c_time_js = os.stat(js).st_ctime 
      if c_time_cof > c_time_js: 
       self._cs_src = f 
       return self.create_cs_html(f) 

    def get_compiled_js_src(self): 
     html = self.mainFrame().toHtml() 
     js_src = '' 
     error = False 

     for line in html.split('\n'): 
      if line.startswith(self._div_start): 
       js_src = line[len(self._div_start):] 
       if js_src.find('Error on line ') > 0: 
        js_src = '!'*5 + ' ' + js_src.rstrip('</div>\n') 
        js_src = 'alert("'+ js_src.replace('"', "'") +'");' 
        error = True 
        #print js_src 
        break 

      elif js_src: 
       js_src += line.rstrip('</div>\n') 
       if line.find('</div>') >= 0: 
        break 

     js_src = unescape(js_src) 

     return js_src, error 

    def load_finished(self, result): 
     js_src, error = self.get_compiled_js_src() 

     name,ext = splitext(self._cs_src) 
     js = name + '.js' 
     print '*** updating', js 

     if error: 
      print js_src 

     fil = open(js, 'w') 
     fil.write(js_src.replace(';',';\n'))  
     fil.close() 

     if not self.prep_compile_file(): 
      self._app.quit() 

def unescape(s): 
    p = htmllib.HTMLParser(None) 
    p.save_bgn() 
    p.feed(s) 
    return p.save_end() 

if __name__ == '__main__': 
    print 'This Python script comes with absolutely no warranty.' 
    print 'Ctrl+C to quit' 

    if len(sys.argv) == 1: 
     print 'coffee-monitor.py cs-src-dir (note: recursive search is not implemented)' 
     exit() 

    cs_dir = os.path.abspath(os.path.expanduser(sys.argv[1])) 

    assert os.path.isdir(cs_dir) 
    CoffeeMonitor(cs_dir) 
+0

歡迎來到Stack Overflow!這可能在理論上回答這個問題,但最好將未來用戶的答案的基本部分包括在內,並提供供參考的鏈接。 [link-dominated answers](// meta.stackexchange.com/questions/8231)可能通過[link rot](// en.wikipedia.org/wiki/Link_rot)失效。 – Mogsdad 2015-12-10 02:28:01

相關問題