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