Skip to content

Commit

Permalink
Added complete publishing functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxalorg committed Jun 21, 2016
1 parent f0bd75d commit 4ffc608
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,40 @@ Directly publish your blog posts to GitHub Pages from the command line.
- Write a blog post in markdown.
- Rename the post appropriately to how you have Jekyll configured.
- Default is `YYYY-MM-DD-slugged-title-string`
- Run the command `ghPublish <username> <path to blog post>`
- Now read Usage below.

## Usage - Simple

Now that you have the file ready.

1. You need to **preview** the file *locally*.
- `ghPublish --preview -f <path to file>`
- This will render your markdown file to html
- Then open it in your default web browser for previewing.
- You can also use this command to render any markdown files.
2. **Commit** the changes to github.
- `ghPublish -u <Username> -f <path to file>`
- This works for both adding new files, and updating existing ones.
- *PS: Username should **exactly** match to a Key in ~/.ghPublish json file.*

That's it. Your blog has been pushed live to `username.github.io`.

## Usage - Detailed

```
$ python3 ghPublish --help
usage: ghPublish.py [-h] (--preview | -u USER) -f FILE [-r REPO] [-l LOC]

Publish your posts on GitHub pages.

optional arguments:
-h, --help show this help message and exit
--preview preview a blog post locally. (default: False)
-u USER, --user USER hithub Username (default: None)
-f FILE, --file FILE path to local file (default: None)
-r REPO, --repo REPO optional repository (default: None)
-l LOC, --location LOC optional file path in repostiory (default: None)
```
---
Expand Down
129 changes: 129 additions & 0 deletions ghPublish/ghPublish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import argparse
import preview
import os
import requests
import json
import base64


def preview_file(post):
"""
Opens the rendered markdown file (as html) locally in a browser.
"""
with open(post) as f:
content = f.read()
title = os.path.basename(post)
preview.Preview(title, content).preview()


class Publish:
def __init__(self, user, fp, repo=None, path=None):
# Set required derived variables
self.title = os.path.basename(fp)
self.path = path + '/' + self.title if path else '_posts/' + self.title
self.owner = user
if repo:
self.repo = repo
else:
self.repo = owner + '.github.io'
self.fp = fp
self.api_url = 'https://api.github.com/repos/{owner}/{repo}/contents/{path}'.format(
owner=self.owner, repo=self.repo,
path=self.path)

# Set base64 encoded content of file
with open(os.path.abspath(self.fp)) as f:
self.content_base64 = base64.b64encode(f.read().encode('utf-8'))

# Get config file
with open(os.path.join(os.path.expanduser('~'), '.ghPublish')) as f:
self.config = json.load(f)

def get_auth_details(self):
return (self.owner, self.get_api_token())

def get_api_token(self):
return self.config[self.owner]

def get_sha_blob(self):
"""
if the current file exists
returns the sha blob
else
returns None
"""
r = requests.get(self.api_url, auth=self.get_auth_details())
try:
return r.json()['sha']
except KeyError:
return None

def publish_post(self):
"""
If it's a new file, add it.
Else, update it.
"""
payload = {'content': self.content_base64.decode('utf-8')}

sha_blob = self.get_sha_blob()
if sha_blob:
commit_msg = 'ghPublish UPDATE: {}'.format(self.title)
payload.update(sha=sha_blob)
payload.update(message=commit_msg)
else:
commit_msg = 'ghPublish ADD: {}'.format(self.title)
payload.update(message=commit_msg)

r = requests.put(self.api_url,
auth=self.get_auth_details(),
data=json.dumps(payload))
try:
url = r.json()['content']['html_url']
return r.status_code, url
except KeyError:
return r.status_code, None


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Publish your posts on GitHub pages.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--preview',
dest='prev',
action='store_true',
help='preview a blog post locally.')
group.add_argument('-u',
'--user',
dest='user',
default=None,
help='hithub Username')
parser.add_argument('-f',
'--file',
dest='file',
required=True,
help='path to local file')
parser.add_argument('-r',
'--repo',
dest='repo',
default=None,
help='optional repository')
parser.add_argument('-l',
'--location',
dest='loc',
default=None,
help='optional file path in repostiory')
args = parser.parse_args()

if args.prev:
preview_file(args.file)
elif args.user:
status, url = Publish(args.user, args.file, args.repo,
args.loc).publish_post()
if status in (200, 201):
print('Sucessfuly published at {}'.format(url))
else:
print(
'Error occurred! Contact the author at: mitesh@miteshshah.com')
else:
print("Run ghPublish --help for usage information")
3 changes: 3 additions & 0 deletions ghPublish/test.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# An exhibit of Markdown

#### WOO! Updating an existing file.
#### WOO! Updating an existing file for the 2nd time! YAY

This note demonstrates some of what [Markdown][1] is capable of doing.

*Note: Feel free to play with this page. Unlike regular notes, this doesn't automatically save itself.*
Expand Down

0 comments on commit 4ffc608

Please sign in to comment.