Cla*_*rae 13 markdown file-conversion dokuwiki
我正在寻找将Markdown文件转换为Dokuwiki格式的工具或脚本,该格式将在 PC 上运行。
这样我就可以在 PC 上使用MarkdownPad创建文档的初稿,然后将它们转换为 Dokuwiki 格式,上传到我无法控制的 Dokuwiki 安装。(这意味着Markdown 插件对我没有用。)
我可以花时间编写一个 Python 脚本来自己进行转换,但我想避免花时间在这上面,如果这样的事情已经存在的话。
我想要支持/转换的 Markdown 标签是:
是否存在这样的工具,或者是否有可用的良好起点?
我发现并考虑过的事情
一开始以为txt2tags会有用,但是虽然markdown和Dokuwiki都可以写,但是跟自己特定的输入格式有很大的关系
我也看过Markdown2Dokuwiki,虽然我当然愿意使用 sed 脚本,即使在 PC 上,它也只支持 Markdown 语法的一小部分。
python-markdown2听起来也很有前途,但它只写出 HTML。
Cla*_*rae 12
从Pandoc 1.13 开始,Pandoc 现在包含我对 DokuWiki 写作的实现 - 并且在那里实现的功能比在这个脚本中更多。所以这个脚本现在几乎是多余的。
最初说过我不想编写 Python 脚本来进行转换,但我最终还是这样做了。
真正节省时间的步骤是使用 Pandoc 解析 Markdown 文本,并写出文档的 JSON 表示。这个 JSON 文件当时很容易解析,并以 DokuWiki 格式写出。
下面是脚本,它实现了我关心的 Markdown 和 DokuWiki 部分 - 以及更多。(我写的对应的测试套件还没上传)
使用要求:
我希望这也可以为其他人节省一些时间......
编辑 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 )
Run Code Online (Sandbox Code Playgroud)
这是我最近一直在使用的另一种方法。
其优点是:
食谱:
在MarkdownPad 2中打开 Markdown 文件

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

将 HTML 粘贴到顶部的“HTML 输入”窗格中
选择全部,然后复制底部“DokuWiki 输出”窗格中的所有文本
| 归档时间: |
|
| 查看次数: |
11018 次 |
| 最近记录: |