My static website generator using poole https://www.xythobuz.de
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

macros.py 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import sys
  4. import re
  5. import itertools
  6. import email.utils
  7. import os.path
  8. import time
  9. import codecs
  10. from datetime import datetime
  11. # -----------------------------------------------------------------------------
  12. # Python 2/3 hacks
  13. # -----------------------------------------------------------------------------
  14. PY3 = sys.version_info[0] == 3
  15. if PY3:
  16. import html
  17. import urllib
  18. import urllib.request
  19. from urllib.error import HTTPError, URLError
  20. def urlparse_foo(link):
  21. return urllib.parse.parse_qs(urllib.parse.urlparse(link).query)['v'][0]
  22. else:
  23. import cgi
  24. import urllib
  25. import urlparse
  26. def urlparse_foo(link):
  27. return urlparse.parse_qs(urlparse.urlparse(link).query)['v'][0]
  28. # -----------------------------------------------------------------------------
  29. # config "system"
  30. # -----------------------------------------------------------------------------
  31. conf = {
  32. "default_lang": "en",
  33. "base_url": "https://www.xythobuz.de",
  34. "birthday": datetime(1994, 1, 22, 0, 0),
  35. "blog_years_back": 6,
  36. }
  37. def get_conf(name):
  38. return conf[name]
  39. # -----------------------------------------------------------------------------
  40. # local vars for compatibility
  41. # -----------------------------------------------------------------------------
  42. DEFAULT_LANG = get_conf("default_lang")
  43. BASE_URL = get_conf("base_url")
  44. # -----------------------------------------------------------------------------
  45. # birthday calculation
  46. # -----------------------------------------------------------------------------
  47. from datetime import timedelta
  48. from calendar import isleap
  49. size_of_day = 1. / 366.
  50. size_of_second = size_of_day / (24. * 60. * 60.)
  51. def date_as_float(dt):
  52. days_from_jan1 = dt - datetime(dt.year, 1, 1)
  53. if not isleap(dt.year) and days_from_jan1.days >= 31+28:
  54. days_from_jan1 += timedelta(1)
  55. return dt.year + days_from_jan1.days * size_of_day + days_from_jan1.seconds * size_of_second
  56. def difference_in_years(start_date, end_date):
  57. return int(date_as_float(end_date) - date_as_float(start_date))
  58. def own_age():
  59. age_dec = difference_in_years(get_conf("birthday"), datetime.now())
  60. age_hex = '0x%X' % age_dec
  61. return '<abbr title="' + str(age_dec) + '">' + str(age_hex) + '</abbr>'
  62. # -----------------------------------------------------------------------------
  63. # sub page helper macro
  64. # -----------------------------------------------------------------------------
  65. def backToParent():
  66. # check for special parent cases
  67. posts = []
  68. if page.get("show_in_quadcopters", "false") == "true":
  69. posts = [p for p in pages if p.url == "quadcopters.html"]
  70. # if not, check for actual parent
  71. if len(posts) == 0:
  72. url = page.get("parent", "") + ".html"
  73. posts = [p for p in pages if p.url == url]
  74. # print if any parent link found
  75. if len(posts) > 0:
  76. p = posts[0]
  77. print('<span class="listdesc">[...back to ' + p.title + ' overview](' + p.url + ')</span>')
  78. # -----------------------------------------------------------------------------
  79. # table helper macro
  80. # -----------------------------------------------------------------------------
  81. def tableHelper(style, header, content):
  82. print("<table>")
  83. if (header != None) and (len(header) == len(style)):
  84. print("<tr>")
  85. for h in header:
  86. print("<th>" + h + "</th>")
  87. print("</tr>")
  88. for ci in range(0, len(content)):
  89. if len(content[ci]) != len(style):
  90. # invalid call of table helper!
  91. continue
  92. print("<tr>")
  93. for i in range(0, len(style)):
  94. s = style[i]
  95. td_style = ""
  96. if "monospaced" in s:
  97. td_style += " font-family: monospace;"
  98. if "align-last-right" in s:
  99. if ci == (len(content) - 1):
  100. td_style += " text-align: right;"
  101. else:
  102. if "align-center" in s:
  103. td_style += " text-align: center;"
  104. elif "align-right" in s:
  105. td_style += " text-align: right;"
  106. elif "align-center" in s:
  107. td_style += " text-align: center;"
  108. td_args = ""
  109. if td_style != "":
  110. td_args = " style=\"" + td_style + "\""
  111. print("<td" + td_args + ">")
  112. if isinstance(content[ci][i], tuple):
  113. text, link = content[ci][i]
  114. print("<a href=\"" + link + "\">" + text + "</a>")
  115. else:
  116. text = content[ci][i]
  117. print(text)
  118. print("</td>")
  119. print("</tr>")
  120. print("</table>")
  121. # -----------------------------------------------------------------------------
  122. # menu helper macro
  123. # -----------------------------------------------------------------------------
  124. def githubCommitBadge(p, showInline = False):
  125. ret = ""
  126. if p.get("github", "") != "":
  127. link = p.get("git", p.github)
  128. linkParts = p.github.split("/")
  129. if len(linkParts) >= 5:
  130. ret += "<a href=\"" + link + "\"><img "
  131. if showInline:
  132. ret += "style =\"vertical-align: middle; padding-bottom: 0.25em;\" "
  133. ret += "src=\"https://img.shields.io/github/last-commit/"
  134. ret += linkParts[3] + "/" + linkParts[4]
  135. ret += ".svg?logo=git&style=flat\" /></a>"
  136. return ret
  137. def printMenuItem(p, yearsAsHeading = False, showDateSpan = False, showOnlyStartDate = False, nicelyFormatFullDate = False, lastyear = "0", lang = "", showLastCommit = True, hide_description = False, updates_as_heading = False):
  138. title = p.title
  139. if lang != "":
  140. if p.get("title_" + lang, "") != "":
  141. title = p.get("title_" + lang, "")
  142. if title == "Blog":
  143. title = p.post
  144. if updates_as_heading:
  145. year = p.get("update", p.get("date", ""))[0:4]
  146. else:
  147. year = p.get("date", "")[0:4]
  148. if year != lastyear:
  149. lastyear = year
  150. if yearsAsHeading:
  151. print("<h4>" + str(year) + "</h4>")
  152. dateto = ""
  153. if p.get("date", "" != ""):
  154. year = p.get("date", "")[0:4]
  155. if showOnlyStartDate:
  156. dateto = " (%s)" % (year)
  157. if p.get("update", "") != "" and p.get("update", "")[0:4] != year:
  158. if showDateSpan:
  159. dateto = " (%s - %s)" % (year, p.get("update", "")[0:4])
  160. if nicelyFormatFullDate:
  161. dateto = " - " + datetime.strptime(p.get("update", p.date), "%Y-%m-%d").strftime("%B %d, %Y")
  162. print("<li>")
  163. print("<a href=\"" + p.url + "\"><b>" + title + "</b></a>" + dateto)
  164. if hide_description == False:
  165. if p.get("description", "") != "":
  166. description = p.get("description", "")
  167. if lang != "":
  168. if p.get("description_" + lang, "") != "":
  169. description = p.get("description_" + lang, "")
  170. print("<br><span class=\"listdesc\">" + description + "</span>")
  171. if showLastCommit:
  172. link = githubCommitBadge(p)
  173. if len(link) > 0:
  174. print("<br>" + link)
  175. print("</li>")
  176. return lastyear
  177. def printRecentMenu(count = 5):
  178. posts = [p for p in pages if "date" in p and p.lang == "en"]
  179. posts.sort(key=lambda p: p.get("update", p.get("date")), reverse=True)
  180. if count > 0:
  181. posts = posts[0:count]
  182. print("<ul id='menulist'>")
  183. lastyear = "0"
  184. for p in posts:
  185. lastyear = printMenuItem(p, count == 0, False, False, True, lastyear, "", False, False, True)
  186. print("</ul>")
  187. def printBlogMenu(year_min=None, year_max=None):
  188. posts = [p for p in pages if "post" in p and p.lang == "en"]
  189. posts.sort(key=lambda p: p.get("date", "9999-01-01"), reverse=True)
  190. if year_min != None:
  191. posts = [p for p in posts if int(p.get("date", "9999-01-01")[0:4]) >= int(year_min)]
  192. if year_max != None:
  193. posts = [p for p in posts if int(p.get("date", "9999-01-01")[0:4]) <= int(year_max)]
  194. print("<ul id='menulist'>")
  195. lastyear = "0"
  196. for p in posts:
  197. lastyear = printMenuItem(p, True, False, False, True, lastyear)
  198. print("</ul>")
  199. def printProjectsMenu():
  200. # prints all pages with parent 'projects' or 'stuff'.
  201. # first the ones without date, sorted by position.
  202. # this first section includes sub-headings for children
  203. # then afterwards those with date, split by year.
  204. # also supports blog posts with parent.
  205. enpages = [p for p in pages if p.lang == "en"]
  206. # select pages without date
  207. dpages = [p for p in enpages if p.get("date", "") == ""]
  208. # only those that have a parent in ['projects', 'stuff']
  209. mpages = [p for p in dpages if any(x in p.get("parent", "") for x in [ 'projects', 'stuff' ])]
  210. # sort by position
  211. mpages.sort(key=lambda p: [int(p.get("position", "999"))])
  212. print("<ul id='menulist'>")
  213. # print all pages
  214. for p in mpages:
  215. printMenuItem(p)
  216. # print subpages for these top-level items
  217. subpages = [sub for sub in enpages if sub.get("parent", "none") == p.get("child-id", "unknown")]
  218. order = p.get("sort-order", "date")
  219. if order == "position":
  220. subpages.sort(key=lambda p: p["position"])
  221. else:
  222. subpages.sort(key=lambda p: p["date"], reverse = True)
  223. if len(subpages) > 0:
  224. print("<ul>")
  225. for sp in subpages:
  226. printMenuItem(sp, False, True, True, False, "0", "", False, True)
  227. print("</ul>")
  228. # slect pages with a date
  229. dpages = [p for p in enpages if p.get("date", "") != ""]
  230. # only those that have a parent in ['projects', 'stuff']
  231. mpages = [p for p in dpages if any(x in p.get("parent", "") for x in [ 'projects', 'stuff' ])]
  232. # sort by date
  233. mpages.sort(key=lambda p: [p.get("date", "9999-01-01")], reverse = True)
  234. # print all pages
  235. lastyear = "0"
  236. for p in mpages:
  237. lastyear = printMenuItem(p, True, True, False, False, lastyear)
  238. # print subpages for these top-level items
  239. subpages = [sub for sub in enpages if sub.get("parent", "none") == p.get("child-id", "unknown")]
  240. order = p.get("sort-order", "date")
  241. if order == "position":
  242. subpages.sort(key=lambda p: p["position"])
  243. else:
  244. subpages.sort(key=lambda p: p["date"], reverse = True)
  245. if len(subpages) > 0:
  246. print("<ul>")
  247. for sp in subpages:
  248. printMenuItem(sp, False, True, True, False, "0", "", False, True)
  249. print("</ul>")
  250. print("</ul>")
  251. def printMenuGeneric(mpages = None, sortKey = None, sortReverse = True):
  252. if mpages == None:
  253. mpages = [p for p in pages if p.get("parent", "__none__") == page["child-id"] and p.lang == "en"]
  254. if sortKey != None:
  255. mpages.sort(key = sortKey, reverse = sortReverse)
  256. if len(mpages) > 0:
  257. print("<ul id='menulist'>")
  258. for p in mpages:
  259. printMenuItem(p, False, True, True)
  260. print("</ul>")
  261. def printMenuDate(mpages = None, sortReverse = True):
  262. sortKey = lambda p: p["date"]
  263. printMenuGeneric(mpages, sortKey, sortReverse)
  264. def printMenuPositional(mpages = None):
  265. printMenuGeneric(mpages, lambda p: int(p["position"]), False)
  266. def printMenu(mpages = None):
  267. order = page.get("sort-order", "date")
  268. if order == "position":
  269. printMenuPositional(mpages)
  270. else:
  271. printMenuDate(mpages)
  272. def printRobotMenuEnglish():
  273. mpages = [p for p in pages if p.get("parent", "") == "xyrobot" and p.lang == "en"]
  274. mpages.sort(key=lambda p: int(p["position"]))
  275. print("<ul id='menulist'>")
  276. for p in mpages:
  277. printMenuItem(p)
  278. print("</ul>")
  279. def printRobotMenuDeutsch():
  280. mpages = [p for p in pages if p.get("parent", "") == "xyrobot" and p.lang == "de"]
  281. mpages.sort(key=lambda p: int(p["position"]))
  282. print("<ul id='menulist'>")
  283. for p in mpages:
  284. printMenuItem(p, False, False, False, False, "0", "de")
  285. print("</ul>")
  286. def printSteamMenuEnglish():
  287. mpages = [p for p in pages if p.get("parent", "") == "steam" and p.lang == "en"]
  288. mpages.sort(key=lambda p: [p.get("date", "9999-01-01")], reverse = True)
  289. print("<ul id='menulist'>")
  290. for p in mpages:
  291. printMenuItem(p, False, False, False, True)
  292. print("</ul>")
  293. def printSteamMenuDeutsch():
  294. # TODO show german pages, or english pages when german not available
  295. printSteamMenuEnglish()
  296. # -----------------------------------------------------------------------------
  297. # lightgallery helper macro
  298. # -----------------------------------------------------------------------------
  299. # call this macro like this:
  300. # lightgallery([
  301. # [ "image-link", "description" ],
  302. # [ "image-link", "thumbnail-link", "description" ],
  303. # [ "youtube-link", "thumbnail-link", "description" ],
  304. # [ "video-link", "mime", "thumbnail-link", "image-link", "description" ],
  305. # [ "video-link", "mime", "", "", "description" ],
  306. # ])
  307. # it will also auto-generate thumbnails and resize and strip EXIF from images
  308. # using the included web-image-resize script.
  309. # and it can generate video thumbnails and posters with the video-thumb script.
  310. def lightgallery_check_thumbnail(link, thumb):
  311. # only check local image links
  312. if not link.startswith('img/'):
  313. return
  314. # generate thumbnail filename web-image-resize will create
  315. x = link.rfind('.')
  316. img = link[:x] + '_small' + link[x:]
  317. # only run when desired thumb path matches calculated ones
  318. if thumb != img:
  319. return
  320. # generate fs path to images
  321. path = os.path.join(os.getcwd(), 'static', link)
  322. img = os.path.join(os.getcwd(), 'static', thumb)
  323. # no need to generate thumb again
  324. if os.path.exists(img):
  325. return
  326. # run web-image-resize to generate thumbnail
  327. script = os.path.join(os.getcwd(), 'web-image-resize')
  328. os.system(script + ' ' + path)
  329. def lightgallery_check_thumbnail_video(link, thumb, poster):
  330. # only check local image links
  331. if not link.startswith('img/'):
  332. return
  333. # generate thumbnail filenames video-thumb will create
  334. x = link.rfind('.')
  335. thumb_l = link[:x] + '_thumb.png'
  336. poster_l = link[:x] + '_poster.png'
  337. # only run when desired thumb path matches calculated ones
  338. if (thumb_l != thumb) or (poster_l != poster):
  339. return
  340. # generate fs path to images
  341. path = os.path.join(os.getcwd(), 'static', link)
  342. thumb_p = os.path.join(os.getcwd(), 'static', thumb)
  343. poster_p = os.path.join(os.getcwd(), 'static', poster)
  344. # no need to generate thumb again
  345. if os.path.exists(thumb_p) or os.path.exists(poster_p):
  346. return
  347. # run video-thumb to generate thumbnail
  348. script = os.path.join(os.getcwd(), 'video-thumb')
  349. os.system(script + ' ' + path)
  350. def lightgallery(links):
  351. global v_ii
  352. try:
  353. v_ii += 1
  354. except NameError:
  355. v_ii = 0
  356. videos = [l for l in links if len(l) == 5]
  357. v_i = -1
  358. for v in videos:
  359. link, mime, thumb, poster, alt = v
  360. v_i += 1
  361. print('<div style="display:none;" id="video' + str(v_i) + '_' + str(v_ii) + '">')
  362. print('<video class="lg-video-object lg-html5" controls preload="none">')
  363. print('<source src="' + link + '" type="' + mime + '">')
  364. print('<a href="' + link + '">' + alt + '</a>')
  365. print('</video>')
  366. print('</div>')
  367. print('<div class="lightgallery">')
  368. v_i = -1
  369. for l in links:
  370. if (len(l) == 3) or (len(l) == 2):
  371. link = img = alt = ""
  372. style = img2 = ""
  373. if len(l) == 3:
  374. link, img, alt = l
  375. else:
  376. link, alt = l
  377. if "youtube.com" in link:
  378. img = "https://img.youtube.com/vi/"
  379. img += urlparse_foo(link)
  380. img += "/0.jpg" # full size preview
  381. #img += "/default.jpg" # default thumbnail
  382. style = ' style="width:300px;"'
  383. img2 = '<img src="lg/video-play.png" class="picthumb">'
  384. else:
  385. x = link.rfind('.')
  386. img = link[:x] + '_small' + link[x:]
  387. lightgallery_check_thumbnail(link, img)
  388. print('<div class="border" style="position:relative;" data-src="' + link + '"><a href="' + link + '"><img class="pic" src="' + img + '" alt="' + alt + '"' + style + '>' + img2 + '</a></div>')
  389. elif len(l) == 5:
  390. v_i += 1
  391. link, mime, thumb, poster, alt = videos[v_i]
  392. if len(thumb) <= 0:
  393. x = link.rfind('.')
  394. thumb = link[:x] + '_thumb.png'
  395. if len(poster) <= 0:
  396. x = link.rfind('.')
  397. poster = link[:x] + '_poster.png'
  398. lightgallery_check_thumbnail_video(link, thumb, poster)
  399. print('<div class="border" data-poster="' + poster + '" data-sub-html="' + alt + '" data-html="#video' + str(v_i) + '_' + str(v_ii) + '"><a href="' + link + '"><img class="pic" src="' + thumb + '"></a></div>')
  400. else:
  401. raise NameError('Invalid number of arguments for lightgallery')
  402. print('</div>')
  403. # -----------------------------------------------------------------------------
  404. # github helper macros
  405. # -----------------------------------------------------------------------------
  406. import json, sys
  407. def print_cnsl_error(s, url):
  408. sys.stderr.write("\n")
  409. sys.stderr.write("warning: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
  410. sys.stderr.write("warning: !!!!!!! WARNING !!!!!\n")
  411. sys.stderr.write("warning: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
  412. sys.stderr.write("warning: " + s + "\n")
  413. sys.stderr.write("warning: URL: \"" + url + "\"\n")
  414. sys.stderr.write("warning: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
  415. sys.stderr.write("warning: !!!!!!! WARNING !!!!!\n")
  416. sys.stderr.write("warning: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
  417. sys.stderr.write("\n")
  418. def http_request(url):
  419. sys.stderr.write('sub : fetching %s\n' % url)
  420. if PY3:
  421. try:
  422. response = urllib.request.urlopen(url, timeout = 10)
  423. except HTTPError as error:
  424. print_cnsl_error("HTTPError: '%s'" % error, url)
  425. return ""
  426. except URLError as error:
  427. print_cnsl_error("URLError: '%s'" % error, url)
  428. return ""
  429. else:
  430. try:
  431. response = urllib.urlopen(url)
  432. except IOError as error:
  433. print_cnsl_error("HTTPError: '%s'" % error, url)
  434. return ""
  435. if response.getcode() != 200:
  436. print_cnsl_error("invalid response code: " + str(response.getcode()), url)
  437. return ""
  438. else:
  439. data = response.read().decode("utf-8")
  440. return data
  441. def restRequest(url):
  442. data = json.loads(http_request(url))
  443. return data
  444. def restReleases(user, repo):
  445. s = "https://api.github.com/repos/"
  446. s += user
  447. s += "/"
  448. s += repo
  449. s += "/releases"
  450. return restRequest(s)
  451. def printLatestRelease(user, repo):
  452. repo_url = "https://github.com/" + user + "/" + repo
  453. print("<div class=\"releasecard\">")
  454. print("Release builds for " + repo + " are <a href=\"" + repo_url + "/releases\">available on GitHub</a>.<br>\n")
  455. releases = restReleases(user, repo)
  456. if len(releases) <= 0:
  457. print("No release has been published on GitHub yet.")
  458. print("</div>")
  459. return
  460. releases.sort(key=lambda x: x["published_at"], reverse=True)
  461. r = releases[0]
  462. release_url = r["html_url"]
  463. 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")
  464. if len(r["assets"]) <= 0:
  465. print("<br>No release assets have been published on GitHub for that.")
  466. print("</div>")
  467. return
  468. print("<ul>")
  469. print("Release Assets:")
  470. for a in r["assets"]:
  471. size = int(a["size"])
  472. ss = " "
  473. if size >= (1024 * 1024):
  474. ss += "(%.1f MiB)" % (size / (1024.0 * 1024.0))
  475. elif size >= 1024:
  476. ss += "(%d KiB)" % (size // 1024)
  477. else:
  478. ss += "(%d Byte)" % (size)
  479. print("<li><a href=\"" + a["browser_download_url"] + "\">" + a["name"] + "</a>" + ss)
  480. print("</ul></div>")
  481. def include_url(url):
  482. data = http_request(url)
  483. if PY3:
  484. encoded = html.escape(data)
  485. else:
  486. encoded = cgi.escape(data)
  487. print(encoded, end="")
  488. # -----------------------------------------------------------------------------
  489. # preconvert hooks
  490. # -----------------------------------------------------------------------------
  491. # -----------------------------------------------------------------------------
  492. # multi language support
  493. # -----------------------------------------------------------------------------
  494. def hook_preconvert_anotherlang():
  495. MKD_PATT = r'\.(?:md|mkd|mdown|markdown)$'
  496. _re_lang = re.compile(r'^[\s+]?lang[\s+]?[:=]((?:.|\n )*)', re.MULTILINE)
  497. vpages = [] # Set of all virtual pages
  498. for p in pages:
  499. current_lang = DEFAULT_LANG # Default language
  500. langs = [] # List of languages for the current page
  501. page_vpages = {} # Set of virtual pages for the current page
  502. text_lang = re.split(_re_lang, p.source)
  503. text_grouped = dict(zip([current_lang,] + \
  504. [lang.strip() for lang in text_lang[1::2]], \
  505. text_lang[::2]))
  506. for lang, text in (iter(text_grouped.items()) if PY3 else text_grouped.iteritems()):
  507. spath = p.fname.split(os.path.sep)
  508. langs.append(lang)
  509. if lang == "en":
  510. filename = re.sub(MKD_PATT, r"%s\g<0>" % "", p.fname).split(os.path.sep)[-1]
  511. else:
  512. filename = re.sub(MKD_PATT, r".%s\g<0>" % lang, p.fname).split(os.path.sep)[-1]
  513. vp = Page(filename, virtual=text)
  514. # Copy real page attributes to the virtual page
  515. for attr in p:
  516. if not ((attr in vp) if PY3 else vp.has_key(attr)):
  517. vp[attr] = p[attr]
  518. # Define a title in the proper language
  519. vp["title"] = p["title_%s" % lang] \
  520. if ((("title_%s" % lang) in p) if PY3 else p.has_key("title_%s" % lang)) \
  521. else p["title"]
  522. # Keep track of the current lang of the virtual page
  523. vp["lang"] = lang
  524. page_vpages[lang] = vp
  525. # Each virtual page has to know about its sister vpages
  526. for lang, vpage in (iter(page_vpages.items()) if PY3 else page_vpages.iteritems()):
  527. vpage["lang_links"] = dict([(l, v["url"]) for l, v in (iter(page_vpages.items()) if PY3 else page_vpages.iteritems())])
  528. vpage["other_lang"] = langs # set other langs and link
  529. vpages += page_vpages.values()
  530. pages[:] = vpages
  531. # -----------------------------------------------------------------------------
  532. # compatibility redirect for old website URLs
  533. # -----------------------------------------------------------------------------
  534. _COMPAT = """ case "%s":
  535. $loc = "%s/%s";
  536. break;
  537. """
  538. _COMPAT_404 = """ default:
  539. $loc = "%s";
  540. break;
  541. """
  542. def hook_preconvert_compat():
  543. fp = open(os.path.join(options.project, "output", "index.php"), 'w')
  544. fp.write("<?\n")
  545. fp.write("// Auto generated xyCMS compatibility index.php\n")
  546. fp.write("$loc = '" + get_conf("base_url") + "/index.de.html';\n")
  547. fp.write("if (isset($_GET['p'])) {\n")
  548. fp.write(" if (isset($_GET['lang'])) {\n")
  549. fp.write(" $_GET['p'] .= 'EN';\n")
  550. fp.write(" }\n")
  551. fp.write(" switch($_GET['p']) {\n")
  552. for p in pages:
  553. if p.get("compat", "") != "":
  554. tmp = p["compat"]
  555. if p.get("lang", DEFAULT_LANG) == DEFAULT_LANG:
  556. tmp = tmp + "EN"
  557. fp.write(_COMPAT % (tmp, get_conf("base_url"), p.url))
  558. fp.write("\n")
  559. fp.write(_COMPAT_404 % "/404.html")
  560. fp.write(" }\n")
  561. fp.write("}\n")
  562. fp.write("if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {\n")
  563. fp.write(" if (php_sapi_name() == 'cgi') {\n")
  564. fp.write(" header('Status: 301 Moved Permanently');\n")
  565. fp.write(" } else {\n")
  566. fp.write(" header('HTTP/1.1 301 Moved Permanently');\n")
  567. fp.write(" }\n")
  568. fp.write("}\n");
  569. fp.write("header('Location: '.$loc);\n")
  570. fp.write("?>")
  571. fp.close()
  572. # -----------------------------------------------------------------------------
  573. # sitemap generation
  574. # -----------------------------------------------------------------------------
  575. _SITEMAP = """<?xml version="1.0" encoding="UTF-8"?>
  576. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  577. %s
  578. </urlset>
  579. """
  580. _SITEMAP_URL = """
  581. <url>
  582. <loc>%s/%s</loc>
  583. <lastmod>%s</lastmod>
  584. <changefreq>%s</changefreq>
  585. <priority>%s</priority>
  586. </url>
  587. """
  588. def hook_preconvert_sitemap():
  589. date = datetime.strftime(datetime.now(), "%Y-%m-%d")
  590. urls = []
  591. for p in pages:
  592. urls.append(_SITEMAP_URL % (BASE_URL, p.url, date, p.get("changefreq", "monthly"), p.get("priority", "0.5")))
  593. fname = os.path.join(options.project, "output", "sitemap.xml")
  594. fp = open(fname, 'w')
  595. fp.write(_SITEMAP % "".join(urls))
  596. fp.close()
  597. # -----------------------------------------------------------------------------
  598. # postconvert hooks
  599. # -----------------------------------------------------------------------------
  600. # -----------------------------------------------------------------------------
  601. # rss feed generation
  602. # -----------------------------------------------------------------------------
  603. _RSS = """<?xml version="1.0" encoding="UTF-8"?>
  604. <?xml-stylesheet href="%s" type="text/xsl"?>
  605. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  606. <channel>
  607. <title>%s</title>
  608. <link>%s</link>
  609. <atom:link href="%s" rel="self" type="application/rss+xml" />
  610. <description>%s</description>
  611. <language>en-us</language>
  612. <pubDate>%s</pubDate>
  613. <lastBuildDate>%s</lastBuildDate>
  614. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  615. <generator>Poole</generator>
  616. <ttl>720</ttl>
  617. %s
  618. </channel>
  619. </rss>
  620. """
  621. _RSS_ITEM = """
  622. <item>
  623. <title>%s</title>
  624. <link>%s</link>
  625. <description>%s</description>
  626. <pubDate>%s</pubDate>
  627. <atom:updated>%s</atom:updated>
  628. <guid>%s</guid>
  629. </item>
  630. """
  631. def hook_postconvert_rss():
  632. items = []
  633. # all pages with "date" get put into feed
  634. posts = [p for p in pages if "date" in p]
  635. # sort by update if available, date else
  636. posts.sort(key=lambda p: p.get("update", p.date), reverse=True)
  637. # only put 20 most recent items in feed
  638. posts = posts[:20]
  639. for p in posts:
  640. title = p.title
  641. if "post" in p:
  642. title = p.post
  643. link = "%s/%s" % (BASE_URL, p.url)
  644. desc = p.html.replace("href=\"img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
  645. desc = desc.replace("src=\"img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
  646. desc = desc.replace("href=\"/img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
  647. desc = desc.replace("src=\"/img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
  648. desc = htmlspecialchars(desc)
  649. date = time.mktime(time.strptime("%s 12" % p.date, "%Y-%m-%d %H"))
  650. date = email.utils.formatdate(date)
  651. update = time.mktime(time.strptime("%s 12" % p.get("update", p.date), "%Y-%m-%d %H"))
  652. update = email.utils.formatdate(update)
  653. items.append(_RSS_ITEM % (title, link, desc, date, update, link))
  654. items = "".join(items)
  655. style = "/css/rss.xsl"
  656. title = "xythobuz.de Blog"
  657. link = "%s" % BASE_URL
  658. feed = "%s/rss.xml" % BASE_URL
  659. desc = htmlspecialchars("xythobuz Electronics & Software Projects")
  660. date = email.utils.formatdate()
  661. rss = _RSS % (style, title, link, feed, desc, date, date, items)
  662. fp = codecs.open(os.path.join(output, "rss.xml"), "w", "utf-8")
  663. fp.write(rss)
  664. fp.close()
  665. # -----------------------------------------------------------------------------
  666. # compatibility redirect for old mobile pages
  667. # -----------------------------------------------------------------------------
  668. _COMPAT_MOB = """ case "%s":
  669. $loc = "%s/%s";
  670. break;
  671. """
  672. _COMPAT_404_MOB = """ default:
  673. $loc = "%s";
  674. break;
  675. """
  676. def hook_postconvert_mobilecompat():
  677. directory = os.path.join(output, "mobile")
  678. if not os.path.exists(directory):
  679. os.makedirs(directory)
  680. fp = codecs.open(os.path.join(directory, "index.php"), "w", "utf-8")
  681. fp.write("<?\n")
  682. fp.write("// Auto generated xyCMS compatibility mobile/index.php\n")
  683. fp.write("$loc = '" + get_conf("base_url") + "/index.de.html';\n")
  684. fp.write("if (isset($_GET['p'])) {\n")
  685. fp.write(" if (isset($_GET['lang'])) {\n")
  686. fp.write(" $_GET['p'] .= 'EN';\n")
  687. fp.write(" }\n")
  688. fp.write(" switch($_GET['p']) {\n")
  689. for p in pages:
  690. if p.get("compat", "") != "":
  691. tmp = p["compat"]
  692. if p.get("lang", DEFAULT_LANG) == DEFAULT_LANG:
  693. tmp = tmp + "EN"
  694. fp.write(_COMPAT_MOB % (tmp, get_conf("base_url"), re.sub(".html", ".html", p.url)))
  695. fp.write("\n")
  696. fp.write(_COMPAT_404_MOB % "/404.mob.html")
  697. fp.write(" }\n")
  698. fp.write("}\n")
  699. fp.write("if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {\n")
  700. fp.write(" if (php_sapi_name() == 'cgi') {\n")
  701. fp.write(" header('Status: 301 Moved Permanently');\n")
  702. fp.write(" } else {\n")
  703. fp.write(" header('HTTP/1.1 301 Moved Permanently');\n")
  704. fp.write(" }\n")
  705. fp.write("}\n");
  706. fp.write("header('Location: '.$loc);\n")
  707. fp.write("?>")
  708. fp.close()
  709. # -----------------------------------------------------------------------------
  710. # displaying filesize for download links
  711. # -----------------------------------------------------------------------------
  712. def hook_postconvert_size():
  713. file_ext = '|'.join(['pdf', 'zip', 'rar', 'ods', 'odt', 'odp', 'doc', 'xls', 'ppt', 'docx', 'xlsx', 'pptx', 'exe', 'brd', 'plist'])
  714. def matched_link(matchobj):
  715. try:
  716. path = matchobj.group(1)
  717. if path.startswith("http") or path.startswith("//") or path.startswith("ftp"):
  718. return '<a href=\"%s\">%s</a>' % (matchobj.group(1), matchobj.group(3))
  719. elif path.startswith("/"):
  720. path = path.strip("/")
  721. path = os.path.join("static/", path)
  722. size = os.path.getsize(path)
  723. if size >= (1024 * 1024):
  724. return "<a href=\"%s\">%s</a>&nbsp;(%.1f MiB)" % (matchobj.group(1), matchobj.group(3), size / (1024.0 * 1024.0))
  725. elif size >= 1024:
  726. return "<a href=\"%s\">%s</a>&nbsp;(%d KiB)" % (matchobj.group(1), matchobj.group(3), size // 1024)
  727. else:
  728. return "<a href=\"%s\">%s</a>&nbsp;(%d Byte)" % (matchobj.group(1), matchobj.group(3), size)
  729. except:
  730. print("Unable to estimate file size for %s" % matchobj.group(1))
  731. return '<a href=\"%s\">%s</a>' % (matchobj.group(1), matchobj.group(3))
  732. _re_url = r'<a href=\"([^\"]*?\.(%s))\">(.*?)<\/a>' % file_ext
  733. for p in pages:
  734. p.html = re.sub(_re_url, matched_link, p.html)