Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ for jsx_path, js_path in my_paths:

# For a single file, you can use a shortcut method.
jsx.transform('path/to/input/file.jsx', js_path='path/to/output/file.js')

# You can also define the path to react.
transformer = jsx.JSXTransformer('path/to/react')
jsx.transform('path/to/input/file.jsx', js_path='path/to/output/file.js', react_path='...')
```

You can also use ``transform_string(jsx)`` method to transform strings:
Expand Down
2 changes: 2 additions & 0 deletions react/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-

# Copyright 2013 Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
23 changes: 15 additions & 8 deletions react/jsx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-

# Copyright 2013 Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,17 +14,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import execjs
import react.source


class JSXTransformer(object):

JSX_TRANSFORMER_JS_EXPR = '(global.JSXTransformer || module.exports)'
react_path = os.path.abspath(os.path.join(__file__, 'js', 'react'))

def __init__(self):
path = react.source.path_for('JSXTransformer.js')
with open(path, 'rU') as f:
def __init__(self, react_path=None):
if react_path is not None:
self.react_path = react_path
with open(os.path.join(self.react_path, 'JSXTransformer.js'), 'rU') as f:
self.context = execjs.compile(f.read())

def transform_string(self, jsx, harmony=False, strip_types=False):
Expand Down Expand Up @@ -56,12 +61,14 @@ def transform(self, jsx_path, js_path=None, **kwargs):


class TransformError(Exception):
def __init__(self, message):
Exception.__init__(self, message)
pass


def transform(jsx_path, **opts):
return JSXTransformer().transform(jsx_path, **opts)
react_path = opts.pop('react_path', None)
return JSXTransformer(react_path=react_path).transform(jsx_path, **opts)


def transform_string(jsx, **opts):
return JSXTransformer().transform_string(jsx, **opts)
react_path = opts.pop('react_path', None)
return JSXTransformer(react_path=react_path).transform_string(jsx, **opts)
25 changes: 0 additions & 25 deletions react/source.py

This file was deleted.

7 changes: 4 additions & 3 deletions react/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-

# Copyright 2013 Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from os.path import dirname, abspath

import os

TEST_ROOT = abspath(dirname(__file__))
TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
8 changes: 4 additions & 4 deletions react/test/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-

# Copyright 2013 Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,8 +17,6 @@
from jsx import *
from source import *

import unittest


if __name__ == '__main__':
unittest.main()
import unittest
unittest.main()
15 changes: 9 additions & 6 deletions react/test/jsx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-

# Copyright 2013 Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,24 +14,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from os.path import join
import os
import unittest

from react import jsx
from react.test import TEST_ROOT
from unittest import TestCase


class TestJSXTransformer(TestCase):
class TestJSXTransformer(unittest.TestCase):

def test_transform(self):
jsx_path = join(TEST_ROOT, 'files/test.jsx')
js_path = join(TEST_ROOT, 'files/test.js')
jsx_path = os.path.join(TEST_ROOT, 'files/test.jsx')
js_path = os.path.join(TEST_ROOT, 'files/test.js')

with open(js_path, 'rU') as js:
self.assertEquals(
jsx.transform(jsx_path),
unicode(js.read()))

malformed_path = join(TEST_ROOT, 'files/malformed.jsx')
malformed_path = os.path.join(TEST_ROOT, 'files/malformed.jsx')
self.assertRaises(
jsx.TransformError,
lambda: jsx.transform(malformed_path))
Expand Down
31 changes: 0 additions & 31 deletions react/test/source.py

This file was deleted.

17 changes: 10 additions & 7 deletions react/utils/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-

# Copyright 2013 Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -18,20 +20,21 @@
from pipeline.exceptions import CompilerError
from react.jsx import JSXTransformer, TransformError


class JSXCompiler(CompilerBase):

output_extension = 'js'

def __init__(self, *args, **kwargs):
CompilerBase.__init__(self, *args, **kwargs)
super(JSXCompiler, self).__init__(*args, **kwargs)
self.transformer = JSXTransformer()

def match_file(self, path):
return path.endswith('.jsx')

def compile_file(self, infile, outfile, outdated=False, force=False):
if not outdated and not force:
return
try:
return self.transformer.transform(infile, outfile)
except TransformError as e:
raise CompilerError(str(e))
if outdated or force:
try:
return self.transformer.transform(infile, outfile)
except TransformError as e:
raise CompilerError(str(e))
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- encoding: utf-8 -*-

from setuptools import setup, find_packages

from react import VERSION


Expand All @@ -11,7 +14,7 @@
license='Apache-2.0',
description='Python bridge to JSX & the React JavaScript library.',
long_description=open('DESCRIPTION').read(),
classifiers =[
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
Expand Down