Commits

David Chambers  committed aa00c62

`print_filesize` now expects a fully-functional path; `id_to_path` now provides such a path.

  • Participants
  • Parent commits bb2c173

Comments (0)

Files changed (1)

 
 def id_to_path(identifier, text):
 	"""
-	Finds the path after the [X] identifier in the text (used for filesize in our markdown files)
+	Finds the path after the [X] identifier in the text (used for filesize in our Markdown files)
 	
-	>>> text = get_contents('mango/examples/2=>basic-markdown-syntax.text')
-	>>> id_to_path('M', text)
-	u'2/m/'
+	>>> path = id_to_path('1', '[1]: /static/downloads/package.zip')
+	>>> path.endswith('/downloads/package.zip')
+	True
 	"""
 	m = re.search(r'(?m)^ {,3}\[' + identifier + r'\]:\s+/(\S+)', text)
 	try:
-		return m.group(1)
+		path = m.group(1)
 	except AttributeError:
 		return False
 
+	return os.path.join(PATH_TO_STATIC, *path.split('/')[1:])
+
 def print_filesize(path_to_file, plaintext=False):
 	"""
-	Prints the filesize of the file given in path_to_file (wrapped in an HTML span unless plaintext is true)
+	Prints the filesize of the file specified by `path_to_file`
+	(wrapped in an HTML span unless `plaintext` is True)
 	
-	#TODO - Can't test this atm as PATH_TO_STATIC is always added as we can't necessarily work around that
+	>>> head, tail = os.path.split(__file__)
+	>>> path_to_file = os.path.join(head, 'examples', '1=>my-first-post.text')
+	>>> print_filesize(path_to_file)
+	u'<span class="filesize">(258\u2009bytes)</span>'
+	>>> print_filesize(path_to_file, True)
+	u'(258\u2009bytes)'
+	>>> print_filesize('DOES_NOT_EXIST')
+	u''
 	"""
 	kb_size = KILOBYTE_SIZE
 	try:
-		filesize = os.path.getsize(os.path.join(PATH_TO_STATIC, path_to_file))
+		filesize = os.path.getsize(path_to_file)
 		bytes = (
 			('bytes', 1),
 			('kB', kb_size**1),
 					before = '<span class="filesize">' + before
 					after += '</span>'
 
-				return ''.join([before, text, after])
+				return u''.join([before, text, after])
 	except:
-		return '' # fail silently
+		return u'' # fail silently
 
 def parse_markdown(text, plaintext=False):
 	"""