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@7 | 31 | 3. Each block is then parsed. |
paul@7 | 32 | """ |
paul@7 | 33 | |
paul@35 | 34 | from common import * |
paul@6 | 35 | import re |
paul@25 | 36 | import sys |
paul@19 | 37 | |
paul@6 | 38 | # Section extraction. |
paul@6 | 39 | |
paul@19 | 40 | sections_regexp_str = r"(?<!{){(?P<type>[^-_*+{}\n:]+)(:[^}\n]+)?}.*?{(?P=type)}" |
paul@6 | 41 | sections_regexp = re.compile(sections_regexp_str, re.DOTALL | re.MULTILINE) |
paul@6 | 42 | |
paul@6 | 43 | def get_regions(s): |
paul@6 | 44 | |
paul@6 | 45 | """ |
paul@6 | 46 | Return a list of regions from 's'. Each region is specified using a tuple of |
paul@6 | 47 | the form (type, text). |
paul@6 | 48 | """ |
paul@6 | 49 | |
paul@6 | 50 | last = 0 |
paul@6 | 51 | regions = [] |
paul@6 | 52 | for match in sections_regexp.finditer(s): |
paul@6 | 53 | start, end = match.span() |
paul@6 | 54 | regions.append((None, s[last:start])) |
paul@6 | 55 | regions.append(get_section_details(s[start:end])) |
paul@6 | 56 | last = end |
paul@6 | 57 | regions.append((None, s[last:])) |
paul@6 | 58 | return regions |
paul@6 | 59 | |
paul@7 | 60 | # Section inspection. |
paul@7 | 61 | |
paul@15 | 62 | section_regexp_str = r"{(?P<sectiontype>[^\n:]*?)(?::(?P<options>.*?))?}(?P<section>.*){(?P=sectiontype)}" |
paul@7 | 63 | section_regexp = re.compile(section_regexp_str, re.DOTALL | re.MULTILINE) |
paul@7 | 64 | |
paul@6 | 65 | def get_section_details(s): |
paul@6 | 66 | |
paul@7 | 67 | "Return the details of a section 's' in the form (type, text)." |
paul@6 | 68 | |
paul@6 | 69 | match = section_regexp.match(s) |
paul@6 | 70 | if match: |
paul@15 | 71 | return (match.group("sectiontype"), match.group("options")), match.group("section") |
paul@6 | 72 | else: |
paul@6 | 73 | return None, s |
paul@6 | 74 | |
paul@14 | 75 | # Heading, table and list extraction. |
paul@7 | 76 | |
paul@36 | 77 | list_regexp_str = r"^\s*(?P<listtype>[*#-])[*#-]*.*(\n\s*(?P=listtype).*?)*(?:\n|$)" |
paul@39 | 78 | table_regexp_str = r"^((?P<celltype>[|]{1,2})((.|\n(?!\n))+?(?P=celltype))+(\n|$))+" |
paul@14 | 79 | blocktext_regexp_str = r"^(?P<type>h\d|bq)\.\s+(?P<text>.*)$" |
paul@7 | 80 | |
paul@14 | 81 | blockelement_regexp = re.compile( |
paul@14 | 82 | "(" + list_regexp_str + ")" |
paul@14 | 83 | "|" |
paul@14 | 84 | "(" + table_regexp_str + ")" |
paul@14 | 85 | "|" |
paul@14 | 86 | "(" + blocktext_regexp_str + ")", |
paul@14 | 87 | re.MULTILINE |
paul@14 | 88 | ) |
paul@14 | 89 | |
paul@14 | 90 | def get_block_elements(s): |
paul@7 | 91 | |
paul@7 | 92 | """ |
paul@14 | 93 | Extract headings, tables and lists from the given string 's'. |
paul@7 | 94 | """ |
paul@7 | 95 | |
paul@7 | 96 | last = 0 |
paul@7 | 97 | blocks = [] |
paul@14 | 98 | for match in blockelement_regexp.finditer(s): |
paul@7 | 99 | start, end = match.span() |
paul@14 | 100 | matchtype = match.group("listtype") and "list" or match.group("celltype") and "table" or match.group("type") |
paul@7 | 101 | blocks.append((None, s[last:start])) |
paul@14 | 102 | blocks.append((matchtype, match.group("text") or s[start:end])) |
paul@7 | 103 | last = end |
paul@7 | 104 | blocks.append((None, s[last:])) |
paul@7 | 105 | return blocks |
paul@7 | 106 | |
paul@7 | 107 | # Block extraction. |
paul@7 | 108 | |
paul@7 | 109 | block_regexp_str = r"^(?:\s*\n)+" |
paul@7 | 110 | block_regexp = re.compile(block_regexp_str, re.MULTILINE) |
paul@7 | 111 | |
paul@7 | 112 | def get_basic_blocks(s): |
paul@7 | 113 | |
paul@7 | 114 | """ |
paul@7 | 115 | Return blocks from the given string 's' by splitting the text on blank lines |
paul@7 | 116 | and eliminating those lines. |
paul@7 | 117 | """ |
paul@7 | 118 | |
paul@7 | 119 | return [b for b in block_regexp.split(s) if b.strip()] |
paul@7 | 120 | |
paul@7 | 121 | # Block inspection. |
paul@7 | 122 | |
paul@7 | 123 | def get_blocks(s): |
paul@7 | 124 | |
paul@7 | 125 | """ |
paul@7 | 126 | Return blocks from the given string 's', inspecting the basic blocks and |
paul@7 | 127 | generating additional block-level text where appropriate. |
paul@7 | 128 | """ |
paul@7 | 129 | |
paul@7 | 130 | blocks = [] |
paul@7 | 131 | |
paul@14 | 132 | for blocktype, blocktext in get_block_elements(s): |
paul@7 | 133 | |
paul@14 | 134 | # Collect heading, list and table blocks. |
paul@7 | 135 | |
paul@7 | 136 | if blocktype is not None: |
paul@7 | 137 | blocks.append((blocktype, blocktext)) |
paul@7 | 138 | |
paul@7 | 139 | # Attempt to find new subblocks in other regions. |
paul@7 | 140 | |
paul@7 | 141 | else: |
paul@7 | 142 | for block in get_basic_blocks(blocktext): |
paul@14 | 143 | blocks.append((None, block)) |
paul@7 | 144 | |
paul@7 | 145 | return blocks |
paul@7 | 146 | |
paul@14 | 147 | # List item inspection. |
paul@14 | 148 | |
paul@17 | 149 | listitem_regexp_str = r"^(?P<marker> *[-*#]+)\s*(?P<text>.*)$" |
paul@7 | 150 | listitem_regexp = re.compile(listitem_regexp_str, re.MULTILINE) |
paul@7 | 151 | |
paul@14 | 152 | def get_list_items(text): |
paul@14 | 153 | |
paul@14 | 154 | "Return a list of (marker, text) tuples for the given list 'text'." |
paul@14 | 155 | |
paul@14 | 156 | items = [] |
paul@14 | 157 | |
paul@14 | 158 | for match in listitem_regexp.finditer(text): |
paul@14 | 159 | items.append((match.group("marker"), match.group("text"))) |
paul@14 | 160 | |
paul@14 | 161 | return items |
paul@14 | 162 | |
paul@36 | 163 | # Content inspection. |
paul@14 | 164 | |
paul@19 | 165 | monospace_regexp_str = r"{{(?P<monotext>.*?)}}" |
paul@36 | 166 | link_regexp_str = r"[[](?P<linktext>.*?)]" |
paul@38 | 167 | image_regexp_str = r"!(?P<imagetext>\w.*?)!" |
paul@36 | 168 | |
paul@36 | 169 | # Word-dependent patterns. |
paul@36 | 170 | # Here, the unbracketed markers must test for the absence of surrounding word |
paul@36 | 171 | # characters. |
paul@36 | 172 | |
paul@36 | 173 | italic_regexp_str = r"(?:(?<!\w)_|\{_\})(?P<italictext>.*?)(?:_(?!\w)|\{_\})" |
paul@36 | 174 | bold_regexp_str = r"(?:(?<!\w)\*|\{\*\})(?P<boldtext>.*?)(?:\*(?!\w)|\{\*\})" |
paul@36 | 175 | del_regexp_str = r"(?:(?<!\w)-|\{-\})(?P<deltext>.*?)(?:-(?!\w)|\{-\})" |
paul@36 | 176 | underline_regexp_str = r"(?:(?<!\w)\+|\{\+\})(?P<underlinetext>.*?)(?:\+(?!\w)|\{\+\})" |
paul@36 | 177 | sub_regexp_str = r"(?:(?<!\w)~|\{~\})(?P<subtext>.*?)(?:~(?!\w)|\{~\})" |
paul@16 | 178 | |
paul@16 | 179 | content_regexp_str = ( |
paul@19 | 180 | "(" + monospace_regexp_str + ")" |
paul@19 | 181 | "|" |
paul@14 | 182 | "(" + link_regexp_str + ")" |
paul@14 | 183 | "|" |
paul@14 | 184 | "(" + image_regexp_str + ")" |
paul@36 | 185 | "|" |
paul@36 | 186 | "(" + italic_regexp_str + ")" |
paul@36 | 187 | "|" |
paul@36 | 188 | "(" + bold_regexp_str + ")" |
paul@36 | 189 | "|" |
paul@36 | 190 | "(" + del_regexp_str + ")" |
paul@36 | 191 | "|" |
paul@36 | 192 | "(" + underline_regexp_str + ")" |
paul@36 | 193 | "|" |
paul@36 | 194 | "(" + sub_regexp_str + ")" |
paul@16 | 195 | ) |
paul@16 | 196 | |
paul@36 | 197 | # Table row inspection. |
paul@36 | 198 | |
paul@36 | 199 | cellsep_regexp_str = r"(?P<celltype>[|]{1,2})" |
paul@36 | 200 | |
paul@16 | 201 | table_content_regexp_str = ( |
paul@16 | 202 | content_regexp_str + |
paul@14 | 203 | "|" |
paul@14 | 204 | "(" + cellsep_regexp_str + ")" |
paul@14 | 205 | ) |
paul@14 | 206 | |
paul@16 | 207 | content_regexp = re.compile(content_regexp_str) |
paul@16 | 208 | table_content_regexp = re.compile(table_content_regexp_str) |
paul@16 | 209 | |
paul@39 | 210 | # Notation conversion. |
paul@39 | 211 | |
paul@39 | 212 | notation_mapping = [ |
paul@39 | 213 | (r"\!", "!"), |
paul@39 | 214 | (r"\-", "-"), |
paul@39 | 215 | (r"\\""\n", "<<BR>> "), |
paul@39 | 216 | (r"\\ ", " "), |
paul@39 | 217 | ] |
paul@39 | 218 | |
paul@39 | 219 | # Translation helpers. |
paul@39 | 220 | |
paul@39 | 221 | markers = { |
paul@39 | 222 | "*" : "*", |
paul@39 | 223 | "#" : "1.", |
paul@39 | 224 | "-" : "*", |
paul@39 | 225 | } |
paul@39 | 226 | |
paul@39 | 227 | def translate_marker(marker): |
paul@39 | 228 | |
paul@39 | 229 | "Translate the given 'marker' to a suitable Moin representation." |
paul@39 | 230 | |
paul@39 | 231 | return " " * len(marker) + markers[marker[-1]] |
paul@39 | 232 | |
paul@39 | 233 | cellseps = { |
paul@39 | 234 | "|" : "||", |
paul@39 | 235 | "||" : "||", |
paul@39 | 236 | } |
paul@39 | 237 | |
paul@39 | 238 | cellextra = { |
paul@39 | 239 | "|" : "", |
paul@39 | 240 | "||" : "'''", |
paul@39 | 241 | } |
paul@39 | 242 | |
paul@39 | 243 | def translate_cellsep(cellsep): |
paul@39 | 244 | |
paul@39 | 245 | "Translate the given 'cellsep' to a suitable Moin representation." |
paul@39 | 246 | |
paul@39 | 247 | return cellseps[cellsep] |
paul@39 | 248 | |
paul@39 | 249 | def translate_cell(cellsep, text): |
paul@39 | 250 | |
paul@39 | 251 | "Using 'cellsep', translate the cell 'text'." |
paul@39 | 252 | |
paul@39 | 253 | return cellextra[cellsep] + parse_text(text) + cellextra[cellsep] |
paul@39 | 254 | |
paul@16 | 255 | def translate_content_match(match): |
paul@16 | 256 | |
paul@16 | 257 | "Translate the content described by the given 'match', returning a string." |
paul@16 | 258 | |
paul@19 | 259 | if match.group("monotext"): |
paul@19 | 260 | return "{{{%s}}}" % match.group("monotext") |
paul@19 | 261 | |
paul@19 | 262 | elif match.group("linktext"): |
paul@16 | 263 | parts = match.group("linktext").split("|") |
paul@16 | 264 | |
paul@16 | 265 | # NOTE: Proper detection of external links required. |
paul@16 | 266 | |
paul@19 | 267 | if len(parts) == 1: |
paul@22 | 268 | label, target, title = None, parts[0], None |
paul@19 | 269 | elif len(parts) == 2: |
paul@22 | 270 | (label, target), title = parts, None |
paul@19 | 271 | else: |
paul@19 | 272 | label, target, title = parts |
paul@19 | 273 | |
paul@21 | 274 | target = target.strip() |
paul@21 | 275 | |
paul@22 | 276 | # Look for namespace links and rewrite them. |
paul@22 | 277 | |
paul@19 | 278 | if target.find(":") != -1: |
paul@16 | 279 | prefix = "" |
paul@19 | 280 | space, rest = target.split(":", 1) |
paul@19 | 281 | if space not in URL_SCHEMES: |
paul@19 | 282 | target = "%s/%s" % (space, rest) |
paul@22 | 283 | |
paul@22 | 284 | # Detect anchors. |
paul@22 | 285 | |
paul@19 | 286 | elif target.startswith("#"): |
paul@16 | 287 | prefix = "" |
paul@22 | 288 | |
paul@22 | 289 | # Detect attachments. |
paul@22 | 290 | |
paul@19 | 291 | elif target.startswith("^"): |
paul@16 | 292 | prefix = "attachment:" |
paul@22 | 293 | |
paul@22 | 294 | # Link to other pages within a space. |
paul@22 | 295 | |
paul@16 | 296 | else: |
paul@16 | 297 | prefix = "../" |
paul@16 | 298 | |
paul@22 | 299 | # Make the link tidier by making a target if none was given. |
paul@22 | 300 | |
paul@22 | 301 | if not label: |
paul@22 | 302 | label = target |
paul@22 | 303 | |
paul@22 | 304 | if not label and not title: |
paul@19 | 305 | return "[[%s%s]]" % (prefix, target) |
paul@22 | 306 | elif not title: |
paul@19 | 307 | return "[[%s%s|%s]]" % (prefix, target, label) |
paul@16 | 308 | else: |
paul@19 | 309 | return "[[%s%s|%s|title=%s]]" % (prefix, target, label, title) |
paul@16 | 310 | |
paul@16 | 311 | elif match.group("imagetext"): |
paul@16 | 312 | parts = match.group("imagetext").split("|") |
paul@16 | 313 | |
paul@16 | 314 | # NOTE: Proper detection of external links required. |
paul@16 | 315 | |
paul@16 | 316 | if parts[0].startswith("http"): |
paul@16 | 317 | prefix = "" |
paul@16 | 318 | else: |
paul@16 | 319 | prefix = "attachment:" |
paul@16 | 320 | |
paul@16 | 321 | # NOTE: Proper options conversion required. |
paul@16 | 322 | |
paul@16 | 323 | if len(parts) == 1: |
paul@16 | 324 | return "{{%s%s}}" % (prefix, parts[0]) |
paul@16 | 325 | else: |
paul@19 | 326 | return "{{%s%s|%s}}" % (prefix, parts[0], parts[1]) |
paul@16 | 327 | |
paul@36 | 328 | elif match.group("italictext"): |
paul@36 | 329 | return "''%s''" % translate_content(match.group("italictext")) |
paul@36 | 330 | |
paul@36 | 331 | elif match.group("boldtext"): |
paul@36 | 332 | return "'''%s'''" % translate_content(match.group("boldtext")) |
paul@36 | 333 | |
paul@36 | 334 | elif match.group("deltext"): |
paul@36 | 335 | return "--(%s)--" % translate_content(match.group("deltext")) |
paul@36 | 336 | |
paul@36 | 337 | elif match.group("underlinetext"): |
paul@36 | 338 | return "__%s__" % translate_content(match.group("underlinetext")) |
paul@36 | 339 | |
paul@36 | 340 | elif match.group("subtext"): |
paul@36 | 341 | return ",,%s,," % translate_content(match.group("subtext")) |
paul@36 | 342 | |
paul@16 | 343 | else: |
paul@39 | 344 | return translate_text(match.group()) |
paul@39 | 345 | |
paul@39 | 346 | def translate_text(s): |
paul@39 | 347 | |
paul@39 | 348 | "Translate the plain text string 's', converting notation." |
paul@39 | 349 | |
paul@39 | 350 | for before, after in notation_mapping: |
paul@39 | 351 | s = s.replace(before, after) |
paul@39 | 352 | return s |
paul@16 | 353 | |
paul@36 | 354 | def translate_content(text, sectiontype=None): |
paul@36 | 355 | |
paul@36 | 356 | """ |
paul@36 | 357 | Return a translation of the given 'text'. If the optional 'sectiontype' is |
paul@36 | 358 | specified, the translation may be modified to a form appropriate to the |
paul@36 | 359 | section being translated. |
paul@36 | 360 | """ |
paul@36 | 361 | |
paul@36 | 362 | parts = [] |
paul@36 | 363 | |
paul@36 | 364 | last = 0 |
paul@36 | 365 | for match in content_regexp.finditer(text): |
paul@36 | 366 | start, end = match.span() |
paul@39 | 367 | parts.append(translate_text(text[last:start])) |
paul@36 | 368 | |
paul@36 | 369 | # Handle unformatted sections. |
paul@36 | 370 | |
paul@36 | 371 | if sectiontype in ("code", "noformat"): |
paul@36 | 372 | parts.append(match.group()) |
paul@36 | 373 | else: |
paul@36 | 374 | parts.append(translate_content_match(match)) |
paul@36 | 375 | |
paul@36 | 376 | last = end |
paul@36 | 377 | |
paul@39 | 378 | parts.append(translate_text(text[last:])) |
paul@36 | 379 | return "".join(parts) |
paul@36 | 380 | |
paul@39 | 381 | def translate_block(blocktype, blocktext): |
paul@39 | 382 | |
paul@39 | 383 | "Translate the block with the given 'blocktype' and 'blocktext'." |
paul@39 | 384 | |
paul@39 | 385 | parts = [] |
paul@39 | 386 | |
paul@39 | 387 | # Translate headings and blockquotes. |
paul@39 | 388 | |
paul@39 | 389 | if blocktypes.has_key(blocktype): |
paul@39 | 390 | parts.append(blocktypes[blocktype] % blocktext) |
paul@39 | 391 | |
paul@39 | 392 | # Translate list items. |
paul@39 | 393 | |
paul@39 | 394 | elif blocktype == "list": |
paul@39 | 395 | for listmarker, listitem in get_list_items(blocktext): |
paul@39 | 396 | parts.append("%s %s" % (translate_marker(listmarker), translate_content(listitem))) |
paul@39 | 397 | |
paul@39 | 398 | # Translate table items. |
paul@39 | 399 | |
paul@39 | 400 | elif blocktype == "table": |
paul@39 | 401 | parts.append("{{{#!table") |
paul@39 | 402 | first = True |
paul@39 | 403 | for cellsep, columns in get_table_rows(blocktext): |
paul@39 | 404 | if not first: |
paul@39 | 405 | parts.append("==") |
paul@39 | 406 | else: |
paul@39 | 407 | first = False |
paul@39 | 408 | moinsep = translate_cellsep(cellsep) |
paul@39 | 409 | parts.append(moinsep.join([translate_cell(cellsep, column) for column in columns])) |
paul@39 | 410 | parts.append("}}}") |
paul@39 | 411 | |
paul@39 | 412 | # Handle anonymous blocks. |
paul@39 | 413 | |
paul@39 | 414 | else: |
paul@39 | 415 | parts.append(translate_content(blocktext).rstrip()) |
paul@39 | 416 | |
paul@39 | 417 | return "\n".join(parts) + "\n" |
paul@39 | 418 | |
paul@14 | 419 | def get_table_rows(text): |
paul@14 | 420 | |
paul@14 | 421 | "Return a list of (cellsep, columns) tuples for the given table 'text'." |
paul@14 | 422 | |
paul@14 | 423 | rows = [] |
paul@14 | 424 | |
paul@39 | 425 | for row in text.split("|\n"): |
paul@39 | 426 | if not row: |
paul@39 | 427 | break |
paul@39 | 428 | |
paul@39 | 429 | row += "|" |
paul@14 | 430 | cellsep = None |
paul@14 | 431 | columns = [""] |
paul@14 | 432 | last = 0 |
paul@39 | 433 | for match in table_content_regexp.finditer(row): |
paul@14 | 434 | start, end = match.span() |
paul@39 | 435 | columns[-1] += row[last:start] |
paul@14 | 436 | |
paul@14 | 437 | if match.group("celltype"): |
paul@14 | 438 | if cellsep is None: |
paul@14 | 439 | cellsep = match.group("celltype") |
paul@14 | 440 | columns.append("") |
paul@14 | 441 | else: |
paul@16 | 442 | columns[-1] += match.group() |
paul@14 | 443 | |
paul@14 | 444 | last = end |
paul@14 | 445 | |
paul@39 | 446 | columns[-1] += row[last:] |
paul@14 | 447 | |
paul@14 | 448 | if cellsep: |
paul@14 | 449 | rows.append((cellsep, columns[1:-1])) |
paul@14 | 450 | |
paul@14 | 451 | return rows |
paul@14 | 452 | |
paul@15 | 453 | sectiontypes = { |
paul@15 | 454 | "code" : "", |
paul@15 | 455 | "noformat" : "", |
paul@15 | 456 | "quote" : "", |
paul@15 | 457 | "info" : "wiki important", |
paul@15 | 458 | "note" : "wiki caution", |
paul@15 | 459 | "tip" : "wiki tip", |
paul@15 | 460 | "warning" : "wiki warning", |
paul@15 | 461 | } |
paul@15 | 462 | |
paul@15 | 463 | # General parsing. |
paul@15 | 464 | |
paul@39 | 465 | def parse_text(s): |
paul@11 | 466 | |
paul@39 | 467 | "Parse the content in the string 's', returning the translation." |
paul@39 | 468 | |
paul@39 | 469 | parts = [] |
paul@11 | 470 | |
paul@11 | 471 | for type, text in get_regions(s): |
paul@11 | 472 | |
paul@11 | 473 | # Handle list, heading, blockquote or anonymous blocks. |
paul@11 | 474 | |
paul@11 | 475 | if type is None: |
paul@11 | 476 | for blocktype, blocktext in get_blocks(text): |
paul@39 | 477 | parts.append("%s\n" % translate_block(blocktype, blocktext)) |
paul@11 | 478 | |
paul@11 | 479 | # Handle sections. |
paul@11 | 480 | |
paul@11 | 481 | else: |
paul@15 | 482 | sectiontype, options = type |
paul@15 | 483 | |
paul@15 | 484 | # Direct translations of sections. |
paul@15 | 485 | |
paul@15 | 486 | mointype = sectiontypes.get(sectiontype) |
paul@15 | 487 | if mointype: |
paul@39 | 488 | parts.append("{{{#!%s\n" % mointype) |
paul@15 | 489 | if options: |
paul@39 | 490 | parts.append("## %s\n" % options) |
paul@15 | 491 | else: |
paul@39 | 492 | parts.append("{{{") |
paul@39 | 493 | parts.append(translate_content(text, sectiontype)) |
paul@39 | 494 | parts.append("}}}\n") |
paul@39 | 495 | |
paul@39 | 496 | return "".join(parts) |
paul@39 | 497 | |
paul@39 | 498 | def parse(s, out): |
paul@39 | 499 | |
paul@39 | 500 | "Parse the content in the string 's', writing a translation to 'out'." |
paul@39 | 501 | |
paul@39 | 502 | out.write(parse_text(s)) |
paul@11 | 503 | |
paul@6 | 504 | if __name__ == "__main__": |
paul@6 | 505 | s = sys.stdin.read() |
paul@35 | 506 | parse(s, sys.stdout) |
paul@6 | 507 | |
paul@6 | 508 | # vim: tabstop=4 expandtab shiftwidth=4 |