瀏覽代碼

add menu helper macro

Thomas Buck 3 年之前
父節點
當前提交
f2f9c77477
共有 5 個檔案被更改,包括 90 行新增61 行删除
  1. 1
    12
      input/index.md
  2. 1
    37
      input/projects.md
  3. 1
    6
      input/projects/3d-printing.md
  4. 1
    6
      input/projects/quadcopters.md
  5. 86
    0
      macros.py

+ 1
- 12
input/index.md 查看文件

@@ -38,18 +38,7 @@ To receive my latest updates, you can subscribe to the <a href="rss.xml"><img sr
38 38
 ### Recent Posts
39 39
 
40 40
 <!--%
41
-from datetime import datetime
42
-posts = [p for p in pages if "date" in p]
43
-posts.sort(key=lambda p: p.get("date"), reverse=True)
44
-for p in posts[0:5]:
45
-    date = datetime.strptime(p.date, "%Y-%m-%d").strftime("%B %d, %Y")
46
-    if "post" in p:
47
-        print "  * **[%s](%s)** - %s" % (p.post, p.url, date)
48
-    else:
49
-        print "  * **[%s](%s)** - %s" % (p.title, p.url, date)
50
-
51
-    if p.get("description", "") != "":
52
-        print "<br><span class=\"listdesc\">" + p.get("description", "") + "</span>"
41
+printRecentMenu()
53 42
 %-->
54 43
 
55 44
 ### Tweets

+ 1
- 37
input/projects.md 查看文件

@@ -10,41 +10,5 @@ Some others are on-going or still recent for other reasons, these are listed fir
10 10
 To receive my latest updates, you can subscribe to the <a href="rss.xml"><img src="img/rss.png">RSS Feed</a>.
11 11
 
12 12
 <!--%
13
-# prints all pages with parent 'projects' or 'stuff'.
14
-# first the ones without date, sorted by position.
15
-# then afterwards those with date, split by year.
16
-# also supports blog posts with parent.
17
-
18
-enpages = [p for p in pages if p.lang == "en"]
19
-
20
-dpages = [p for p in enpages if p.get("date", "") == ""]
21
-mpages = [p for p in dpages if any(x in p.get("parent", "") for x in [ 'projects', 'stuff' ])]
22
-mpages.sort(key=lambda p: [int(p.get("position", "999"))])
23
-for p in mpages:
24
-    print "  * **[%s](%s)**" % (p.title, p.url)
25
-    if p.get("description", "") != "":
26
-        print "<br><span class=\"listdesc\">" + p.get("description", "") + "</span>"
27
-
28
-dpages = [p for p in enpages if p.get("date", "") != ""]
29
-mpages = [p for p in dpages if any(x in p.get("parent", "") for x in [ 'projects', 'stuff' ])]
30
-mpages.sort(key=lambda p: [p.get("date", "9999-01-01")], reverse = True)
31
-lastyear = "0"
32
-for p in mpages:
33
-    title = p.title
34
-    if p.title == "Blog":
35
-        title = p.post
36
-
37
-    year = p.get("date", "")[0:4]
38
-    if year != lastyear:
39
-        lastyear = year
40
-        print "\n\n#### %s\n" % (year)
41
-
42
-    dateto = ""
43
-    if p.get("update", "") != "" and p.get("update", "")[0:4] != year:
44
-        dateto = " (%s - %s)" % (year, p.get("update", "")[0:4])
45
-
46
-    print "  * **[%s](%s)**%s" % (title, p.url, dateto)
47
-
48
-    if p.get("description", "") != "":
49
-        print "<br><span class=\"listdesc\">" + p.get("description", "") + "</span>"
13
+printProjectsMenu()
50 14
 %-->

+ 1
- 6
input/projects/3d-printing.md 查看文件

@@ -9,12 +9,7 @@ In the meantime, I've modified my printers quite a bit and also written about it
9 9
 Here are all the articles that are part of my 3D printing series:
10 10
 
11 11
 <!--%
12
-mpages = [p for p in pages if p.get("parent", "") == "3d-printing" and p.lang == "en"]
13
-mpages.sort(key=lambda p: int(p["position"]))
14
-for p in mpages:
15
-    print "  * **[%s](%s)**" % (p.title, p.url) # markdown list item
16
-    if p.get("description", "") != "":
17
-        print "<br><span class=\"listdesc\">" + p.get("description", "") + "</span>"
12
+print3DPrintingMenu()
18 13
 %-->
19 14
 
20 15
 If you're interested in my 3D print designs, take a look [at my Thingiverse account](https://www.thingiverse.com/xythobuz/designs) or my [Git Repository](https://git.xythobuz.de/thomas/3d-print-designs).

+ 1
- 6
input/projects/quadcopters.md 查看文件

@@ -11,10 +11,5 @@ All of these I'm flying with [FatShark Dominator v3 Camo Blue](https://hobbyking
11 11
 Of course I'm not flying illegally, I have a model flight insurance and I am a registered UAV pilot in the European Union! 👮
12 12
 
13 13
 <!--%
14
-mpages = [p for p in pages if p.get("parent", "") == "quadcopters" and p.lang == "en"]
15
-mpages.sort(key=lambda p: int(p["position"]))
16
-for p in mpages:
17
-    print "  * **[%s](%s)**" % (p.title, p.url)
18
-    if p.get("description", "") != "":
19
-        print "<br><span class=\"listdesc\">" + p.get("description", "") + "</span>"
14
+printQuadcopterMenu()
20 15
 %-->

+ 86
- 0
macros.py 查看文件

@@ -10,6 +10,92 @@ DEFAULT_LANG = "en"
10 10
 BASE_URL = "https://www.xythobuz.de"
11 11
 
12 12
 # -----------------------------------------------------------------------------
13
+# menu helper macro
14
+# -----------------------------------------------------------------------------
15
+
16
+def printMenuItem(p, yearsAsHeading = False, showDateSpan = False, showOnlyStartDate = False, nicelyFormatFullDate = False, lastyear = "0", lang = ""):
17
+    title = p.title
18
+    if lang != "":
19
+        if p.get("title_" + lang, "") != "":
20
+            title = p.get("title_" + lang, "")
21
+    if p.title == "Blog":
22
+        title = p.post
23
+
24
+    year = p.get("date", "")[0:4]
25
+    if year != lastyear:
26
+        lastyear = year
27
+        if yearsAsHeading:
28
+            print "\n\n#### %s\n" % (year)
29
+
30
+    dateto = ""
31
+    if p.get("date", "" != ""):
32
+        year = p.get("date", "")[0:4]
33
+        if showOnlyStartDate:
34
+            dateto = " (%s)" % (year)
35
+
36
+        if p.get("update", "") != "" and p.get("update", "")[0:4] != year:
37
+            if showDateSpan:
38
+                dateto = " (%s - %s)" % (year, p.get("update", "")[0:4])
39
+
40
+        if nicelyFormatFullDate:
41
+            dateto = " - " + datetime.strptime(p.date, "%Y-%m-%d").strftime("%B %d, %Y")
42
+
43
+    print "  * **[%s](%s)**%s" % (title, p.url, dateto)
44
+
45
+    if p.get("description", "") != "":
46
+        description = p.get("description", "")
47
+        if lang != "":
48
+            if p.get("description_" + lang, "") != "":
49
+                description = p.get("description_" + lang, "")
50
+        print "<br><span class=\"listdesc\">" + description + "</span>"
51
+
52
+    return lastyear
53
+
54
+def printRecentMenu(count = 5):
55
+    posts = [p for p in pages if "date" in p]
56
+    posts.sort(key=lambda p: p.get("date"), reverse=True)
57
+    for p in posts[0:count]:
58
+        printMenuItem(p, False, False, False, True)
59
+
60
+def printBlogMenu():
61
+    posts = [p for p in pages if "post" in p]
62
+    posts.sort(key=lambda p: p.get("date", "9999-01-01"), reverse=True)
63
+    lastyear = "0"
64
+    for p in posts:
65
+        lastyear = printMenuItem(p, True, False, False, True, lastyear)
66
+
67
+def printProjectsMenu():
68
+    # prints all pages with parent 'projects' or 'stuff'.
69
+    # first the ones without date, sorted by position.
70
+    # then afterwards those with date, split by year.
71
+    # also supports blog posts with parent.
72
+    enpages = [p for p in pages if p.lang == "en"]
73
+
74
+    dpages = [p for p in enpages if p.get("date", "") == ""]
75
+    mpages = [p for p in dpages if any(x in p.get("parent", "") for x in [ 'projects', 'stuff' ])]
76
+    mpages.sort(key=lambda p: [int(p.get("position", "999"))])
77
+    for p in mpages:
78
+        printMenuItem(p)
79
+
80
+    dpages = [p for p in enpages if p.get("date", "") != ""]
81
+    mpages = [p for p in dpages if any(x in p.get("parent", "") for x in [ 'projects', 'stuff' ])]
82
+    mpages.sort(key=lambda p: [p.get("date", "9999-01-01")], reverse = True)
83
+    lastyear = "0"
84
+    for p in mpages:
85
+        lastyear = printMenuItem(p, True, True, False, False, lastyear)
86
+
87
+def print3DPrintingMenu():
88
+    mpages = [p for p in pages if p.get("parent", "") == "3d-printing" and p.lang == "en"]
89
+    mpages.sort(key=lambda p: int(p["position"]))
90
+    for p in mpages:
91
+        printMenuItem(p, False, True, True)
92
+
93
+def printQuadcopterMenu():
94
+    mpages = [p for p in pages if p.get("parent", "") == "quadcopters" and p.lang == "en"]
95
+    mpages.sort(key=lambda p: int(p["position"]))
96
+    for p in mpages:
97
+        printMenuItem(p, False, True, True)
98
+# -----------------------------------------------------------------------------
13 99
 # lightgallery helper macro
14 100
 # -----------------------------------------------------------------------------
15 101
 

Loading…
取消
儲存