blogit.py 15 KB

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