My static website generator using poole https://www.xythobuz.de
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

macros.py 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. if PY3:
  420. response = urllib.request.urlopen(url, timeout = 5)
  421. else:
  422. response = urllib.urlopen(url)
  423. if response.getcode() != 200:
  424. raise RuntimeError("invalid response code: " + str(response.getcode()))
  425. data = response.read().decode("utf-8")
  426. return data
  427. def include_url(url, fallback = None):
  428. sys.stderr.write('sub : fetching page "%s"\n' % url)
  429. if fallback == None:
  430. print_cnsl_error("include_url() without fallback option", url)
  431. try:
  432. data = http_request(url)
  433. except Exception as e:
  434. if fallback != None:
  435. sys.stderr.write('sub : fetching fallback page "%s"\n' % url)
  436. try:
  437. data = http_request(fallback)
  438. except Exception as e:
  439. print_cnsl_error(str(e), fallback)
  440. return
  441. else:
  442. print_cnsl_error(str(e), url)
  443. return
  444. if PY3:
  445. encoded = html.escape(data)
  446. else:
  447. encoded = cgi.escape(data)
  448. print(encoded, end="")
  449. def restRequest(url):
  450. sys.stderr.write('sub : fetching REST "%s"\n' % url)
  451. data = json.loads(http_request(url))
  452. return data
  453. def restReleases(user, repo):
  454. s = "https://api.github.com/repos/"
  455. s += user
  456. s += "/"
  457. s += repo
  458. s += "/releases"
  459. return restRequest(s)
  460. def printLatestRelease(user, repo):
  461. repo_url = "https://github.com/" + user + "/" + repo
  462. print("<div class=\"releasecard\">")
  463. print("Release builds for " + repo + " are <a href=\"" + repo_url + "/releases\">available on GitHub</a>.<br>\n")
  464. releases = restReleases(user, repo)
  465. if len(releases) <= 0:
  466. print("No release has been published on GitHub yet.")
  467. print("</div>")
  468. return
  469. releases.sort(key=lambda x: x["published_at"], reverse=True)
  470. r = releases[0]
  471. release_url = r["html_url"]
  472. 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")
  473. if len(r["assets"]) <= 0:
  474. print("<br>No release assets have been published on GitHub for that.")
  475. print("</div>")
  476. return
  477. print("<ul>")
  478. print("Release Assets:")
  479. for a in r["assets"]:
  480. size = int(a["size"])
  481. ss = " "
  482. if size >= (1024 * 1024):
  483. ss += "(%.1f MiB)" % (size / (1024.0 * 1024.0))
  484. elif size >= 1024:
  485. ss += "(%d KiB)" % (size // 1024)
  486. else:
  487. ss += "(%d Byte)" % (size)
  488. print("<li><a href=\"" + a["browser_download_url"] + "\">" + a["name"] + "</a>" + ss)
  489. print("</ul></div>")
  490. # -----------------------------------------------------------------------------
  491. # preconvert hooks
  492. # -----------------------------------------------------------------------------
  493. # -----------------------------------------------------------------------------
  494. # multi language support
  495. # -----------------------------------------------------------------------------
  496. def hook_preconvert_anotherlang():
  497. MKD_PATT = r'\.(?:md|mkd|mdown|markdown)$'
  498. _re_lang = re.compile(r'^[\s+]?lang[\s+]?[:=]((?:.|\n )*)', re.MULTILINE)
  499. vpages = [] # Set of all virtual pages
  500. for p in pages:
  501. current_lang = DEFAULT_LANG # Default language
  502. langs = [] # List of languages for the current page
  503. page_vpages = {} # Set of virtual pages for the current page
  504. text_lang = re.split(_re_lang, p.source)
  505. text_grouped = dict(zip([current_lang,] + \
  506. [lang.strip() for lang in text_lang[1::2]], \
  507. text_lang[::2]))
  508. for lang, text in (iter(text_grouped.items()) if PY3 else text_grouped.iteritems()):
  509. spath = p.fname.split(os.path.sep)
  510. langs.append(lang)
  511. if lang == "en":
  512. filename = re.sub(MKD_PATT, r"%s\g<0>" % "", p.fname).split(os.path.sep)[-1]
  513. else:
  514. filename = re.sub(MKD_PATT, r".%s\g<0>" % lang, p.fname).split(os.path.sep)[-1]
  515. vp = Page(filename, virtual=text)
  516. # Copy real page attributes to the virtual page
  517. for attr in p:
  518. if not ((attr in vp) if PY3 else vp.has_key(attr)):
  519. vp[attr] = p[attr]
  520. # Define a title in the proper language
  521. vp["title"] = p["title_%s" % lang] \
  522. if ((("title_%s" % lang) in p) if PY3 else p.has_key("title_%s" % lang)) \
  523. else p["title"]
  524. # Keep track of the current lang of the virtual page
  525. vp["lang"] = lang
  526. page_vpages[lang] = vp
  527. # Each virtual page has to know about its sister vpages
  528. for lang, vpage in (iter(page_vpages.items()) if PY3 else page_vpages.iteritems()):
  529. vpage["lang_links"] = dict([(l, v["url"]) for l, v in (iter(page_vpages.items()) if PY3 else page_vpages.iteritems())])
  530. vpage["other_lang"] = langs # set other langs and link
  531. vpages += page_vpages.values()
  532. pages[:] = vpages
  533. # -----------------------------------------------------------------------------
  534. # compatibility redirect for old website URLs
  535. # -----------------------------------------------------------------------------
  536. _COMPAT = """ case "%s":
  537. $loc = "%s/%s";
  538. break;
  539. """
  540. _COMPAT_404 = """ default:
  541. $loc = "%s";
  542. break;
  543. """
  544. def hook_preconvert_compat():
  545. fp = open(os.path.join(options.project, "output", "index.php"), 'w')
  546. fp.write("<?\n")
  547. fp.write("// Auto generated xyCMS compatibility index.php\n")
  548. fp.write("$loc = '" + get_conf("base_url") + "/index.de.html';\n")
  549. fp.write("if (isset($_GET['p'])) {\n")
  550. fp.write(" if (isset($_GET['lang'])) {\n")
  551. fp.write(" $_GET['p'] .= 'EN';\n")
  552. fp.write(" }\n")
  553. fp.write(" switch($_GET['p']) {\n")
  554. for p in pages:
  555. if p.get("compat", "") != "":
  556. tmp = p["compat"]
  557. if p.get("lang", DEFAULT_LANG) == DEFAULT_LANG:
  558. tmp = tmp + "EN"
  559. fp.write(_COMPAT % (tmp, get_conf("base_url"), p.url))
  560. fp.write("\n")
  561. fp.write(_COMPAT_404 % "/404.html")
  562. fp.write(" }\n")
  563. fp.write("}\n")
  564. fp.write("if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {\n")
  565. fp.write(" if (php_sapi_name() == 'cgi') {\n")
  566. fp.write(" header('Status: 301 Moved Permanently');\n")
  567. fp.write(" } else {\n")
  568. fp.write(" header('HTTP/1.1 301 Moved Permanently');\n")
  569. fp.write(" }\n")
  570. fp.write("}\n");
  571. fp.write("header('Location: '.$loc);\n")
  572. fp.write("?>")
  573. fp.close()
  574. # -----------------------------------------------------------------------------
  575. # sitemap generation
  576. # -----------------------------------------------------------------------------
  577. _SITEMAP = """<?xml version="1.0" encoding="UTF-8"?>
  578. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  579. %s
  580. </urlset>
  581. """
  582. _SITEMAP_URL = """
  583. <url>
  584. <loc>%s/%s</loc>
  585. <lastmod>%s</lastmod>
  586. <changefreq>%s</changefreq>
  587. <priority>%s</priority>
  588. </url>
  589. """
  590. def hook_preconvert_sitemap():
  591. date = datetime.strftime(datetime.now(), "%Y-%m-%d")
  592. urls = []
  593. for p in pages:
  594. urls.append(_SITEMAP_URL % (BASE_URL, p.url, date, p.get("changefreq", "monthly"), p.get("priority", "0.5")))
  595. fname = os.path.join(options.project, "output", "sitemap.xml")
  596. fp = open(fname, 'w')
  597. fp.write(_SITEMAP % "".join(urls))
  598. fp.close()
  599. # -----------------------------------------------------------------------------
  600. # postconvert hooks
  601. # -----------------------------------------------------------------------------
  602. # -----------------------------------------------------------------------------
  603. # rss feed generation
  604. # -----------------------------------------------------------------------------
  605. _RSS = """<?xml version="1.0" encoding="UTF-8"?>
  606. <?xml-stylesheet href="%s" type="text/xsl"?>
  607. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  608. <channel>
  609. <title>%s</title>
  610. <link>%s</link>
  611. <atom:link href="%s" rel="self" type="application/rss+xml" />
  612. <description>%s</description>
  613. <language>en-us</language>
  614. <pubDate>%s</pubDate>
  615. <lastBuildDate>%s</lastBuildDate>
  616. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  617. <generator>Poole</generator>
  618. <ttl>720</ttl>
  619. %s
  620. </channel>
  621. </rss>
  622. """
  623. _RSS_ITEM = """
  624. <item>
  625. <title>%s</title>
  626. <link>%s</link>
  627. <description>%s</description>
  628. <pubDate>%s</pubDate>
  629. <atom:updated>%s</atom:updated>
  630. <guid>%s</guid>
  631. </item>
  632. """
  633. def hook_postconvert_rss():
  634. items = []
  635. # all pages with "date" get put into feed
  636. posts = [p for p in pages if "date" in p]
  637. # sort by update if available, date else
  638. posts.sort(key=lambda p: p.get("update", p.date), reverse=True)
  639. # only put 20 most recent items in feed
  640. posts = posts[:20]
  641. for p in posts:
  642. title = p.title
  643. if "post" in p:
  644. title = p.post
  645. link = "%s/%s" % (BASE_URL, p.url)
  646. desc = p.html.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 = desc.replace("href=\"/img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
  649. desc = desc.replace("src=\"/img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
  650. desc = htmlspecialchars(desc)
  651. date = time.mktime(time.strptime("%s 12" % p.date, "%Y-%m-%d %H"))
  652. date = email.utils.formatdate(date)
  653. update = time.mktime(time.strptime("%s 12" % p.get("update", p.date), "%Y-%m-%d %H"))
  654. update = email.utils.formatdate(update)
  655. items.append(_RSS_ITEM % (title, link, desc, date, update, link))
  656. items = "".join(items)
  657. style = "/css/rss.xsl"
  658. title = "xythobuz.de Blog"
  659. link = "%s" % BASE_URL
  660. feed = "%s/rss.xml" % BASE_URL
  661. desc = htmlspecialchars("xythobuz Electronics & Software Projects")
  662. date = email.utils.formatdate()
  663. rss = _RSS % (style, title, link, feed, desc, date, date, items)
  664. fp = codecs.open(os.path.join(output, "rss.xml"), "w", "utf-8")
  665. fp.write(rss)
  666. fp.close()
  667. # -----------------------------------------------------------------------------
  668. # compatibility redirect for old mobile pages
  669. # -----------------------------------------------------------------------------
  670. _COMPAT_MOB = """ case "%s":
  671. $loc = "%s/%s";
  672. break;
  673. """
  674. _COMPAT_404_MOB = """ default:
  675. $loc = "%s";
  676. break;
  677. """
  678. def hook_postconvert_mobilecompat():
  679. directory = os.path.join(output, "mobile")
  680. if not os.path.exists(directory):
  681. os.makedirs(directory)
  682. fp = codecs.open(os.path.join(directory, "index.php"), "w", "utf-8")
  683. fp.write("<?\n")
  684. fp.write("// Auto generated xyCMS compatibility mobile/index.php\n")
  685. fp.write("$loc = '" + get_conf("base_url") + "/index.de.html';\n")
  686. fp.write("if (isset($_GET['p'])) {\n")
  687. fp.write(" if (isset($_GET['lang'])) {\n")
  688. fp.write(" $_GET['p'] .= 'EN';\n")
  689. fp.write(" }\n")
  690. fp.write(" switch($_GET['p']) {\n")
  691. for p in pages:
  692. if p.get("compat", "") != "":
  693. tmp = p["compat"]
  694. if p.get("lang", DEFAULT_LANG) == DEFAULT_LANG:
  695. tmp = tmp + "EN"
  696. fp.write(_COMPAT_MOB % (tmp, get_conf("base_url"), re.sub(".html", ".html", p.url)))
  697. fp.write("\n")
  698. fp.write(_COMPAT_404_MOB % "/404.mob.html")
  699. fp.write(" }\n")
  700. fp.write("}\n")
  701. fp.write("if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {\n")
  702. fp.write(" if (php_sapi_name() == 'cgi') {\n")
  703. fp.write(" header('Status: 301 Moved Permanently');\n")
  704. fp.write(" } else {\n")
  705. fp.write(" header('HTTP/1.1 301 Moved Permanently');\n")
  706. fp.write(" }\n")
  707. fp.write("}\n");
  708. fp.write("header('Location: '.$loc);\n")
  709. fp.write("?>")
  710. fp.close()
  711. # -----------------------------------------------------------------------------
  712. # displaying filesize for download links
  713. # -----------------------------------------------------------------------------
  714. def hook_postconvert_size():
  715. file_ext = '|'.join(['pdf', 'zip', 'rar', 'ods', 'odt', 'odp', 'doc', 'xls', 'ppt', 'docx', 'xlsx', 'pptx', 'exe', 'brd', 'plist'])
  716. def matched_link(matchobj):
  717. try:
  718. path = matchobj.group(1)
  719. if path.startswith("http") or path.startswith("//") or path.startswith("ftp"):
  720. return '<a href=\"%s\">%s</a>' % (matchobj.group(1), matchobj.group(3))
  721. elif path.startswith("/"):
  722. path = path.strip("/")
  723. path = os.path.join("static/", path)
  724. size = os.path.getsize(path)
  725. if size >= (1024 * 1024):
  726. return "<a href=\"%s\">%s</a>&nbsp;(%.1f MiB)" % (matchobj.group(1), matchobj.group(3), size / (1024.0 * 1024.0))
  727. elif size >= 1024:
  728. return "<a href=\"%s\">%s</a>&nbsp;(%d KiB)" % (matchobj.group(1), matchobj.group(3), size // 1024)
  729. else:
  730. return "<a href=\"%s\">%s</a>&nbsp;(%d Byte)" % (matchobj.group(1), matchobj.group(3), size)
  731. except:
  732. print("Unable to estimate file size for %s" % matchobj.group(1))
  733. return '<a href=\"%s\">%s</a>' % (matchobj.group(1), matchobj.group(3))
  734. _re_url = r'<a href=\"([^\"]*?\.(%s))\">(.*?)<\/a>' % file_ext
  735. for p in pages:
  736. p.html = re.sub(_re_url, matched_link, p.html)