Skip to content

Commit

Permalink
fetch specific sha with cdn.rawgit.com; fixes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
bast committed Mar 16, 2017
1 parent 24ca8ae commit ff9e2bd
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions cicero/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,39 @@ def render_github_markdown(path):
from .title import extract_title
from .images import fix_images

(owner, repo, _ref) = path.split('/')[0:3]
file_path = '/'.join(path.split('/')[3:])

# we need to translate the reference to a sha (the reference can be a sha)
# the reason for this is that cdn.rawgit.com caches files forever
# the reference may change but the sha won't
sha = get_sha_github(owner, repo, _ref)

root = '{0}/{1}/{2}/'.format(owner, repo, sha)
if '/' in file_path:
root += '/'.join(file_path.split('/')[:-1]) + '/'
last_file = file_path.split('/')[-1]
else:
last_file = file_path

prefix = 'https://cdn.rawgit.com/{0}'.format(root)

try:
url = 'https://raw.githubusercontent.com/{}'.format(path)
url = prefix + '/' + last_file

response = _urlopen(url)

markdown = response.read().decode("utf-8")
if markdown == 'Not Found':
return flask.render_template('404.html')

# we do not use https://raw.githubusercontent.com because it does not handle svg files

# we define root as everything except the last file
root = '/'.join(path.split('/')[:-1])
prefix = 'https://cdn.rawgit.com/{}/'.format(root)

title = extract_title(markdown)
style = flask.request.args.get('style')
if style is None:
style = 'default'

try:
url = 'https://raw.githubusercontent.com/{}/{}'.format(root, 'remark.html')
url = prefix + '/' + 'remark.html'
response = _urlopen(url)
template = response.read().decode("utf-8")
return flask.render_template_string(template,
Expand Down

0 comments on commit ff9e2bd

Please sign in to comment.