use native formatter, fixup commandline arguments
This commit is contained in:
parent
0537d952b2
commit
1ecfee3e50
3 changed files with 86 additions and 33 deletions
76
moin2doku.py
76
moin2doku.py
|
@ -13,9 +13,10 @@ from shutil import copyfile, copystat
|
||||||
from os import listdir
|
from os import listdir
|
||||||
from os.path import isdir, basename
|
from os.path import isdir, basename
|
||||||
from doku import DokuWiki
|
from doku import DokuWiki
|
||||||
|
from moinformat import moin2doku
|
||||||
|
|
||||||
def check_dirs(moin_pages_dir, output_dir):
|
def check_dirs(moin_pages_dir, output_dir):
|
||||||
if not isdir(moin_pages_dir):
|
if moin_pages_dir and not isdir(moin_pages_dir):
|
||||||
print >> sys.stderr, "MoinMoin pages directory doesn't exist!"
|
print >> sys.stderr, "MoinMoin pages directory doesn't exist!"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@ -37,6 +38,23 @@ def get_path_names(moin_pages_dir):
|
||||||
def readfile(filename):
|
def readfile(filename):
|
||||||
return file(filename, 'r').readlines()
|
return file(filename, 'r').readlines()
|
||||||
|
|
||||||
|
def readfile2(filename):
|
||||||
|
with open(filename, 'r') as f:
|
||||||
|
text = f.read()
|
||||||
|
return unicode(text.decode('utf-8'))
|
||||||
|
|
||||||
|
def writefile2(filename, content, overwrite=False):
|
||||||
|
dir = os.path.split(filename)[0]
|
||||||
|
if not isdir(dir):
|
||||||
|
os.makedirs(dir);
|
||||||
|
|
||||||
|
if os.path.exists(filename) and overwrite == False:
|
||||||
|
raise OSError, 'File already exists: %s' % filename
|
||||||
|
|
||||||
|
f = file(filename, 'w')
|
||||||
|
f.write(content)
|
||||||
|
f.close()
|
||||||
|
|
||||||
def writefile(filename, content, overwrite=False):
|
def writefile(filename, content, overwrite=False):
|
||||||
dir = os.path.split(filename)[0]
|
dir = os.path.split(filename)[0]
|
||||||
if not isdir(dir):
|
if not isdir(dir):
|
||||||
|
@ -183,11 +201,17 @@ def convert_markup(pagename, content):
|
||||||
return content
|
return content
|
||||||
|
|
||||||
def print_help():
|
def print_help():
|
||||||
print "Usage: moinconv.py <moinmoin pages directory> <output directory>"
|
program = sys.argv[0]
|
||||||
|
print "Usage: %s -m <moinmoin pages directory> -d <output directory>" % program
|
||||||
print "Convert MoinMoin pages to DokuWiki."
|
print "Convert MoinMoin pages to DokuWiki."
|
||||||
print "Options:"
|
print "Options:"
|
||||||
print "-o - overwrite output files"
|
print "-m DIR - MoinMoin pages dir"
|
||||||
print "-f FILE - convert signle file"
|
print "-d DIR - Dokuwiki pages dir"
|
||||||
|
print "-f - overwrite output files"
|
||||||
|
print "-F FILE - convert single file"
|
||||||
|
print ""
|
||||||
|
print "%s -m moinmoin/data/pages /var/lib/dokuwiki/pages" % program
|
||||||
|
print "%s -F moinmoin/data/pages/frontpage -d out" % program
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# return unicode encoded wikiname
|
# return unicode encoded wikiname
|
||||||
|
@ -215,11 +239,18 @@ def convertfile(pagedir, overwrite = False):
|
||||||
print "SKIP %s: skip backups" % pagedir
|
print "SKIP %s: skip backups" % pagedir
|
||||||
return
|
return
|
||||||
|
|
||||||
content = readfile(curr_rev)
|
content = readfile2(curr_rev)
|
||||||
content = convert_markup(pagename, content)
|
# print "content:[%s]" % content
|
||||||
|
# content = convert_markup(pagename, content)
|
||||||
|
content = moin2doku(pagename, content)
|
||||||
|
|
||||||
out_file = os.path.join(output_dir, dw.wikiFN(pagename))
|
out_file = os.path.join(output_dir, dw.wikiFN(pagename))
|
||||||
print "dokuname: [%s]" % out_file
|
print "dokuname: [%s]" % out_file
|
||||||
writefile(out_file, content, overwrite = overwrite)
|
try:
|
||||||
|
writefile2(out_file, content, overwrite = overwrite)
|
||||||
|
except OSError, e:
|
||||||
|
print e
|
||||||
|
return 0
|
||||||
|
|
||||||
ns = dw.getNS(dw.cleanID(pagename))
|
ns = dw.getNS(dw.cleanID(pagename))
|
||||||
copy_attachments(pagedir, ns)
|
copy_attachments(pagedir, ns)
|
||||||
|
@ -230,36 +261,41 @@ def convertfile(pagedir, overwrite = False):
|
||||||
# "main" starts here
|
# "main" starts here
|
||||||
#
|
#
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 'hof:', [ "help" ])
|
opts, args = getopt.getopt(sys.argv[1:], 'hfm:d:F:', [ "help" ])
|
||||||
except getopt.GetoptError, e:
|
except getopt.GetoptError, e:
|
||||||
print >> sys.stderr, 'Incorrect parameters! Use --help switch to learn more.: %s' % e
|
print >> sys.stderr, 'Incorrect parameters! Use --help switch to learn more.: %s' % e
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
overwrite = False
|
overwrite = False
|
||||||
inputfile = None
|
input_file = None
|
||||||
|
moin_pages_dir = None
|
||||||
|
output_dir = None
|
||||||
for o, a in opts:
|
for o, a in opts:
|
||||||
if o == "--help" or o == "-h":
|
if o == "--help" or o == "-h":
|
||||||
print_help()
|
print_help()
|
||||||
if o == "-o":
|
|
||||||
overwrite = True
|
|
||||||
if o == "-f":
|
if o == "-f":
|
||||||
inputfile = a
|
overwrite = True
|
||||||
|
if o == "-m":
|
||||||
|
moin_pages_dir = a
|
||||||
|
if o == "-d":
|
||||||
|
output_dir = a
|
||||||
|
if o == "-F":
|
||||||
|
input_file = a
|
||||||
|
|
||||||
if len(args) != 2:
|
if not moin_pages_dir and not input_file:
|
||||||
print >> sys.stderr, 'Incorrect parameters! Use --help switch to learn more.'
|
print_help()
|
||||||
|
print >> sys.stderr, 'No input file or page dir to process'
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
(moin_pages_dir, output_dir) = args
|
|
||||||
|
|
||||||
check_dirs(moin_pages_dir, output_dir)
|
check_dirs(moin_pages_dir, output_dir)
|
||||||
|
|
||||||
print 'Input dir is: %s.' % moin_pages_dir
|
print "Input dir is: '%s'" % moin_pages_dir
|
||||||
print 'Output dir is: %s.' % output_dir
|
print "Output dir is: '%s'" % output_dir
|
||||||
|
|
||||||
dw = DokuWiki()
|
dw = DokuWiki()
|
||||||
|
|
||||||
if inputfile != None:
|
if input_file != None:
|
||||||
res = convertfile(inputfile, overwrite = overwrite)
|
res = convertfile(input_file, overwrite = overwrite)
|
||||||
else:
|
else:
|
||||||
pathnames = get_path_names(moin_pages_dir)
|
pathnames = get_path_names(moin_pages_dir)
|
||||||
converted = 0
|
converted = 0
|
||||||
|
|
|
@ -7,19 +7,33 @@ from MoinMoin.parser.wiki import Parser
|
||||||
from text_dokuwiki import Formatter
|
from text_dokuwiki import Formatter
|
||||||
from MoinMoin.request import RequestCLI
|
from MoinMoin.request import RequestCLI
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import StringIO
|
||||||
|
|
||||||
|
def moin2doku(pagename, text):
|
||||||
|
parser = Parser(text, request)
|
||||||
|
|
||||||
|
# this needed for macros
|
||||||
|
request.formatter = formatter
|
||||||
|
|
||||||
|
p = Page(request, pagename)
|
||||||
|
formatter.setPage(p)
|
||||||
|
|
||||||
|
output = StringIO.StringIO()
|
||||||
|
|
||||||
|
# wrap sys.stdout as RequestCLI has no interface to say where to output
|
||||||
|
stdout = sys.stdout
|
||||||
|
sys.stdout = output
|
||||||
|
parser.format(formatter)
|
||||||
|
sys.stdout = stdout
|
||||||
|
|
||||||
|
return output.getvalue()
|
||||||
|
|
||||||
request = RequestCLI()
|
request = RequestCLI()
|
||||||
formatter = Formatter(request)
|
formatter = Formatter(request)
|
||||||
|
|
||||||
# pages/playground\(2f\)SyntaxReference/revisions/00000001
|
if __name__ == "__main__":
|
||||||
with open('syntaxreference.txt', 'r') as f:
|
# pages/playground\(2f\)SyntaxReference/revisions/00000001
|
||||||
text = f.read()
|
with open('syntaxreference.txt', 'r') as f:
|
||||||
|
text = f.read()
|
||||||
parser = Parser(text, request)
|
print moin2doku('test', text)
|
||||||
|
|
||||||
# this needed for macros
|
|
||||||
request.formatter = formatter
|
|
||||||
|
|
||||||
p = Page(request, "test")
|
|
||||||
formatter.setPage(p)
|
|
||||||
|
|
||||||
parser.format(formatter)
|
|
||||||
|
|
|
@ -12,12 +12,15 @@ from MoinMoin.formatter.base import FormatterBase
|
||||||
from MoinMoin import config
|
from MoinMoin import config
|
||||||
from MoinMoin.Page import Page
|
from MoinMoin.Page import Page
|
||||||
|
|
||||||
|
# TODO: let base class MoinMoin/formatter/base.py handle not implemented methods
|
||||||
|
|
||||||
class Formatter(FormatterBase):
|
class Formatter(FormatterBase):
|
||||||
"""
|
"""
|
||||||
Send Dokuwiki formatted data.
|
Send Dokuwiki formatted data.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
hardspace = ' '
|
hardspace = ' '
|
||||||
|
# hardspace = ' '
|
||||||
|
|
||||||
def __init__(self, request, **kw):
|
def __init__(self, request, **kw):
|
||||||
apply(FormatterBase.__init__, (self, request), kw)
|
apply(FormatterBase.__init__, (self, request), kw)
|
||||||
|
|
Loading…
Reference in a new issue