从 Confluence XHTML 迁移到 Asciidoctor

您可以使用此 Groovy 脚本将 Atlassian Confluence XHTML 页面转换为 Asciidoctor。

该脚本调用 Pandoc 将从 Confluence 导出的单个或多个 HTML 文件转换为 AsciiDoc 文件。在运行此脚本之前,您需要安装 Pandoc。如果您在运行此脚本时遇到问题,可以使用脚本中引用的 Pandoc 命令手动将 XHTML 文件转换为 AsciiDoc。

示例 1. convert.groovy
// This script is provided by melix.
// The source can be found at https://gist.github.com/melix/6020336

@Grab('net.sourceforge.htmlcleaner:htmlcleaner:2.4')
import org.htmlcleaner.*

def src = new File('html').toPath()
def dst = new File('asciidoc').toPath()

def cleaner = new HtmlCleaner()
def props = cleaner.properties
props.translateSpecialEntities = false
def serializer = new SimpleHtmlSerializer(props)

src.toFile().eachFileRecurse { f ->
    def relative = src.relativize(f.toPath())
    def target = dst.resolve(relative)
    if (f.isDirectory()) {
        target.toFile().mkdir()
    } else if (f.name.endsWith('.html')) {
        def tmpHtml = File.createTempFile('clean', 'html')
        println "Converting $relative"
        def result = cleaner.clean(f)
        result.traverse({ tagNode, htmlNode ->
                tagNode?.attributes?.remove 'class'
                if ('td' == tagNode?.name || 'th'==tagNode?.name) {
                    tagNode.name='td'
                    String txt = tagNode.text
                    tagNode.removeAllChildren()
                    tagNode.insertChild(0, new ContentNode(txt))
                }

            true
        } as TagNodeVisitor)
        serializer.writeToFile(
                result, tmpHtml.absolutePath, "utf-8"
        )
        "pandoc -f html-native_divs -t asciidoctor $tmpHtml --wrap=none -o ${target}.adoc".execute().waitFor()
        tmpHtml.delete()
    }/* else {
        "cp html/$relative $target".execute()
    }*/
}

此脚本由 Cédric Champeau (melix) 创建。您可以在此 gist 找到此脚本的源代码。

该脚本旨在本地运行,处理从 Confluence 导出的 HTML 文件或包含 HTML 文件的目录。

用法

  1. 将脚本内容保存到工作目录中的 convert.groovy 文件。

  2. 根据您特定的操作系统要求,使该文件可执行。

  3. 在工作目录中创建一个 html 目录用于输入文件,以及一个 asciidoc 目录用于输出文件。

  4. 将单个文件或包含文件的目录放入上述 html 目录中。

  5. 运行 groovy convert 以转换 html 目录中包含的文件。

  6. asciidoc 目录中查找生成的输出文件,并确认它符合您的要求。