如何使代码语法荧光笔兼容?

How to make code syntax highlighters compatible?

有几个用于代码语法高亮的标签,例如precode。如何让它们在同一个站点兼容?

# Use PreCode for Windows Live Writer
<pre class="brush: py; toolbar: false;">
...
</pre>

# Install the plugin SyntaxHighlighter Evolved (for WordPress), the source codes will render as:
<div id="highlighter_539499" class="syntaxhighlighter  python">
...
</div>

# in markdown, insert code blocks by:
```python
...
```
# will render as:
<pre><code class="python">
...
</code></pre>

# OR
[code lang=python]
...
[/code]

PS:我安装插件SyntaxHighlighter Evolved,使用PreCode in Windows Live Writer 离线博客和 WP markdown 编辑器 用于在线博客。

令人沮丧的是,没有这方面的标准。 HTML5 规范提供了 suggestion,但随后明确指出它不是正式规范:

There is no formal way to indicate the language of computer code being marked up. Authors who wish to mark code elements with the language used, e.g. so that syntax highlighting scripts can use the right rules, can use the class attribute, e.g. by adding a class prefixed with "language-" to the element.

还提供了一个例子:

The following example shows how a block of code could be marked up using the pre and code elements.

<pre><code class="language-pascal">var i: Integer;
begin
   i := 1;
end.</code></pre>

A class is used in that example to indicate the language used.

因为这只是一个建议,没有人必须遵循它。我的建议是将任何不支持 HTML5 建议格式的语法突出显示项目提交错误报告。如果他们不想破坏与现有用户的向后兼容性(可以理解),他们总是可以添加第二种格式(一些项目已经这样做了)。如果每个人都这样做,那么最终每个人都会使用相同的格式,而目前的差距将不复存在。如果您需要一些论据来说明为什么 HTML5 规范是一种好的格式,我相当冗长的 analysis(几年前的)可能会有所帮助。

关于“language-”前缀的最后一点说明。虽然一些荧光笔确实接受它,但我还没有看到任何需要它的东西。而且,事实上,大多数 Markdown 实现(支持 [non-standard] fenced-code-blocks)不插入前缀。如果你真的想要这个前缀,那么你可以自己将它添加到你的 Markdown 中:

```language-python
...
```

我也遇到过一些荧光笔,它们也接受替代前缀:“lang-”。我的观点是,建议的前缀部分似乎是最弱的 link,我不希望在那里看到任何一致性。当然,正式的规范会解决这个问题,但在那之前,我们只能使用现有的东西。