"git show"와 함께 "source-highlight"를 사용하는 방법은 무엇입니까?

"git show"와 함께 "source-highlight"를 사용하는 방법은 무엇입니까?

사용less와 함께 사용되는 소스 강조 표시훌륭하게 작동하지만 git 출력에 사용된 언어를 git show알아낼 수 있는 파일 확장자가 없기 때문에 사용 방법을 찾는 데 어려움을 겪고 있습니다 ...source-highlight

less somefile.rb # result is syntax colourised
git show master:somefile.rb | less # no colouring

call을 사용하는 git show master:somefile.rb | less것은 실제로 호출하는 것과 같습니다 less somefile(즉, not .rb). 확장자가 없기 때문에 소스 강조 표시는 구문을 추측할 수 없습니다.

소스 강조 표시가 추측을 하도록 하는 확장되지 않은 방법이 있습니까, 아니면 어떻게든 --lang-def옵션을 LESSOPEN변수에 전달할 수 있습니까?

편집 1 아 그런가보네요소스 강조 표시는 다른 방법을 사용할 수 있습니다언어는 추론되지만 소스 파일에는 이러한 언어가 없습니다.

답변1

파이프 파일과 작동하도록 gnu source-highlight의 src-hilite-lesspipe.sh를 수정했습니다.https://gist.github.com/jaygooby/9494858d3d481a64819d227a9318f6c7

이는 또한 일반적인 방식으로 호출하는 것을 의미합니다.

less code.py

확장자가 없는 파일에 대해 소스 코드 강조 표시를 수행할 수도 있습니다.언어적 추론 기능소스 강조 표시는 다음을 사용합니다.

less /tmp/mycode

그리고 (이 작업을 수행하는 원래 동기) 파이프라인 파일은 다음과 같습니다.

cat /tmp/file.rb | less
git show master:obfusicated.perl # implicit pipe to less via git's pager

코드가 source-highlight 구문에 정의 파일이 없다는 것을 알고 있는 경우 유사한 언어를 설정하여 추측을 무시할 수 있습니다. c는 일반적으로 좋은 대안입니다.

SRCLANG=c git show master:app/views/layouts/application.html.erb

다음은 그 요지의 원본입니다.https://gist.github.com/jaygooby/9494858d3d481a64819d227a9318f6c7

#! /bin/bash
#
# Based on http://git.savannah.gnu.org/cgit/src-highlite.git/tree/src/src-hilite-lesspipe.sh.in
# by Lorenzo Bettini
#
# Modified by Jay Caines-Gooby to support piped files
# [email protected]
# @jaygooby
#
# Typically called by setting:
#
# export LESSOPEN="|-/path/to/src-hilite-lesspipe.sh %s"
# export LESS=-R
#
# If we're less-ing a file, %s will be replaced by the name of the file. If
# there's no file and we're reading from a pipe, then %s is set to -
#
# This script differs from the original src-hilite-lesspipe.sh
# in that it can handle pipes and files with no extensions and will
# attempt to guess their language using the file command.
#
# So as well as invoking on regular files:
#
# less some.rb
# less some.py
#
# It will should be able to work on:
#
# less no-extension-but-contains-perl
#
# and even with more complex examples (my original motivation
# https://unix.stackexchange.com/questions/469982/how-can-i-use-source-highlight-with-git-show)
#
# git show master:some.rb
#
# It uses bashisms to do this, so is no longer a pure POSIX sh script.
set -eu

# Users can override the guessed language by setting SRCLANG:
# SRCLANG=c git show master:app/views/layouts/application.html.erb
SRCLANG=${SRCLANG:-}

guess_language() {
  lang=$(echo -e ${1:-} | file - | cut -d" " -f2)
  echo $(tr [A-Z] [a-z] <<< "$lang")
}

# check if the language passed as $1 is known to source-highlight
# In an earlier version of this script I set a fallback (c.lang)
# but this causes issues with paging man pages etc
check_language_is_known() {
  fallback=""
  lang=$(source-highlight --lang-list | cut -d' ' -f1 | grep "${1:-}" || true)
  lang=${lang:-$fallback}
  echo $lang
}

for source in "$@"; do
  case $source in
    *ChangeLog|*changelog)
      source-highlight --failsafe -f esc --lang-def=changelog.lang --style-file=esc.style -i "$source" ;;
    *Makefile|*makefile)
      source-highlight --failsafe -f esc --lang-def=makefile.lang --style-file=esc.style -i "$source" ;;
    *.tar|*.tgz|*.gz|*.bz2|*.xz)
      lesspipe "$source" ;;
    *)

      # naive check for a file extension; let source-highlight infer language
      # but only when source isn't - (ie. from a piped file)
      if [[ "$source" != "-" && $(basename "$source") =~ \. ]]; then
        source-highlight --failsafe --infer-lang -f esc --style-file=esc.style -i "$source"
      else
        # We're being piped to, or the filename doesn't have an extension
        # so guess the language.

        # When we're being piped to, we cat stdin, but when it's a file
        # without an extension, we cat the file instead.

        # unset IFS so line breaks are preserved and capture the file's contents
        # (will only work for files up to bash's available memory). There should
        # be a better way to replicate this with tee or process substitution...
        IFS= file=$([ "source" = "-" ] && cat || cat "$source")
        lang=$(guess_language $file)
        lang=$(check_language_is_known $lang)

        # Don't call if source-highlight doesn't know the language
        # BUT also let users override the guessed lang if the environment
        # variable SRCLANG is set. This can help where you know e.g. your
        # source code is c-like, but source-highlight has no specific syntax
        # definition for your code
        [ -n "$SRCLANG" ] && lang="$SRCLANG"

        if [ -n "$lang" ]; then
          echo $file | source-highlight --failsafe -f esc --src-lang=$lang --style-file=esc.style
        else
          echo $file
        fi
      fi

      ;;
  esac
done

답변2

source-highlight확장 기능을 사용하지 않고 언어를 추측해보세요. 바라보다6.1 입력 언어를 찾는 방법.

안타깝게도 이 함수를 사용하려면 입력이 일반 파일이어야 합니다. 표준 입력을 식별하기 위해 이를 사용하려고 하면 다음 오류가 발생합니다.

source-highlight: missing feature: language inference requires input file

git따라서 출력 형식을 지정할 때 호출되는 방식 인 파이프라인 중간에서는 사용할 수 없습니다 .

git호출기를 구성하기 위한 몇 가지 옵션이 있습니다. 설명서를 참조하세요.코어 호출기그리고휴대용 소형 무선 호출기. <명령어>. 그러나 추가 정보(예: 파일 이름)를 페이지네이터에 전달하는 메커니즘은 없습니다. git-show제출물을 표시할 때 형식 지정 옵션만 있습니다. 지정하는 "페이저 명령"은 다음 단계를 수행하는 추가 래퍼 스크립트여야 합니다.

  1. 전체 입력을 읽고 임시 파일에 저장합니다.
  2. source-highlight ...이 파일을 호출
  3. 출력을 실제 호출기로 라우팅

파일을 식별하는 데 적합한 "sha-bang"이 없는 파일의 경우에는 여전히 실패합니다.

관련 정보