Permalink
Browse files

change !code macro to use file paths

  • Loading branch information...
1 parent 0c40ba6 commit 47e514aad5667e16139397a828efdb3a4a8b1f39 Chris Anderson committed Mar 2, 2009
Showing with 16 additions and 25 deletions.
  1. +5 −3 README.md
  2. +11 −22 python/couchapp/file_manager.py
View
@@ -53,22 +53,24 @@ CouchApp provides some neat helpers for getting code into your view and render f
### !code for code
-The `!code path.to.code` macro inserts the string value found at `path.to.code` of the design document, into the current file, at the position of the macro. Here's an example:
+The `!code path/to/code.js` macro inserts the content of the named file, into the current file, at the position of the macro. Here's an example:
function(doc) {
- // !code lib.parser.html
+ // !code lib/parsers/parse_html.js
var parsed = new parseHTML(doc.html);
emit(doc.key, parsed);
}
-When you run `couchapp push` the `!code` line will be replaced with the contents of the file found at `lib/parser/html.js`. Simple as that.
+When you run `couchapp push` the `!code` line will be replaced with the contents of the file found at `lib/parsers/parse_html.js`. Simple as that.
As we begin to see more apps built using CouchApp, we plan to include helpful functions in the standard library (as supplied by `couchapp generate`). Thank you for helping us expand this section! :)
### !json for data
After all the `!code` includes have been processed (insuring that included code may also use the `!json` macro), `couchapp push` does another pass through the function, running the `!json path.to.json` macros. This accumulates the data found in the JSON design doc at `path.to.json` into a single object for inclusion. After all the `!json` macros have been processed, the accumlated object is serialized to json and stored in variable names corresponding to their path's roots.
+The JSON paths use JavaScript style dot notation. Address the nodes as though you are running inside a `with(designDoc){ ... }` block. See the examples below.
+
Here's an example. It's a lot of code to look at, but the principles are simple. Also, if you think you can explain it better than I have, please send a patch.
#### A Subset of the Design Doc Fields (for context)
@@ -575,40 +575,29 @@ def apply_lib(self, funcs, app_dir, verbose=False):
continue
old_v = v
try:
- funcs[k] = self.process_include(self.process_requires(v, app_dir, verbose=verbose),
- app_dir, verbose=verbose)
+ funcs[k] = self.run_json_macros(
+ self.run_code_macros(v, app_dir, verbose=verbose),
+ app_dir, verbose=verbose)
except ValueError, e:
print >>sys.stderr, "Error running !code or !json on function \"%s\": %s" % (k, e)
sys.exit(-1)
if old_v != funcs[k]:
self.objects[_md5(funcs[k].encode('utf-8')).hexdigest()] = old_v
- def process_requires(self, f_string, app_dir, verbose=False):
+ def run_code_macros(self, f_string, app_dir, verbose=False):
def rreq(mo):
- if mo.group(2).startswith('_attachments'):
- # someone want to include from attachments
- # for now just read the file and return it
- path = os.path.join(app_dir, mo.group(2).strip(' '))
- try:
- library = read_file(path)
- except IOError, e:
- if verbose>=2:
- print >>sys.stderr, e
- return f_string
- return library
-
- fields = mo.group(2).split('.')
- library = self.doc
- for field in fields:
- if not field in library:
- raise ValueError("Can't find file: %s" % mo.group(2))
- library = library[field]
+ # just read the file and return it
+ path = os.path.join(app_dir, mo.group(2).strip(' '))
+ try:
+ library = read_file(path)
+ except IOError, e:
+ raise ValueError("Can't find file: %s" % mo.group(2))
return library
re_code = re.compile('(\/\/|#)\ ?!code (.*)')
return re_code.sub(rreq, f_string)
- def process_include(self, f_string, app_dir, verbose=False):
+ def run_json_macros(self, f_string, app_dir, verbose=False):
included = {}
varstrings = []

0 comments on commit 47e514a

Please sign in to comment.