Add geshi support to blame page
Add geshi support to blame page

This uses a really nasty hack to take apart the HTML table that geshi
returns, in order to inject a table cell with the blame data.
Unfortunatly because geshi only returns a complete block of HTML,
there's no good way to do this.

--- a/css/gitphp.css
+++ b/css/gitphp.css
@@ -40,9 +40,9 @@
 a.text:hover { text-decoration:underline; color:#880000; }
 table { padding:8px 4px; }
 th { padding:2px 5px; font-size:12px; text-align:left; }
-tr.light:hover { background-color:#edece6; }
-tr.dark { background-color:#f6f6f0; }
-tr.dark:hover { background-color:#edece6; }
+.light:hover { background-color:#edece6; }
+.dark { background-color:#f6f6f0; }
+.dark:hover { background-color:#edece6; }
 td { padding:2px 5px; font-size:12px; vertical-align:top; }
 td.link { padding:2px 5px; font-family:sans-serif; font-size:10px; }
 td.filesize { text-align: right; }
@@ -139,3 +139,8 @@
 table.code tr.light:hover { background-color:#ffffff; }
 table.code tr.dark:hover { background-color:#f6f6f0; }
 
+td#blame {
+	white-space: nowrap;
+	text-align: left;
+}
+

--- a/include/controller/Controller_Blame.class.php
+++ b/include/controller/Controller_Blame.class.php
@@ -121,6 +121,40 @@
 
 		$this->tpl->assign('tree', $commit->GetTree());
 
+		if (GitPHP_Config::GetInstance()->GetValue('geshi', true)) {
+			include_once(GitPHP_Config::GetInstance()->GetValue('geshiroot', 'lib/geshi/') . "geshi.php");
+			if (class_exists('GeSHi')) {
+				$geshi = new GeSHi("",'php');
+				if ($geshi) {
+					$lang = $geshi->get_language_name_from_extension(substr(strrchr($blob->GetPath(),'.'),1));
+					if (!empty($lang)) {
+						$geshi->enable_classes();
+						$geshi->enable_strict_mode(GESHI_MAYBE);
+						$geshi->set_source($blob->GetData());
+						$geshi->set_language($lang);
+						$geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
+						$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
+						$output = $geshi->parse_code();
+
+						$bodystart = strpos($output, '<td');
+						$bodyend = strrpos($output, '</tr>');
+
+						if (($bodystart !== false) && ($bodyend !== false)) {
+							$geshihead = substr($output, 0, $bodystart);
+							$geshifoot = substr($output, $bodyend);
+							$geshibody = substr($output, $bodystart, $bodyend);
+
+							$this->tpl->assign('geshihead', $geshihead);
+							$this->tpl->assign('geshibody', $geshibody);
+							$this->tpl->assign('geshifoot', $geshifoot);
+							$this->tpl->assign('extracss', $geshi->get_stylesheet());
+							$this->tpl->assign('geshi', true);
+						}
+					}
+				}
+			}
+		}
+
 		$blame = $blob->GetBlame();
 		$this->tpl->assign('blame', $blob->GetBlame());
 	}

--- a/templates/blame.tpl
+++ b/templates/blame.tpl
@@ -25,6 +25,25 @@
  {include file='path.tpl' pathobject=$blob target='blob'}
  
  <div class="page_body">
+   {if $geshi}
+     {$geshihead}
+       <td class="ln" id="blame">
+	{foreach from=$blob->GetData(true) item=blobline name=blob}
+	  {assign var=blamecommit value=$blame[$smarty.foreach.blob.iteration]}
+	  {if $blamecommit}
+	    {if $opened}</div>{/if}
+	    <div class="{cycle values="light,dark"}">
+	    {assign var=opened value=true}
+	    <a href="{$SCRIPT_NAME}?p={$project->GetProject()|urlencode}&a=commit&h={$blamecommit->GetHash()}" title="{$blamecommit->GetTitle()}">{$blamecommit->GetAuthorEpoch()|date_format:"%F %X"}</a>
+	    {$blamecommit->GetAuthor()}
+	  {/if}
+	  &nbsp;<br />
+	{/foreach}
+	{if $opened}</div>{/if}
+	</td>
+     {$geshibody}
+     {$geshifoot}
+   {else}
  	<table class="code">
 	{foreach from=$blob->GetData(true) item=blobline name=blob}
 	  {assign var=blamecommit value=$blame[$smarty.foreach.blob.iteration]}
@@ -47,6 +66,7 @@
 	  </tr>
 	{/foreach}
 	</table>
+  {/if}
  </div>
 
  {include file='footer.tpl'}

comments