My static website generator using poole https://www.xythobuz.de
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import re
  2. import itertools
  3. import email.utils
  4. import os.path
  5. import time
  6. import codecs
  7. from datetime import datetime
  8. DEFAULT_LANG = "en"
  9. BASE_URL = "http://www.xythobuz.de"
  10. # -----------------------------------------------------------------------------
  11. # preconvert hooks
  12. # -----------------------------------------------------------------------------
  13. def hook_preconvert_anotherlang():
  14. MKD_PATT = r'\.(?:md|mkd|mdown|markdown)$'
  15. _re_lang = re.compile(r'^[\s+]?lang[\s+]?[:=]((?:.|\n )*)', re.MULTILINE)
  16. vpages = [] # Set of all virtual pages
  17. for p in pages:
  18. current_lang = DEFAULT_LANG # Default language
  19. langs = [] # List of languages for the current page
  20. page_vpages = {} # Set of virtual pages for the current page
  21. text_lang = re.split(_re_lang, p.source)
  22. text_grouped = dict(zip([current_lang,] + \
  23. [lang.strip() for lang in text_lang[1::2]], \
  24. text_lang[::2]))
  25. for lang, text in text_grouped.iteritems():
  26. spath = p.fname.split(os.path.sep)
  27. langs.append(lang)
  28. if lang == "en":
  29. filename = re.sub(MKD_PATT, "%s\g<0>" % "", p.fname).split(os.path.sep)[-1]
  30. else:
  31. filename = re.sub(MKD_PATT, ".%s\g<0>" % lang, p.fname).split(os.path.sep)[-1]
  32. vp = Page(filename, virtual=text)
  33. # Copy real page attributes to the virtual page
  34. for attr in p:
  35. if not vp.has_key(attr):
  36. vp[attr] = p[attr]
  37. # Define a title in the proper language
  38. vp["title"] = p["title_%s" % lang] \
  39. if p.has_key("title_%s" % lang) \
  40. else p["title"]
  41. # Keep track of the current lang of the virtual page
  42. vp["lang"] = lang
  43. # Fix post name if exists
  44. if vp.has_key("post"):
  45. if lang == "en":
  46. vp["post"] = vp["post"][:]
  47. else:
  48. vp["post"] = vp["post"][:-len(lang) - 1]
  49. page_vpages[lang] = vp
  50. # Each virtual page has to know about its sister vpages
  51. for lang, vpage in page_vpages.iteritems():
  52. vpage["lang_links"] = dict([(l, v["url"]) for l, v in page_vpages.iteritems()])
  53. vpage["other_lang"] = langs # set other langs and link
  54. vpages += page_vpages.values()
  55. pages[:] = vpages
  56. _COMPAT = """ case "%s":
  57. $loc = "%s/%s";
  58. break;
  59. """
  60. _COMPAT_404 = """ default:
  61. $loc = "%s";
  62. break;
  63. """
  64. def hook_preconvert_compat():
  65. fp = open(os.path.join(options.project, "output", "index.php"), 'w')
  66. fp.write("<?\n")
  67. fp.write("// Auto generated xyCMS compatibility index.php\n")
  68. fp.write("$loc = 'http://xythobuz.de/index.de.html';\n")
  69. fp.write("if (isset($_GET['p'])) {\n")
  70. fp.write(" if (isset($_GET['lang'])) {\n")
  71. fp.write(" $_GET['p'] .= 'EN';\n")
  72. fp.write(" }\n")
  73. fp.write(" switch($_GET['p']) {\n")
  74. for p in pages:
  75. if p.get("compat", "") != "":
  76. tmp = p["compat"]
  77. if p.get("lang", DEFAULT_LANG) == DEFAULT_LANG:
  78. tmp = tmp + "EN"
  79. fp.write(_COMPAT % (tmp, "http://xythobuz.de", p.url))
  80. fp.write("\n")
  81. fp.write(_COMPAT_404 % "/404.html")
  82. fp.write(" }\n")
  83. fp.write("}\n")
  84. fp.write("if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {\n")
  85. fp.write(" if (php_sapi_name() == 'cgi') {\n")
  86. fp.write(" header('Status: 301 Moved Permanently');\n")
  87. fp.write(" } else {\n")
  88. fp.write(" header('HTTP/1.1 301 Moved Permanently');\n")
  89. fp.write(" }\n")
  90. fp.write("}\n");
  91. fp.write("header('Location: '.$loc);\n")
  92. fp.write("?>")
  93. fp.close()
  94. _SITEMAP = """<?xml version="1.0" encoding="UTF-8"?>
  95. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  96. %s
  97. </urlset>
  98. """
  99. _SITEMAP_URL = """
  100. <url>
  101. <loc>%s/%s</loc>
  102. <lastmod>%s</lastmod>
  103. <changefreq>%s</changefreq>
  104. <priority>%s</priority>
  105. </url>
  106. """
  107. def hook_preconvert_sitemap():
  108. date = datetime.strftime(datetime.now(), "%Y-%m-%d")
  109. urls = []
  110. for p in pages:
  111. urls.append(_SITEMAP_URL % (BASE_URL, p.url, date, p.get("changefreq", "monthly"), p.get("priority", "0.5")))
  112. fname = os.path.join(options.project, "output", "sitemap.xml")
  113. fp = open(fname, 'w')
  114. fp.write(_SITEMAP % "".join(urls))
  115. fp.close()
  116. # -----------------------------------------------------------------------------
  117. # postconvert hooks
  118. # -----------------------------------------------------------------------------
  119. _RSS = """<?xml version="1.0"?>
  120. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  121. <channel>
  122. <title>%s</title>
  123. <link>%s</link>
  124. <atom:link href="%s" rel="self" type="application/rss+xml" />
  125. <description>%s</description>
  126. <language>en-us</language>
  127. <pubDate>%s</pubDate>
  128. <lastBuildDate>%s</lastBuildDate>
  129. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  130. <generator>Poole</generator>
  131. %s
  132. </channel>
  133. </rss>
  134. """
  135. _RSS_ITEM = """
  136. <item>
  137. <title>%s</title>
  138. <link>%s</link>
  139. <description>%s</description>
  140. <pubDate>%s</pubDate>
  141. <guid>%s</guid>
  142. </item>
  143. """
  144. def hook_postconvert_rss():
  145. items = []
  146. posts = [p for p in pages if "post" in p] # get all blog post pages
  147. posts.sort(key=lambda p: p.date, reverse=True)
  148. posts = posts[:10]
  149. for p in posts:
  150. title = p.post
  151. link = "%s/%s" % (BASE_URL, p.url)
  152. desc = p.html.replace("href=\"img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
  153. desc = desc.replace("src=\"img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
  154. desc = htmlspecialchars(desc)
  155. date = time.mktime(time.strptime("%s 12" % p.date, "%Y-%m-%d %H"))
  156. date = email.utils.formatdate(date)
  157. items.append(_RSS_ITEM % (title, link, desc, date, link))
  158. items = "".join(items)
  159. title = "xythobuz.de Blog"
  160. link = "%s/blog.html" % BASE_URL
  161. feed = "%s/rss.xml" % BASE_URL
  162. desc = htmlspecialchars("xythobuz Electronics & Software Projects")
  163. date = email.utils.formatdate()
  164. rss = _RSS % (title, link, feed, desc, date, date, items)
  165. fp = codecs.open(os.path.join(output, "rss.xml"), "w", "utf-8")
  166. fp.write(rss)
  167. fp.close()
  168. _COMPAT_MOB = """ case "%s":
  169. $loc = "%s/%s";
  170. break;
  171. """
  172. _COMPAT_404_MOB = """ default:
  173. $loc = "%s";
  174. break;
  175. """
  176. def hook_postconvert_mobilecompat():
  177. directory = os.path.join(output, "mobile")
  178. if not os.path.exists(directory):
  179. os.makedirs(directory)
  180. fp = codecs.open(os.path.join(directory, "index.php"), "w", "utf-8")
  181. fp.write("<?\n")
  182. fp.write("// Auto generated xyCMS compatibility mobile/index.php\n")
  183. fp.write("$loc = 'http://www.xythobuz.de/index.de.html';\n")
  184. fp.write("if (isset($_GET['p'])) {\n")
  185. fp.write(" if (isset($_GET['lang'])) {\n")
  186. fp.write(" $_GET['p'] .= 'EN';\n")
  187. fp.write(" }\n")
  188. fp.write(" switch($_GET['p']) {\n")
  189. for p in pages:
  190. if p.get("compat", "") != "":
  191. tmp = p["compat"]
  192. if p.get("lang", DEFAULT_LANG) == DEFAULT_LANG:
  193. tmp = tmp + "EN"
  194. fp.write(_COMPAT_MOB % (tmp, "http://xythobuz.de", re.sub(".html", ".html", p.url)))
  195. fp.write("\n")
  196. fp.write(_COMPAT_404_MOB % "/404.mob.html")
  197. fp.write(" }\n")
  198. fp.write("}\n")
  199. fp.write("if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {\n")
  200. fp.write(" if (php_sapi_name() == 'cgi') {\n")
  201. fp.write(" header('Status: 301 Moved Permanently');\n")
  202. fp.write(" } else {\n")
  203. fp.write(" header('HTTP/1.1 301 Moved Permanently');\n")
  204. fp.write(" }\n")
  205. fp.write("}\n");
  206. fp.write("header('Location: '.$loc);\n")
  207. fp.write("?>")
  208. fp.close()