Publish插件:SwiftPygments
Publish插件:SwiftPygments
使用Splash和其官方Publish插件official plugin可以非常方便地在给Swift添加语法高亮。但我们需要给其他语言添加语法高亮时,我们可以用SwiftPygmentsPublishPlugin。
SwiftPygmentsPublishPlugin
一个用来给 Publish 添加语法高亮的插件。
Pygments是一个用Python编写的语法高亮工具,支持大量语言。
需求
- Python
- Pygments: https://pygments.org
- Swift 5
- 在.css文件中添加Pygments色彩配置
SwiftPygments使用PythonKit与Pygments交互。
安装
- 安装Python。
- 安装Pygments。
1$ pip3 install pygments
- 在Swift Package中添加SwiftPygmentsPublishPlugin。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
let package = Package(
    ...
    dependencies: [
    .package(url: "https://github.com/Ze0nC/SwiftPygmentsPublishPlugin", .branch("master"))
    ],
    targets: [
        .target(
            ...
            dependencies: [
                ...
                "SwiftPygmentsPublishPlugin"
            ]
        )
    ]
    ...
)
用法
- 在build pipeline中添加.pygments()。
SwiftPygmentsPublishPlugin必须在addMarkdownFiles()之前添加。
1
2
3
4
5
6
7
8
import SwiftPygmentsPublishPlugin
...
try MyWebsite().publish(using: [
    .installPlugin(.pygments()),
    ...
    .addMarkdownFiles(),
    ...
])
- 在css文件中增加一个Pygments的色彩配置。你可以在网上找到很多不同风格的配置。
- 在markdown文件中添加代码。
你需要指定代码的语言才能正确添加语法高亮
Swift
1
2
3
4
```swift
let str = "This is Swift code."
print(str)
```
输出:
1
2
let str = "This is Swift code."
print(str)
Python
1
2
3
4
```python
str = "This is also Swift code."
print(str)
```
输出:
1
2
str = "This is also Swift code."
print(str)
默认
如果没有指定代码语言,则默认使用swift语法高亮。
1
2
let str = "This is also Swift code."
print(str)
连LaTeX都可以
1
2
3
4
5
6
```latex
\documentclass[12pt]{article}
\begin{document}
This is \LaTeX
\end{document}
```
输出:
1
2
3
4
\documentclass[12pt]{article}
\begin{document}
This is \LaTeX
\end{document}
备注
这个插件只会高亮代码块,而不会高亮段落中的代码。
享受带有高亮的Publish网站吧。
致谢
感谢John Sundell (@johnsundell)开发了Publish以及SplashPublishPlugin插件