Commits

David Chambers committed aa16841

Moved `id_to_path` and `print_filesize` to `models.py` (to avoid circular imports). Fixed code responsible for filesize substitutions (hopefully).

Comments (0)

Files changed (2)

 md = markdown.Markdown(extensions=('meta',) + MARKDOWN_EXTENSIONS)
 update_template = loader.get_template('update.dhtml')
 
+def id_to_path(identifier, text):
+    """
+    Finds the path after the [X] identifier in the text (used for filesize in our Markdown files)
+    
+    >>> path = id_to_path('1', '[1]: /static/downloads/package.zip')
+    >>> path.endswith('/downloads/package.zip')
+    True
+    """
+    m = re.search(r'(?m)^ {,3}\[%s\]:\s+/(\S+)' % identifier, text)
+    try:
+        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 specified by `path_to_file`
+    (wrapped in an HTML span unless `plaintext` is True)
+    
+    >>> 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(path_to_file)
+        bytes = (
+            ('bytes', 1),
+            ('kB', kb_size**1),
+            ('MB', kb_size**2),
+            ('GB', kb_size**3),
+            ('TB', kb_size**4),
+        )
+        for t in bytes:
+            if filesize <= t[1] * kb_size:
+                if t[0] == 'bytes':
+                    text = u'%s\u2009bytes' % filesize
+                else:
+                    text = u'≈%s\u2009%s' % (round(float(filesize)/t[1], 1), t[0])
+
+                before, after = '(', ')'
+                if not plaintext:
+                    before = '<span class="filesize">' + before
+                    after += '</span>'
+
+                return u''.join([before, text, after])
+    except:
+        return u'' # fail silently
+
 class Document:
     def __init__(self,
                  body,
         self.html = html
 
     def convert(self):
-        self.body = re.sub(RE['\r\n?'], '\n', self.body) + '\n' # keep regular expressions as simple as possible
+        body = re.sub(RE['\r\n?'], '\n', self.body) + '\n' # keep regular expressions as simple as possible
+        body = body.replace('\\', r'\\') # double up literal backslashes
+
         self.body = re.sub(RE['ref-style'], # e.g. Download the [icon set][1] {{ filesize }}.
-                lambda m: m.group(1) + print_filesize(id_to_path(m.group('id'), text), plaintext=plaintext), self.body)
+                lambda m: m.group(1) + print_filesize(id_to_path(m.group('id'), self.body), plaintext=True), self.body)
         self.body = re.sub(RE['inline'], # e.g. Download the [icon set](/downloads/icon-set.zip) {{ filesize }}.
-                lambda m: m.group(1) + print_filesize(m.group('path'), plaintext=plaintext), self.body)
+                lambda m: m.group(1) + print_filesize(m.group('path'), plaintext=True), self.body)
 
         # don't touch self.body beyond this point
-        body = self.body.replace('\\', r'\\') # double up literal backslashes
+
+        body = re.sub(RE['ref-style'],
+                lambda m: m.group(1) + print_filesize(id_to_path(m.group('id'), body), plaintext=False), body)
+        body = re.sub(RE['inline'],
+                lambda m: m.group(1) + print_filesize(m.group('path'), plaintext=False), body)
 
         # excerpts
         snippets = []
 from mango.models import Document
 from mango.settings import *
 
-def id_to_path(identifier, text):
-    """
-    Finds the path after the [X] identifier in the text (used for filesize in our Markdown files)
-    
-    >>> path = id_to_path('1', '[1]: /static/downloads/package.zip')
-    >>> path.endswith('/downloads/package.zip')
-    True
-    """
-    m = re.search(r'(?m)^ {,3}\[%s\]:\s+/(\S+)' % identifier, text)
-    try:
-        path = m.group(1)
-    except AttributeError:
-        return False
-
-    return os.path.join(PATH_TO_STATIC, *path.split('/')[1:])
-    
 def get_contents(filepath):
     """
     Returns the contents of the file as a UTF-8 encoded string
     f.close()
     return u
 
-def print_filesize(path_to_file, plaintext=False):
-    """
-    Prints the filesize of the file specified by `path_to_file`
-    (wrapped in an HTML span unless `plaintext` is True)
-    
-    >>> 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(path_to_file)
-        bytes = (
-            ('bytes', 1),
-            ('kB', kb_size**1),
-            ('MB', kb_size**2),
-            ('GB', kb_size**3),
-            ('TB', kb_size**4),
-        )
-        for t in bytes:
-            if filesize <= t[1] * kb_size:
-                if t[0] == 'bytes':
-                    text = u'%s\u2009bytes' % filesize
-                else:
-                    text = u'≈%s\u2009%s' % (round(float(filesize)/t[1], 1), t[0])
-
-                before, after = '(', ')'
-                if not plaintext:
-                    before = '<span class="filesize">' + before
-                    after += '</span>'
-
-                return u''.join([before, text, after])
-    except:
-        return u'' # fail silently
-
 def get_posts(path_to_posts, include_pages=False, reverse=True):
     """
     Returns all of the posts in the directory and all directories below it