Skip to content

Commit

Permalink
Adds an include macro for ticket #8
Browse files Browse the repository at this point in the history
  • Loading branch information
smartt committed Sep 11, 2012
1 parent 29a8cb8 commit 5103d44
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------

Expand Down Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion jsmacro.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self):
# //@MACRO <ARGUMENTS>
# ...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()

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions testfiles/include-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


var bar = "Hello World";
16 changes: 16 additions & 0 deletions testfiles/include-in.js
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions testfiles/include-out.js
Original file line number Diff line number Diff line change
@@ -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');
};

0 comments on commit 5103d44

Please sign in to comment.