gpt4 book ai didi

syntax-highlighting - 如何扩展 pygments.lexers.shell 的关键字列表

转载 作者:行者123 更新时间:2023-12-04 03:04:19 26 4
gpt4 key购买 nike

我正在使用 JekyllPygments转换 Markdown到静态html页面。内容准备GitHub pages .为了显示代码示例(本示例中的 shell 命令),我将以下部分添加到文件中:

{% highlight sh %}
$ ls -1a
.
..
README
{% endhighlight %}

参数sh 指的是配置词法分析器的shell。您还可以选择其他词法分析器,例如用于突出显示文本的 console

我注意到 shell 词法分析器没有突出显示一些基本命令,例如 ls。这也可以在 source code of the lexer 中看到.以下摘录显示了 shell 词法分析器的关键字定义(在 BashLexer 类中找到)。

...
'basic': [
(r'\b(if|fi|else|while|do|done|for|then|return|function|case|'
r'select|continue|until|esac|elif)\s*\b',
Keyword),
(r'\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|'
r'complete|declare|dirs|disown|echo|enable|eval|exec|exit|'
r'export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|'
r'local|logout|popd|printf|pushd|pwd|read|readonly|set|shift|'
r'shopt|source|suspend|test|time|times|trap|true|type|typeset|'
r'ulimit|umask|unalias|unset|wait)\s*\b(?!\.)',
Name.Builtin),
(r'#.*\n', Comment),
(r'\\[\w\W]', String.Escape),
(r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
(r'[\[\]{}()=]', Operator),
(r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
(r'&&|\|\|', Operator),
],
...

有没有办法扩展关键字列表,或者您可以推荐另一个词法分析器?

最佳答案

您可以将关键字添加到 Pygments 词法分析器,方法是编写您自己的词法分析器来添加新关键字,通过子类化另一个词法分析器。见 pygments doc了解更多信息。我制作了一个子类化的词法分析器,它向 C++ 词法分析器添加了几个关键字,可以在 this Github repo 中看到。 .

我不确定如何添加 EXTRA_KEYWORDS 以外的其他类型的关键字。

关于syntax-highlighting - 如何扩展 pygments.lexers.shell 的关键字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12989257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com