Cover image
Brian T. Liao
May 31, 2019 • 8 min read

Summer Update Plus New Scripting

Hey it’s summer. I took an hiatus but I’m back. Got some stuff to rant. Probably will publish to Medium too and link it here. Get some view from there. I’m interning at Amazon Boston over the summer.

Using jekyll is kind of hard. I was manually setting everything up, the configuration of date and title and folder and what a mess.

This blog was never meant to be clean code (I’m sorry!!! It’s just supposed to look pretty and usable). So I write markdown with hacky HTML. That got hard so I wrote a script to convert my own domain specific language into HTML. This is a how-to comment I have for every page - have a look:

Image
pic pic-small pic-large
<a href="" target="_blank"><img src="" class="pic"/></a><small></small>

;>pic;>Data Science and Machine Learning;>https://i.imgur.com/MctcYW5.png;>

Link
<a href="" target="_blank"></a>

,>EECS 126: Probability,>https://inst.eecs.berkeley.edu/~ee126/fa18/,>

For images, pic pic-small pic-large are classes with CSS for image sizes.

;>pic;>Data Science and Machine Learning;>https://i.imgur.com/MctcYW5.png;>

generates the HTML

<a href="https://i.imgur.com/MctcYW5.png" target="_blank"><img src="https://i.imgur.com/MctcYW5.png" class="pic"/></a><small>Data Science and Machine Learning</small>

using the hacky delim ;>. This creates an image with a link, description and size. Now I don’t have to write all that HTML!

For links, I use ,> as a delim.

,>EECS 126: Probability,>https://inst.eecs.berkeley.edu/~ee126/fa18/,>

becomes

<a href="https://inst.eecs.berkeley.edu/~ee126/fa18/" target="_blank">EECS 126: Probability</a>

using the same magic. Basically I like target=”_blank” which opens links in a new tab.

Now I’m sure there are better ways to do this but hey it works and nobody knows better! I work in AWS and like everything, hey it works and nobody knows better!

It’s just reality accept it.


Here’s the scripts since they are cool. My workflow is Atom or VS Code with no plugins cause I don’t know how and terminal and jupyter to test. It works really well. I’d like to learn Clojure to do more of this REPL development. Back to topic. Generating pages:

import argparse
import datetime
import sys

def gen_file(blog_type, file_name, page_text):
    file_title = '_posts/%s/%s' % (blog_type, file_name)
    with open(file_title, 'w') as f:
        f.write(page_text)

def gen_page_text(title, summary, blog_type, file_name, date):
    page_text = '''---
title: %s
summary: %s
cover-image: hipster.jpg
blog: %s
file-name: %s
date: %s 08:00:00
---
''' % (title, summary, blog_type, file_name, date)
    page_text += '''
Image
pic pic-small pic-large
<a href="" target="_blank"><img src="" class="pic"/></a><small></small>

;>pic;>Data Science and Machine Learning;>https://i.imgur.com/MctcYW5.png;>

Link
<a href="" target="_blank"></a>

,>EECS 126: Probability,>https://inst.eecs.berkeley.edu/~ee126/fa18/,>

'''
    return page_text

parser = argparse.ArgumentParser(description='Create a post')
parser.add_argument('blog type',
                    help='blog type can be: academics, cool, general, notes, or personal')
parser.add_argument('blog title',
                    help='title of blog post')

args = vars(parser.parse_args())
blog_type = args['blog type']
title = args['blog title']
if blog_type not in {"academics", "cool", "general", "notes", "personal"}:
    print("invalid blog type")
    sys.exit()

date = str(datetime.date.today())
file_name = "%s-%s.md" % (date, title.replace(" ", "-"))
page_text = gen_page_text(title, title, blog_type, file_name, date)
gen_file(blog_type, file_name, page_text)

And formatting pages:

import sys
import argparse

def replace_links(line):
    out = ""
    i = 0
    while i < len(line):
        start = line.find(",>", i)
        if start != i:
            out += line[i]
            i += 1
        else:
            mid = line.find(",>", start + 2)
            end = line.find(",>", mid + 2)
            phrase = line[start + 2:mid]
            link = line[mid + 2:end]
            replacement = "<a href=\"%s\" target=\"_blank\">%s</a>" % (link, phrase)
            out += replacement
            i += end - start + 2
    return out

def replace_imgs(line):
    out = ""
    i = 0
    while i < len(line):
        start = line.find(";>", i)
        if start != i:
            out += line[i]
            i += 1
        else:
            mid1 = line.find(";>", start + 2)
            mid2 = line.find(";>", mid1 + 2)
            end = line.find(";>", mid2 + 2)
            style = line[start + 2:mid1]
            phrase = line[mid1 + 2:mid2]
            link = line[mid2 + 2:end]
            replacement = "<a href=\"%s\" target=\"_blank\"><img src=\"%s\" class=\"%s\"/></a><small>%s</small>" % (link, link, style, phrase)
            out += replacement
            i += end - start + 2
    return out

parser = argparse.ArgumentParser(description='Format a post')
parser.add_argument('blog type',
                    help='blog type can be: academics, cool, general, notes, or personal')
parser.add_argument('file name',
                    help='name of page')

args = vars(parser.parse_args())
blog_type = args['blog type']
file_name = args['file name']
if blog_type not in {"academics", "cool", "general", "notes", "personal"}:
    print("invalid blog type")
    sys.exit()

path = "_posts/%s/%s" % (blog_type, file_name)
backup = "_posts/%s/0-backup-%s" % (blog_type, file_name)
out_text = ""
backup_text = ""

with open(path, 'r') as f:
    for line in f:
        out_text += replace_imgs(replace_links(line))
        backup_text += line

with open(backup, 'w') as g:
    g.write(backup_text)

with open(path, "w") as g:
    g.write(out_text)

I am the god of best practices and “it works.” Done and push.

Post by: Brian T. Liao