Browse Source

Add macro to show file size in links

Thomas Buck 5 years ago
parent
commit
67c6c522bb
3 changed files with 30 additions and 6 deletions
  1. 3
    3
      input/blog/2013/2013_06_04_gfs.md
  2. 2
    2
      input/stuff/c250.md
  3. 25
    1
      macros.py

+ 3
- 3
input/blog/2013/2013_06_04_gfs.md View File

@@ -11,10 +11,10 @@ date = datetime.strptime(page["date"], "%Y-%m-%d").strftime("%B %d, %Y")
11 11
 print "*Posted at %s.*" % date
12 12
 %-->
13 13
 
14
-Am Montag habe ich in der Schule eine [GFS][gfs] über meinen Quadrocopter gehalten. Hier die Präsentation als [ODP (2,8MB)][odp] und [PDF (3,3MB)][pdf].
14
+Am Montag habe ich in der Schule eine [GFS][gfs] über meinen Quadrocopter gehalten. Hier die Präsentation als [ODP][odp] und [PDF][pdf].
15 15
 
16 16
 Welche Note ich dafür bekomme weiß ich noch nicht, allerdings bin ich recht zuversichtlich :)
17 17
 
18
- [gfs]: http://de.wikipedia.org/wiki/Gleichwertige_Feststellung_von_Schülerleistungen
18
+ [gfs]: https://de.wikipedia.org/wiki/Gleichwertige_Feststellung_von_Sch%C3%BClerleistungen
19 19
  [odp]: files/quadgfs.odp
20
- [pdf]: files/quadgfs.pdf
20
+ [pdf]: files/quadgfs.pdf

+ 2
- 2
input/stuff/c250.md View File

@@ -12,7 +12,7 @@ compat: c250
12 12
 
13 13
 In short:
14 14
 
15
-*   Place [Mio C220 / C250 Pseudo Unlock (4.3MB)][1] on a SD Card.
15
+*   Place [Mio C220 / C250 Pseudo Unlock][1] on a SD Card.
16 16
 *   Insert the Card while the device is running. A file-manager will start.
17 17
 *   Backup your "MioMap" Folder which is inside "\My Flash Drive\".
18 18
 *   Delete the folder "MioMap" inside "\My Flash Drive\".
@@ -48,6 +48,6 @@ Du kannst natürlich auch das Menü ändern. Das ganze Zeug findet sich in "\My
48 48
 
49 49
 Der ganze Aufbau des Systems ist wirklich sehr einfach. Du kannst problemlos weitere Programme ins Menü hinzufügen.
50 50
 
51
-### [Download Mio C250 / C220 Pseudo Unlock (4.3MB)][1]
51
+### [Download Mio C250 / C220 Pseudo Unlock][1]
52 52
 
53 53
  [1]: files/c250_pseudo_unlock.zip

+ 25
- 1
macros.py View File

@@ -236,4 +236,28 @@ def hook_postconvert_mobilecompat():
236 236
     fp.write("}\n");
237 237
     fp.write("header('Location: '.$loc);\n")
238 238
     fp.write("?>")
239
-    fp.close()
239
+    fp.close()
240
+
241
+def hook_postconvert_size():
242
+    file_ext = '|'.join(['pdf', 'zip', 'rar', 'odp', 'exe', 'brd', 'mp3', 'mp4', 'plist']).encode("utf-8")
243
+    def matched_link(matchobj):
244
+        try:
245
+            path = matchobj.group(1)
246
+            if path.startswith("http") or path.startswith("//"):
247
+                return '<a href=\"%s\">%s</a>' % (matchobj.group(1), matchobj.group(3))
248
+            elif path.startswith("/"):
249
+                path = path.strip("/")
250
+            path = os.path.join("static/", path)
251
+            size = os.path.getsize(path)
252
+            if size >= (1024 * 1024):
253
+                return  "<a href=\"%s\">%s</a>&nbsp;(%.1f MiB)" % (matchobj.group(1), matchobj.group(3), size / (1024.0 * 1024.0))
254
+            elif size >= 1024:
255
+                return  "<a href=\"%s\">%s</a>&nbsp;(%d KiB)" % (matchobj.group(1), matchobj.group(3), size // 1024)
256
+            else:
257
+                return  "<a href=\"%s\">%s</a>&nbsp;(%d Byte)" % (matchobj.group(1), matchobj.group(3), size)
258
+        except:
259
+            print "Unable to estimate file size for %s" % matchobj.group(1)
260
+            return '<a href=\"%s\">%s</a>' % (matchobj.group(1), matchobj.group(3))
261
+    _re_url = '<a href=\"([^\"]*?\.(%s))\">(.*?)<\/a>' % file_ext
262
+    for p in pages:
263
+        p.html = re.sub(_re_url, matched_link, p.html)

Loading…
Cancel
Save