"""
    Some tools for fetching articles and their metadata from the repo.
"""

from flask import Markup
from os import path, listdir
import markdown
import subprocess


def get_articles():
    """ Fetch a list of article dicts with the following properties:

        - content
        - author
        - date_added
    """
    articles = []
    article_dir = path.join(path.dirname(__file__), '..', 'articles')
    for article_file in listdir(article_dir):
        article_path = path.relpath(path.join(article_dir, article_file))
        article = {}
        article['title'] = article_file.rstrip('.md')
        article['date_added'] = _get_date_added(article_path)
        article['author'] = _get_author(article_path)
        with open(article_path) as article_fp:
            article_md = article_fp.read()
        article['content'] = Markup(markdown.markdown(article_md))
        articles.append(article)
    return articles


def _get_first_commit(article_path):
    raw_output = subprocess.check_output('git rev-list HEAD "%s"' % article_path)
    all_commits = raw_output.strip().split('\n')
    first_commit = all_commits[-1]
    return first_commit


def _get_date_added(article_path):
    first_commit = _get_first_commit(article_path)
    date_added = subprocess.check_output('git show -s --format="%%ci" %s --' % first_commit)
    nicely_formatted_date_added = ' '.join(date_added.split()[:2])
    return nicely_formatted_date_added


def _get_author(article_path):
    first_commit = _get_first_commit(article_path)
    raw_output = subprocess.check_output('git show -s --format="%%aN" "%s" --' % first_commit)
    return raw_output.strip()

@@ -525,16 +525,59 @@
 _file))%0A
+        print %22Article path%22, article_path%0A
         
@@ -589,16 +589,16 @@
 le = %7B%7D%0A
-
         
@@ -1016,45 +1016,14 @@
     
-raw_output = subprocess.check_output(
+cmd = 
 'git
@@ -1053,24 +1053,81 @@
 article_path
+%0A    raw_output = subprocess.check_output(cmd, shell=True
 )%0A    all_co
@@ -1320,45 +1320,14 @@
     
-date_added = subprocess.check_output(
+cmd = 
 'git
@@ -1368,24 +1368,81 @@
 first_commit
+%0A    date_added = subprocess.check_output(cmd, shell=True
 )%0A    nicely
@@ -1629,45 +1629,14 @@
     
-raw_output = subprocess.check_output(
+cmd = 
 'git
@@ -1683,16 +1683,73 @@
 t_commit
+%0A    raw_output = subprocess.check_output(cmd, shell=True
 )%0A    re

Send calls to system shell to avoid chrasing on *nix.