fhl は、コードの配色を highlight.css に書き出します(--single-html を付けたときは <style> の中に書きます)。ライブラリは CSS を書き出しません。fhl が書き出す CSS か、自分で用意した CSS を使ってください。
fhl --theme dark:github docs/ # GitHub のダークの配色だけ
fhl --theme light docs/ # 既定のテーマのライトの配色だけ
fhl --theme both:tokyo-night docs/ # Tokyo Night のライトとダークの配色
fhl --list-themes
fhl --print-css --theme both:github > highlight.css # CSS だけを書き出す(自分のビルドで使う)
どのテーマにも、ライトとダークの配色があります。--theme MODE:THEME で、どちらを書き出すかとテーマを選びます。
MODE |
書き出すもの |
|---|---|
light |
ライトの配色だけ |
dark |
ダークの配色だけ |
both |
ライトの配色。閲覧者のデバイスがダークを優先する設定(prefers-color-scheme)のときは、ダークの配色に切り替わる |
THEME を省略すると default になります。--theme を指定しなければ both:default です。both は、既定では閲覧者のデバイスの設定に従います。
サイトに独自のライトとダークの切り替えがある場合は、--theme-selector MODE:SELECTOR を使います。MODE(light か dark)の配色を、指定した CSS セレクターの下に追加します。最初はデバイスの設定に従い、セレクターが当てはまるときはそちらが優先されます。ライトとダークの両方にセレクターを付けるには、1 つのコマンドの中で、このオプションをセレクターごとに 1 回ずつ繰り返します(両方の CSS が 1 回の実行で書き出されます)。
| 配色を決めるもの | オプション |
|---|---|
| デバイスの設定だけ | なし |
デバイスの設定、次にサイトの切り替え(<html data-theme="light"> か "dark") |
--theme-selector dark:':root[data-theme="dark"]' --theme-selector light:':root[data-theme="light"]' |
サイトの切り替えだけ(<html data-theme="dark"> か、指定なし) |
--theme-selector dark:':root[data-theme="dark"]' --theme-selector light:':root:not([data-theme="dark"])' |
ヒント: デバイスの設定を完全に上書きするには、最後の行のように、ライトの配色に :not(…) を使ったセレクターを付けて、ダークのセレクターが当てはまらないときには必ず当てはまるようにします。こうすると、CSS に @media (prefers-color-scheme: dark) のブロックが残っていても、デバイスの設定が表に出ることはありません。
--no-frame を付けると、pre.highlight の背景色と文字色を書き出さないので、コードブロックに自分のスタイルを当てられます。
fhl --print-css --theme both:github \
--theme-selector dark:':root[data-theme="dark"]' \
--theme-selector light:':root[data-theme="light"]' --no-frame > highlight.css
1 つのセレクターで、複数の要素に当てはめることもできます。CSS のカンマと同じく、dark:':root[data-theme="dark"], .dark-mode' と書けば、その両方の下にダークの配色が入ります。
既定のテーマ default は fhl 独自の配色で、キーワードを太字にします。どの色も、背景とのコントラスト比が 6 以上あります(WCAG の AA を上回ります)。ほかのテーマは元のテーマの色をそのまま使っているので、コントラストがそれより低い色もあります。
THEME |
名前 | ライト | ダーク |
|---|---|---|---|
default |
default | fhl's own colors | fhl's own colors |
github |
GitHub | GitHub Light Default (primer/github-vscode-theme) | GitHub Dark Default (primer/github-vscode-theme) |
vscode |
VS Code | VS Code Light+ (microsoft/vscode) | VS Code Dark+ (microsoft/vscode) |
solarized |
Solarized | Solarized Light (as shipped with VS Code) | Solarized Dark (as shipped with VS Code) |
one |
One | One Light (atom/one-light-syntax) | One Dark (atom/one-dark-syntax) |
catppuccin |
Catppuccin | Catppuccin Latte (catppuccin/vscode) | Catppuccin Mocha (catppuccin/vscode) |
tokyo-night |
Tokyo Night | Tokyo Night Light (enkia/tokyo-night-vscode-theme) | Tokyo Night (enkia/tokyo-night-vscode-theme) |
同じコードを、テーマごとに表示しています。
default// Greet says hello to name.
func Greet(name string) string {
if name == "" {
return "hello, world\n"
}
return fmt.Sprintf("hello, %s (%d)", name, 42)
}
github)// Greet says hello to name.
func Greet(name string) string {
if name == "" {
return "hello, world\n"
}
return fmt.Sprintf("hello, %s (%d)", name, 42)
}
vscode)// Greet says hello to name.
func Greet(name string) string {
if name == "" {
return "hello, world\n"
}
return fmt.Sprintf("hello, %s (%d)", name, 42)
}
solarized)// Greet says hello to name.
func Greet(name string) string {
if name == "" {
return "hello, world\n"
}
return fmt.Sprintf("hello, %s (%d)", name, 42)
}
one)// Greet says hello to name.
func Greet(name string) string {
if name == "" {
return "hello, world\n"
}
return fmt.Sprintf("hello, %s (%d)", name, 42)
}
catppuccin)// Greet says hello to name.
func Greet(name string) string {
if name == "" {
return "hello, world\n"
}
return fmt.Sprintf("hello, %s (%d)", name, 42)
}
tokyo-night)// Greet says hello to name.
func Greet(name string) string {
if name == "" {
return "hello, world\n"
}
return fmt.Sprintf("hello, %s (%d)", name, 42)
}
:root {
--highlight-keyword: #a3195b; /* keyword (if, func, etc.) */
--highlight-string: #17663a; /* string */
/* … */
}
/* nf: function name (Name.Function) */
pre.highlight .nf { color: var(--highlight-function); }
:root に置いた CSS 変数で決まります。どのクラスも、そのうちの 1 つから色を取ります。pre.highlight で始まるので、ページのほかの場所で highlight クラスを使っていても影響しません。<pre class="highlight" data-lang="…"><code class="language-…"> で、各トークンはクラスの付いた <span> です。行や空白は要素で囲みません。説明は fhl が書き出す highlight.css のコメントそのままです。
| 変数 | 色を付けるもの |
|---|---|
--highlight-background |
code block background |
--highlight-text |
plain text (names, whitespace, and anything not covered by another role) |
--highlight-comment |
comment |
--highlight-keyword |
keyword (if, func, etc.) |
--highlight-type |
type and class name |
--highlight-string |
string |
--highlight-escape |
escape and interpolation inside a string (\n, ${x}, etc.) |
--highlight-number |
number |
--highlight-constant |
constant (true, nil, constant names, etc.) |
--highlight-function |
function name |
--highlight-builtin |
builtin name (print, len, etc.) |
--highlight-variable |
variable |
--highlight-attribute |
attribute and property |
--highlight-tag |
tag (HTML element names, YAML keys, etc.) |
--highlight-operator |
operator |
--highlight-punctuation |
punctuation (brackets, commas, etc.) |
--highlight-preproc |
preprocessor (#include, etc.) |
--highlight-decorator |
decorator and annotation (those starting with @) |
--highlight-inserted |
added diff line |
--highlight-deleted |
removed diff line |
--highlight-heading |
heading (diff file name lines, etc.) |
--highlight-muted |
muted part (shell prompts and output) |
--highlight-error |
part that could not be tokenized |
クラス名は Pygments、Chroma、Rouge と同じ短い名前なので、それらのために書いた CSS もほとんどそのまま使えます。意味の列は fhl が書き出す highlight.css のコメントそのままです。
| クラス | 意味 | トークンの種類 |
|---|---|---|
err |
part that could not be tokenized | Error |
x |
other part | Other |
k |
keyword | Keyword |
kc |
constant keyword (true, nil, etc.) | Keyword.Constant |
kd |
declaration keyword (var, func, etc.) | Keyword.Declaration |
kn |
namespace keyword (import, package, etc.) | Keyword.Namespace |
kp |
keyword-like word | Keyword.Pseudo |
kr |
reserved word | Keyword.Reserved |
kt |
type keyword (int, string, etc.) | Keyword.Type |
n |
name | Name |
na |
attribute name | Name.Attribute |
nb |
builtin name | Name.Builtin |
bp |
special builtin name (self, this, etc.) | Name.Builtin.Pseudo |
nc |
class name | Name.Class |
no |
constant name | Name.Constant |
nd |
decorator and annotation | Name.Decorator |
ni |
character reference (&, etc.) | Name.Entity |
ne |
exception name | Name.Exception |
nf |
function name | Name.Function |
fm |
special function name (init, etc.) | Name.Function.Magic |
py |
property name | Name.Property |
nl |
label | Name.Label |
nn |
namespace and package name | Name.Namespace |
nx |
other name | Name.Other |
nt |
tag (HTML element names, YAML keys, etc.) | Name.Tag |
nv |
variable | Name.Variable |
vc |
class variable | Name.Variable.Class |
vg |
global variable | Name.Variable.Global |
vi |
instance variable | Name.Variable.Instance |
vm |
special variable | Name.Variable.Magic |
l |
literal (a value written out directly) | Literal |
ld |
date | Literal.Date |
s |
string | Literal.String |
sa |
string prefix (the r in r"…", etc.) | Literal.String.Affix |
sb |
backtick string | Literal.String.Backtick |
sc |
character | Literal.String.Char |
dl |
string delimiter | Literal.String.Delimiter |
sd |
documentation string (docstring) | Literal.String.Doc |
s2 |
double-quoted string | Literal.String.Double |
se |
escape (\n, etc.) | Literal.String.Escape |
sh |
heredoc | Literal.String.Heredoc |
si |
interpolation inside a string (${x}, etc.) | Literal.String.Interpol |
sx |
other string | Literal.String.Other |
sr |
regular expression | Literal.String.Regex |
s1 |
single-quoted string | Literal.String.Single |
ss |
symbol (:name, etc.) | Literal.String.Symbol |
m |
number | Literal.Number |
mb |
binary number | Literal.Number.Bin |
mf |
floating-point number | Literal.Number.Float |
mh |
hexadecimal number | Literal.Number.Hex |
mi |
integer | Literal.Number.Integer |
il |
long integer | Literal.Number.Integer.Long |
mo |
octal number | Literal.Number.Oct |
o |
operator | Operator |
ow |
word operator (and, in, etc.) | Operator.Word |
or |
reserved operator | Operator.Reserved |
p |
punctuation | Punctuation |
c |
comment | Comment |
ch |
#! on the first line | Comment.Hashbang |
cm |
multi-line comment | Comment.Multiline |
cp |
preprocessor (#include, etc.) | Comment.Preproc |
cpf |
preprocessor file name | Comment.PreprocFile |
c1 |
single-line comment | Comment.Single |
cs |
special comment | Comment.Special |
g |
other | Generic |
gd |
removed diff line | Generic.Deleted |
ge |
emphasis | Generic.Emph |
gr |
error output | Generic.Error |
gh |
heading | Generic.Heading |
gi |
added diff line | Generic.Inserted |
go |
command output | Generic.Output |
gp |
shell prompt | Generic.Prompt |
gs |
strong emphasis | Generic.Strong |
gu |
subheading | Generic.Subheading |
gt |
error traceback | Generic.Traceback |
gl |
underline | Generic.Underline |
変数を変えてください。クラスのルールに手を加える必要はありません。
:root {
--highlight-background: #fffdf5;
--highlight-keyword: #b00040;
}
自分のファイルを highlight.css のあとに読み込むか、highlight.css の変数を直接書き換えます。同じ変数を使うほかのクラスと違う色を、あるクラスだけに付けたいときは、pre.highlight .nb { color: … } のように、そのクラスのルールを追加します。
このサイトの上のサンプルも、同じ考え方で作っています。各テーマの変数を :root ではなくクラス(.theme-github など)の下に置いているので、サンプルごとに別の配色になります。