def handleText(man, line, suffix=' '): # init RE_WORDS if man.words_re == None: text = "" i = 0 for (fun, wre) in man.words: if text <> "": text = text + "|" text = text + "(?P<a" + str(i) + ">" + wre + ")" i = i + 1 man.words_re = re.compile(text) # look in line match = man.words_re.search(line) while match: idx = int(match.lastgroup[1:]) fun, wre = man.words[idx] word = line[:match.start()] if word: man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, man.factory.makeWord(word))) line = line[match.end():] fun(man, match) match = man.words_re.search(line) # end of line man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, man.factory.makeWord(line + suffix)))
def handle_mailto_link(man, match): url = match.group('email_auto') man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW_LINK, doc.Link("mailto:" + url))) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word(url))) man.send(doc.CloseEvent(doc.L_WORD, doc.ID_END_LINK, "link"))
def new_row(man, match): # build the row table = doc.Table() row_kind = doc.TAB_HEADER row_node = doc.Row(row_kind) table.content.append(row_node) use_par_style(row_node, match.group(1)) man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW_ROW, table)) row = match.group("row") while row: # scan the cell cell_node = doc.Cell(doc.TAB_NORMAL) while row: kind = doc.TAB_NORMAL if row[0] == '_': cell_node.kind = doc.TAB_HEADER kind = doc.TAB_HEADER row = row[1:] continue elif len(row) >= 2: if row[0] == '\\' and row[1] >= '0' and row[1] <= '9': cell_node.setInfo(doc.INFO_HSPAN, int(row[1])) row = row[2:] continue elif row[0] == '/' and row[1] >= '0' and row[1] <= '9': cell_node.setInfo(doc.INFO_VSPAN, int(row[1])) row = row[2:] continue new_row = consume_par_style(cell_node, row) if row == new_row: break row = new_row # find end pref = "" match = TABLE_SEP.search(row) while match and match.group() == "==": p = row.find("))", match.end()) if p < 0: pref = pref + row[:match.end()] row = row[match.end():] else: pref = pref + row[:p + 2] row = row[p + 2:] match = TABLE_SEP.search(row) if match: cell = pref + row[:match.start()] row = row[match.start() + 1:] else: cell = pref + row row = '' # dump object if required man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW_CELL, cell_node)) tparser.handleText(man, cell) if cell_node.kind == doc.TAB_NORMAL: row_node.kind = doc.TAB_NORMAL
def handleImage(man, match): image = match.group("image") width = match.group("image_width") if width <> None: width = int(width) height = match.group("image_height") if height <> None: height = int(height) label = match.group("image_label") left = len(match.group("left")) right = len(match.group("right")) if left == right: if not left: align = doc.ALIGN_NONE else: align = doc.ALIGN_CENTER cls = doc.L_PAR elif left > right: align = doc.ALIGN_RIGHT else: align = doc.ALIGN_LEFT if align == doc.ALIGN_NONE: man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Image(image, width, height, label))) else: man.send( doc.ObjectEvent( doc.L_PAR, doc.ID_NEW, doc.EmbeddedImage(image, width, height, label, align)))
def new_link(man, match, ltag, utag): target = match.group(utag) label = match.group(ltag) link = doc.Link(target) # peal label if label and label[0] == '(': i = label.find(')') if i >= 0: link.setInfo(doc.INFO_CLASS, label[1:i]) label = label[i + 1:] tooltip = None if label and label[-1] == ')': i = label.rfind('(') if i >= 0: tooltip = label[i + 1:-1] label = label[:i] # build the link # tooltip are ignored now: what to do with this? if not label: label = target man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW_LINK, link)) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word(label))) man.send(doc.CloseEvent(doc.L_WORD, doc.ID_END_LINK, "link"))
def handle_ref(man, match): try: url = man.defs[match.group("id_ref")][0] man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW_LINK, doc.Link(url))) man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word(match.group("text_ref")))) man.send(doc.CloseEvent(doc.L_WORD, doc.ID_END_LINK, "link")) except KeyError: common.warn("reference %s is unknown!" % match.group("id_ref"))
def new_image(man, match): tmatch = match image = match.group('image') # alt pealing alt = None if image[-1] == ')': i = image.rfind('(') if i >= 0: alt = image[i + 1:-1] image = image[:i] # style pealing info = doc.Info() image = use_par_style(info, image) # dimension pealing match = WXH_RE.search(image) if match: info.setInfo(doc.INFO_WIDTH, int(match.group(1))) info.setInfo(doc.INFO_HEIGHT, int(match.group(2))) image = image[:match.start()] else: match = WH_RE.search(image) if match: info.setInfo(doc.INFO_WIDTH, int(match.group(1))) info.setInfo(doc.INFO_HEIGHT, int(match.group(2))) image = image[:match.start()] else: match = PERCENT_RE.search(image) if match: info.setInfo(doc.INFO_PERCENT_SIZE, int(match.group(1))) image = image[:match.start()] # beginning link if any link = tmatch.group('iurl') if link: man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW_LINK, doc.Link(link))) # build the image if info.getInfo(doc.INFO_ALIGN): node = doc.Image(image, None, None, alt) else: node = doc.EmbeddedImage(image, None, None, alt) node.mergeInfo(info) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, node)) # end link if link: man.send(doc.CloseEvent(doc.L_WORD, doc.ID_END_LINK, "link"))
def handle_term(man, word): """Handle a hashed word.""" res = man.doc.resolve_hash(word) if res == None: man.warn("hash term '#%s' is unknown!" % word) res = doc.Word(word) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, res))
def new_header(man, match): level = int(match.group(1)) header = doc.Header(level) use_par_style(header, match.group(2)) title = match.group("text") man.send(doc.ObjectEvent(doc.L_HEAD, doc.ID_NEW, header)) tparser.handleText(man, title) man.send(doc.Event(doc.L_HEAD, doc.ID_TITLE))
def handle_head_under(man, match, level, hrule): while man.top().getHeaderLevel() < 0: if isinstance(man.top(), doc.Par): title = man.top() if title.isEmpty() and hrule: man.send( doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, doc.HorizontalLine())) return man.pop() man.top().remove(title) hd = doc.Header(level) man.send(doc.ObjectEvent(doc.L_HEAD, doc.ID_NEW, hd)) man.send(doc.Event(doc.L_HEAD, doc.ID_TITLE)) hd.set_title(title) break man.pop()
def handle_header(man, match): level = len(match.group("level")) title = match.group("title") if title[-level:] == match.group("level"): title = title[:-level] man.send(doc.ObjectEvent(doc.L_HEAD, doc.ID_NEW, doc.Header(level))) tparser.handleText(man, title) man.send(doc.Event(doc.L_HEAD, doc.ID_TITLE))
def handleTerm(man, match): # record the term id = match.group("termid") de = match.group("termdef") if man.lexicon.exists(id): common.onError(man.message("term \"%s\" already defined!" % id)) return term = LexPar(id) man.lexicon.add(id, term) # finalize the parsing man.doc.addLabel(label(id), term) man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, term)) man.reparse(de)
def handleNewPar(man, match): man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_END, doc.Par()))
def handlePercent(man, match): man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word(match.group('percent'))))
def handleNonParsed(man, match): man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word(match.group('nonparsed')[:-2])))
def handleSmiley(man, match): image = doc.Image( man.doc.getVar("THOT_BASE") + "smileys/" + SMILEYS[match.group(0)]) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, image))
def handleEntity(man, match): glyph = doc.Glyph(ENTITIES[match.group(0)]) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, glyph))
def handleFootNote(man, match): man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW_STYLE, doc.FootNote()))
def handleDouble(man, match): man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word("#")))
def handleLexicon(man, match): if match.group("garbage"): common.onWarning(man.message("garbage after lexicon!")) man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, Lexicon(man.lexicon.lexicon)))
def processLink(man, target, text): man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW_LINK, doc.Link(target))) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, text)) man.send(doc.CloseEvent(doc.L_WORD, doc.ID_END_LINK, "link"))
def handleHLine(man, match): man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, doc.HorizontalLine()))
def __init__(self, man, block, re): self.old = man.getParser() man.setParser(self) self.block = block self.re = re man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, self.block))
def handleVar(man, match): id = match.group('varid') val = man.doc.getVar(id) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, man.factory.makeWord(val)))
def handleHeader(man, match): level = 6 - len(match.group(1)) title = match.group(2) man.send(doc.ObjectEvent(doc.L_HEAD, doc.ID_NEW, doc.Header(level))) tparser.handleText(man, title) man.send(doc.Event(doc.L_HEAD, doc.ID_TITLE))
def handleLineBreak(man, match): man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.LineBreak()))
def handleRow(man, match): table = doc.Table() if match.group(4) == '^': kind = doc.TAB_HEADER else: kind = doc.TAB_NORMAL row = doc.Row(kind) table.content.append(row) man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW_ROW, table)) row = match.group(1) object = None while row: # look kind if row[0] == '^': kind = doc.TAB_HEADER else: kind = doc.TAB_NORMAL row = row[1:] # find end pref = "" match = TABLE_SEP.search(row) while match and match.group() == "%%": p = row.find("%%", match.end()) if p < 0: pref = pref + row[:match.end()] row = row[match.end():] else: pref = pref + row[:p + 2] row = row[p + 2:] match = TABLE_SEP.search(row) if match: last = match.start() else: last = len(row) cell = pref + row[:last] row = row[last:] # dump object if required if cell == '' and object: #object.span += 1 object.setInfo(doc.INFO_HSPAN, object.getInfo(doc.INFO_HSPAN, 0) + 1) continue if object: man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW_CELL, object)) tparser.handleText(man, text) # strip and find align total = len(cell) cell = cell.lstrip() left = total - len(cell) total = len(cell) cell = cell.rstrip() right = total - len(cell) if left < right: align = doc.TAB_LEFT elif left > right: align = doc.TAB_RIGHT else: align = doc.TAB_CENTER # generate cell object = doc.Cell(kind, align, 1) text = cell # dump final object man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW_CELL, object)) tparser.handleText(man, text)
def handleRef(man, match): man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, man.factory.makeRef(match.group("ref"))))
def handleIndent(man, match): if match.group(1): IndentParser(man, match) else: man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_END, doc.Par()))
def __init__(self, man, match): self.old = man.getParser() man.setParser(self) self.block = highlight.CodeBlock(man, '') man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, self.block)) self.block.add(match.group(1))