Skip to content

Commit

Permalink
Another step toward #7. Opens up more questions.
Browse files Browse the repository at this point in the history
  • Loading branch information
slashroot authored and slashroot committed Nov 4, 2012
1 parent 9799e76 commit f03bc8e
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions coldbrew/templatetags/coldbrew.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,25 +55,27 @@ def coffeescript(source_file_path):
else: else:
base_filename = filename base_filename = filename


output_directory = os.path.join(coldbrew_settings.COFFEESCRIPT_LOCATION,
coldbrew_settings.COFFEESCRIPT_OUTPUT_DIR,
os.path.dirname(source_file_path))
full_path = os.path.join(coldbrew_settings.COFFEESCRIPT_LOCATION, source_file_path) full_path = os.path.join(coldbrew_settings.COFFEESCRIPT_LOCATION, source_file_path)
hashed_mtime = get_hashed_mtime(full_path) hashed_mtime = get_hashed_mtime(full_path)

# TODO: Resolve #7 and fix this.
output_parent = settings.STATICFILES_DIRS[0]
output_directory = os.path.join(output_parent, coldbrew_settings.COFFEESCRIPT_OUTPUT_DIR)
output_path = os.path.join(output_directory, "%s-%s.js" % (base_filename, hashed_mtime)) output_path = os.path.join(output_directory, "%s-%s.js" % (base_filename, hashed_mtime))


# If the file already exists, we're not going to even bother reading the input again. # Now we know to which path we'll write; let's just make the URL.
if os.path.exists(output_path): url = "%s/%s-%s.js" % (settings.COFFEESCRIPT_OUTPUT_DIR,
url = "%s/%s-%s.js" % (settings.COFFEESCRIPT_OUTPUT_DIR,
base_filename, base_filename,
hashed_mtime hashed_mtime
) )

# If the file already exists, we're not going to even bother reading the input again.
# Instead, just return the URL.
if os.path.exists(output_path):
return url return url




coffeescript_string = get_string_from_path(full_path) coffeescript_string = get_string_from_path(full_path)


quiet, compile_result = compile(coffeescript_string) quiet, compile_result = compile(coffeescript_string)


if not quiet: if not quiet:
Expand All @@ -85,18 +87,10 @@ def coffeescript(source_file_path):
compiled_file.write(compile_result) compiled_file.write(compile_result)
compiled_file.close() compiled_file.close()


# Remove old files
compiled_filename = os.path.split(output_path)[-1]
for filename in os.listdir(output_directory):
if filename.startswith(base_filename) and filename != compiled_filename:
os.remove(os.path.join(output_directory, filename))

# If DEBUG is on, we want to see if a staticfiles directory is at the beginning # If DEBUG is on, we want to see if a staticfiles directory is at the beginning
# of our output_path. If it is, we know to use that path instead of STATIC_ROOT. # of our output_path. If it is, we know to use that path instead of STATIC_ROOT.
if settings.DEBUG: if settings.DEBUG:
for static_dir in settings.STATICFILES_DIRS: for static_dir in settings.STATICFILES_DIRS:
if output_path.startswith(static_dir): if output_path.startswith(static_dir):
return output_path[len(static_dir):].replace(os.sep, '/').lstrip("/") return output_path[len(static_dir):].replace(os.sep, '/').lstrip("/")

return url
relative_output_path = output_path[len(settings.STATIC_ROOT):].replace(os.sep, '/').lstrip("/")
return relative_output_path

0 comments on commit f03bc8e

Please sign in to comment.