PlayDocs 是一个命令行工具,用于将 Swift Playgrounds 转换为 Markdown 和 HTML。 Playgrounds 从一开始就支持渲染 markdown,所以为什么不利用这一点来生成文档呢?这些文档既可以作为可执行代码存在于您的存储库中,也可以作为文档或帖子存在于您的 wiki 或博客上。
您可以将 PlayDocs 用作命令行工具或作为您自己代码中的框架。
以下是通过 Playground 生成 Markdown 文件的基本命令
playdocs convert ./MyPlayground.playground --open
如果要输出为 HTML
playdocs convert ./MyPlayground.playground --html --open
指定目标路径
playdocs convert ./MyPlayground.playground --destination /path/to/file/MyFile.md --open
PlayDocs 还可以帮助您轻松创建新的 Playground 文件,以便您快速开始编写文档
playdocs new HelloPlayDocs --open
如果您希望 Playground 以 MacOS 为目标平台
playdocs new HelloPlayDocs --macos --open
如果您不希望 Playground 中包含任何样板代码
playdocs new HelloPlayDocs --macos --empty --open
要在 Swift 中进行转换,您可以使用 PlayDocsKit
// Convert swift source from a swift source string to a markdown string
public func convertToMarkdown(from source: SwiftSource) -> MarkdownSource
// Convert swift source from a file to a markdown file
public func convertToMarkdown(from source: URL, to destination: URL) throws
// Convert swift source from a string to an html string
public func convertToHTML(from source: SwiftSource) throws -> HTMLSource
// Convert swift source from a file to a html file
public func convertToHTML(from source: URL, to destination: URL) throws
// Custom conversion allowing caller to convert each Chunk as seen fit
public func convert(from source: SwiftSource, prepending header: String = "", appending footer: String = "", conversion convert: (Chunk) -> String) -> String
所有 Swift 代码的语法高亮都通过 John Sundell 的 Splash 实现。
注意:目前,语法高亮的主题是硬编码的,但将来这个项目应该使主题可配置。
安装命令行工具
$ mint install ldstreet/PlayDocs
在终端中运行
git clone https://github.com/ldstreet/PlayDocs.git && cd PlayDocs && make install
添加到您的 Marathonfile
marathon add https://github.com/ldstreet/PlayDocs.git
或者使用内联依赖语法
import PlayDocsKit // https://github.com/ldstreet/PlayDocs.git
要使用 PlayDocsKit,请将以下内容添加到您的 Package.swift
文件中。
// 🏓 A framework for converting Playgrounds and Swift files to markdown and html
.package(url: "https://github.com/ldstreet/PlayDocs.git", from: "0.1.0"),
.target(name: "MyPackage", dependencies: ["PlayDocsKit"]),