如何在 PC 上将 Markdown 文件转换为 Dokuwiki

如何在 PC 上将 Markdown 文件转换为 Dokuwiki

我正在寻找一个工具或脚本来转换Markdown文件到多库维基格式,可在PC上运行。

这样我就可以使用MarkdownPad在 PC 上创建文档的初稿,然后将其转换为 Dokuwiki 格式,上传到我无法控制的 Dokuwiki 安装中。(这意味着Markdown 插件对我没什么用。

可以花时间自己编写一个 Python 脚本来进行转换,但如果这样的事情已经存在,我想避免花时间在这上面。

我希望支持/转换的 Markdown 标签是:

  • 标题级别 1 - 5
  • 粗体、斜体、下划线、固定宽度字体
  • 编号和未编号列表
  • 超链接
  • 水平线

是否存在这样的工具,或者是否有一个良好的起点可用?


我发现并考虑的事情

答案1

停止新闻 - 2014 年 8 月

自从Pandoc 1.13,Pandoc 现在包含我对 DokuWiki 写作的实现 - 并且那里实现的功能比此脚本多得多。因此此脚本现在几乎是多余的。


我最初说过我不想编写 Python 脚本来进行转换,但最终我还是这么做了。

真正节省时间的步骤是使用 Pandoc 解析 Markdown 文本,并写出文档的 JSON 表示。然后,这个 JSON 文件就相当容易解析,并以 DokuWiki 格式写出。

下面是脚本,它实现了我关心的 Markdown 和 DokuWiki 的部分内容 - 以及其他一些内容。(我还没有上传我编写的相应测试套件)

使用要求:

  • Python(我在 Windows 上使用的是 2.7)
  • 已安装 Pandoc,并且 pandoc.exe 位于您的 PATH 中(或者编辑脚本以输入 Pandoc 的完整路径)

我希望这也能为其他人节省一些时间......

编辑2:2013-06-26:我现在把这段代码放到了 GitHub 上,https://github.com/claremacrae/markdown_to_dokuwiki.py。请注意,那里的代码增加了对更多格式的支持,并且还包含一个测试套件。

编辑1:调整后添加了用于解析 Markdown 反引号样式代码示例的代码:

# -*- coding: latin-1 -*-

import sys
import os
import json

__doc__ = """This script will read a text file in Markdown format,
and convert it to DokuWiki format.

The basic approach is to run pandoc to convert the markdown to JSON,
and then to parse the JSON output, and convert it to dokuwiki, which
is written to standard output

Requirements:
 - pandoc is in the user's PATH
"""

# TODOs
# underlined, fixed-width
# Code quotes

list_depth = 0
list_depth_increment = 2

def process_list( list_marker, value ):
    global list_depth
    list_depth += list_depth_increment
    result = ""
    for item in value:
        result += '\n' + list_depth * unicode( ' ' ) + list_marker + process_container( item )
    list_depth -= list_depth_increment
    if list_depth == 0:
        result += '\n'
    return result

def process_container( container ):
    if isinstance( container, dict ):
        assert( len(container) == 1 )
        key = container.keys()[ 0 ]
        value = container.values()[ 0 ]
        if key == 'Para':
            return process_container( value ) + '\n\n'
        if key == 'Str':
            return value
        elif key == 'Header':
            level = value[0]
            marker = ( 7 - level ) * unicode( '=' )
            return marker + unicode(' ') + process_container( value[1] ) + unicode(' ') + marker + unicode('\n\n')
        elif key == 'Strong':
            return unicode('**') + process_container( value ) + unicode('**')
        elif key == 'Emph':
            return unicode('//') + process_container( value ) + unicode('//')
        elif key == 'Code':
            return unicode("''") + value[1] + unicode("''")
        elif key == "Link":
            url = value[1][0]
            return unicode('[[') + url + unicode('|') + process_container( value[0] ) + unicode(']]')
        elif key == "BulletList":
            return process_list( unicode( '* ' ), value)
        elif key == "OrderedList":
            return process_list( unicode( '- ' ), value[1])
        elif key == "Plain":
            return process_container( value )
        elif key == "BlockQuote":
            # There is no representation of blockquotes in DokuWiki - we'll just
            # have to spit out the unmodified text
            return '\n' + process_container( value ) + '\n'

        #elif key == 'Code':
        #    return unicode("''") + process_container( value ) + unicode("''")
        else:
            return unicode("unknown map key: ") + key + unicode( " value: " ) + str( value )

    if isinstance( container, list ):
        result = unicode("")
        for value in container:
            result += process_container( value )
        return result

    if isinstance( container, unicode ):
        if container == unicode( "Space" ):
            return unicode( " " )
        elif container == unicode( "HorizontalRule" ):
            return unicode( "----\n\n" )

    return unicode("unknown") + str( container )

def process_pandoc_jason( data ):
    assert( len(data) == 2 )
    result = unicode('')
    for values in data[1]:
        result += process_container( values )
    print result

def convert_file( filename ):
    # Use pandoc to parse the input file, and write it out as json
    tempfile = "temp_script_output.json"
    command = "pandoc --to=json \"%s\" --output=%s" % ( filename, tempfile )
    #print command
    os.system( command )

    input_file = open(tempfile, 'r' )
    input_text = input_file.readline()
    input_file.close()

    ## Parse the data
    data = json.loads( input_text )
    process_pandoc_jason( data )

def main( files ):
    for filename in files:
        convert_file( filename )

if __name__ == "__main__":
    files = sys.argv[1:]

    if len( files ) == 0:
        sys.stderr.write( "Supply one or more filenames to convert on the command line\n" )
        return_code = 1
    else:
        main( files )
        return_code = 0

    sys.exit( return_code )

答案2

这是我最近一直在使用的替代方法。

其优点是:

  • 它将MarkDown 语法范围更加广泛比 Python 脚本我的其他答案
  • 它不需要安装 Python
  • 不需要安装 pandoc

菜谱:

  1. 在以下位置打开 Markdown 文件MarkdownPad 2

    MarkdownPad 2 截图

  2. 选择编辑 -> “将文档复制为 HTML”

  3. 跑步html2doku维基

    HTML 到 DokuWiki 截图

  4. 将 HTML 粘贴到顶部的“HTML 输入”窗格中

  5. 选择全部,然后复制底部“DokuWiki 输出”窗格中的所有文本

答案3

并非理想,但可行

Markdown -> HTML -> Dokuwiki

第一次转换由 pandoc 完成

第二 -HTML-Wiki转换器-DokuWikiPerl 模块

提示:撤销现有代码的操作

silverstripe-doc-重组git-repo 包含用于转换的代码(PHP)从 Dokuwiki 到 Markdown

相关内容