Skip to content

Commit

Permalink
Add highlight option (prettify, deferred-prettify)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertkrimen committed Jan 18, 2012
1 parent c1db146 commit 1f52a01
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
23 changes: 19 additions & 4 deletions gist_it/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ def parse_style( style_option ):
else:
return '1'

def parse_highlight( option ):
if option is None or option is True:
return 'prettify'
elif option is False:
return '0'

option = str( option ).lower().strip()
if option == '1' or option == '' or option == 'true':
return 'prettify'
elif option == '0' or option == 'false' or option == 'none' or option == 'no':
return '0'
elif option == 'deferred-prettify':
return option
else:
return 'prettify'

def parse_slice( slice_option ):
if slice_option is None:
return ( 0, 0 )
Expand Down Expand Up @@ -80,8 +96,7 @@ def keylist( self ):
'raw_path', 'raw_url',
'user_repository', 'user_repository_branch_path', 'user_repository_url',
'start_line', 'end_line',
'footer',
'style',
'footer', 'style', 'highlight',
]

@classmethod
Expand All @@ -92,7 +107,7 @@ def match( self, location ):
return match

@classmethod
def parse( self, location, slice_option = None, footer_option = None, style_option = None ):
def parse( self, location, slice_option = None, footer_option = None, style_option = None, highlight_option = None ):
match = self.match( location )
if not match:
return None
Expand Down Expand Up @@ -129,8 +144,8 @@ def parse( self, location, slice_option = None, footer_option = None, style_opti
parse[ 'end_line' ] = slice_option[1]

parse[ 'footer' ] = parse_footer( footer_option )

parse[ 'style' ] = parse_style( style_option )
parse[ 'highlight' ] = parse_highlight( highlight_option )

return Gist( **parse )

Expand Down
3 changes: 2 additions & 1 deletion gist_it/appengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ def dispatch_gist_it( dispatch, location ):
slice_option = dispatch.request.get( 'slice' )
footer_option = dispatch.request.get( 'footer' )
style_option = dispatch.request.get( 'style' )
highlight_option = dispatch.request.get( 'highlight' )

gist = gist_it.Gist.parse( location, slice_option = slice_option, footer_option = footer_option, style_option = style_option )
gist = gist_it.Gist.parse( location, slice_option = slice_option, footer_option = footer_option, style_option = style_option, highlight_option = highlight_option )
if not gist:
dispatch.response.set_status( 500 )
dispatch.response.out.write( "Unable to parse \"%s\": Not a valid repository path?" % ( location ) )
Expand Down
4 changes: 4 additions & 0 deletions jinja2-assets/gist.jinja.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="gist-it-gist">
<div class="gist-file">
<div class="gist-data">
{% if gist.highlight == 'prettify' or gist.highlight == 'deferred-prettify' %}
<pre class="prettyprint">{{ cgi.escape( document ) }}</pre>
{% else %}
<pre>{{ cgi.escape( document ) }}</pre>
{% endif %}
</div>
{% if gist.footer != '0' %}
<div class="gist-meta">
Expand Down
6 changes: 6 additions & 0 deletions jinja2-assets/gist.jinja.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{% if gist.highlight == 'prettify' %}
if ( 'prettyPrint' in window ) {} else {
document.write( '<script type="text/javascript" src="{{ base }}/assets/prettify/prettify.js"></script>' );
}
{% endif %}
{% if gist.style != '0' %}
document.write( '<link rel="stylesheet" href="{{ base }}/assets/embed.css"/>' );
{% endif %}
{% if gist.highlight == 'prettify' %}
document.write( '<link rel="stylesheet" href="{{ base }}/assets/prettify/prettify.css"/>' );
{% endif %}
document.write( '{{ gist_html.encode( 'string_escape' ) }}' );
{% if gist.highlight == 'prettify' %}
document.write( '<script type="text/javascript">prettyPrint();</script>' );
{% endif %}
22 changes: 22 additions & 0 deletions t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,26 @@ $test->get( "$base/github/robertkrimen/gist-it-example/blob/master/example.js?st
$test->status_code_is( 200 );
like( $test->response->decoded_content, qr{\Qdocument.write( '<link rel="stylesheet" href="$base/assets/embed.css"/>' )\E} );

$test->get( "$base/github/robertkrimen/gist-it-example/blob/master/example.js?highlight=0" );
$body = $test->response->decoded_content;
$test->status_code_is( 200 );
unlike( $body, qr/\Qif ( 'prettyPrint' in window ) {} else {\E/ );
unlike( $body, qr{\Qdocument.write( '<script type="text/javascript" src="$base/assets/prettify/prettify.js"></script>' )\E} );
like( $body, qr{\Qdocument.write( '<link rel="stylesheet" href="$base/assets/embed.css"/>' )\E} );
unlike( $body, qr{\Qdocument.write( '<link rel="stylesheet" href="$base/assets/prettify/prettify.css"/>' )\E} );
unlike( $body, qr{\Qdocument.write( '<script type="text/javascript">prettyPrint();</script>' )\E} );

$test->get( "$base/github/robertkrimen/gist-it-example/blob/master/example.js?highlight=1" );
$body = $test->response->decoded_content;
$test->status_code_is( 200 );
like( $body, qr{\Qdocument.write( '<link rel="stylesheet" href="$base/assets/prettify/prettify.css"/>' )\E} );
like( $body, qr{\Qdocument.write( '<script type="text/javascript">prettyPrint();</script>' )\E} );

$test->get( "$base/github/robertkrimen/gist-it-example/blob/master/example.js?highlight=deferred-prettify" );
$body = $test->response->decoded_content;
$test->status_code_is( 200 );
unlike( $body, qr{\Qdocument.write( '<link rel="stylesheet" href="$base/assets/prettify/prettify.css"/>' )\E} );
unlike( $body, qr{\Qdocument.write( '<script type="text/javascript">prettyPrint();</script>' )\E} );
unlike( $body, qr{\Qdocument.write( '<link rel="stylesheet" href="$base/assets/prettify/prettify.css"/>' )\E} );
like( $body, qr!\Q<pre class="prettyprint">function Xyzzy() {\n return "Nothing happens";\n}\n</pre>\E! );
done_testing;
25 changes: 25 additions & 0 deletions test/test_highlight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# vim: ft=python:
import random
import unittest2
import sys;
import os;

sys.path.append( os.path.join( sys.path[0], ".." ) )
import gist_it

class t( unittest2.TestCase ):
def runTest( self ):
self.assertEqual( gist_it.parse_highlight( 0 ), '0' )
self.assertEqual( gist_it.parse_highlight( '0' ), '0' )
self.assertEqual( gist_it.parse_highlight( False ), '0' )
self.assertEqual( gist_it.parse_highlight( True ), 'prettify' )
self.assertEqual( gist_it.parse_highlight( None ), 'prettify' )
self.assertEqual( gist_it.parse_highlight( ' 1' ), 'prettify' )
self.assertEqual( gist_it.parse_highlight( 'yes ' ), 'prettify' )
self.assertEqual( gist_it.parse_highlight( ' no' ), '0' )
self.assertEqual( gist_it.parse_highlight( 'none ' ), '0' )
self.assertEqual( gist_it.parse_highlight( 'deferred-prettify' ), 'deferred-prettify' )

if __name__ == '__main__':
unittest2.main()

0 comments on commit 1f52a01

Please sign in to comment.