Permalink
Browse files

Added the ability to specify line numbers to embed part of the file

  • Loading branch information...
1 parent 6841f28 commit a5c6ff92c11d3b10c8f39d92e19f4275ed1dd00e @sudar committed Aug 20, 2011
Showing with 39 additions and 6 deletions.
  1. +13 −1 app.py
  2. +26 −5 gist_it.py
View
14 app.py
@@ -32,6 +32,18 @@
import gist_it
+'''
+Filter lines from the content, based on the start and end line indexes
+'''
+def filter_lines(content, start_line = 0, end_line = 0):
+ if (start_line == 0 and end_line == 0):
+ return content
+
+ if (end_line == 0):
+ return '\n'.join(content.splitlines()[start_line:])
+
+ return '\n'.join(content.splitlines()[start_line:end_line])
+
def render_gist_html( base, gist, document ):
result = jinja2.get_template( 'gist.jinja.html' ).render( cgi = cgi, base = base, gist = gist, document = document )
return result
@@ -98,7 +110,7 @@ def get( self, location ):
self.response.out.write( "Unable to fetch \"%s\": (%i)" % ( gist.raw_url, response.status_code ) )
return
else:
- gist_html = str( render_gist_html( base, gist, response.content ) ).strip()
+ gist_html = str( render_gist_html( base, gist, filter_lines(response.content, gist.start_line, gist.end_line) ) ).strip()
callback = self.request.get( 'callback' );
if callback != '':
result = render_gist_js_callback( callback, gist, gist_html )
View
@@ -30,20 +30,41 @@ def parse( self, location ):
parse = {}
- # user / respository / format / branch / path
+ # user / respository / format / branch / path : startline : endline
user = parse[ 'user' ] = splitpath[ 0 ]
repository = parse[ 'repository' ] = splitpath[ 1 ]
branch = parse[ 'branch' ] = splitpath[ 3 ]
- path = parse[ 'path' ] = '/'.join( splitpath[ 4: ] )
+
+ if (':' in splitpath[-1]) :
+ # line numbers were specified
+ split_filename = splitpath[-1].split(':')
+ filename = split_filename[0]
+
+ if (len(split_filename) > 2):
+ #end line is also specified
+ parse['start_line'] = int(split_filename[1])
+ parse['end_line'] = int(split_filename[2])
+ else:
+ # end line was not specified
+ parse['start_line'] = int(split_filename[1])
+ parse['end_line'] = 0
+
+ else:
+ # line numbers were not specified
+ filename = splitpath[-1]
+ parse['start_line'] = 0
+ parse['end_line'] = 0
+
+ path = parse[ 'path' ] = '/'.join( splitpath[ 4: -1] ) + filename
# format = blob
splitpath[ 2 ] = 'blob'
- blob_path = parse[ 'blob_path' ] = '/'.join( splitpath )
+ blob_path = parse[ 'blob_path' ] = '/'.join( splitpath[:-1] )
blob_url = parse[ 'blob_url' ] = urlparse.urljoin( 'https://github.com', blob_path )
# format = raw
splitpath[ 2 ] = 'raw'
- raw_path = parse[ 'raw_path' ] = '/'.join( splitpath )
+ raw_path = parse[ 'raw_path' ] = '/'.join( splitpath[:-1] )
raw_url = parse[ 'raw_url' ] = urlparse.urljoin( 'https://github.com', raw_path )
user_repository = parse[ 'user_repository' ] = '/'.join([ user, repository ])
@@ -56,6 +77,6 @@ def __init__( self, **arguments ):
for key in [ 'user', 'repository', 'branch', 'path',
'blob_path', 'blob_url',
'raw_path', 'raw_url',
- 'user_repository', 'user_repository_branch_path', 'user_repository_url' ]:
+ 'user_repository', 'user_repository_branch_path', 'user_repository_url', 'start_line', 'end_line' ]:
setattr( self, key, arguments[ key ] )

0 comments on commit a5c6ff9

Please sign in to comment.