paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@7 | 3 | """ |
paul@7 | 4 | Confluence Wiki syntax parsing. |
paul@7 | 5 | |
paul@34 | 6 | Copyright (C) 2012, 2013 Paul Boddie <paul@boddie.org.uk> |
paul@8 | 7 | |
paul@8 | 8 | This software is free software; you can redistribute it and/or |
paul@8 | 9 | modify it under the terms of the GNU General Public License as |
paul@8 | 10 | published by the Free Software Foundation; either version 2 of |
paul@8 | 11 | the License, or (at your option) any later version. |
paul@8 | 12 | |
paul@8 | 13 | This software is distributed in the hope that it will be useful, |
paul@8 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@8 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@8 | 16 | GNU General Public License for more details. |
paul@8 | 17 | |
paul@8 | 18 | You should have received a copy of the GNU General Public |
paul@8 | 19 | License along with this library; see the file LICENCE.txt |
paul@8 | 20 | If not, write to the Free Software Foundation, Inc., |
paul@8 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paul@8 | 22 | |
paul@8 | 23 | -------- |
paul@8 | 24 | |
paul@8 | 25 | The basic procedure is as follows: |
paul@8 | 26 | |
paul@7 | 27 | 1. Wiki pages are first split up into regions. |
paul@7 | 28 | 2. Then, within these regions, the text is split into blocks. |
paul@7 | 29 | 1. First, lists are identified. |
paul@7 | 30 | 2. Additionally, other block-like elements are identified. |
paul@78 | 31 | 3. Each block is then split into regions. |
paul@7 | 32 | """ |
paul@7 | 33 | |
paul@35 | 34 | from common import * |
paul@6 | 35 | import re |
paul@25 | 36 | import sys |
paul@41 | 37 | import codecs |
paul@77 | 38 | import operator |
paul@19 | 39 | |
paul@6 | 40 | # Section extraction. |
paul@6 | 41 | |
paul@88 | 42 | sections_regexp_str = r"(?<!{){(?P<type>[^-_*+{}\n:]+)(?P<options>:[^}\n]+)?}" \ |
paul@88 | 43 | r"|" \ |
paul@88 | 44 | r"^(?P<rowstart>[|]{1,2})" \ |
paul@88 | 45 | r"|" \ |
paul@88 | 46 | r"(?P<rowend>[|]{1,2}(\n|$))" \ |
paul@88 | 47 | r"|" \ |
paul@88 | 48 | r"^(?P<listitem>\s*[*#-]+\s+.*(\n|$))" |
paul@88 | 49 | |
paul@6 | 50 | sections_regexp = re.compile(sections_regexp_str, re.DOTALL | re.MULTILINE) |
paul@6 | 51 | |
paul@6 | 52 | def get_regions(s): |
paul@6 | 53 | |
paul@6 | 54 | """ |
paul@6 | 55 | Return a list of regions from 's'. Each region is specified using a tuple of |
paul@6 | 56 | the form (type, text). |
paul@6 | 57 | """ |
paul@6 | 58 | |
paul@6 | 59 | last = 0 |
paul@76 | 60 | regions = [""] |
paul@75 | 61 | depth = 0 |
paul@86 | 62 | had_row = False |
paul@88 | 63 | had_item = False |
paul@75 | 64 | |
paul@6 | 65 | for match in sections_regexp.finditer(s): |
paul@6 | 66 | start, end = match.span() |
paul@86 | 67 | is_start = match.group("options") or match.group("rowstart") |
paul@76 | 68 | is_section = is_section_marker(match.group("type")) |
paul@86 | 69 | is_row = match.group("rowstart") or match.group("rowend") |
paul@88 | 70 | is_item = match.group("listitem") |
paul@75 | 71 | |
paul@75 | 72 | # The start of a region is either indicated by a marker with options or |
paul@75 | 73 | # by a marker where no region is currently active. |
paul@75 | 74 | |
paul@75 | 75 | if is_start or not depth: |
paul@75 | 76 | |
paul@75 | 77 | # Where no region is active, add the text since the last match as a |
paul@75 | 78 | # "null" region. |
paul@75 | 79 | |
paul@75 | 80 | if not depth: |
paul@76 | 81 | regions[-1] += s[last:start] |
paul@75 | 82 | |
paul@75 | 83 | # A new region is maintained as a string. |
paul@75 | 84 | |
paul@76 | 85 | if is_section: |
paul@76 | 86 | regions.append(s[start:end]) |
paul@76 | 87 | |
paul@86 | 88 | # A new row may either continue a table region or start a new |
paul@86 | 89 | # table region. |
paul@86 | 90 | |
paul@86 | 91 | elif is_row: |
paul@86 | 92 | if (last != start or not had_row): |
paul@86 | 93 | regions.append(s[start:end]) |
paul@86 | 94 | else: |
paul@86 | 95 | regions[-2] += regions[-1] + s[start:end] |
paul@86 | 96 | regions.pop() |
paul@86 | 97 | |
paul@88 | 98 | # A list item may either continue a list region or start a new |
paul@88 | 99 | # list region. |
paul@88 | 100 | |
paul@88 | 101 | elif is_item: |
paul@88 | 102 | if (last != start or not had_item): |
paul@88 | 103 | regions.append(s[start:end]) |
paul@88 | 104 | else: |
paul@88 | 105 | regions[-2] += regions[-1] + s[start:end] |
paul@88 | 106 | regions.pop() |
paul@88 | 107 | |
paul@76 | 108 | # Certain markers may be standalone macros. |
paul@76 | 109 | |
paul@76 | 110 | else: |
paul@76 | 111 | regions[-1] += s[start:end] |
paul@75 | 112 | |
paul@75 | 113 | # Where a region is active, add the text since the last match as |
paul@75 | 114 | # well as the text in this match to the region. |
paul@75 | 115 | |
paul@75 | 116 | else: |
paul@75 | 117 | regions[-1] += s[last:end] |
paul@75 | 118 | |
paul@86 | 119 | if is_section or is_row: |
paul@76 | 120 | depth += 1 |
paul@75 | 121 | |
paul@75 | 122 | # The end of a region is indicated by a marker with no options. |
paul@75 | 123 | |
paul@75 | 124 | else: |
paul@75 | 125 | # Where no region is active, the text since the last match plus the |
paul@75 | 126 | # marker are added to the current "null" region. |
paul@75 | 127 | |
paul@75 | 128 | if not depth: |
paul@75 | 129 | |
paul@75 | 130 | # Add to the string portion of the "null" region. |
paul@75 | 131 | |
paul@76 | 132 | regions[-1] += s[last:end] |
paul@75 | 133 | |
paul@75 | 134 | # Where a region is active, the end marker and preceding text is |
paul@75 | 135 | # either incorporated into the current region if more than one |
paul@75 | 136 | # region is active, or the preceding text is incorporated into the |
paul@75 | 137 | # current region and the details of the region are then obtained. |
paul@75 | 138 | |
paul@75 | 139 | else: |
paul@86 | 140 | if depth > 1 or (not is_section and not is_row): |
paul@75 | 141 | regions[-1] += s[last:end] |
paul@75 | 142 | |
paul@75 | 143 | # Terminate the active region, interpreting its contents. |
paul@75 | 144 | |
paul@75 | 145 | else: |
paul@76 | 146 | regions[-1] += s[last:end] |
paul@76 | 147 | regions.append("") |
paul@76 | 148 | |
paul@86 | 149 | if is_section or is_row: |
paul@76 | 150 | depth -= 1 |
paul@75 | 151 | |
paul@86 | 152 | had_row = is_row |
paul@88 | 153 | had_item = is_item |
paul@6 | 154 | last = end |
paul@75 | 155 | |
paul@75 | 156 | # Where a region is still active, terminate it. |
paul@75 | 157 | |
paul@76 | 158 | regions[-1] += s[last:] |
paul@75 | 159 | |
paul@76 | 160 | return [get_section_details(s) for s in regions if s] |
paul@75 | 161 | |
paul@76 | 162 | def is_section_marker(sectiontype): |
paul@76 | 163 | return sectiontypes.has_key(sectiontype) or sectiontype == "color" |
paul@6 | 164 | |
paul@7 | 165 | # Section inspection. |
paul@7 | 166 | |
paul@15 | 167 | section_regexp_str = r"{(?P<sectiontype>[^\n:]*?)(?::(?P<options>.*?))?}(?P<section>.*){(?P=sectiontype)}" |
paul@7 | 168 | section_regexp = re.compile(section_regexp_str, re.DOTALL | re.MULTILINE) |
paul@7 | 169 | |
paul@6 | 170 | def get_section_details(s): |
paul@6 | 171 | |
paul@7 | 172 | "Return the details of a section 's' in the form (type, text)." |
paul@6 | 173 | |
paul@6 | 174 | match = section_regexp.match(s) |
paul@6 | 175 | if match: |
paul@15 | 176 | return (match.group("sectiontype"), match.group("options")), match.group("section") |
paul@6 | 177 | else: |
paul@6 | 178 | return None, s |
paul@6 | 179 | |
paul@14 | 180 | # Heading, table and list extraction. |
paul@7 | 181 | |
paul@41 | 182 | list_regexp_str = r"^\s*(?P<listtype>[*#-])[*#-]*\s+.*(\n\s*(?P=listtype).*?)*(?:\n|$)" |
paul@39 | 183 | table_regexp_str = r"^((?P<celltype>[|]{1,2})((.|\n(?!\n))+?(?P=celltype))+(\n|$))+" |
paul@14 | 184 | blocktext_regexp_str = r"^(?P<type>h\d|bq)\.\s+(?P<text>.*)$" |
paul@7 | 185 | |
paul@14 | 186 | blockelement_regexp = re.compile( |
paul@14 | 187 | "(" + list_regexp_str + ")" |
paul@14 | 188 | "|" |
paul@14 | 189 | "(" + table_regexp_str + ")" |
paul@14 | 190 | "|" |
paul@14 | 191 | "(" + blocktext_regexp_str + ")", |
paul@14 | 192 | re.MULTILINE |
paul@14 | 193 | ) |
paul@14 | 194 | |
paul@14 | 195 | def get_block_elements(s): |
paul@7 | 196 | |
paul@7 | 197 | """ |
paul@14 | 198 | Extract headings, tables and lists from the given string 's'. |
paul@7 | 199 | """ |
paul@7 | 200 | |
paul@7 | 201 | last = 0 |
paul@7 | 202 | blocks = [] |
paul@14 | 203 | for match in blockelement_regexp.finditer(s): |
paul@7 | 204 | start, end = match.span() |
paul@14 | 205 | matchtype = match.group("listtype") and "list" or match.group("celltype") and "table" or match.group("type") |
paul@7 | 206 | blocks.append((None, s[last:start])) |
paul@14 | 207 | blocks.append((matchtype, match.group("text") or s[start:end])) |
paul@7 | 208 | last = end |
paul@7 | 209 | blocks.append((None, s[last:])) |
paul@7 | 210 | return blocks |
paul@7 | 211 | |
paul@7 | 212 | # Block extraction. |
paul@7 | 213 | |
paul@7 | 214 | block_regexp_str = r"^(?:\s*\n)+" |
paul@7 | 215 | block_regexp = re.compile(block_regexp_str, re.MULTILINE) |
paul@7 | 216 | |
paul@7 | 217 | def get_basic_blocks(s): |
paul@7 | 218 | |
paul@7 | 219 | """ |
paul@7 | 220 | Return blocks from the given string 's' by splitting the text on blank lines |
paul@7 | 221 | and eliminating those lines. |
paul@7 | 222 | """ |
paul@7 | 223 | |
paul@7 | 224 | return [b for b in block_regexp.split(s) if b.strip()] |
paul@7 | 225 | |
paul@7 | 226 | # Block inspection. |
paul@7 | 227 | |
paul@7 | 228 | def get_blocks(s): |
paul@7 | 229 | |
paul@7 | 230 | """ |
paul@7 | 231 | Return blocks from the given string 's', inspecting the basic blocks and |
paul@7 | 232 | generating additional block-level text where appropriate. |
paul@7 | 233 | """ |
paul@7 | 234 | |
paul@7 | 235 | blocks = [] |
paul@7 | 236 | |
paul@14 | 237 | for blocktype, blocktext in get_block_elements(s): |
paul@7 | 238 | |
paul@14 | 239 | # Collect heading, list and table blocks. |
paul@7 | 240 | |
paul@7 | 241 | if blocktype is not None: |
paul@7 | 242 | blocks.append((blocktype, blocktext)) |
paul@7 | 243 | |
paul@7 | 244 | # Attempt to find new subblocks in other regions. |
paul@7 | 245 | |
paul@7 | 246 | else: |
paul@7 | 247 | for block in get_basic_blocks(blocktext): |
paul@14 | 248 | blocks.append((None, block)) |
paul@7 | 249 | |
paul@7 | 250 | return blocks |
paul@7 | 251 | |
paul@14 | 252 | # List item inspection. |
paul@14 | 253 | |
paul@41 | 254 | listitem_regexp_str = r"^(?P<marker> *[-*#]+)\s+(?P<text>.*)$" |
paul@7 | 255 | listitem_regexp = re.compile(listitem_regexp_str, re.MULTILINE) |
paul@7 | 256 | |
paul@14 | 257 | def get_list_items(text): |
paul@14 | 258 | |
paul@14 | 259 | "Return a list of (marker, text) tuples for the given list 'text'." |
paul@14 | 260 | |
paul@14 | 261 | items = [] |
paul@14 | 262 | |
paul@14 | 263 | for match in listitem_regexp.finditer(text): |
paul@14 | 264 | items.append((match.group("marker"), match.group("text"))) |
paul@14 | 265 | |
paul@14 | 266 | return items |
paul@14 | 267 | |
paul@36 | 268 | # Content inspection. |
paul@14 | 269 | |
paul@19 | 270 | monospace_regexp_str = r"{{(?P<monotext>.*?)}}" |
paul@36 | 271 | link_regexp_str = r"[[](?P<linktext>.*?)]" |
paul@38 | 272 | image_regexp_str = r"!(?P<imagetext>\w.*?)!" |
paul@71 | 273 | macro_regexp_str = r"{(?P<macro>.*?):(?P<options>.*?)}" |
paul@36 | 274 | |
paul@36 | 275 | # Word-dependent patterns. |
paul@36 | 276 | # Here, the unbracketed markers must test for the absence of surrounding word |
paul@36 | 277 | # characters. |
paul@36 | 278 | |
paul@36 | 279 | italic_regexp_str = r"(?:(?<!\w)_|\{_\})(?P<italictext>.*?)(?:_(?!\w)|\{_\})" |
paul@36 | 280 | bold_regexp_str = r"(?:(?<!\w)\*|\{\*\})(?P<boldtext>.*?)(?:\*(?!\w)|\{\*\})" |
paul@36 | 281 | del_regexp_str = r"(?:(?<!\w)-|\{-\})(?P<deltext>.*?)(?:-(?!\w)|\{-\})" |
paul@36 | 282 | underline_regexp_str = r"(?:(?<!\w)\+|\{\+\})(?P<underlinetext>.*?)(?:\+(?!\w)|\{\+\})" |
paul@36 | 283 | sub_regexp_str = r"(?:(?<!\w)~|\{~\})(?P<subtext>.*?)(?:~(?!\w)|\{~\})" |
paul@16 | 284 | |
paul@16 | 285 | content_regexp_str = ( |
paul@19 | 286 | "(" + monospace_regexp_str + ")" |
paul@19 | 287 | "|" |
paul@14 | 288 | "(" + link_regexp_str + ")" |
paul@14 | 289 | "|" |
paul@14 | 290 | "(" + image_regexp_str + ")" |
paul@36 | 291 | "|" |
paul@71 | 292 | "(" + macro_regexp_str + ")" |
paul@71 | 293 | "|" |
paul@36 | 294 | "(" + italic_regexp_str + ")" |
paul@36 | 295 | "|" |
paul@36 | 296 | "(" + bold_regexp_str + ")" |
paul@36 | 297 | "|" |
paul@36 | 298 | "(" + del_regexp_str + ")" |
paul@36 | 299 | "|" |
paul@36 | 300 | "(" + underline_regexp_str + ")" |
paul@36 | 301 | "|" |
paul@36 | 302 | "(" + sub_regexp_str + ")" |
paul@16 | 303 | ) |
paul@16 | 304 | |
paul@36 | 305 | # Table row inspection. |
paul@36 | 306 | |
paul@36 | 307 | cellsep_regexp_str = r"(?P<celltype>[|]{1,2})" |
paul@36 | 308 | |
paul@16 | 309 | table_content_regexp_str = ( |
paul@16 | 310 | content_regexp_str + |
paul@14 | 311 | "|" |
paul@14 | 312 | "(" + cellsep_regexp_str + ")" |
paul@14 | 313 | ) |
paul@14 | 314 | |
paul@16 | 315 | content_regexp = re.compile(content_regexp_str) |
paul@16 | 316 | table_content_regexp = re.compile(table_content_regexp_str) |
paul@16 | 317 | |
paul@14 | 318 | def get_table_rows(text): |
paul@14 | 319 | |
paul@14 | 320 | "Return a list of (cellsep, columns) tuples for the given table 'text'." |
paul@14 | 321 | |
paul@14 | 322 | rows = [] |
paul@14 | 323 | |
paul@39 | 324 | for row in text.split("|\n"): |
paul@39 | 325 | if not row: |
paul@39 | 326 | break |
paul@39 | 327 | |
paul@39 | 328 | row += "|" |
paul@14 | 329 | cellsep = None |
paul@14 | 330 | columns = [""] |
paul@14 | 331 | last = 0 |
paul@39 | 332 | for match in table_content_regexp.finditer(row): |
paul@14 | 333 | start, end = match.span() |
paul@39 | 334 | columns[-1] += row[last:start] |
paul@14 | 335 | |
paul@14 | 336 | if match.group("celltype"): |
paul@14 | 337 | if cellsep is None: |
paul@14 | 338 | cellsep = match.group("celltype") |
paul@14 | 339 | columns.append("") |
paul@14 | 340 | else: |
paul@16 | 341 | columns[-1] += match.group() |
paul@14 | 342 | |
paul@14 | 343 | last = end |
paul@14 | 344 | |
paul@39 | 345 | columns[-1] += row[last:] |
paul@14 | 346 | |
paul@14 | 347 | if cellsep: |
paul@14 | 348 | rows.append((cellsep, columns[1:-1])) |
paul@14 | 349 | |
paul@14 | 350 | return rows |
paul@14 | 351 | |
paul@70 | 352 | # Notation conversion. |
paul@70 | 353 | |
paul@70 | 354 | notation_mapping = [ |
paul@70 | 355 | (r"\!", "!"), |
paul@70 | 356 | (r"\-", "-"), |
paul@70 | 357 | (r"\\""\n", "<<BR>>"), |
paul@70 | 358 | (r"\\ ", "<<BR>>"), |
paul@70 | 359 | (r"\~", "~"), |
paul@70 | 360 | ] |
paul@70 | 361 | |
paul@70 | 362 | preformatted_notation_mapping = [ |
paul@70 | 363 | (r"\!", "!"), |
paul@70 | 364 | (r"\-", "-"), |
paul@70 | 365 | (r"\\""\n", "\n"), |
paul@70 | 366 | (r"\\ ", "\n"), |
paul@70 | 367 | (r"\~", "~"), |
paul@70 | 368 | ] |
paul@70 | 369 | |
paul@70 | 370 | # Translation helpers. |
paul@70 | 371 | |
paul@70 | 372 | markers = { |
paul@70 | 373 | "*" : "*", |
paul@70 | 374 | "#" : "1.", |
paul@70 | 375 | "-" : "*", |
paul@70 | 376 | } |
paul@70 | 377 | |
paul@70 | 378 | cellseps = { |
paul@70 | 379 | "|" : "\n|| ", |
paul@70 | 380 | "||" : "\n|| ", |
paul@70 | 381 | } |
paul@70 | 382 | |
paul@70 | 383 | cellextra = { |
paul@70 | 384 | "|" : "", |
paul@70 | 385 | "||" : "'''", |
paul@70 | 386 | } |
paul@70 | 387 | |
paul@15 | 388 | sectiontypes = { |
paul@42 | 389 | "code" : "", |
paul@42 | 390 | "noformat" : "", |
paul@42 | 391 | "quote" : "", |
paul@68 | 392 | "info" : "#!wiki important", |
paul@68 | 393 | "note" : "#!wiki caution", |
paul@68 | 394 | "tip" : "#!wiki tip", |
paul@68 | 395 | "warning" : "#!wiki warning", |
paul@42 | 396 | } |
paul@42 | 397 | |
paul@66 | 398 | preformatted_sectiontypes = (None, "noformat") |
paul@66 | 399 | |
paul@71 | 400 | macroargs = { |
paul@71 | 401 | "color" : "col", |
paul@71 | 402 | } |
paul@71 | 403 | |
paul@42 | 404 | macrotypes = { |
paul@71 | 405 | "anchor" : "<<Anchor(%(args)s)>>", |
paul@71 | 406 | "color" : "<<Color2(%(content)s, %(args)s)>>", |
paul@15 | 407 | } |
paul@15 | 408 | |
paul@70 | 409 | class ConfluenceParser: |
paul@70 | 410 | |
paul@70 | 411 | "A parser for Confluence markup." |
paul@70 | 412 | |
paul@70 | 413 | def __init__(self): |
paul@70 | 414 | self.max_level = self.level = 0 |
paul@71 | 415 | self.in_heading = False |
paul@72 | 416 | self.held_anchors = [] |
paul@76 | 417 | self.macro = None |
paul@77 | 418 | self.sections = [] |
paul@70 | 419 | |
paul@70 | 420 | def translate_marker(self, marker): |
paul@70 | 421 | |
paul@70 | 422 | "Translate the given 'marker' to a suitable Moin representation." |
paul@70 | 423 | |
paul@70 | 424 | return " " * len(marker) + markers[marker[-1]] |
paul@70 | 425 | |
paul@70 | 426 | def translate_cellsep(self, cellsep): |
paul@70 | 427 | |
paul@70 | 428 | "Translate the given 'cellsep' to a suitable Moin representation." |
paul@70 | 429 | |
paul@70 | 430 | return cellseps[cellsep] |
paul@70 | 431 | |
paul@70 | 432 | def translate_cell(self, cellsep, text): |
paul@15 | 433 | |
paul@70 | 434 | "Using 'cellsep', translate the cell 'text'." |
paul@70 | 435 | |
paul@70 | 436 | return cellextra[cellsep] + self.parse_text(text).strip() + cellextra[cellsep] |
paul@70 | 437 | |
paul@70 | 438 | def translate_content_match(self, match): |
paul@70 | 439 | |
paul@70 | 440 | "Translate the content described by the given 'match', returning a string." |
paul@70 | 441 | |
paul@70 | 442 | if match.group("monotext"): |
paul@70 | 443 | self.enter_section(); self.leave_section() |
paul@70 | 444 | return "{{{%s}}}" % match.group("monotext") |
paul@11 | 445 | |
paul@70 | 446 | elif match.group("linktext"): |
paul@70 | 447 | parts = match.group("linktext").split("|") |
paul@70 | 448 | |
paul@70 | 449 | # NOTE: Proper detection of external links required. |
paul@70 | 450 | |
paul@70 | 451 | if len(parts) == 1: |
paul@70 | 452 | label, target, title = None, parts[0], None |
paul@70 | 453 | elif len(parts) == 2: |
paul@70 | 454 | (label, target), title = parts, None |
paul@70 | 455 | else: |
paul@70 | 456 | label, target, title = parts |
paul@39 | 457 | |
paul@70 | 458 | target = target.strip() |
paul@70 | 459 | |
paul@70 | 460 | # Look for namespace links and rewrite them. |
paul@70 | 461 | |
paul@70 | 462 | if target.find(":") != -1: |
paul@70 | 463 | prefix = "" |
paul@70 | 464 | space, rest = target.split(":", 1) |
paul@70 | 465 | if space not in URL_SCHEMES: |
paul@85 | 466 | rest = get_page_title(rest) |
paul@70 | 467 | target = "%s/%s" % (space, rest) |
paul@70 | 468 | |
paul@70 | 469 | # Detect anchors. |
paul@70 | 470 | |
paul@70 | 471 | elif target.startswith("#"): |
paul@70 | 472 | prefix = "" |
paul@70 | 473 | |
paul@70 | 474 | # Detect attachments. |
paul@70 | 475 | |
paul@70 | 476 | elif target.startswith("^"): |
paul@70 | 477 | prefix = "attachment:" |
paul@70 | 478 | |
paul@70 | 479 | # Link to other pages within a space. |
paul@11 | 480 | |
paul@70 | 481 | else: |
paul@70 | 482 | prefix = "../" |
paul@70 | 483 | |
paul@70 | 484 | # Make the link tidier by making a target if none was given. |
paul@70 | 485 | |
paul@70 | 486 | if not label: |
paul@70 | 487 | label = target |
paul@42 | 488 | |
paul@85 | 489 | target = get_page_title(target) |
paul@85 | 490 | |
paul@70 | 491 | if not label and not title: |
paul@70 | 492 | return "[[%s%s]]" % (prefix, target) |
paul@70 | 493 | elif not title: |
paul@70 | 494 | return "[[%s%s|%s]]" % (prefix, target, label) |
paul@70 | 495 | else: |
paul@70 | 496 | return "[[%s%s|%s|title=%s]]" % (prefix, target, label, title) |
paul@70 | 497 | |
paul@70 | 498 | elif match.group("imagetext"): |
paul@70 | 499 | parts = match.group("imagetext").split("|") |
paul@70 | 500 | |
paul@70 | 501 | # NOTE: Proper detection of external links required. |
paul@70 | 502 | |
paul@70 | 503 | if parts[0].startswith("http"): |
paul@70 | 504 | prefix = "" |
paul@70 | 505 | else: |
paul@70 | 506 | prefix = "attachment:" |
paul@42 | 507 | |
paul@70 | 508 | # NOTE: Proper options conversion required. |
paul@70 | 509 | |
paul@70 | 510 | if len(parts) == 1: |
paul@70 | 511 | return "{{%s%s}}" % (prefix, parts[0]) |
paul@70 | 512 | else: |
paul@70 | 513 | return "{{%s%s|%s}}" % (prefix, parts[0], parts[1]) |
paul@70 | 514 | |
paul@71 | 515 | elif match.group("macro"): |
paul@71 | 516 | macro_name = match.group("macro") |
paul@72 | 517 | if macrotypes.has_key(macro_name): |
paul@71 | 518 | argname = macroargs.get(macro_name) |
paul@72 | 519 | result = macrotypes[macro_name] % { |
paul@71 | 520 | "args" : quote_macro_argument((argname and ("%s=" % argname) or "") + match.group("options")) |
paul@71 | 521 | } |
paul@72 | 522 | if not self.forbids_macros(): |
paul@72 | 523 | return result |
paul@72 | 524 | if macro_name == "anchor": |
paul@72 | 525 | self.held_anchors.append(result) |
paul@72 | 526 | return "" |
paul@71 | 527 | |
paul@70 | 528 | elif match.group("italictext"): |
paul@70 | 529 | return "''%s''" % self.translate_content(match.group("italictext")) |
paul@70 | 530 | |
paul@70 | 531 | elif match.group("boldtext"): |
paul@70 | 532 | return "'''%s'''" % self.translate_content(match.group("boldtext")) |
paul@70 | 533 | |
paul@70 | 534 | elif match.group("deltext"): |
paul@70 | 535 | return "--(%s)--" % self.translate_content(match.group("deltext")) |
paul@70 | 536 | |
paul@70 | 537 | elif match.group("underlinetext"): |
paul@70 | 538 | return "__%s__" % self.translate_content(match.group("underlinetext")) |
paul@70 | 539 | |
paul@70 | 540 | elif match.group("subtext"): |
paul@70 | 541 | return ",,%s,," % self.translate_content(match.group("subtext")) |
paul@11 | 542 | |
paul@70 | 543 | else: |
paul@70 | 544 | return self.translate_text(match.group()) |
paul@70 | 545 | |
paul@70 | 546 | def translate_text(self, s, preformatted=False): |
paul@70 | 547 | |
paul@70 | 548 | "Translate the plain text string 's', converting notation." |
paul@70 | 549 | |
paul@70 | 550 | for before, after in preformatted and preformatted_notation_mapping or notation_mapping: |
paul@70 | 551 | s = s.replace(before, after) |
paul@70 | 552 | return s |
paul@70 | 553 | |
paul@77 | 554 | def translate_content(self, text): |
paul@70 | 555 | |
paul@70 | 556 | """ |
paul@70 | 557 | Return a translation of the given 'text'. If the optional 'sectiontype' is |
paul@70 | 558 | specified, the translation may be modified to a form appropriate to the |
paul@70 | 559 | section being translated. |
paul@70 | 560 | """ |
paul@70 | 561 | |
paul@70 | 562 | parts = [] |
paul@77 | 563 | preformatted = self.is_preformatted() |
paul@11 | 564 | |
paul@70 | 565 | last = 0 |
paul@70 | 566 | for match in content_regexp.finditer(text): |
paul@70 | 567 | start, end = match.span() |
paul@70 | 568 | parts.append(self.translate_text(text[last:start], preformatted)) |
paul@70 | 569 | |
paul@70 | 570 | # Handle unformatted sections. |
paul@70 | 571 | |
paul@77 | 572 | if self.sections and self.sections[-1] in ("code", "noformat"): |
paul@70 | 573 | parts.append(match.group()) |
paul@70 | 574 | else: |
paul@70 | 575 | parts.append(self.translate_content_match(match)) |
paul@70 | 576 | |
paul@70 | 577 | last = end |
paul@70 | 578 | |
paul@70 | 579 | parts.append(self.translate_text(text[last:], preformatted)) |
paul@70 | 580 | return "".join(parts) |
paul@70 | 581 | |
paul@77 | 582 | def is_preformatted(self): |
paul@77 | 583 | return reduce(operator.or_, [x in preformatted_sectiontypes for x in self.sections], False) |
paul@77 | 584 | |
paul@70 | 585 | def translate_block(self, blocktype, blocktext): |
paul@70 | 586 | |
paul@70 | 587 | "Translate the block with the given 'blocktype' and 'blocktext'." |
paul@70 | 588 | |
paul@71 | 589 | if blocktype in headings: |
paul@71 | 590 | self.in_heading = True |
paul@72 | 591 | self.held_anchors = [] |
paul@71 | 592 | |
paul@70 | 593 | parts = [] |
paul@42 | 594 | |
paul@70 | 595 | # Translate headings and blockquotes. |
paul@70 | 596 | |
paul@70 | 597 | if blocktypes.has_key(blocktype): |
paul@82 | 598 | text = self.parse_text(blocktext) |
paul@72 | 599 | for anchor in self.held_anchors: |
paul@72 | 600 | parts.append(anchor) |
paul@72 | 601 | parts.append(blocktypes[blocktype] % text) |
paul@70 | 602 | |
paul@70 | 603 | # Translate list items. |
paul@70 | 604 | |
paul@70 | 605 | elif blocktype == "list": |
paul@70 | 606 | for listmarker, listitem in get_list_items(blocktext): |
paul@82 | 607 | parts.append("%s %s" % (self.translate_marker(listmarker), self.parse_text(listitem))) |
paul@70 | 608 | |
paul@70 | 609 | # Translate table items. |
paul@70 | 610 | |
paul@70 | 611 | elif blocktype == "table": |
paul@70 | 612 | |
paul@70 | 613 | # Enter the table. |
paul@70 | 614 | |
paul@70 | 615 | self.enter_section() |
paul@70 | 616 | |
paul@70 | 617 | table_parts = [] |
paul@42 | 618 | first = True |
paul@70 | 619 | |
paul@70 | 620 | for cellsep, columns in get_table_rows(blocktext): |
paul@42 | 621 | if not first: |
paul@70 | 622 | table_parts.append("==") |
paul@42 | 623 | else: |
paul@42 | 624 | first = False |
paul@70 | 625 | moinsep = self.translate_cellsep(cellsep) |
paul@70 | 626 | table_parts.append(moinsep.join([self.translate_cell(cellsep, column) for column in columns])) |
paul@70 | 627 | |
paul@70 | 628 | # Nest the section appropriately. |
paul@70 | 629 | |
paul@70 | 630 | opening, closing = self.nest_section() |
paul@42 | 631 | |
paul@70 | 632 | parts.append("%s#!table" % opening) |
paul@70 | 633 | parts += table_parts |
paul@70 | 634 | parts.append(closing) |
paul@11 | 635 | |
paul@70 | 636 | # Leave the table. |
paul@70 | 637 | |
paul@70 | 638 | self.leave_section() |
paul@70 | 639 | |
paul@70 | 640 | # Handle anonymous blocks. |
paul@11 | 641 | |
paul@11 | 642 | else: |
paul@82 | 643 | parts.append(self.parse_text(blocktext)) |
paul@70 | 644 | |
paul@71 | 645 | if blocktype in headings: |
paul@71 | 646 | self.in_heading = False |
paul@71 | 647 | |
paul@70 | 648 | return "\n".join(parts) |
paul@70 | 649 | |
paul@70 | 650 | def translate_section(self, sectiontype, options, text): |
paul@70 | 651 | |
paul@70 | 652 | """ |
paul@70 | 653 | Translate the section with the given 'sectiontype', 'options' and |
paul@70 | 654 | 'text'. |
paul@70 | 655 | """ |
paul@70 | 656 | |
paul@70 | 657 | parts = [] |
paul@70 | 658 | |
paul@70 | 659 | # Enter the section. |
paul@70 | 660 | |
paul@77 | 661 | self.enter_section(sectiontype) |
paul@70 | 662 | |
paul@77 | 663 | # Sections can contain other sections. |
paul@77 | 664 | |
paul@77 | 665 | section_content = self.parse_text(text.strip()) |
paul@70 | 666 | |
paul@70 | 667 | # Nest the section appropriately. |
paul@70 | 668 | |
paul@70 | 669 | opening, closing = self.nest_section() |
paul@77 | 670 | mointype = sectiontypes.get(sectiontype) |
paul@70 | 671 | |
paul@70 | 672 | parts.append("%s%s\n" % (opening, mointype or "")) |
paul@70 | 673 | if options: |
paul@70 | 674 | parts.append("## %s\n" % options) |
paul@70 | 675 | parts.append(section_content) |
paul@70 | 676 | parts.append("\n%s\n" % closing) |
paul@70 | 677 | |
paul@70 | 678 | # Leave the section. |
paul@70 | 679 | |
paul@70 | 680 | self.leave_section() |
paul@15 | 681 | |
paul@70 | 682 | return parts |
paul@70 | 683 | |
paul@77 | 684 | def enter_section(self, sectiontype=None): |
paul@70 | 685 | self.level += 1 |
paul@70 | 686 | self.max_level = max(self.level, self.max_level) |
paul@77 | 687 | self.sections.append(sectiontype) |
paul@70 | 688 | |
paul@70 | 689 | def leave_section(self): |
paul@70 | 690 | self.level -= 1 |
paul@70 | 691 | if not self.level: |
paul@70 | 692 | self.max_level = 0 |
paul@77 | 693 | self.sections.pop() |
paul@70 | 694 | |
paul@70 | 695 | def nest_section(self): |
paul@70 | 696 | level = 3 + self.max_level - self.level |
paul@70 | 697 | opening = "{" * level |
paul@70 | 698 | closing = "}" * level |
paul@70 | 699 | return opening, closing |
paul@15 | 700 | |
paul@70 | 701 | # General parsing. |
paul@70 | 702 | |
paul@82 | 703 | def parse_text(self, s, top=False): |
paul@70 | 704 | |
paul@70 | 705 | "Parse the content in the string 's', returning the translation." |
paul@70 | 706 | |
paul@70 | 707 | parts = [] |
paul@70 | 708 | |
paul@70 | 709 | # Control spacing between blocks and other blocks or sections. |
paul@70 | 710 | |
paul@70 | 711 | preceded_by_block = False |
paul@70 | 712 | |
paul@70 | 713 | for type, text in get_regions(s): |
paul@70 | 714 | |
paul@70 | 715 | # Handle list, heading, blockquote or anonymous blocks. |
paul@70 | 716 | |
paul@70 | 717 | if type is None: |
paul@78 | 718 | |
paul@78 | 719 | # Where the region is the same as the provided text, return |
paul@78 | 720 | # immediately. This is the base case of the recursive parsing |
paul@78 | 721 | # process. |
paul@78 | 722 | |
paul@82 | 723 | if text == s and not top: |
paul@82 | 724 | return self.translate_content(text) |
paul@78 | 725 | |
paul@78 | 726 | # Otherwise, obtain and translate the blocks. |
paul@78 | 727 | |
paul@42 | 728 | if preceded_by_block: |
paul@42 | 729 | parts.append("\n") |
paul@42 | 730 | |
paul@70 | 731 | first = True |
paul@70 | 732 | for blocktype, blocktext in get_blocks(text): |
paul@70 | 733 | if not first: |
paul@70 | 734 | parts.append("\n") |
paul@70 | 735 | else: |
paul@70 | 736 | first = False |
paul@70 | 737 | parts.append("%s" % self.translate_block(blocktype, blocktext)) |
paul@42 | 738 | |
paul@70 | 739 | if not first: |
paul@70 | 740 | preceded_by_block = True |
paul@42 | 741 | |
paul@70 | 742 | # Handle sections. |
paul@42 | 743 | |
paul@15 | 744 | else: |
paul@70 | 745 | sectiontype, options = type |
paul@70 | 746 | |
paul@70 | 747 | # Direct translations of sections. |
paul@70 | 748 | |
paul@70 | 749 | if sectiontypes.has_key(sectiontype): |
paul@70 | 750 | if preceded_by_block: |
paul@70 | 751 | parts.append("\n") |
paul@70 | 752 | |
paul@70 | 753 | parts += self.translate_section(sectiontype, options, text) |
paul@70 | 754 | preceded_by_block = True |
paul@39 | 755 | |
paul@78 | 756 | # Translations of macros acting as sections. |
paul@70 | 757 | |
paul@76 | 758 | elif macrotypes.has_key(sectiontype): |
paul@78 | 759 | |
paul@78 | 760 | # Prevent the production of macros in places they would |
paul@78 | 761 | # produce illegal Moin syntax. |
paul@78 | 762 | |
paul@76 | 763 | if not self.forbids_macros(): |
paul@76 | 764 | self.macro = sectiontype |
paul@76 | 765 | argname = macroargs.get(sectiontype) |
paul@76 | 766 | parts.append(macrotypes[sectiontype] % { |
paul@76 | 767 | "content" : quote_macro_argument(self.parse_text(text)), |
paul@76 | 768 | "args" : quote_macro_argument((argname and ("%s=" % argname) or "") + options) |
paul@76 | 769 | }) |
paul@76 | 770 | self.macro = None |
paul@78 | 771 | |
paul@78 | 772 | # Include the contents of section-based macros where the |
paul@78 | 773 | # macros themselves are not allowed. |
paul@78 | 774 | |
paul@76 | 775 | else: |
paul@76 | 776 | parts.append(self.translate_content(text)) |
paul@76 | 777 | |
paul@70 | 778 | preceded_by_block = False |
paul@70 | 779 | |
paul@70 | 780 | # Unrecognised sections. |
paul@70 | 781 | |
paul@70 | 782 | else: |
paul@70 | 783 | parts += self.translate_section(sectiontype, None, text) |
paul@70 | 784 | preceded_by_block = False |
paul@70 | 785 | |
paul@70 | 786 | return "".join(parts) |
paul@39 | 787 | |
paul@71 | 788 | def forbids_macros(self): |
paul@76 | 789 | return self.in_heading or self.macro |
paul@71 | 790 | |
paul@39 | 791 | def parse(s, out): |
paul@39 | 792 | |
paul@39 | 793 | "Parse the content in the string 's', writing a translation to 'out'." |
paul@39 | 794 | |
paul@70 | 795 | parser = ConfluenceParser() |
paul@82 | 796 | out.write(parser.parse_text(s, top=True)) |
paul@11 | 797 | |
paul@6 | 798 | if __name__ == "__main__": |
paul@62 | 799 | s = codecs.getreader("utf-8")(sys.stdin).read() |
paul@41 | 800 | out = codecs.getwriter("utf-8")(sys.stdout) |
paul@41 | 801 | parse(s, out) |
paul@6 | 802 | |
paul@6 | 803 | # vim: tabstop=4 expandtab shiftwidth=4 |