blogit.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # Blogit.py is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License, version 3
  5. # as published by the Free Software Foundation;
  6. #
  7. # Blogit.py is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with Blogit.py; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. #============================================================================
  16. # Copyright (C) 2013 Oz Nahum <nahumoz@gmail.com>
  17. #============================================================================
  18. # Note about Summary
  19. # has to be 1 line, no '\n' allowed!
  20. """
  21. Summary: |
  22. some summary ...
  23. Your post
  24. """
  25. """
  26. Everything the Header can't have ":" or "..." in it, you can't have title
  27. with ":" it makes markdown break!
  28. """
  29. """
  30. The content directory can contain only mardown or txt files, no images
  31. allowed!
  32. """
  33. import os
  34. import re
  35. import datetime
  36. import argparse
  37. import sys
  38. from distutils import dir_util
  39. import shutil
  40. from StringIO import StringIO
  41. import codecs
  42. try:
  43. import yaml # in debian python-yaml
  44. from jinja2 import Environment, FileSystemLoader # in debian python-jinja2
  45. except ImportError, e:
  46. print e
  47. print "On Debian based system you can install the dependencies with: "
  48. print "apt-get install python-yaml python-jinja2"
  49. sys.exit(1)
  50. try:
  51. import markdown2
  52. renderer = 'md2'
  53. except ImportError, e:
  54. try:
  55. import markdown
  56. renderer = 'md1'
  57. except ImportError, e:
  58. print e
  59. print "try: sudo pip install markdown2"
  60. sys.exit(1)
  61. CONFIG = {
  62. 'content_root': 'content', # where the markdown files are
  63. 'output_to': 'oz123.github.com',
  64. 'templates': 'templates',
  65. 'date_format': '%Y-%m-%d',
  66. 'base_url': 'http://oz123.github.com',
  67. 'http_port': 3030,
  68. 'content_encoding': 'utf-8',
  69. 'author': 'Oz Nahum Tiram',
  70. 'editor': 'editor'
  71. }
  72. # EDIT THIS PARAMETER TO CHANGE ARCHIVE SIZE
  73. # 0 Means that all the entries will be in the archive
  74. # 10 meas that all the entries except the last 10
  75. ARCHIVE_SIZE = 0
  76. GLOBAL_TEMPLATE_CONTEXT = {
  77. 'media_base': '/media/',
  78. 'media_url': '../media/',
  79. 'site_url': 'http://oz123.github.com',
  80. 'last_build': datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
  81. 'twitter': 'https://twitter.com/#!/OzNTiram',
  82. 'stackoverflow': "http://stackoverflow.com/users/492620/oz123",
  83. 'github': "https://github.com/oz123",
  84. }
  85. KINDS = {
  86. 'writing': {
  87. 'name': 'writing', 'name_plural': 'writings',
  88. },
  89. 'note': {
  90. 'name': 'note', 'name_plural': 'notes',
  91. },
  92. 'link': {
  93. 'name': 'link', 'name_plural': 'links',
  94. },
  95. 'photo': {
  96. 'name': 'photo', 'name_plural': 'photos',
  97. },
  98. 'page': {
  99. 'name': 'page', 'name_plural': 'pages',
  100. },
  101. }
  102. jinja_env = Environment(loader=FileSystemLoader(CONFIG['templates']))
  103. class Tag(object):
  104. def __init__(self, name):
  105. super(Tag, self).__init__()
  106. self.name = name
  107. self.prepare()
  108. self.permalink = GLOBAL_TEMPLATE_CONTEXT["site_url"]
  109. def prepare(self):
  110. _slug = self.name.lower()
  111. _slug = re.sub(r'[;;,. ]', '-', _slug)
  112. self.slug = _slug
  113. class Entry(object):
  114. def __init__(self, path):
  115. super(Entry, self).__init__()
  116. path = path.split('content/')[-1]
  117. self.path = path
  118. self.prepare()
  119. def __str__(self):
  120. return self.path
  121. def __repr__(self):
  122. return self.path
  123. @property
  124. def name(self):
  125. return os.path.splitext(os.path.basename(self.path))[0]
  126. @property
  127. def abspath(self):
  128. return os.path.abspath(os.path.join(CONFIG['content_root'], self.path))
  129. @property
  130. def destination(self):
  131. dest = "%s/%s/index.html" % (KINDS[
  132. self.kind]['name_plural'], self.name)
  133. print dest
  134. return os.path.join(CONFIG['output_to'], dest)
  135. @property
  136. def title(self):
  137. return self.header['title']
  138. @property
  139. def summary_html(self):
  140. return "%s" % markdown2.markdown(self.header['summary'].strip())
  141. @property
  142. def credits_html(self):
  143. return "%s" % markdown2.markdown(self.header['credits'].strip())
  144. @property
  145. def summary_atom(self):
  146. summarya = markdown2.markdown(self.header['summary'].strip())
  147. summarya = re.sub("<p>|</p>", "", summarya)
  148. more = '<a href="%s"> continue reading...</a>' % (self.permalink)
  149. return summarya+more
  150. @property
  151. def published_html(self):
  152. if self.kind in ['link', 'note', 'photo']:
  153. return self.header['published'].strftime("%B %d, %Y %I:%M %p")
  154. return self.header['published'].strftime("%B %d, %Y")
  155. @property
  156. def published_atom(self):
  157. return self.published.strftime("%Y-%m-%dT%H:%M:%SZ")
  158. @property
  159. def atom_id(self):
  160. return "tag:%s,%s:%s" % \
  161. (
  162. self.published.strftime("%Y-%m-%d"),
  163. self.permalink,
  164. GLOBAL_TEMPLATE_CONTEXT["site_url"]
  165. )
  166. @property
  167. def body_html(self):
  168. if renderer == 'md2':
  169. return markdown2.markdown(self.body, extras=['fenced-code-blocks', 'hilite'])
  170. if renderer == 'md1':
  171. return markdown.markdown(self.body, extensions=['fenced_code', 'codehilite(linenums=False)'])
  172. @property
  173. def permalink(self):
  174. return "/%s/%s" % (KINDS[self.kind]['name_plural'], self.name)
  175. @property
  176. def tags(self):
  177. tags = list()
  178. for t in self.header['tags']:
  179. tags.append(Tag(t))
  180. return tags
  181. def prepare(self):
  182. file = codecs.open(self.abspath, 'r')
  183. header = ['---']
  184. while True:
  185. line = file.readline()
  186. line = line.rstrip()
  187. if not line:
  188. break
  189. header.append(line)
  190. self.header = yaml.load(StringIO('\n'.join(header)))
  191. for h in self.header.items():
  192. if h:
  193. try:
  194. setattr(self, h[0], h[1])
  195. except:
  196. pass
  197. body = list()
  198. for line in file.readlines():
  199. body.append(line)
  200. self.body = ''.join(body)
  201. file.close()
  202. if self.kind == 'link':
  203. from urlparse import urlparse
  204. self.domain_name = urlparse(self.url).netloc
  205. elif self.kind == 'photo':
  206. pass
  207. elif self.kind == 'note':
  208. pass
  209. elif self.kind == 'writing':
  210. pass
  211. def render(self):
  212. if not self.header['public']:
  213. return False
  214. try:
  215. os.makedirs(os.path.dirname(self.destination))
  216. except:
  217. pass
  218. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  219. context['entry'] = self
  220. # this is redundant ! every time we render entry we get_template?
  221. # todo: make template class property !
  222. template = jinja_env.get_template("entry.html")
  223. try:
  224. html = template.render(context)
  225. except Exception, e:
  226. print context
  227. print self.path
  228. print e
  229. sys.exit()
  230. destination = codecs.open(
  231. self.destination, 'w', CONFIG['content_encoding'])
  232. destination.write(html)
  233. destination.close()
  234. # before returning write log to csv
  235. # file name, date first seen, date rendered
  236. # self.path , date-first-seen, if rendered datetime.now
  237. return True
  238. class Link(Entry):
  239. def __init__(self, path):
  240. super(Link, self).__init__(path)
  241. @property
  242. def permalink(self):
  243. print "self.url", self.url
  244. raw_input()
  245. return self.url
  246. def entry_factory():
  247. pass
  248. def _sort_entries(entries):
  249. _entries = dict()
  250. sorted_entries = list()
  251. for entry in entries:
  252. _published = entry.header['published'].isoformat()
  253. _entries[_published] = entry
  254. sorted_keys = sorted(_entries.keys())
  255. sorted_keys.reverse()
  256. for key in sorted_keys:
  257. sorted_entries.append(_entries[key])
  258. return sorted_entries
  259. def render_index(entries):
  260. """
  261. this function renders the main page located at index.html
  262. under oz123.github.com
  263. """
  264. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  265. context['entries'] = entries[:10]
  266. template = jinja_env.get_template('entry_index.html')
  267. html = template.render(context)
  268. destination = codecs.open("%s/index.html" % CONFIG[
  269. 'output_to'], 'w', CONFIG['content_encoding'])
  270. destination.write(html)
  271. destination.close()
  272. def render_archive(entries, render_to=None):
  273. """
  274. this function creates the archive page
  275. """
  276. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  277. context['entries'] = entries[ARCHIVE_SIZE:]
  278. template = jinja_env.get_template('archive_index.html')
  279. html = template.render(context)
  280. if not render_to:
  281. render_to = "%s/archive/index.html" % CONFIG['output_to']
  282. dir_util.mkpath("%s/archive" % CONFIG['output_to'])
  283. destination = codecs.open("%s/archive/index.html" % CONFIG[
  284. 'output_to'], 'w', CONFIG['content_encoding'])
  285. destination.write(html)
  286. destination.close()
  287. def render_atom_feed(entries, render_to=None):
  288. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  289. context['entries'] = entries[:10]
  290. template = jinja_env.get_template('atom.xml')
  291. html = template.render(context)
  292. if not render_to:
  293. render_to = "%s/atom.xml" % CONFIG['output_to']
  294. destination = codecs.open(render_to, 'w', CONFIG['content_encoding'])
  295. destination.write(html)
  296. destination.close()
  297. def render_tag_pages(tag_tree):
  298. context = GLOBAL_TEMPLATE_CONTEXT.copy()
  299. for t in tag_tree.items():
  300. context['tag'] = t[1]['tag']
  301. context['entries'] = _sort_entries(t[1]['entries'])
  302. destination = "%s/tags/%s" % (CONFIG['output_to'], context['tag'].slug)
  303. try:
  304. os.makedirs(destination)
  305. except:
  306. pass
  307. template = jinja_env.get_template('tag_index.html')
  308. html = template.render(context)
  309. file = codecs.open("%s/index.html" %
  310. destination, 'w', CONFIG['content_encoding'])
  311. file.write(html)
  312. file.close()
  313. render_atom_feed(context[
  314. 'entries'], render_to="%s/atom.xml" % destination)
  315. def build():
  316. print
  317. print "Rendering website now..."
  318. print
  319. print " entries:"
  320. entries = list()
  321. tags = dict()
  322. for root, dirs, files in os.walk(CONFIG['content_root']):
  323. for fileName in files:
  324. try:
  325. if fileName.endswith('md') or fileName.endswith('markdown'):
  326. entry = Entry(os.path.join(root, fileName))
  327. except Exception, e:
  328. print "Found some problem in: ", entry.path
  329. print e
  330. print "Please correct this problem ..."
  331. sys.exit()
  332. if entry.render():
  333. entries.append(entry)
  334. for tag in entry.tags:
  335. if tag.name not in tags:
  336. tags[tag.name] = {
  337. 'tag': tag,
  338. 'entries': list(),
  339. }
  340. tags[tag.name]['entries'].append(entry)
  341. print " %s" % entry.path
  342. print " :done"
  343. print
  344. print " tag pages & their atom feeds:"
  345. render_tag_pages(tags)
  346. print " :done"
  347. print
  348. print " site wide index"
  349. entries = _sort_entries(entries)
  350. render_index(entries)
  351. print "................done"
  352. print " archive index"
  353. render_archive(entries)
  354. print "................done"
  355. print " site wide atom feeds"
  356. render_atom_feed(entries)
  357. print "...........done"
  358. print
  359. print "All done "
  360. def preview(PREVIEW_ADDR='127.0.1.1', PREVIEW_PORT=11000):
  361. """
  362. launch an HTTP to preview the website
  363. """
  364. import SimpleHTTPServer
  365. import SocketServer
  366. Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
  367. httpd = SocketServer.TCPServer(("", CONFIG['http_port']), Handler)
  368. os.chdir(CONFIG['output_to'])
  369. print "and ready to test at http://127.0.0.1:%d" % CONFIG['http_port']
  370. print "Hit Ctrl+C to exit"
  371. try:
  372. httpd.serve_forever()
  373. except KeyboardInterrupt:
  374. print
  375. print "Shutting Down... Bye!."
  376. print
  377. httpd.server_close()
  378. def publish(GITDIRECTORY=CONFIG['output_to']):
  379. pass
  380. def new_post(GITDIRECTORY=CONFIG['output_to'],
  381. kind=KINDS['writing']):
  382. """
  383. This function should create a template for a new post with a title
  384. read from the user input.
  385. Most other fields should be defaults.
  386. """
  387. title = raw_input("Give the title of the post: ")
  388. while ':' in title:
  389. title = raw_input("Give the title of the post (':' not allowed): ")
  390. author = CONFIG['author']
  391. date = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d')
  392. tags = '['+raw_input("Give the tags, separated by ', ':")+']'
  393. published = 'yes'
  394. chronological = 'yes'
  395. summary = ("summary: |\n Type your summary here.\n Do not change the "
  396. "indentation"
  397. "to the left\n ...\n\nStart writing your post here!")
  398. # make file name
  399. fname = os.path.join(os.getcwd(), 'content', kind['name_plural'],
  400. datetime.datetime.strftime(datetime.datetime.now(),
  401. '%Y'),
  402. date+'-'+title.replace(' ', '-')+'.markdown')
  403. with open(fname, 'w') as npost:
  404. npost.write('title: %s\n' % title)
  405. npost.write('author: %s\n' % author)
  406. npost.write('published: %s\n' % date)
  407. npost.write('tags: %s\n' % tags)
  408. npost.write('public: %s\n' % published)
  409. npost.write('chronological: %s\n' % chronological)
  410. npost.write('kind: %s\n' % kind['name'])
  411. npost.write('%s' % summary)
  412. os.system('%s %s' % (CONFIG['editor'], fname))
  413. def clean(GITDIRECTORY="oz123.github.com"):
  414. directoriestoclean = ["writings", "notes", "links", "tags", "archive"]
  415. os.chdir(GITDIRECTORY)
  416. for directory in directoriestoclean:
  417. shutil.rmtree(directory)
  418. def dist(SOURCEDIR=os.getcwd()+"/content/",
  419. DESTDIR="oz123.github.com/writings_raw/content/"):
  420. """
  421. sync raw files from SOURCE to DEST
  422. """
  423. import subprocess as sp
  424. sp.call(["rsync", "-avP", SOURCEDIR, DESTDIR], shell=False,
  425. cwd=os.getcwd())
  426. if __name__ == '__main__':
  427. parser = argparse.ArgumentParser(
  428. description='blogit - a tool to blog on github.')
  429. parser.add_argument('-b', '--build', action="store_true",
  430. help='convert the markdown files to HTML')
  431. parser.add_argument('-p', '--preview', action="store_true",
  432. help='Launch HTTP server to preview the website')
  433. parser.add_argument('-c', '--clean', action="store_true",
  434. help='clean output files')
  435. parser.add_argument('-n', '--new', action="store_true",
  436. help='create new post')
  437. parser.add_argument('-d', '--dist', action="store_true",
  438. help='sync raw files from SOURCE to DEST')
  439. args = parser.parse_args()
  440. if len(sys.argv) < 2:
  441. parser.print_help()
  442. sys.exit()
  443. if args.clean:
  444. clean()
  445. if args.build:
  446. build()
  447. if args.dist:
  448. dist()
  449. if args.preview:
  450. preview()
  451. if args.new:
  452. new_post()