diff --git a/README.markdown b/README.markdown index d99ae66..0c14ad5 100644 --- a/README.markdown +++ b/README.markdown @@ -55,7 +55,7 @@ This tool was developed to meet a desire to strip Debug and Test code from produ jsmacro doesn't bother to clean up extra whitespace or line-breaks that result in macro parsing, since that's the job of a JavaScript minifier (which in my case, is the tool that runs next in my build process, right after jsmacro.) - + Supported Macros ---------------- @@ -93,6 +93,10 @@ Future Ideas Changes ------- +v0.2.18 + + - Adds support for a //@include foo.js macro. + v0.2.14 - jsmacro.py now runs on Python 3. Testing environments include: Python 2.6, Python 2.7, PyPy 1.6 (which is Python 2.7.1), and Python 3.2. diff --git a/jsmacro.py b/jsmacro.py index 5fab002..448bea3 100644 --- a/jsmacro.py +++ b/jsmacro.py @@ -59,7 +59,7 @@ def __init__(self): # //@MACRO # ...some code # //@end - self.re_wrapped_macro = re.compile("(\s*\/\/[\@|#])([a-z]+)\s+(\w*?\s)(.*?)(\s*\/\/[\@|#]end(if)?)", re.M | re.S) + self.re_wrapped_macro = re.compile("(\s*\/\/[\@|#])([a-z]+)\s+([\w\.\-\_\/]*?\s)(.*?)(\s*\/\/[\@|#]end(if)?)", re.M | re.S) self.reset() @@ -136,6 +136,15 @@ def handle_ifndef(self, arg, text): else: return "\n{s}".format(s=parts[0]) + def handle_include(self, arg, text): + """ + Used to include an external (JavaScript) file. + """ + # open the file (relative to the src file we're working with) + # run the parser over it + # return the output + return self.parse(os.path.realpath('{base}/{arg}'.format(base=self._basepath, arg=arg))) + def handle_macro(self, mo): method = mo.group(2) args = mo.group(3).strip() @@ -149,6 +158,9 @@ def handle_macro(self, mo): def parse(self, file_name): now = datetime.now() + # Save this for the @import implementation + self._basepath = os.path.realpath(os.path.dirname(file_name)) + fp = open(file_name, 'r') text = fp.read() fp.close() diff --git a/testfiles/include-data.js b/testfiles/include-data.js new file mode 100644 index 0000000..d1348aa --- /dev/null +++ b/testfiles/include-data.js @@ -0,0 +1,3 @@ + + + var bar = "Hello World"; \ No newline at end of file diff --git a/testfiles/include-in.js b/testfiles/include-in.js new file mode 100644 index 0000000..d889589 --- /dev/null +++ b/testfiles/include-in.js @@ -0,0 +1,16 @@ + +var foo = function() { + alert('starting...'); + + //@include ../testfiles/include-data.js + //@end + + //@include include-data.js + //@end + + alert('This.'); + alert('That.'); +}; + +//@include ifdef-hash-in.js +//@end diff --git a/testfiles/include-out.js b/testfiles/include-out.js new file mode 100644 index 0000000..d532030 --- /dev/null +++ b/testfiles/include-out.js @@ -0,0 +1,17 @@ + +var foo = function() { + alert('starting...'); + + var bar = "Hello World"; + + var bar = "Hello World"; + + alert('This.'); + alert('That.'); +}; + +var foo = function() { + + alert('Foo is defined'); +}; +