blogit.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #!/usr/bin/env python
  2. # Copyleft (C) 2010 Mir Nazim <hello@mirnazim.org>
  3. # Copyleft (C) 2013 Oz Nahum <nahumoz@gmail.com>
  4. #
  5. # Everyone is permitted to copy and distribute verbatim or modified
  6. # copies of this license document, and changing it is allowed as long
  7. # as the name is changed.
  8. #
  9. # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  10. #
  11. # 0. You just DO WHATEVER THE FUCK YOU WANT TO. (IT'S SLOPPY CODE ANYWAY)
  12. #
  13. # WARANTIES:
  14. # 0. Are you kidding me?
  15. # 1. Seriously, Are you fucking kidding me?
  16. # 2. If anything goes wrong, sue the "The Empire".
  17. # Note about Summary
  18. # has to be 1 line, no '\n' allowed!
  19. """
  20. Summary: |
  21. some summary ...
  22. Your post
  23. """
  24. """
  25. Everything the Header can't have ":" or "..." in it, you can't have title
  26. with ":" it makes markdown break!
  27. """
  28. """
  29. The content directory can contain only mardown or txt files, no images
  30. allowed!
  31. """
  32. import os
  33. import re
  34. import datetime
  35. import yaml # in debian python-yaml
  36. from StringIO import StringIO
  37. import codecs
  38. from jinja2 import Environment, FileSystemLoader # in debian python-jinja2
  39. try:
  40. import markdown2
  41. except ImportError:
  42. import markdown as markdown2
  43. import argparse
  44. import sys
  45. from distutils import dir_util
  46. import shutil
  47. CONFIG = {
  48. 'content_root': 'content', # where the markdown files are
  49. 'output_to': 'oz123.github.com',
  50. 'templates': 'templates',
  51. 'date_format': '%Y-%m-%d',
  52. 'base_url': 'http://oz123.github.com',
  53. 'http_port': 3030,
  54. 'content_encoding': 'utf-8',
  55. }
  56. # EDIT THIS PARAMETER TO CHANGE ARCHIVE SIZE
  57. # 0 Means that all the entries will be in the archive
  58. # 10 meas that all the entries except the last 10
  59. ARCHIVE_SIZE = 0
  60. GLOBAL_TEMPLATE_CONTEXT = {
  61. 'media_base': '/media/',
  62. 'media_url': '../media/',
  63. 'site_url': 'http://oz123.github.com',
  64. 'last_build': datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
  65. 'twitter': 'https://twitter.com/#!/OzNTiram',
  66. 'stackoverflow': "http://stackoverflow.com/users/492620/oz123",
  67. 'github': "https://github.com/oz123",
  68. 'google_analytics': """
  69. <script type="text/javascript">
  70. var _gaq = _gaq || [];
  71. _gaq.push(['_setAccount', 'UA-36587163-1']);
  72. _gaq.push(['_trackPageview']);
  73. (function() {
  74. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  75. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  76. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  77. })();
  78. </script>""",
  79. 'disquss' : """<div id="disqus_thread"></div>
  80. <script type="text/javascript">
  81. /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
  82. var disqus_shortname = 'oz123githubcom'; // required: replace example with your forum shortname
  83. /* * * DON'T EDIT BELOW THIS LINE * * */
  84. (function() {
  85. var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  86. dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  87. (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  88. })();
  89. </script>
  90. <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  91. <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
  92. """
  93. }
  94. KINDS = {
  95. 'writing': {
  96. 'name': 'writing', 'name_plural': 'writings',
  97. },
  98. 'note': {
  99. 'name': 'note', 'name_plural': 'notes',
  100. },
  101. 'link': {
  102. 'name': 'link', 'name_plural': 'links',
  103. },
  104. 'photo': {
  105. 'name': 'photo', 'name_plural': 'photos',
  106. },
  107. 'page': {
  108. 'name': 'page', 'name_plural': 'pages',
  109. },
  110. }
  111. jinja_env = Environment(loader=FileSystemLoader(CONFIG['templates']))
  112. class Tag(object):
  113. def __init__(self, name):
  114. super(Tag, self).__init__()
  115. self.name = name
  116. self.prepare()
  117. self.permalink = GLOBAL_TEMPLATE_CONTEXT["site_url"]
  118. def prepare(self):
  119. _slug = self.name.lower()
  120. _slug = re.sub(r'[;;,. ]', '-', _slug)
  121. self.slug = _slug
  122. class Entry(object):
  123. def __init__(self, path):
  124. super(Entry, self).__init__()
  125. path = path.split('content/')[-1]
  126. self.path = path
  127. self.prepare()
  128. def __str__(self):
  129. return self.path
  130. def __repr__(self):
  131. return self.path
  132. @property
  133. def name(self):
  134. return os.path.splitext(os.path.basename(self.path))[0]
  135. @property
  136. def abspath(self):
  137. return os.path.abspath(os.path.join(CONFIG['content_root'], self.path))
  138. @property
  139. def destination(self):
  140. dest = "%s/%s/index.html" % (KINDS[
  141. self.kind]['name_plural'], self.name)
  142. print dest
  143. return os.path.join(CONFIG['output_to'], dest)
  144. @property
  145. def title(self):
  146. return self.header['title']
  147. @property
  148. def summary_html(self):
  149. return "%s" % markdown2.markdown(self.header['summary'].strip())
  150. @property
  151. def credits_html(self):
  152. return "%s" % markdown2.markdown(self.header['credits'].strip())
  153. @property
  154. def summary_atom(self):
  155. summarya = markdown2.markdown(self.header['summary'].strip())
  156. summarya = re.sub("<p>|</p>", "", summarya)
  157. more = '<a href="%s"> continue reading...</a>' % (self.permalink)
  158. return summarya+more
  159. @property
  160. def published_html(self):
  161. if self.kind in ['link', 'note', 'photo']:
  162. return self.header['published'].strftime("%B %d, %Y %I:%M %p")
  163. return self.header['published'].strftime("%B %d, %Y")
  164. @property
  165. def published_atom(self):
  166. return self.published.strftime("%Y-%m-%dT%H:%M:%SZ")
  167. @property
  168. def atom_id(self):
  169. return "tag:%s,%s:%s" % \
  170. (
  171. self.published.strftime("%Y-%m-%d"),
  172. self.permalink,
  173. GLOBAL_TEMPLATE_CONTEXT["site_url"]
  174. )
  175. @property
  176. def body_html(self):
  177. return markdown2.markdown(self.body) # , extras=['code-color'])
  178. @property
  179. def permalink(self):
  180. return "/%s/%s" % (KINDS[self.kind]['name_plural'], self.name)
  181. @property
  182. def tags(self):
  183. tags = list()
  184. for t in self.header['tags']:
  185. tags.append(Tag(t))
  186. return tags
  187. def prepare(self):
  188. file = codecs.open(self.abspath, 'r')
  189. header = ['---']
  190. while True:
  191. line = file.readline()
  192. line = line.rstrip()
  193. if not line:
  194. break
  195. header.append(line)
  196. self.header = yaml.load(StringIO('\n'.join(header)))
  197. for h in self.header.items():
  198. if h:
  199. try:
  200. setattr(self, h[0], h[1])
  201. except:
  202. pass
  203. body = list()
  204. for line in file.readlines():
  205. body.append(line)
  206. self.body = ''.join(body)
  207. file.close()
  208. if self.kind == 'link':
  209. from urlparse import urlparse
  210. self.domain_name = urlparse(self.url).netloc
  211. elif self.kind == 'photo':
  212. pass
  213. elif self.kind == 'note':
  214. pass
  215. elif self.kind == 'writing':
  216. pass
  217. def render(self):
  218. if not self.header['public']:
  219. return False
  220. try:
  221. os.makedirs(os.path.dirname(self.destination))
  222. except:
  223. pass
  224. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  225. context['entry'] = self
  226. template = jinja_env.get_template("entry.html")
  227. html = template.render(context)
  228. destination = codecs.open(
  229. self.destination, 'w', CONFIG['content_encoding'])
  230. destination.write(html)
  231. destination.close()
  232. return True
  233. class Link(Entry):
  234. def __init__(self, path):
  235. super(Link, self).__init__(path)
  236. @property
  237. def permalink(self):
  238. print "self.url", self.url
  239. raw_input()
  240. return self.url
  241. def entry_factory():
  242. pass
  243. def _sort_entries(entries):
  244. _entries = dict()
  245. sorted_entries = list()
  246. for entry in entries:
  247. _published = entry.header['published'].isoformat()
  248. _entries[_published] = entry
  249. sorted_keys = sorted(_entries.keys())
  250. sorted_keys.reverse()
  251. for key in sorted_keys:
  252. sorted_entries.append(_entries[key])
  253. return sorted_entries
  254. def render_index(entries):
  255. """
  256. this function renders the main page located at index.html
  257. under oz123.github.com
  258. """
  259. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  260. context['entries'] = entries[:10]
  261. template = jinja_env.get_template('entry_index.html')
  262. html = template.render(context)
  263. destination = codecs.open("%s/index.html" % CONFIG[
  264. 'output_to'], 'w', CONFIG['content_encoding'])
  265. destination.write(html)
  266. destination.close()
  267. def render_archive(entries, render_to=None):
  268. """
  269. this function creates the archive page
  270. """
  271. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  272. context['entries'] = entries[ARCHIVE_SIZE:]
  273. template = jinja_env.get_template('archive_index.html')
  274. html = template.render(context)
  275. if not render_to:
  276. render_to = "%s/archive/index.html" % CONFIG['output_to']
  277. dir_util.mkpath("%s/archive" % CONFIG['output_to'])
  278. destination = codecs.open("%s/archive/index.html" % CONFIG[
  279. 'output_to'], 'w', CONFIG['content_encoding'])
  280. destination.write(html)
  281. destination.close()
  282. def render_atom_feed(entries, render_to=None):
  283. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  284. context['entries'] = entries[:10]
  285. template = jinja_env.get_template('atom.xml')
  286. html = template.render(context)
  287. if not render_to:
  288. render_to = "%s/atom.xml" % CONFIG['output_to']
  289. destination = codecs.open(render_to, 'w', CONFIG['content_encoding'])
  290. destination.write(html)
  291. destination.close()
  292. def render_tag_pages(tag_tree):
  293. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  294. for t in tag_tree.items():
  295. context['tag'] = t[1]['tag']
  296. context['entries'] = _sort_entries(t[1]['entries'])
  297. destination = "%s/tags/%s" % (CONFIG['output_to'], context['tag'].slug)
  298. try:
  299. os.makedirs(destination)
  300. except:
  301. pass
  302. template = jinja_env.get_template('tag_index.html')
  303. html = template.render(context)
  304. file = codecs.open("%s/index.html" %
  305. destination, 'w', CONFIG['content_encoding'])
  306. file.write(html)
  307. file.close()
  308. render_atom_feed(context[
  309. 'entries'], render_to="%s/atom.xml" % destination)
  310. def build():
  311. print
  312. print "Rendering website now..."
  313. print
  314. print " entries:"
  315. entries = list()
  316. tags = dict()
  317. for root, dirs, files in os.walk(CONFIG['content_root']):
  318. for fileName in files:
  319. try:
  320. if fileName.endswith('md') or fileName.endswith('markdown'):
  321. entry = Entry(os.path.join(root, fileName))
  322. except Exception, e:
  323. print "Found some problem in: ", fileName
  324. print e
  325. raw_input("Please correct")
  326. sys.exit()
  327. if entry.render():
  328. entries.append(entry)
  329. for tag in entry.tags:
  330. if tag.name not in tags:
  331. tags[tag.name] = {
  332. 'tag': tag,
  333. 'entries': list(),
  334. }
  335. tags[tag.name]['entries'].append(entry)
  336. print " %s" % entry.path
  337. print " :done"
  338. print
  339. print " tag pages & their atom feeds:"
  340. render_tag_pages(tags)
  341. print " :done"
  342. print
  343. print " site wide index"
  344. entries = _sort_entries(entries)
  345. render_index(entries)
  346. print "................done"
  347. print " archive index"
  348. render_archive(entries)
  349. print "................done"
  350. print " site wide atom feeds"
  351. render_atom_feed(entries)
  352. print "...........done"
  353. print
  354. print "All done "
  355. def preview(PREVIEW_ADDR='127.0.1.1', PREVIEW_PORT=11000):
  356. """
  357. launch an HTTP to preview the website
  358. """
  359. import SimpleHTTPServer
  360. import SocketServer
  361. Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
  362. httpd = SocketServer.TCPServer(("", CONFIG['http_port']), Handler)
  363. os.chdir(CONFIG['output_to'])
  364. print "and ready to test at http://127.0.0.1:%d" % CONFIG['http_port']
  365. print "Hit Ctrl+C to exit"
  366. try:
  367. httpd.serve_forever()
  368. except KeyboardInterrupt:
  369. print
  370. print "Shutting Down... Bye!."
  371. print
  372. httpd.server_close()
  373. def publish(GITDIRECTORY="oz123.github.com"):
  374. pass
  375. def clean(GITDIRECTORY="oz123.github.com"):
  376. directoriestoclean = ["writings", "notes", "links", "tags", "archive"]
  377. os.chdir(GITDIRECTORY)
  378. for directory in directoriestoclean:
  379. shutil.rmtree(directory)
  380. def dist(SOURCEDIR=os.getcwd()+"/content/", DESTDIR="oz123.github.com/writings_raw/content/"):
  381. """
  382. sync raw files from SOURCE to DEST
  383. """
  384. import subprocess as sp
  385. sp.call(["rsync", "-avP", SOURCEDIR, DESTDIR], shell=False, cwd=os.getcwd())
  386. if __name__ == '__main__':
  387. parser = argparse.ArgumentParser(
  388. description='blogit - a tool to blog on github.')
  389. parser.add_argument('-b', '--build', action="store_true",
  390. help='convert the markdown files to HTML')
  391. parser.add_argument('-p', '--preview', action="store_true",
  392. help='Launch HTTP server to preview the website')
  393. parser.add_argument('-c', '--clean', action="store_true",
  394. help='clean output files')
  395. parser.add_argument('-d', '--dist', action="store_true",
  396. help='sync raw files from SOURCE to DEST')
  397. args = parser.parse_args()
  398. if len(sys.argv) < 2:
  399. parser.print_help()
  400. sys.exit()
  401. if args.clean:
  402. clean()
  403. if args.build:
  404. build()
  405. if args.dist:
  406. dist()
  407. if args.preview:
  408. preview()