diff --git a/README.md b/README.md index b3e0d45..568ddcc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +### NOTE: This project is no longer actively maintained. + # PyReact PyReact is a Python wrapper around the [React](http://facebook.github.io/react/) JavaScript library and [JSX](http://facebook.github.io/react/docs/jsx-in-depth.html). @@ -13,7 +15,7 @@ Specifically, it provides an API to transform JSX files into JavaScript from wit Alternatively, add it into your `requirements` file: - PyReact==0.4.1 + PyReact==0.6.0 **Dependencies**: PyReact uses [PyExecJS](https://github.com/doloopwhile/PyExecJS) to execute the bundled React code, which requires that a JS runtime environment is installed on your machine. We don't explicitly set a dependency on a runtime environment; Mac OS X comes bundled with one. If you're on a different platform, we recommend [PyV8](https://code.google.com/p/pyv8/). @@ -28,10 +30,10 @@ from react import jsx # For multiple paths, use the JSXTransformer class. transformer = jsx.JSXTransformer() for jsx_path, js_path in my_paths: - transformer.transform(jsx_path, js_path) + transformer.transform(jsx_path, js_path=js_path) # For a single file, you can use a shortcut method. -jsx.transform('path/to/input/file.jsx', 'path/to/output/file.js') +jsx.transform('path/to/input/file.jsx', js_path='path/to/output/file.js') ``` You can also use ``transform_string(jsx)`` method to transform strings: diff --git a/VERSIONS.md b/VERSIONS.md index a9953d8..475bcd3 100644 --- a/VERSIONS.md +++ b/VERSIONS.md @@ -11,3 +11,7 @@ This file lists the version(s) of React that are bundled with each version of Py | 0.3.1 | 0.10.0| | 0.4.0 | 0.11.0| | 0.4.1 | 0.11.2| +| 0.5.0 | 0.12.0| +| 0.5.1 | 0.12.1| +| 0.5.2 | 0.12.1| +| 0.6.0 | 0.13.3| diff --git a/react/__init__.py b/react/__init__.py index b155da1..4d1129d 100644 --- a/react/__init__.py +++ b/react/__init__.py @@ -13,5 +13,5 @@ # limitations under the License. -VERSION = '0.4.1' -REACT_VERSION = '0.11.2' +VERSION = '0.6.0' +REACT_VERSION = '0.13.3' diff --git a/react/jsx.py b/react/jsx.py index 2a8bba2..070c6f4 100644 --- a/react/jsx.py +++ b/react/jsx.py @@ -18,29 +18,37 @@ class JSXTransformer(object): + JSX_TRANSFORMER_JS_EXPR = '(global.JSXTransformer || module.exports)' + def __init__(self): path = react.source.path_for('JSXTransformer.js') with open(path, 'rU') as f: self.context = execjs.compile(f.read()) - def transform_string(self, jsx): + def transform_string(self, jsx, harmony=False, strip_types=False): """ Transform ``jsx`` JSX string into javascript :param jsx: JSX source code :type jsx: basestring + :keyword harmony: Transform ES6 code into ES3 (default: False) + :type harmony: bool + :keyword strip_types: Strip type declarations (default: False) + :type harmony: bool :return: compiled JS code :rtype: str """ + opts = {'harmony': harmony, 'stripTypes': strip_types} try: - result = self.context.call('JSXTransformer.transform', jsx) - js = result['code'] - return js + result = self.context.call( + '%s.transform' % self.JSX_TRANSFORMER_JS_EXPR, jsx, opts) except execjs.ProgramError as e: - raise TransformError(e.message[7:]) + raise TransformError(str(e)) + js = result['code'] + return js - def transform(self, jsx_path, js_path=None): + def transform(self, jsx_path, js_path=None, **kwargs): with open(jsx_path, 'rU') as i: - js = self.transform_string(i.read()) + js = self.transform_string(i.read(), **kwargs) if js_path: with open(js_path, 'wb') as o: o.write(js.encode('utf8')) @@ -52,5 +60,8 @@ def __init__(self, message): Exception.__init__(self, message) -def transform(jsx_path, js_path=None): - return JSXTransformer().transform(jsx_path, js_path) +def transform(jsx_path, **opts): + return JSXTransformer().transform(jsx_path, **opts) + +def transform_string(jsx, **opts): + return JSXTransformer().transform_string(jsx, **opts) diff --git a/react/test/files/malformed.jsx b/react/test/files/malformed.jsx index c51c230..d9708c3 100644 --- a/react/test/files/malformed.jsx +++ b/react/test/files/malformed.jsx @@ -1,8 +1,6 @@ -/** @jsx React.DOM */ - var testComponent =
; \ No newline at end of file + ; diff --git a/react/test/files/test.js b/react/test/files/test.js index 250af5c..b5066dc 100644 --- a/react/test/files/test.js +++ b/react/test/files/test.js @@ -1,9 +1,7 @@ -/** @jsx React.DOM */ - var testComponent = - React.DOM.div(null, - React.DOM.ul(null, - React.DOM.li(null, "Hello"), - React.DOM.li(null, "World") + React.createElement("div", null, + React.createElement("ul", null, + React.createElement("li", null, "Hello"), + React.createElement("li", null, "World") ) - ); \ No newline at end of file + ); diff --git a/react/test/files/test.jsx b/react/test/files/test.jsx index f47a6da..e836e31 100644 --- a/react/test/files/test.jsx +++ b/react/test/files/test.jsx @@ -1,9 +1,7 @@ -/** @jsx React.DOM */ - var testComponent =
-
; \ No newline at end of file + ; diff --git a/react/test/jsx.py b/react/test/jsx.py index 8b59ae6..bd749bf 100644 --- a/react/test/jsx.py +++ b/react/test/jsx.py @@ -33,3 +33,15 @@ def test_transform(self): self.assertRaises( jsx.TransformError, lambda: jsx.transform(malformed_path)) + + def test_transform_string(self): + result = jsx.transform_string('
') + self.assertEquals(result, 'React.createElement("div", null)') + + def test_transform_string_harmony(self): + result = jsx.transform_string('var {x} = y', harmony=True) + self.assertEquals(result, 'var $__0= y,x=$__0.x') + + def test_transform_string_strip_types(self): + result = jsx.transform_string('var x: Number = 1', strip_types=True) + self.assertEquals(result, 'var x = 1') diff --git a/react/utils/pipeline.py b/react/utils/pipeline.py index b95c762..7fc8932 100644 --- a/react/utils/pipeline.py +++ b/react/utils/pipeline.py @@ -34,4 +34,4 @@ def compile_file(self, infile, outfile, outdated=False, force=False): try: return self.transformer.transform(infile, outfile) except TransformError as e: - raise CompilerError(e.message) + raise CompilerError(str(e)) diff --git a/setup.py b/setup.py index cb82869..59ab67b 100644 --- a/setup.py +++ b/setup.py @@ -30,6 +30,6 @@ 'js/react/JSXTransformer.js', ]}, install_requires=[ - 'PyExecJS >= 1.0.4', + 'PyExecJS >= 1.0.5', ] )