やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

source-highlightをインストールしてシンタックスハイライトする

 コードに色つけて表示するやつ。

手順

  1. インストール
  2. 設定
  3. 使ってみる

1. インストール

sudo apt install -y source-highlight 

1-1. バージョン

source-highlight --version
  • 3.1.8

1-2. ヘルプ

$ source-highlight --help
GNU GNU source-highlight 3.1.8

Highlight the syntax of a source file (e.g. Java) into a specific format (e.g.
HTML)

Usage: source-highlight [OPTIONS]... < input_file > output_file
       source-highlight [OPTIONS]... -i input_file -o output_file
       source-highlight [OPTIONS]... [FILES]...

  -h, --help                    Print help and exit
      --detailed-help           Print help, including all details and hidden
                                  options, and exit
  -V, --version                 Print version and exit
  -i, --input=filename          input file. default std input
  -o, --output=filename         output file. default std output (when the third
                                  invocation form is used). If STDOUT is
                                  specified, the output is directed to standard
                                  output

you can simply specify some files at the command line and also use regular
expressions (for instance *.java).  In this case the name for the output files
will be formed using the name of the source file with a .<ext> appended, where
<ext> is the extension chosen according to the output format specified (for
instance .html).

  -s, --src-lang=STRING         source language (use --lang-list to get the
                                  complete list).  If not specified, the source
                                  language will be guessed from the file
                                  extension.
      --lang-list               list all the supported language and associated
                                  language definition file
      --outlang-list            list all the supported output language and
                                  associated language definition file
  -f, --out-format=STRING       output format (use --outlang-list to get the
                                  complete list)  (default=`html')
  -d, --doc                     create an output file that can be used as a
                                  stand alone document (e.g., not to be
                                  included in another one)
      --no-doc                  cancel the --doc option even if it is implied
                                  (e.g., when css is given)
  -c, --css=filename            the external style sheet filename.  Implies
                                  --doc
  -T, --title=STRING            give a title to the output document.  Implies
                                  --doc
  -t, --tab=INT                 specify tab length.  (default=`8')
  -H, --header=filename         file to insert as header
  -F, --footer=filename         file to insert as footer
      --style-file=filename     specify the file containing format options
                                  (default=`default.style')
      --style-css-file=filename specify the file containing format options (in
                                  css syntax)
      --style-defaults=filename specify the file containing defaults for format
                                  options  (default=`style.defaults')
      --outlang-def=filename    output language definition file
      --outlang-map=filename    output language map file
                                  (default=`outlang.map')
      --data-dir=path           directory where language definition files and
                                  language maps are searched for.  If not
                                  specified these files are searched for in the
                                  current directory and in the data dir
                                  installation directory
      --output-dir=path         output directory
      --lang-def=filename       language definition file
      --lang-map=filename       language map file  (default=`lang.map')
      --show-lang-elements=filename
                                prints the language elements that are defined
                                  in the language definition file
      --infer-lang              force to infer source script language
                                  (overriding given language specification)

Lines:
  -n, --line-number[=padding]   number all output lines, using the specified
                                  padding character  (default=`0')
      --line-number-ref[=prefix]
                                number all output lines and generate an anchor,
                                  made of the specified prefix + the line
                                  number  (default=`line')

Filtering output:

 Mode: linerange
  specifying line ranges
      --line-range=STRING       generate only the lines in the specified
                                  range(s)
      --range-separator=STRING  the optional separator to be printed among
                                  ranges (e.g., "...")
      --range-context=INT       number of (context) lines generated even if not
                                  in range

 Mode: regexrange
  specifying regular expression delimited ranges
      --regex-range=STRING      generate only the lines within the specified
                                  regular expressions

reference generation:
      --gen-references=STRING   generate references  (possible
                                  values="inline", "postline", "postdoc"
                                  default=`inline')
      --ctags-file=filename     specify the file generated by ctags that will
                                  be used to generate references
                                  (default=`tags')
      --ctags=cmd               how to run the ctags command.  If this option
                                  is not specified, ctags will be executed with
                                  the default value.  If it is specified with
                                  an empty string, ctags will not be executed
                                  at all  (default=`ctags --excmd=n
                                  --tag-relative=yes')

testing:
  -v, --verbose                 verbose mode on
  -q, --quiet                   print no progress information
      --binary-output           write output files in binary mode
      --statistics              print some statistics (i.e., elapsed time)
      --gen-version             put source-highlight version in the generated
                                  file  (default=on)
      --check-lang=filename     only check the correctness of a language
                                  definition file
      --check-outlang=filename  only check the correctness of an output
                                  language definition file
      --failsafe                if no language definition is found for the
                                  input, it is simply copied to the output
  -g, --debug-langdef[=type]    debug a language definition.  In dump mode just
                                  dumps all the steps; in interactive, at each
                                  step, waits for some input (press ENTER to
                                  step)  (possible values="interactive",
                                  "dump" default=`dump')
      --show-regex=filename     show the regular expression automaton
                                  corresponding to a language definition file

Maintained by Lorenzo Bettini <http://www.lorenzobettini.it>
Report bugs to <bug-source-highlight at gnu.org>

2. 設定

src-hilite-lesspipe.sh

 lessに色をつけるスクリプトを探す。

$ find /usr -name 'src-hilite-lesspipe.sh'
/usr/share/source-highlight/src-hilite-lesspipe.sh

 以下でできた。

cd /tmp/work
echo -e 'echo '\''Hello world!!'\' > a.sh
/usr/share/source-highlight/src-hilite-lesspipe.sh a.sh | less -R

 が、catしたら使えない……。これじゃ困るときが来る。

cat a.sh > tmp.tmp | /usr/share/source-highlight/src-hilite-lesspipe.sh tmp.tmp | less -R

 src-hilite-lesspipe.shの中身をみてみる。-i "$source"によってファイルから読むようになっている。

#! /bin/bash

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" ;;
        *) source-highlight --failsafe --infer-lang -f esc --style-file=esc.style -i "$source" ;;
    esac
done

 標準入力から読む。

echo -e 'echo '\''Hello world!!'\' | source-highlight -s bash -f esc256 -t 4 | less -R

 ファイルから読む。

cd /tmp/work
echo -e 'echo '\''Hello world!!'\' > a.sh
source-highlight -f esc256 -t 4 -i a.sh | less -R

 ふつうにコマンド使ったほうが応用できる。

  -f, --out-format=STRING       output format (use --outlang-list to get the
                                  complete list)  (default=`html')

出力形式の一覧

 他の形式でハイライトとかできないの? と思って調べてみた。

source-highlight --help | grep -C 3 'html'
  -f, --out-format=STRING       output format (use --outlang-list to get the
                                  complete list)  (default=`html')
$ source-highlight --outlang-list
docbook = docbook.outlang
esc = esc.outlang
esc256 = esc256.outlang
groff_man = groff_man.outlang
groff_mm = groff_mm.outlang
groff_mm_color = groff_mm_color.outlang
html = html.outlang
html-css = htmlcss.outlang
html5 = html5.outlang
htmltable = htmltable.outlang
javadoc = javadoc.outlang
latex = latex.outlang
latexcolor = latexcolor.outlang
mediawiki = mediawiki.outlang
odf = odf.outlang
sexp = sexp.outlang
texinfo = texinfo.outlang
xhtml = xhtml.outlang
xhtml-css = xhtmlcss.outlang
xhtmltable = xhtmltable.outlang

 端末に出すならesc。256色使えるならesc256

 256色対応端末なら~/.bash_profileの末尾に以下を記述して端末を起動すると256色になる。

export TERM=xterm-256color

前回まで

対象環境

$ uname -a
Linux raspberrypi 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux