1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
#!/usr/bin/env python3
# vim: set sts=4 expandtab:
"""
moin2ascii - convert a moinmoin text file to an AsciiDoc
Copyright (C) 2009 Osamu Aoki, GPL
"""
import sys, os, re
VERSION = '1.0.1'
def process_text(line):
# line = re.sub(r"\\",'@bslash@',line)
line = re.sub(r"\\",r'\\',line)
line = re.sub(r"\~",r'\~',line)
line = re.sub(r"\^",r'\^',line)
line = re.sub(r"\*",r'\*',line)
line = re.sub(r'{{{',r'`',line)
line = re.sub(r'}}}',r'`',line)
line = re.sub(r"'''",r'**',line)
line = re.sub(r"''",r'__',line)
line = re.sub(r"'",r"\'",line)
# line = re.sub(r"\&",r'&',line)
# line = re.sub(r"\<",r'>',line)
# line = re.sub(r"\>",r'<',line)
line = re.sub(r'\[\[([^|]+)\|([^]]+)\]\]',r'\1[\2]',line)
line = re.sub(r'\[\[([^|]+)\\]\]',r'\1[\1]',line)
return line
def process_indent(line):
if re.match(r'^=',line):
line = re.sub(r'^=(.*) =+',r'==\1',line)
elif line[0:2] == ' *':
line = "-" + line[2:]
elif line[0:3] == ' *':
line = "--" + line[3:]
elif re.match(r'^ +[0-9]\.',line):
line = line[1:]
elif line[0:3] == '/!\\':
line = "WARNING:" + line[3:]
elif line[0:3] == r'<!>':
line = "CAUTION:" + line[3:]
elif line[0:3] == r'{i}':
line = "TIP:" + line[3:]
elif line[0:3] == r'(!)':
line = "NOTE:" + line[3:]
line = process_text(line)
if line[0:2] == '--':
line = " *" + line[2:]
return line
def process_table(line):
line = re.sub(r'\|\| *([^ ].*[^ ]) *\|\|',r'\1',line)
items = re.split(r' *\|\| *',line,8)
i = 0
for item in items:
items[i] = process_text(item)
i = i + 1
return items
if __name__ == '__main__':
m2a_mode = 'NORMAL'
m2a_table_mode = 0
m2a_table_popcon = 0
for line in sys.stdin.readlines():
line=line[0:-1]
if line == r"{{{#!plain":
line = "{{{"
if m2a_mode != 'SCREEN':
if line[0:2] == '##':
print("//" + line[2:])
elif line[0:2] == '||':
if m2a_mode == 'NORMAL':
table_items = [[]]
table_rows = 0
table_cols = 0
table_cols_width = [0]
table_item = process_table(line)
table_title = table_item[0]
if len(table_item) != 1:
table_pkg = table_item[1] # '', '1', '2'
else:
table_pkg =0
print("\n." + table_title)
print('[grid="all"]')
m2a_mode = 'TABLE_HEAD'
elif m2a_mode == 'TABLE_HEAD':
line = re.sub("'''",'',line)
table_items = [process_table(line)]
i = 0
m2a_mode = 'TABLE_BODY'
elif m2a_mode == 'TABLE_BODY':
table_item = process_table(line)
if table_pkg == "1":
table_item[1] = "@@@popcon1@@@"
table_item[2] = "@@@psize1@@@"
if table_pkg == "2":
table_item[2] = "@@@popcon2@@@"
table_item[3] = "@@@psize2@@@"
table_items = table_items + [table_item]
m2a_mode = 'TABLE_BODY'
else:
if m2a_mode == 'TABLE_BODY':
# now time to print table
table_rows = len(table_items)
# find table_cols_width
table_cols_width = [0,0,0,0,0,0,0,0,0]
for table_item in table_items:
i = 0
for item in table_item:
table_cols_width[i] = max(table_cols_width[i], len(item))
i = i + 1
# print ruler
ruler1 = ""
ruler2 = ""
i = 0
for item in table_cols_width:
if item != 0:
ruler1 = ruler1 + "`" + ( "-" * item )
ruler2 = ruler2 + "-" + ( "-" * item )
print(ruler1)
# print table_items
j = 0
for table_item in table_items:
i = 0
for item in table_item:
print(item + " " * (table_cols_width[i] - len(item)), end=' ')
i = i + 1
print("")
j = j + 1
if j == 1:
print(ruler2)
print(ruler2)
m2a_mode = 'NORMAL'
# so this is normal text
if re.match("^{{{$", line):
print('')
print('--------------------')
m2a_mode = 'SCREEN'
elif re.match("^ +{{{$", line):
print(' +\n--------------------')
m2a_mode = 'SCREEN'
else:
if re.match("^ +\*", line):
if m2a_mode != 'BULLET':
print("")
m2a_mode = 'BULLET'
else:
m2a_mode = 'NORMAL'
print(process_indent(line))
else:
if re.match("^}}}$", line):
print('--------------------')
m2a_mode = 'NORMAL'
else:
print(line)
m2a_mode = 'SCREEN'
"""
Design: Convert text depending on mode:
MODE = NORMAL
-------------
This is default mode.
This can be base text including buletted/numerated list items.
Processing:
+ Ajust header leads: "s/^=/==/"
+ Adjust buletted/numerated list items.
+ /!\ for "WARNING"
+ <!> for "CAUTION"
+ {i} for "TIP"
+ (!) for "NOTE"
+ content text adjustments and processing
MODE = TABLE
------------
This is table section.
It starts at a line matching "^||"
It ends at a line matching "^[^|][^|]"
Processing:
+ The first line is translated to "TITLE of TABLE".
+ .. If first line contains 2,3,4th argument, they are processed (popcon/size).
Insert: @@@popcon@@@, @@@size@@@
+ The second line is translated to "HEADER".
+ content text adjustments and processing
MODE = SCREEN
-------------
This is screen section.
It starts at a line matching "^{{{" or "^ {{{"
It ends at a line matching "^}}}"
Processing:
+ verbatim copy as screen ....
+ In case of "^ {{{", continuation of indented list item.
process_line()
------------------
Content text adjustments and processing inclises folowing
+ format converting
* URL: [[http://...|...]] --> http://...[...]
* URL: [[http://...]] --> http://...
* The verbatim text are {{{....}}} --> +++....++++
* ''' for strong *
* '' for italics _
* @{@...@}@ to new style (predefined)
+ reporting to stderr
* normal text with funny strings
* @{@...@}@ unprocessed
"""
|