+sub get_norm_rel_path { # http://www.perlmonks.org/bare/?node_id=11907
+ my $unnormpath = shift;
+ while ($unnormpath =~ m!/\.!) {
+ $unnormpath =~ s!/[^\/]+/\.\.!!;
+ # print "Path is now -+$unnormpath+-\n";
+ }
+ return $unnormpath;
+}
+sub get_markdown {
+ my $tfilename = shift;
+ my $thash = shift;
+ my $rethtmlstr = "";
+ use open ":encoding(utf8)"; # needed to have utf8 survive through the shell pipe
+ my $cmd_markdownify = $GIT . " " . git_cmd() . " cat-file blob " . $thash . " | perl -e 'my \$str = do { local \$/; <STDIN> }; \$str =~ s/<!--.*?--\s*>//gs; print \$str;' | markdown |";
+ open (FOO, $cmd_markdownify) or die_error(500, "Open git-cat-file blob '$thash' failed");
+ while (<FOO>) {
+ if ($_ =~ /(<img[^>]src=")(.*?)"/) {
+ my $origcut = "".$2;
+ my $testcut = "".$2;
+ my $is_anchor = ($testcut =~ /^#/);
+ my $is_absolute = ($testcut =~ /^http/);
+ my $is_relative_up = ($testcut =~ /^\.\./);
+ my $is_local_link = ((!$is_anchor) and (!$is_absolute));
+ my $tdir = dirname($tfilename);
+ my $is_tdir_proper = (($tdir ne "") and ($tdir ne "."));
+ #print "XX: $origcut ($is_anchor, $is_absolute - $is_local_link) ($is_relative_up, $is_tdir_proper, $tdir, $tfilename)\n"; # dbg
+ if ($is_local_link) {
+ if ($is_relative_up) { # normalize
+ if ($is_tdir_proper) {
+ # cheat with absolute path here:
+ my $resolved = get_norm_rel_path( File::Spec->rel2abs ("$origcut", "/$tdir" ) );
+ $resolved = substr $resolved, 1;
+ #print "YY: $resolved\n";
+ $_ =~ s!(<img[^>]src=")(.*?)"!$1?p=$project;a=blob_plain;f=$resolved"!gi;
+ }
+ } else {
+ $_ =~ s!(<img[^>]src=")(.*?)"!$1?p=$project;a=blob_plain;f=$2"!gi;
+ #print "ZZ: $_\n";
+ }
+ }
+ }
+ if ($_ =~ /(<a[^>]href=")(.*?)"/) {
+ my $origcut = "".$2;
+ my $testcut = "".$2;
+ my $is_anchor = ($testcut =~ /^#/);
+ my $is_absolute = ($testcut =~ /^http/);
+ my $is_relative_up = ($testcut =~ /^\.\./);
+ my $is_local_link = ((!$is_anchor) and (!$is_absolute));
+ my $tdir = dirname($tfilename);
+ my $is_tdir_proper = (($tdir ne "") and ($tdir ne "."));
+ #print "XX: $origcut ($is_anchor, $is_absolute - $is_local_link) ($is_relative_up, $is_tdir_proper, $tdir, $tfilename)\n"; # dbg
+ if ($is_local_link) {
+ if ($is_relative_up) { # normalize
+ if ($is_tdir_proper) {
+ # cheat with absolute path here:
+ my $resolved = get_norm_rel_path( File::Spec->rel2abs ("$origcut", "/$tdir" ) );
+ $resolved = substr $resolved, 1;
+ #print "YY: $resolved\n";
+ $_ =~ s!(<a[^>]href=")(.*?)"!$1?p=$project;a=blob;f=$resolved"!gi;
+ }
+ } else {
+ $_ =~ s!(<a[^>]href=")(.*?)"!$1?p=$project;a=blob;f=$2"!gi;
+ #print "ZZ: $_\n";
+ }
+ }
+ }
+ $rethtmlstr .= $_;
+ }
+ close(FOO);
+ return $rethtmlstr;
+}
+