My static website generator using poole https://www.xythobuz.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

macros.py 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. import re
  2. import itertools
  3. import email.utils
  4. import os.path
  5. import time
  6. import codecs
  7. from datetime import datetime
  8. DEFAULT_LANG = "en"
  9. BASE_URL = "https://www.xythobuz.de"
  10. # -----------------------------------------------------------------------------
  11. # menu helper macro
  12. # -----------------------------------------------------------------------------
  13. def printMenuItem(p, yearsAsHeading = False, showDateSpan = False, showOnlyStartDate = False, nicelyFormatFullDate = False, lastyear = "0", lang = ""):
  14. title = p.title
  15. if lang != "":
  16. if p.get("title_" + lang, "") != "":
  17. title = p.get("title_" + lang, "")
  18. if p.title == "Blog":
  19. title = p.post
  20. year = p.get("date", "")[0:4]
  21. if year != lastyear:
  22. lastyear = year
  23. if yearsAsHeading:
  24. print "\n\n#### %s\n" % (year)
  25. dateto = ""
  26. if p.get("date", "" != ""):
  27. year = p.get("date", "")[0:4]
  28. if showOnlyStartDate:
  29. dateto = " (%s)" % (year)
  30. if p.get("update", "") != "" and p.get("update", "")[0:4] != year:
  31. if showDateSpan:
  32. dateto = " (%s - %s)" % (year, p.get("update", "")[0:4])
  33. if nicelyFormatFullDate:
  34. dateto = " - " + datetime.strptime(p.date, "%Y-%m-%d").strftime("%B %d, %Y")
  35. print " * **[%s](%s)**%s" % (title, p.url, dateto)
  36. if p.get("description", "") != "":
  37. description = p.get("description", "")
  38. if lang != "":
  39. if p.get("description_" + lang, "") != "":
  40. description = p.get("description_" + lang, "")
  41. print "<br><span class=\"listdesc\">" + description + "</span>"
  42. return lastyear
  43. def printRecentMenu(count = 5):
  44. posts = [p for p in pages if "date" in p]
  45. posts.sort(key=lambda p: p.get("date"), reverse=True)
  46. for p in posts[0:count]:
  47. printMenuItem(p, False, False, False, True)
  48. def printBlogMenu():
  49. posts = [p for p in pages if "post" in p]
  50. posts.sort(key=lambda p: p.get("date", "9999-01-01"), reverse=True)
  51. lastyear = "0"
  52. for p in posts:
  53. lastyear = printMenuItem(p, True, False, False, True, lastyear)
  54. def printProjectsMenu():
  55. # prints all pages with parent 'projects' or 'stuff'.
  56. # first the ones without date, sorted by position.
  57. # then afterwards those with date, split by year.
  58. # also supports blog posts with parent.
  59. enpages = [p for p in pages if p.lang == "en"]
  60. dpages = [p for p in enpages if p.get("date", "") == ""]
  61. mpages = [p for p in dpages if any(x in p.get("parent", "") for x in [ 'projects', 'stuff' ])]
  62. mpages.sort(key=lambda p: [int(p.get("position", "999"))])
  63. for p in mpages:
  64. printMenuItem(p)
  65. dpages = [p for p in enpages if p.get("date", "") != ""]
  66. mpages = [p for p in dpages if any(x in p.get("parent", "") for x in [ 'projects', 'stuff' ])]
  67. mpages.sort(key=lambda p: [p.get("date", "9999-01-01")], reverse = True)
  68. lastyear = "0"
  69. for p in mpages:
  70. lastyear = printMenuItem(p, True, True, False, False, lastyear)
  71. def print3DPrintingMenu():
  72. mpages = [p for p in pages if p.get("parent", "") == "3d-printing" and p.lang == "en"]
  73. mpages.sort(key=lambda p: int(p["position"]))
  74. for p in mpages:
  75. printMenuItem(p, False, True, True)
  76. def printQuadcopterMenu():
  77. mpages = [p for p in pages if p.get("parent", "") == "quadcopters" and p.lang == "en"]
  78. mpages.sort(key=lambda p: int(p["position"]))
  79. for p in mpages:
  80. printMenuItem(p, False, True, True)
  81. def printQuadcopterRelatedMenu():
  82. mpages = [p for p in pages if p.get("show_in_quadcopters", "false") == "true"]
  83. mpages.sort(key=lambda p: [p.get("date", "9999-01-01")], reverse = True)
  84. for p in mpages:
  85. printMenuItem(p, False, True, True)
  86. def printRobotMenuEnglish():
  87. mpages = [p for p in pages if p.get("parent", "") == "xyrobot" and p.lang == "en"]
  88. mpages.sort(key=lambda p: int(p["position"]))
  89. for p in mpages:
  90. printMenuItem(p)
  91. def printRobotMenuDeutsch():
  92. mpages = [p for p in pages if p.get("parent", "") == "xyrobot" and p.lang == "de"]
  93. mpages.sort(key=lambda p: int(p["position"]))
  94. for p in mpages:
  95. printMenuItem(p, False, False, False, False, "0", "de")
  96. # -----------------------------------------------------------------------------
  97. # lightgallery helper macro
  98. # -----------------------------------------------------------------------------
  99. # call this macro like this
  100. # lightgallery([
  101. # [ "image-link", "description" ],
  102. # [ "image-link", "thumbnail-link", "description" ],
  103. # [ "youtube-link", "thumbnail-link", "description" ],
  104. # [ "video-link", "mime", "thumbnail-link", "image-link", "description" ]
  105. # ])
  106. def lightgallery(links):
  107. videos = [l for l in links if len(l) == 5]
  108. v_i = 0
  109. for v in videos:
  110. link, mime, thumb, poster, alt = v
  111. v_i += 1
  112. print '<div style="display:none;" id="video' + str(v_i) + '">'
  113. print '<video class="lg-video-object lg-html5" controls preload="none">'
  114. print '<source src="' + link + '" type="' + mime + '">'
  115. print 'Your browser does not support HTML5 video.'
  116. print '</video>'
  117. print '</div>'
  118. print '<div class="lightgallery">'
  119. v_i = 0
  120. for l in links:
  121. if (len(l) == 3) or (len(l) == 2):
  122. link = img = alt = ""
  123. if len(l) == 3:
  124. link, img, alt = l
  125. else:
  126. link, alt = l
  127. x = link.rfind('.')
  128. img = link[:x] + '_small' + link[x:]
  129. print '<div class="border" data-src="' + link + '"><a href="' + link + '"><img class="pic" src="' + img + '" alt="' + alt + '"></a></div>'
  130. elif len(l) == 5:
  131. v_i += 1
  132. link, mime, thumb, poster, alt = v
  133. print '<div class="border" data-poster="' + poster + '" data-sub-html="' + alt + '" data-html="#video' + str(v_i) + '"><a href="' + link + '"><img class="pic" src="' + thumb + '"></a></div>'
  134. else:
  135. raise NameError('Invalid number of arguments for lightgallery')
  136. print '</div>'
  137. # -----------------------------------------------------------------------------
  138. # github helper macros
  139. # -----------------------------------------------------------------------------
  140. import urllib, json
  141. def restRequest(url):
  142. response = urllib.urlopen(url)
  143. data = json.loads(response.read())
  144. return data
  145. def restReleases(user, repo):
  146. s = "https://api.github.com/repos/"
  147. s += user
  148. s += "/"
  149. s += repo
  150. s += "/releases"
  151. return restRequest(s)
  152. def printLatestRelease(user, repo):
  153. repo_url = "https://github.com/" + user + "/" + repo
  154. print("<div class=\"releasecard\">")
  155. print("Release builds for " + repo + " are <a href=\"" + repo_url + "/releases\">available on GitHub</a>.<br>\n")
  156. releases = restReleases(user, repo)
  157. if len(releases) <= 0:
  158. print("No release has been published on GitHub yet.")
  159. print("</div>")
  160. return
  161. releases.sort(key=lambda x: x["published_at"], reverse=True)
  162. r = releases[0]
  163. release_url = r["html_url"]
  164. print("Latest release of <a href=\"" + repo_url + "\">" + repo + "</a>, at the time of this writing: <a href=\"" + release_url + "\">" + r["name"] + "</a> (" + datetime.strptime(r["published_at"], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H:%M:%S") + ")\n")
  165. if len(r["assets"]) <= 0:
  166. print("<br>No release assets have been published on GitHub for that.")
  167. print("</div>")
  168. return
  169. print("<ul>")
  170. print("Release Assets:")
  171. for a in r["assets"]:
  172. size = int(a["size"])
  173. ss = " "
  174. if size >= (1024 * 1024):
  175. ss += "(%.1f MiB)" % (size / (1024.0 * 1024.0))
  176. elif size >= 1024:
  177. ss += "(%d KiB)" % (size // 1024)
  178. else:
  179. ss += "(%d Byte)" % (size)
  180. print("<li><a href=\"" + a["browser_download_url"] + "\">" + a["name"] + "</a>" + ss)
  181. print("</ul></div>")
  182. # -----------------------------------------------------------------------------
  183. # preconvert hooks
  184. # -----------------------------------------------------------------------------
  185. def hook_preconvert_anotherlang():
  186. MKD_PATT = r'\.(?:md|mkd|mdown|markdown)$'
  187. _re_lang = re.compile(r'^[\s+]?lang[\s+]?[:=]((?:.|\n )*)', re.MULTILINE)
  188. vpages = [] # Set of all virtual pages
  189. for p in pages:
  190. current_lang = DEFAULT_LANG # Default language
  191. langs = [] # List of languages for the current page
  192. page_vpages = {} # Set of virtual pages for the current page
  193. text_lang = re.split(_re_lang, p.source)
  194. text_grouped = dict(zip([current_lang,] + \
  195. [lang.strip() for lang in text_lang[1::2]], \
  196. text_lang[::2]))
  197. for lang, text in text_grouped.iteritems():
  198. spath = p.fname.split(os.path.sep)
  199. langs.append(lang)
  200. if lang == "en":
  201. filename = re.sub(MKD_PATT, "%s\g<0>" % "", p.fname).split(os.path.sep)[-1]
  202. else:
  203. filename = re.sub(MKD_PATT, ".%s\g<0>" % lang, p.fname).split(os.path.sep)[-1]
  204. vp = Page(filename, virtual=text)
  205. # Copy real page attributes to the virtual page
  206. for attr in p:
  207. if not vp.has_key(attr):
  208. vp[attr] = p[attr]
  209. # Define a title in the proper language
  210. vp["title"] = p["title_%s" % lang] \
  211. if p.has_key("title_%s" % lang) \
  212. else p["title"]
  213. # Keep track of the current lang of the virtual page
  214. vp["lang"] = lang
  215. # Fix post name if exists
  216. if vp.has_key("post"):
  217. if lang == "en":
  218. vp["post"] = vp["post"][:]
  219. else:
  220. vp["post"] = vp["post"][:-len(lang) - 1]
  221. page_vpages[lang] = vp
  222. # Each virtual page has to know about its sister vpages
  223. for lang, vpage in page_vpages.iteritems():
  224. vpage["lang_links"] = dict([(l, v["url"]) for l, v in page_vpages.iteritems()])
  225. vpage["other_lang"] = langs # set other langs and link
  226. vpages += page_vpages.values()
  227. pages[:] = vpages
  228. _COMPAT = """ case "%s":
  229. $loc = "%s/%s";
  230. break;
  231. """
  232. _COMPAT_404 = """ default:
  233. $loc = "%s";
  234. break;
  235. """
  236. def hook_preconvert_compat():
  237. fp = open(os.path.join(options.project, "output", "index.php"), 'w')
  238. fp.write("<?\n")
  239. fp.write("// Auto generated xyCMS compatibility index.php\n")
  240. fp.write("$loc = 'https://www.xythobuz.de/index.de.html';\n")
  241. fp.write("if (isset($_GET['p'])) {\n")
  242. fp.write(" if (isset($_GET['lang'])) {\n")
  243. fp.write(" $_GET['p'] .= 'EN';\n")
  244. fp.write(" }\n")
  245. fp.write(" switch($_GET['p']) {\n")
  246. for p in pages:
  247. if p.get("compat", "") != "":
  248. tmp = p["compat"]
  249. if p.get("lang", DEFAULT_LANG) == DEFAULT_LANG:
  250. tmp = tmp + "EN"
  251. fp.write(_COMPAT % (tmp, "https://www.xythobuz.de", p.url))
  252. fp.write("\n")
  253. fp.write(_COMPAT_404 % "/404.html")
  254. fp.write(" }\n")
  255. fp.write("}\n")
  256. fp.write("if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {\n")
  257. fp.write(" if (php_sapi_name() == 'cgi') {\n")
  258. fp.write(" header('Status: 301 Moved Permanently');\n")
  259. fp.write(" } else {\n")
  260. fp.write(" header('HTTP/1.1 301 Moved Permanently');\n")
  261. fp.write(" }\n")
  262. fp.write("}\n");
  263. fp.write("header('Location: '.$loc);\n")
  264. fp.write("?>")
  265. fp.close()
  266. _SITEMAP = """<?xml version="1.0" encoding="UTF-8"?>
  267. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  268. %s
  269. </urlset>
  270. """
  271. _SITEMAP_URL = """
  272. <url>
  273. <loc>%s/%s</loc>
  274. <lastmod>%s</lastmod>
  275. <changefreq>%s</changefreq>
  276. <priority>%s</priority>
  277. </url>
  278. """
  279. def hook_preconvert_sitemap():
  280. date = datetime.strftime(datetime.now(), "%Y-%m-%d")
  281. urls = []
  282. for p in pages:
  283. urls.append(_SITEMAP_URL % (BASE_URL, p.url, date, p.get("changefreq", "monthly"), p.get("priority", "0.5")))
  284. fname = os.path.join(options.project, "output", "sitemap.xml")
  285. fp = open(fname, 'w')
  286. fp.write(_SITEMAP % "".join(urls))
  287. fp.close()
  288. # -----------------------------------------------------------------------------
  289. # postconvert hooks
  290. # -----------------------------------------------------------------------------
  291. _RSS = """<?xml version="1.0"?>
  292. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  293. <channel>
  294. <title>%s</title>
  295. <link>%s</link>
  296. <atom:link href="%s" rel="self" type="application/rss+xml" />
  297. <description>%s</description>
  298. <language>en-us</language>
  299. <pubDate>%s</pubDate>
  300. <lastBuildDate>%s</lastBuildDate>
  301. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  302. <generator>Poole</generator>
  303. %s
  304. </channel>
  305. </rss>
  306. """
  307. _RSS_ITEM = """
  308. <item>
  309. <title>%s</title>
  310. <link>%s</link>
  311. <description>%s</description>
  312. <pubDate>%s</pubDate>
  313. <guid>%s</guid>
  314. </item>
  315. """
  316. def hook_postconvert_rss():
  317. items = []
  318. posts = [p for p in pages if "date" in p]
  319. posts.sort(key=lambda p: p.date, reverse=True)
  320. posts = posts[:10]
  321. for p in posts:
  322. title = p.title
  323. if "post" in p:
  324. title = p.post
  325. link = "%s/%s" % (BASE_URL, p.url)
  326. desc = p.html.replace("href=\"img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
  327. desc = desc.replace("src=\"img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
  328. desc = desc.replace("href=\"/img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
  329. desc = desc.replace("src=\"/img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
  330. desc = htmlspecialchars(desc)
  331. date = time.mktime(time.strptime("%s 12" % p.date, "%Y-%m-%d %H"))
  332. date = email.utils.formatdate(date)
  333. items.append(_RSS_ITEM % (title, link, desc, date, link))
  334. items = "".join(items)
  335. title = "xythobuz.de Blog"
  336. link = "%s" % BASE_URL
  337. feed = "%s/rss.xml" % BASE_URL
  338. desc = htmlspecialchars("xythobuz Electronics & Software Projects")
  339. date = email.utils.formatdate()
  340. rss = _RSS % (title, link, feed, desc, date, date, items)
  341. fp = codecs.open(os.path.join(output, "rss.xml"), "w", "utf-8")
  342. fp.write(rss)
  343. fp.close()
  344. _COMPAT_MOB = """ case "%s":
  345. $loc = "%s/%s";
  346. break;
  347. """
  348. _COMPAT_404_MOB = """ default:
  349. $loc = "%s";
  350. break;
  351. """
  352. def hook_postconvert_mobilecompat():
  353. directory = os.path.join(output, "mobile")
  354. if not os.path.exists(directory):
  355. os.makedirs(directory)
  356. fp = codecs.open(os.path.join(directory, "index.php"), "w", "utf-8")
  357. fp.write("<?\n")
  358. fp.write("// Auto generated xyCMS compatibility mobile/index.php\n")
  359. fp.write("$loc = 'https://www.xythobuz.de/index.de.html';\n")
  360. fp.write("if (isset($_GET['p'])) {\n")
  361. fp.write(" if (isset($_GET['lang'])) {\n")
  362. fp.write(" $_GET['p'] .= 'EN';\n")
  363. fp.write(" }\n")
  364. fp.write(" switch($_GET['p']) {\n")
  365. for p in pages:
  366. if p.get("compat", "") != "":
  367. tmp = p["compat"]
  368. if p.get("lang", DEFAULT_LANG) == DEFAULT_LANG:
  369. tmp = tmp + "EN"
  370. fp.write(_COMPAT_MOB % (tmp, "https://www.xythobuz.de", re.sub(".html", ".html", p.url)))
  371. fp.write("\n")
  372. fp.write(_COMPAT_404_MOB % "/404.mob.html")
  373. fp.write(" }\n")
  374. fp.write("}\n")
  375. fp.write("if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {\n")
  376. fp.write(" if (php_sapi_name() == 'cgi') {\n")
  377. fp.write(" header('Status: 301 Moved Permanently');\n")
  378. fp.write(" } else {\n")
  379. fp.write(" header('HTTP/1.1 301 Moved Permanently');\n")
  380. fp.write(" }\n")
  381. fp.write("}\n");
  382. fp.write("header('Location: '.$loc);\n")
  383. fp.write("?>")
  384. fp.close()
  385. def hook_postconvert_size():
  386. file_ext = '|'.join(['pdf', 'zip', 'rar', 'ods', 'odt', 'odp', 'doc', 'xls', 'ppt', 'docx', 'xlsx', 'pptx', 'exe', 'brd', 'mp3', 'mp4', 'plist'])
  387. def matched_link(matchobj):
  388. try:
  389. path = matchobj.group(1)
  390. if path.startswith("http") or path.startswith("//") or path.startswith("ftp"):
  391. return '<a href=\"%s\">%s</a>' % (matchobj.group(1), matchobj.group(3))
  392. elif path.startswith("/"):
  393. path = path.strip("/")
  394. path = os.path.join("static/", path)
  395. size = os.path.getsize(path)
  396. if size >= (1024 * 1024):
  397. return "<a href=\"%s\">%s</a>&nbsp;(%.1f MiB)" % (matchobj.group(1), matchobj.group(3), size / (1024.0 * 1024.0))
  398. elif size >= 1024:
  399. return "<a href=\"%s\">%s</a>&nbsp;(%d KiB)" % (matchobj.group(1), matchobj.group(3), size // 1024)
  400. else:
  401. return "<a href=\"%s\">%s</a>&nbsp;(%d Byte)" % (matchobj.group(1), matchobj.group(3), size)
  402. except:
  403. print "Unable to estimate file size for %s" % matchobj.group(1)
  404. return '<a href=\"%s\">%s</a>' % (matchobj.group(1), matchobj.group(3))
  405. _re_url = '<a href=\"([^\"]*?\.(%s))\">(.*?)<\/a>' % file_ext
  406. for p in pages:
  407. p.html = re.sub(_re_url, matched_link, p.html)