Gitweb: how to display markdown file in html format automatically like github

前端 未结 4 1944
既然无缘
既然无缘 2021-02-06 01:01

Markdown is important for documentation, it is very nice to see README.md can be automatically show in html format in github like https://github.com/twitter/bootstr

4条回答
  •  别那么骄傲
    2021-02-06 01:29

    Here are my modifications to gitweb.cgi based on the accepted answer, so that:

    • README.md in the project root is displayed (as in accepted answer)
    • Any *.md file is displayed if it is chosen as file from the tree view
    • local links among .md files in a project are preserved (i.e. (my parent)[../parent.md] would refer correctly); should work for images as well

    Note that I'm using sudo apt-get install libtext-markdown-perl on Ubuntu to provide the required markdown executable.

    Here are the modifications as a diff patch:

    --- /usr/share/gitweb/gitweb.cgi.orig   2016-04-13 10:28:03.268872899 +0200
    +++ /usr/share/gitweb/gitweb.cgi    2016-04-13 10:39:02.344875516 +0200
    @@ -16,8 +16,9 @@
     use Encode;
     use Fcntl ':mode';
     use File::Find qw();
    -use File::Basename qw(basename);
    +use File::Basename qw(basename dirname);
     use Time::HiRes qw(gettimeofday tv_interval);
    +use File::Spec; # hack
     binmode STDOUT, ':utf8';
    
     our $t0 = [ gettimeofday() ];
    @@ -6585,6 +6586,20 @@
            print "\n
    \n"; # class="readme" } + # hack + if (!$prevent_xss) { + $file_name = "README.md"; + my $proj_head_hash = git_get_head_hash($project); + my $readme_blob_hash = git_get_hash_by_path($proj_head_hash, "README.md", "blob"); + + if ($readme_blob_hash) { # if README.md exists + print "
    $file_name
    \n"; + print "
    "; # TODO find/create a better CSS class than page_body + print get_markdown($file_name, $readme_blob_hash); + print "
    "; + } + } + # we need to request one more than 16 (0..15) to check if # those 16 are all my @commitlist = $head ? parse_commits($head, 17) : (); @@ -7059,6 +7074,9 @@ $fd = run_highlighter($fd, $highlight, $syntax) if $syntax; + # hack + my $ismarkdown = ($file_name =~ /md$/); + git_header_html(undef, $expires); my $formats_nav = ''; if (defined $hash_base && (my %co = parse_commit($hash_base))) { @@ -7102,6 +7120,10 @@ href(action=>"blob_plain", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name) . qq!" />\n!; + } elsif ($ismarkdown) { + print qq!
    \n!; + print get_markdown($file_name, $hash); + print qq!
    \n!; # $cmd_markdownify } else { my $nr; while (my $line = <$fd>) { @@ -7119,6 +7141,79 @@ git_footer_html(); } +# hack +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 \$/; }; \$str =~ s/
    热议问题