threads / discuss / 5515

gitweb: Add committags support

Subject: gitweb: Add committags support

## tl;dr

2 messages between Sep 8, 2006 and Sep 8, 2006.

replies: 1people: 1as markdown or json

Jakub Narebski· Sep 8, 2006, 10:46 UTC · lore

Below is very preliminary code (think early alpha) to add committags support (a la gitweb-xmms2) to gitweb.

One of the problems is having committags support in the subject/title line, which is hyperlink itself (with title and class attribute).

-- >8 --
our %committags = (
	'commitsha' => {
		'pattern' => qr/[0-9a-fA-F]{40}/,
		'sub' => sub {
			my $hash_text = shift;
			if (git_get_type($hash_text) eq "commit") {
				return
					$cgi->a({-href => href(action=>"commit", hash=>$hash_text),
					        -class => "text"}, $hash_text);
			}
			return undef;
		},
		'islink' => 1,
	},
	'mantis' => {
		'pattern' => qr/(BUG|FEATURE)\(\d+\)/,
		'options' => [ 'http://bugs.xmms2.xmms.se/view.php?id=' ],
		'sub' => sub {
			my $match = shift;
			my $URL = shift;
			my ($issue) = $match =~ /(\d+)/;
			return
				$cgi->a({-href => "$URL$issue"},
				        $match);
		},
		'islink' => 1,
	},
);
sub format_log_line_html_generic {
	my $line = shift;
	my $a_attr = shift;
	my @committags = @_;
	my %subst;
	$line = esc_html($line);
	$line =~ s/ / /g;
	
	for my $ctname (@committags) {
		next unless exists $committags{$ctname};
		my $wrap = ref($a_attr) eq "HASH" && %$a_attr &&
			$committags{$ctname}{'islink'};
		my @opts =
			exists $committags{$ctname}{'options'} ?
			@{$committags{$ctname}{'options'}} :
			();
		while ($line =~ m/($committags{$ctname}{'pattern'})/gc) {
			my $match = $1;
			my $repl = $committags{$ctname}{'sub'}->($match, @opts);
			next unless $repl;
			if ($wrap) {
				$repl = $cgi->end_a() . $repl . $cgi->start_a($a_attr);
			}
			$subst{quotemeta $match} = $repl;
		}
	}
	while (my ($from, $to) = each %subst) {
		$line =~ s/$from/$to/g;
	}
	return $line;
}
foreach my $line (@log) {
	print format_log_line_html_generic($line, undef, keys %committags) . "<br/>\n";
#	print format_log_line_html_generic($line, {-href=>"href", -title => "TITEL"}, keys %committags) . "<br/>\n";
}
-- 
Jakub Narebski
Poland
Jakub Narebski· Sep 8, 2006, 20:57 UTC · re: Jakub Narebski · lore

Re: gitweb: Add committags support

Jakub Narebski wrote:
Show 28 quoted lines
> our %committags = (
>         'commitsha' => {
>                 'pattern' => qr/[0-9a-fA-F]{40}/,
>                 'sub' => sub {
>                         my $hash_text = shift;
>                         if (git_get_type($hash_text) eq "commit") {
>                                 return
>                                         $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
>                                                 -class => "text"}, $hash_text);
>                         }
>                         return undef;
>                 },
>                 'islink' => 1,
>         },
>         'mantis' => {
>                 'pattern' => qr/(BUG|FEATURE)\(\d+\)/,
>                 'options' => [ 'http://bugs.xmms2.xmms.se/view.php?id=' ],
>                 'sub' => sub {
>                         my $match = shift;
>                         my $URL = shift;
>                         my ($issue) = $match =~ /(\d+)/;
>                         return
>                                 $cgi->a({-href => "$URL$issue"},
>                                         $match);
>                 },
>                 'islink' => 1,
>         },
> );
Yet another committag:
        'URL' => { # simple URL, no query strings
                   # because of the whole esc_html() + ' '->'&nbsp;' thing
                'pattern' => qr!(http|ftp)s?://[a-zA-Z0-9%./]+!,
                'sub' => sub {
                        my $url = shift;
                        return
                                $cgi->a({-href => $url},
                                        $url); # should be perhaps shortened
                },
                'islink' => 1,
        },
-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git

← back to recent threads