import re
import requests
from pygments import highlight
from pygments.lexers import guess_lexer, guess_lexer_for_filename
from pygments.formatters import HtmlFormatter
import boto3

s3 = boto3.resource('s3')

def validate_link(link):
  pattern = re.compile("^https:\/\/github\.com\/([a-zA-Z0-9.\-_]*)\/([a-zA-Z0-9.\-_]*)\/blob\/([a-zA-Z0-9.\-_]*)([a-zA-Z0-9.\/\-_]*)\/([a-zA-Z0-9.\-_]*)#L(\d*)-L(\d*)$")
  match = pattern.match(link)
  if match:
    return (match.group(1), match.group(2), match.group(3), match.group(5), int(match.group(6)), int(match.group(7)))
  else:
    raise Exception("Link provided was not a valid github link.")

def handler(event, context):
  github_link = event['github_link']

  owner, repo, branch, filename, start_line, end_line = validate_link(github_link)
  request = requests.get("https://api.github.com/repos/%s/%s/contents/%s?ref=%s" % (owner, repo, filename, branch)).json()
  file = request['content'].decode(request['encoding'])
  chunk = '\n'.join(file.splitlines()[start_line-1:end_line])

  lexer = guess_lexer_for_filename(filename, chunk)
  formatter = HtmlFormatter(linenos=True)
  html = highlight(chunk, lexer, formatter)

  s3.Object('gitshame', "chunks/%s.html" % request['sha']).put(Body=html)

@@ -499,16 +499,32 @@
 roup(3),
+ match.group(4),
  match.g
@@ -574,16 +574,16 @@
 up(7)))%0A
-
   else:%0A
@@ -738,16 +738,22 @@
  branch,
+ path,
  filenam
@@ -874,16 +874,18 @@
 contents
+%25s
 /%25s?ref=
@@ -903,16 +903,22 @@
 r, repo,
+ path,
  filenam
@@ -936,16 +936,114 @@
 .json()%0A
+  print %22https://api.github.com/repos/%25s/%25s/contents/%25s?ref=%25s%22 %25 (owner, repo, filename, branch)%0A
   file =

fix path regex