# Generate this week's friday hack
# Please first update _data/friday_hacks.yml before
# running this shit bro
import yaml
from datetime import datetime, timedelta
from os import listdir
from os.path import isfile, join
import re

with open('../_data/friday_hacks.yml', 'r') as fin:
    doc = yaml.load(fin)
    start_date = datetime.strptime(doc['start_date'],
                               '%Y-%m-%d %H:%M:%S +0800')
    now = datetime.today()
    hacks = doc['hacks']
    cur = start_date
    next_hack = None
    next_date = None
    for hack in hacks:
        if cur > now:
            next_hack = hack
            next_date = cur
            break
    if not next_hack:
        print "Dude semester's over"
        quit()

    if not next_hack.get('speaker'):
        print "Dude no hackz"
        quit()

    name = raw_input("Your name bro? ")

    # so future-proof it's sick
    fhre = re.compile(
        r'^20[0-9][0-9]-[01][0-9]-[0-3][0-9]-friday-hacks-([1-9][0-9]*)-[a-z]*-[0-9]+\.md$')

    num = 0
    # so.. tempted... to... use lazy evaluation
    for f in listdir('../_posts/'):
        result = fhre.search(f)
        if result:
            cur = int(result.group(1))
            if cur > num:
                num = cur
    
    num += 1
    # now witness templating in raw string
    content = '''\
---
layout: friday_hack
title: "Friday Hacks #{num}, {month} {day}"
date: {now}
author: {author}
---

--- say something as introduction ---

{{% include friday_hack_header.html %}}

### {talk_name}

#### Talk Description:

--- describe ----

#### Speaker Profile

--- describe ----

'''.format(num=num,
           now=datetime.today(),
           month=next_date.strftime("%B"),
           day=next_date.day,
           talk_name=next_hack['title'],
           author=name)

    filename = '../_posts/{now}-friday-hacks-{num}-{month}-{day}.md'.format(
            now=next_date.strftime("%Y-%m-%d"),
            num=num,
            month=next_date.strftime('%b'),
            day=next_date.day,
        )

    with open(filename, 'a') as fout:
        fout.write(content)

@@ -753,15 +753,14 @@
 et('
-speaker
+topics
 '):%0A
@@ -1499,109 +1499,8 @@
 %7D%7D%0A%0A
-### %7Btalk_name%7D%0A%0A#### Talk Description:%0A%0A--- describe ----%0A%0A#### Speaker Profile%0A%0A--- describe ----%0A%0A
 '''.
@@ -1636,60 +1636,209 @@
     
-talk_name=next_hack%5B'title'%5D,%0A           author=name
+author=name) + '%5Cn'.join(%5B'''%0A### %7Btalk_name%7D%0A%0A#### Talk Description:%0A%0A--- describe ----%0A%0A#### Speaker Profile%0A%0A--- describe ----%0A%0A'''.format(talk_name=topic%5B'title'%5D) for topic in next_hack%5B'topics'%5D%5D
 )%0A%0A 
@@ -1917,28 +1917,24 @@
 at(%0A        
- 
-   
 now=next_dat
@@ -1965,20 +1965,16 @@
         
-    
 num=num,
@@ -1978,28 +1978,24 @@
 um,%0A        
-   
- 
 month=next_d
@@ -2018,28 +2018,24 @@
 '),%0A        
-    
 day=next_dat
@@ -2037,28 +2037,24 @@
 t_date.day,%0A
-    
     )%0A%0A    w

update gen friday hacks script