sbin/doc/markdown2dokuwiki.py
changeset 1072 ac1eacb3aa33
parent 1071 02c04d2c8fd8
child 1073 687133dc13cf
equal deleted inserted replaced
1071:02c04d2c8fd8 1072:ac1eacb3aa33
     1 import re, sys
       
     2 
       
     3 inputname = sys.argv[1]
       
     4 outputname = re.sub("\.\w+$",".dokuwiki",inputname)
       
     5 
       
     6 print "Converting %s to %s"%(inputname,outputname)
       
     7 
       
     8 inputfile = open(inputname,'r')
       
     9 
       
    10 markdowntext = inputfile.read()
       
    11 
       
    12 # Replacing title levels: #italic# -> ======italic======
       
    13 
       
    14 wikitext = re.sub("(?m)(^#+|#+$)", lambda matches: "=" * (7 - len(matches.group(1))), markdowntext)
       
    15 
       
    16 # Replacing italics: *italic* -> //italic//
       
    17 
       
    18 wikitext = re.sub("(?m)([^*])\*([^*]+)\*($|[^*])", lambda matches: matches.group(1) + "//" + matches.group(2) + "//" + matches.group(3), wikitext)
       
    19 
       
    20 # Replacing lists: - -> *
       
    21 
       
    22 wikitext = re.sub("(?m)^(\s*)(-)\s", lambda matches: " " * ( 2 + len(matches.group(1)) / 2) + "* ", wikitext)
       
    23 
       
    24 # Replacing lists: 1. -> -
       
    25 
       
    26 wikitext = re.sub("(?m)^(\s*)(\d+\.)\s", lambda matches: " " * ( 2 + len(matches.group(1)) / 2) + "- ", wikitext)
       
    27 
       
    28 # Replacing escaped underscores \_ -> _
       
    29 
       
    30 wikitext = re.sub("(?m)(\\\_)", "_", wikitext)
       
    31 
       
    32 # Escaping URL templates {{ -> %%{%%{
       
    33 
       
    34 wikitext = re.sub("(?m)({{)", "%%{%%{", wikitext)
       
    35 
       
    36 outputfile = open(outputname,'w')
       
    37 
       
    38 outputfile.write(wikitext)
       
    39 
       
    40 outputfile.close()
       
    41 
       
    42 inputfile.close()