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 handleMath(man, match): text = match.group("latexmath") if text == "": man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word("$"))) else: man.doc.addFeature(BUILDER) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, MathWord(text, BUILDER)))
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 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 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_squote(man, match): text = match.group("sqtext") t = i18n.getTranslator(man.doc) man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word(t.get(i18n.GLYPH_OPEN_SQUOTE)))) tparser.handleText(man, text, '') man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word(t.get(i18n.GLYPH_CLOSE_SQUOTE))))
def new_image(man, match): tmatch = match image = match.group('image') # alt pealing alt = None if image[-1] == ')': i = image.rfind('(') if i >= 0: label = image[i + 1:-1] alt = doc.Par() alt.append(doc.Word(label)) 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.Figure(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 handleFigure(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") if label == None: caption = None else: caption = doc.Par() caption.append(doc.Word(label)) left = len(match.group("left")) right = len(match.group("right")) if left == right: align = doc.ALIGN_CENTER elif left > right: align = doc.ALIGN_RIGHT else: align = doc.ALIGN_LEFT man.send( doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, doc.Figure(image, width, height, caption, align)))
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 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) res = doc.Tag(word, man.doc) 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 parse(self, man, line): m = line_re.match(line) if m == None: man.error("unsupported syntax") elif m.group("end"): man.setParser(self.old) for (c, s) in self.defs: man.addWord((s, c)) elif m.group("hex"): g = doc.Glyph(int(m.group("hex"), 16)) self.defs.append((escape(m.group("hex_val")), lambda man, match: man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, g)))) elif m.group("dec"): g = doc.Glyph(int(m.group("dec"))) self.defs.append((escape(m.group("dec_val")), lambda man, match: man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, g)))) else: g = doc.Word(m.group("chr")) self.defs.append((escape(m.group("chr_val")), lambda man, match: man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, g))))
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 handleImage(man, match): if match.group("left") or match.group("right"): handleFigure(man, match) else: 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") if label == None: caption = None else: caption = doc.Par() caption.append(doc.Word(label)) man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Image(image, width, height, caption)))
def new_multi_blockquote(man, match): bq = BlockQuote() use_par_style(bq, match.group(1)) man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_END, bq)) tparser.handleText(man, match.group("text"))
def new_styled_table(man, match): table = doc.Table() use_par_style(table, match.group(1)) man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW_ROW, table))
def handleDouble(man, match): man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word("#")))
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 handleRef(man, match): man.send( doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, man.factory.makeRef(match.group("ref"))))
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 new_footnote_multi(man, match): fn = doc.FootNote(doc.FOOTNOTE_DEF, match.group(1)) use_par_style(fn, match.group(2)) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW_STYLE, fn)) tparser.handleText(man, match.group("text"))
def handle_link(man, match): URL = match.group('URL') text = match.group('text') 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(text))) man.send(doc.CloseEvent(doc.L_WORD, doc.ID_END_LINK, "link"))
def handle_hrule(man, match): man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, doc.HorizontalLine()))
def new_single_code(man, match): block = highlight.CodeBlock(man, "") block.add(match.group(1)) man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_NEW, block))
def handle_word(man, word): man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, doc.Word(word)))
def handle_backtrick(man, match): text = match.group("text_backtrick") word = doc.Word(text) style = doc.Style(doc.STYLE_CODE) style.append(word) man.send(doc.ObjectEvent(doc.L_WORD, doc.ID_NEW, style))
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 handle_new_par(man, match): man.send(doc.ObjectEvent(doc.L_PAR, doc.ID_END, doc.Par()))