From ee373ba8c691bf8705efcb77a9978c41d70bb24b Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Sun, 18 Nov 2007 17:38:31 -0800 Subject: pretty much done --- camping/gitweb.rb | 178 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 151 insertions(+), 27 deletions(-) diff --git a/camping/gitweb.rb b/camping/gitweb.rb index 0e0f2c4..91959be 100644 --- a/camping/gitweb.rb +++ b/camping/gitweb.rb @@ -2,11 +2,6 @@ require 'rubygems' require 'camping' require 'git' -begin - require 'syntax/convertors/html' -rescue LoadError -end - # this is meant to be a git-less web head to your git repo # # install dependencies @@ -45,7 +40,7 @@ module GitWeb::Controllers def get @headers['Content-Type'] = 'text/css' ending = File.read(__FILE__).gsub(/.*__END__/m, '') - ending.gsub(/__END__.*/m, '') + ending.gsub(/__JS__.*/m, '') end end @@ -134,6 +129,15 @@ module GitWeb::Controllers File.new(file).read end end + + class Download < R '/download/(\d+)/(.*?)/(\w+)' + def get repo_id, file, sha + @repo = Repository.find repo_id + @git = Git.bare(@repo.path) + @headers["Content-Disposition"] = "attachment; filename=#{file}" + @git.gblob(sha).contents + end + end class Diff < R '/diff/(\d+)/(\w+)/(\w+)' def get repo_id, tree1, tree2 @@ -160,9 +164,9 @@ module GitWeb::Views def layout html do head do - title 'gitweb' - #link :href=>R(Stylesheet), :rel=>'stylesheet', :type=>'text/css' - #script :type => "text/javascript", :language => "JavaScript", :src => R(JsHighlight) + title 'test' + link :href=>R(Stylesheet), :rel=>'stylesheet', :type=>'text/css' + script '', :type => "text/javascript", :language => "JavaScript", :src => R(JsHighlight) end style <<-END, :type => 'text/css' body { color: #333; } @@ -173,9 +177,8 @@ module GitWeb::Views .odd { background: #eee; } .tag { margin: 5px; padding: 1px 3px; border: 1px solid #8a8; background: #afa;} .indent { padding: 0px 15px;} - .tip { border-top: 1px solid #aaa; color: #666; padding: 10px; } END - body do + body :onload => "sh_highlightDocument();" do self << yield end end @@ -290,17 +293,19 @@ module GitWeb::Views end def blob - link :rel => "stylesheet", :type => "text/css", - :href => "http://drnicwilliams.com/external/CodeHighlighter/styles.css" - script :src => "http://drnicwilliams.com/external/CodeHighlighter/clean_tumblr_pre.js" - ext = File.extname(@file).gsub('.', '') - ext = 'ruby' if ext == 'rb' + + case ext + when 'rb' : classnm = 'sh_ruby' + end a.options 'repo', :href => R(View, @repo) h3 'blob : ' + @blob.sha h4 @file - pre { code @blob.contents, :class => ext } + + a 'download file', :href => R(Download, @repo, @file, @blob.sha) + + div.indent { pre @blob.contents, :class => classnm } end def diff @@ -314,18 +319,11 @@ module GitWeb::Views span.space ' : ' a @tree2, :href => R(Tree, @repo, @tree2) end - + @diff.each do |file| h3 file.path - begin - convertor = Syntax::Convertors::HTML.for_syntax "diff" - self << convertor.convert( file.patch ) - rescue - div.indent { pre file.patch } - div.tip 'tip: if you run "gem install syntax", this will be highlighted' - end + div.indent { pre file.patch, :class => 'sh_diff' } end - end @@ -367,4 +365,130 @@ end def GitWeb.create GitWeb::Models.create_schema -end \ No newline at end of file +end + +__END__ +pre.sh_sourceCode { + background-color: white; + color: black; + font-style: normal; + font-weight: normal; +} + +pre.sh_sourceCode .sh_keyword { color: blue; font-weight: bold; } /* language keywords */ +pre.sh_sourceCode .sh_type { color: darkgreen; } /* basic types */ +pre.sh_sourceCode .sh_string { color: red; font-family: monospace; } /* strings and chars */ +pre.sh_sourceCode .sh_regexp { color: orange; font-family: monospace; } /* regular expressions */ +pre.sh_sourceCode .sh_specialchar { color: pink; font-family: monospace; } /* e.g., \n, \t, \\ */ +pre.sh_sourceCode .sh_comment { color: brown; font-style: italic; } /* comments */ +pre.sh_sourceCode .sh_number { color: purple; } /* literal numbers */ +pre.sh_sourceCode .sh_preproc { color: darkblue; font-weight: bold; } /* e.g., #include, import */ +pre.sh_sourceCode .sh_symbol { color: darkred; } /* e.g., <, >, + */ +pre.sh_sourceCode .sh_function { color: black; font-weight: bold; } /* function calls and declarations */ +pre.sh_sourceCode .sh_cbracket { color: red; } /* block brackets (e.g., {, }) */ +pre.sh_sourceCode .sh_todo { font-weight: bold; background-color: cyan; } /* TODO and FIXME */ + +/* for Perl, PHP, Prolog, Python, shell, Tcl */ +pre.sh_sourceCode .sh_variable { color: darkgreen; } + +/* line numbers (not yet implemented) */ +pre.sh_sourceCode .sh_linenum { color: black; font-family: monospace; } + +/* Internet related */ +pre.sh_sourceCode .sh_url { color: blue; text-decoration: underline; font-family: monospace; } + +/* for ChangeLog and Log files */ +pre.sh_sourceCode .sh_date { color: blue; font-weight: bold; } +pre.sh_sourceCode .sh_time, pre.sh_sourceCode .sh_file { color: darkblue; font-weight: bold; } +pre.sh_sourceCode .sh_ip, pre.sh_sourceCode .sh_name { color: darkgreen; } + +/* for LaTeX */ +pre.sh_sourceCode .sh_italics { color: darkgreen; font-style: italic; } +pre.sh_sourceCode .sh_bold { color: darkgreen; font-weight: bold; } +pre.sh_sourceCode .sh_underline { color: darkgreen; text-decoration: underline; } +pre.sh_sourceCode .sh_fixed { color: green; font-family: monospace; } +pre.sh_sourceCode .sh_argument { color: darkgreen; } +pre.sh_sourceCode .sh_optionalargument { color: purple; } +pre.sh_sourceCode .sh_math { color: orange; } +pre.sh_sourceCode .sh_bibtex { color: blue; } + +/* for diffs */ +pre.sh_sourceCode .sh_oldfile { color: orange; } +pre.sh_sourceCode .sh_newfile { color: darkgreen; } +pre.sh_sourceCode .sh_difflines { color: blue; } + +/* for css */ +pre.sh_sourceCode .sh_selector { color: purple; } +pre.sh_sourceCode .sh_property { color: blue; } +pre.sh_sourceCode .sh_value { color: darkgreen; font-style: italic; } + +__JS__ + +/* Copyright (C) 2007 gnombat@users.sourceforge.net */ +/* License: http://shjs.sourceforge.net/doc/license.html */ + +function sh_highlightString(inputString,language,builder){var patternStack={_stack:[],getLength:function(){return this._stack.length;},getTop:function(){var stack=this._stack;var length=stack.length;if(length===0){return undefined;} +return stack[length-1];},push:function(state){this._stack.push(state);},pop:function(){if(this._stack.length===0){throw"pop on empty stack";} +this._stack.pop();}};var pos=0;var currentStyle=undefined;var output=function(s,style){var length=s.length;if(length===0){return;} +if(!style){var pattern=patternStack.getTop();if(pattern!==undefined&&!('state'in pattern)){style=pattern.style;}} +if(currentStyle!==style){if(currentStyle){builder.endElement();} +if(style){builder.startElement(style);}} +builder.text(s);pos+=length;currentStyle=style;};var endOfLinePattern=/\r\n|\r|\n/g;endOfLinePattern.lastIndex=0;var inputStringLength=inputString.length;while(posposWithinLine){output(line.substring(posWithinLine,bestMatch.index),null);} +pattern=state[bestMatchIndex];var newStyle=pattern.style;var matchedString;if(newStyle instanceof Array){for(var subexpression=0;subexpression0){patternStack.pop();}}}}} +if(currentStyle){builder.endElement();} +currentStyle=undefined;if(endOfLineMatch){builder.text(endOfLineMatch[0]);} +pos=startOfNextLine;}} +function sh_getClasses(element){var result=[];var htmlClass=element.className;if(htmlClass&&htmlClass.length>0){var htmlClasses=htmlClass.split(" ");for(var i=0;i0){result.push(htmlClasses[i]);}}} +return result;} +function sh_addClass(element,name){var htmlClasses=sh_getClasses(element);for(var i=0;i0&&url.charAt(0)==='<'&&url.charAt(url.length-1)==='>'){url=url.substr(1,url.length-2);} +if(sh_isEmailAddress(url)){url='mailto:'+url;} +a.setAttribute('href',url);a.appendChild(this._document.createTextNode(this._currentText));this._currentParent.appendChild(a);} +else{this._currentParent.appendChild(this._document.createTextNode(this._currentText));} +this._currentText=null;} +this._currentParent=this._currentParent.parentNode;},text:function(s){if(this._currentText===null){this._currentText=s;} +else{this._currentText+=s;}},close:function(){if(this._currentText!==null){this._currentParent.appendChild(this._document.createTextNode(this._currentText));this._currentText=null;} +this._element.appendChild(this._documentFragment);}};function sh_highlightElement(htmlDocument,element,language){sh_addClass(element,"sh_sourceCode");var inputString;if(element.childNodes.length===0){return;} +else{inputString=sh_getText(element);} +sh_builder.init(htmlDocument,element);sh_highlightString(inputString,language,sh_builder);sh_builder.close();} +function sh_highlightHTMLDocument(htmlDocument){if(!window.sh_languages){return;} +var nodeList=htmlDocument.getElementsByTagName("pre");for(var i=0;i element with class='"+htmlClass+"', but no such language exists";}}}}} +function sh_highlightDocument(){sh_highlightHTMLDocument(document);} + +if(!this.sh_languages){this.sh_languages={};} +sh_languages['css']=[[{'next':1,'regex':/\/\/\//g,'style':'sh_comment'},{'next':7,'regex':/\/\//g,'style':'sh_comment'},{'next':8,'regex':/\/\*\*/g,'style':'sh_comment'},{'next':14,'regex':/\/\*/g,'style':'sh_comment'},{'regex':/(?:\.|#)[A-Za-z0-9_]+/g,'style':'sh_selector'},{'next':15,'regex':/\{/g,'state':1,'style':'sh_cbracket'},{'regex':/~|!|%|\^|\*|\(|\)|-|\+|=|\[|\]|\\|:|;|,|\.|\/|\?|&|<|>|\|/g,'style':'sh_symbol'}],[{'exit':true,'regex':/$/g},{'regex':/(?:?)/g,'style':'sh_url'},{'regex':/(?:?)/g,'style':'sh_url'},{'next':2,'regex'://g,'style':'sh_keyword'},{'next':5,'regex':/<(?:\/)?[A-Za-z][A-Za-z0-9]*/g,'state':1,'style':'sh_keyword'},{'regex':/&(?:[A-Za-z0-9]+);/g,'style':'sh_preproc'},{'regex':/@[A-Za-z]+/g,'style':'sh_type'},{'regex':/(?:TODO|FIXME)(?:[:]?)/g,'style':'sh_todo'}],[{'exit':true,'regex':/>/g,'style':'sh_preproc'},{'next':3,'regex':/"/g,'style':'sh_string'}],[{'regex':/\\(?:\\|")/g},{'exit':true,'regex':/"/g,'style':'sh_string'}],[{'exit':true,'regex':/-->/g,'style':'sh_comment'},{'next':4,'regex'://g,'style':'sh_comment'},{'next':11,'regex'://g,'style':'sh_comment'},{'next':19,'regex'://g,'style':'sh_comment'},{'next':26,'regex'://g,'style':'sh_comment'},{'next':3,'regex'://g,'style':'sh_comment'},{'next':4,'regex'://g,'style':'sh_comment'},{'next':11,'regex':/