def handleMatch(self, m): src = m.group(2).strip() if src: script = etree.Element('script') script.set('src', src) else: script = '' return script
def create_slide(self, buf, i): cont = etree.Element('div') cont.set('class', 'slide') cont.set('id', str(i)) i += 1 for b in buf: cont.append(b) return cont
def handleMatch(self, m): el = etree.Element("a") href = m.group(2) if not re.match('^(ftp|https?)://', href, flags=re.IGNORECASE): href = 'http://%s' % href el.set('href', self.unescape(href)) el.text = AtomicString(m.group(2)) return el
def add_permalink(self, c, elem_id): permalink = etree.Element("a") #permalink.text = ("%spara;" % AMP_SUBSTITUTE # if self.use_permalinks is True # else self.use_permalinks) permalink.attrib["href"] = "#" + elem_id permalink.attrib["class"] = "self-link" c.append(permalink)
def search_and_change(self, element): for parent, child in list(self.iterparent(element)): if child.tag == "img" and parent.tag != "figure": d = child.attrib.copy() child.clear() child.tag = "figure" img = etree.Element("img") img.attrib = d child.append(img) caption = etree.Element("figcaption") caption.text = d.get("title", "") child.append(caption)
def handleMatch(self, m): subsc = m.group(3) text = subsc el = etree.Element('sub') el.text = AtomicString(text) return el
def _replace_block(self, text): # Parse configuration params m = self.FENCED_BLOCK_RE.search(text) if not m: m = self.BLOCK_RE.search(text) if not m: return text, False # Parse configuration params img_format = m.group('format') if m.group( 'format') else self.config['format'] classes = m.group('classes') if m.group( 'classes') else self.config['classes'] alt = m.group('alt') if m.group('alt') else self.config['alt'] title = m.group('title') if m.group('title') else self.config['title'] # Extract diagram source end convert it code = m.group('code') diagram = self.generate_uml_image(code, img_format) if img_format == 'png': data = 'data:image/png;base64,{0}'.format( base64.b64encode(diagram).decode('ascii')) img = etree.Element('img') img.attrib['src'] = data img.attrib['classes'] = classes img.attrib['alt'] = alt img.attrib['title'] = title elif img_format == 'svg': # Firefox handles only base64 encoded SVGs data = 'data:image/svg+xml;base64,{0}'.format( base64.b64encode(diagram).decode('ascii')) img = etree.Element('img') img.attrib['src'] = data img.attrib['classes'] = classes img.attrib['alt'] = alt img.attrib['title'] = title elif img_format == 'txt': # logger.debug(diagram) img = etree.Element('pre') code = etree.SubElement(img, 'code') code.attrib['class'] = 'text' code.text = AtomicString(diagram.decode('UTF-8')) return text[:m.start()] + etree.tostring( img).decode() + text[m.end():], True
def equationDiv(self, eq_id): """ Create equation container tag. """ eqn = etree.Element('div') eqn.set('class', 'moose-katex-equation') eqn.set('id', eq_id) return eqn
def handleMatch(self, m): if m.group('plainurl'): url = m.group('url') a = etree.Element('a') a.text = url a.set('href', url) a.set('class', 'plainurl') if m.group('itemprop'): a.set('itemprop', m.group('itemprop')) return a else: url = m.group('email') a = etree.Element('a') a.text = url a.set('href', 'mailto:%s' % url) a.set('class', 'email') return a
def make_anchor(self, text): el = etree.Element('a') el.text = text el.set('class', "linkpatcher_link") el.set('href', path_to_url(self.db_value_map[text], plugin.linkpatcher_plugin_globals.nav, True)) return el
def appendText(text, tag=None): if tag is None: textBuffer.append(text) else: flushTextBuffer() elem = etree.Element(tag) elem.text = text root.append(elem)
def handleMatch(self, m): url = m.group('url').strip() audio_elem = etree.Element('audio') audio_elem.set('controls', '') source_elem = etree.SubElement(audio_elem, 'source') source_elem.set('src', url) source_elem.set('type', 'audio/mpeg') return audio_elem
def render_auto_link(self, token): el = etree.Element('a') if token.mailto: target = 'mailto:{}'.format(token.target) else: target = self.escape_url(token.target) el.set('href', target) return self.append_elems(el, self.render_inner(token))
def render_document(self, token): self.footnotes.update(token.footnotes) # python-markdown recognizes and strips *this* hardcoded <div> el = etree.Element(getattr(token, 'root_tag', 'div')) self.append_elems(el, self.render_inner_join(token)) self.append_newline_inside(el) elt = etree.ElementTree(el) return elt
def render_iframe(url, width, height): iframe = etree.Element('iframe') iframe.set('width', width) iframe.set('height', height) iframe.set('src', url) iframe.set('allowfullscreen', 'true') iframe.set('frameborder', '0') return iframe
def render_image(self, token): # note that the attributes are sorted before output HTML, # NOT by the order specified. annoying when taking diffs, # requiring a customized HTML serializer el = etree.Element('img', src=token.src, alt=self.render_to_plain(token)) if token.title: el.set('title', self.escape_html(token.title)) return el
def __sub(self, match): """ Substitution method for regex replacement. """ name = match.group('name') markdown = match.group('markdown') settings = self.getSettings(match.group('settings')) # Use the content in database if name in self.__database: div = etree.Element('div') div.set('markdown', '1') item = self.__database[name] div.text = item.markdown # Use the default elif settings['default']: div = etree.Element('div') div.set('markdown', '1') div.text = markdown # Produce error else: help_div = etree.Element('div') heading = etree.SubElement(help_div, 'h3') heading.text = "Adding Markdown for '{}' Item.".format(name) p = etree.SubElement(help_div, 'p') p.text = "To add content for the '{}' item, simply add a block similar to what is ' \ 'shown below in the markdown file '{}'.".format(name, self.markdown.current.filename) pre = etree.SubElement(help_div, 'pre') code = etree.SubElement(pre, 'code') code.set('class', 'language-text') code.text = '!SQA-template-item {}\nThe content placed here should be valid markdown ' \ 'that will replace the template description.\n!END-template-item' \ .format(name) title = 'Missing Template Item: {}'.format(name) div = self.createErrorElement(title=title, message=markdown, markdown=True, help_button=help_div) return etree.tostring(div)
def handle_match(m): node = etree.Element('script') node.set('type', 'math/tex; mode=display') if '\\begin' in m.group(2): node.text = AtomicString(m.group(2) + m.group(4) + m.group(5)) else: node.text = AtomicString(m.group(3)) return node
def run(self, parent, blocks): path = re.match(r"^\\include\((.*)\)$", blocks[0]).group(1) del blocks[0] el = etree.Element("div") el.set('class', 'serif-include') with open(path, 'r') as f: el.text = self.parser.parseChunk(parent, f.read()) parent.append(el)
def render_iframe(url, width, height): iframe = etree.Element("iframe") iframe.set("width", width) iframe.set("height", height) iframe.set("src", url) iframe.set("allowfullscreen", "true") iframe.set("frameborder", "0") return iframe
def handleMatch(self, m): from wiki import models article_title = m.group('wikiTitle') absolute = False if article_title.startswith("/"): absolute = True article_title = article_title.strip("/") # Use this to calculate some kind of meaningful path # from the link, regardless of whether or not something can be # looked up path_from_link = "" if absolute: base_path = self.config['base_url'][0] path_from_link = os_path.join(base_path, article_title) urlpath = None path = path_from_link try: urlpath = models.URLPath.get_by_path(article_title) path = urlpath.get_absolute_url() except models.URLPath.DoesNotExist: pass else: urlpath = models.URLPath.objects.get(article=self.markdown.article) source_components = urlpath.path.strip("/").split("/") # We take the first (self.config['default_level'] - 1) components, so adding # one more component would make a path of length self.config['default_level'] starting_level = max(0, self.config['default_level'][0] - 1) starting_path = "/".join(source_components[:starting_level]) path_from_link = os_path.join(starting_path, article_title) lookup = models.URLPath.objects.none() if urlpath.parent: lookup = urlpath.parent.get_descendants().filter( slug=article_title) else: lookup = urlpath.get_descendants().filter(slug=article_title) if lookup.count() > 0: urlpath = lookup[0] path = urlpath.get_absolute_url() else: urlpath = None path = self.config['base_url'][0] + path_from_link label = m.group('linkTitle') a = etree.Element('a') a.set('href', path) if not urlpath: a.set('class', self.config['html_class'][0] + " linknotfound") else: a.set('class', self.config['html_class'][0]) a.text = label return a
def html(self): """ Return html containing collapsible items. """ ul = etree.Element('ul') ul.set('class', "collapsible") ul.set('data-collapsible', "expandable") for param in self._parameters: if param['name'] == 'type': continue li = etree.SubElement(ul, 'li') header = etree.SubElement(li, 'div') header.set('class', "collapsible-header") description = param['description'].strip() if description: btn = etree.SubElement(header, 'i') btn.set('class', 'material-icons') btn.text = 'keyboard_arrow_down' header_name = etree.SubElement(header, 'div') header_name.set('class', 'moose-parameter-name') header_name.text = param['name'] default = self._formatParam(param, 'default').strip() if default: default_span = etree.SubElement(header, 'span') default_span.set('class', 'moose-parameter-header-default') default_span.text = ' ({})'.format(default) if description: div = etree.SubElement(header, 'span') div.set('class', 'moose-parameter-header-description ') div.text = ': ' + description body = etree.SubElement(li, 'div') body.set('class', "collapsible-body") if description: div = etree.SubElement(body, 'div') div.set('class', 'moose-parameter-description') div.text = description div = etree.SubElement(body, 'div') div.set('class', 'moose-parameter-default') if default: div.text = 'Default: {}'.format(default) else: div.text = 'Default: None' div = etree.SubElement(body, 'div') div.set('class', 'moose-parameter-type') div.text = 'Type: {}'.format(self._formatParam(param, 'cpp_type')) return ul
def build_element(self, m): # group(1) is everything before the pattern # group(2) is the first group of the pattern img_id = m.group(1) options = m.group(2).split() if m.group(2) else [] caption = m.group(4).strip() if m.group(4) else '' position = 'inline' img_size = 'MI' for option in options: if option in ['left', 'right', 'center', 'inline']: position = option elif option == 'big': img_size = 'BI' elif option == 'small': img_size = 'SI' elif option == 'orig': img_size = '' img = etree.Element('img') img_url = '/images/proxy/%s' % (img_id, ) if img_size: img_url += '?size=' + img_size img.set('alt', (caption or img_id).replace("\n", " ")) img.set('c2c:url-proxy', img_url) img.set('c2c:role', 'embedded-image') img.set('c2c:document-id', img_id) img.set('c2c:size', img_size) fig = etree.Element('figure') fig.append(img) fig.set('c2c:position', position) fig.set('c2c:role', 'embedded-figure') fig.set('c2c:size', img_size) if caption: img_caption = etree.Element('figcaption') img_caption.text = caption fig.append(img_caption) return fig
def handleMatch(self, m, data): ref = m.group(1) text = m.group(2) uri = _resolve_ref(ref) el = etree.Element('a') el.attrib['href'] = uri el.attrib['target'] = 'blank' el.text = text[1:] if text is not None else ref return el, m.start(0), m.end(0)
def html(self): """ Returns an html li tag. """ el = etree.Element('li') a = etree.SubElement(el, 'a') a.set('href', self.remote) a.text = self.filename return el
def make_link(self, category, obj, url, text): """Make an <a> element Override this to set custom attributes, e.g. title. """ el = etree.Element('a') el.set('href', url) el.text = AtomicString(text) return el
def embed_imgur(link): a = link.path.split('/')[2] el = etree.Element('iframe') el.set('width', '100%') el.set('height', '550') el.set('frameborder', '0') el.set('allowfullscreen', '') el.set('src', '//imgur.com/a/' + a + '/embed') return el
def handleMatch(self, m): el = etree.Element('span') el.set('class', 'type') types = m.group(3) # Make sure types are shown as type1 | type2 types = re.sub(' or ', ' | ', types) types = re.sub(r'(?<=\w)[|](?=\w)', ' | ', types) el.text = AtomicString(types) return el
def get_link_markdown_elem(self, current_uid): url, display_text, classes = self.__get_link_components(current_uid) a = etree.Element('a') a.text = display_text a.set('href', url) if classes: class_text = ' '.join(classes) a.set('class', class_text) return a
def handleMatch(self, match): attachment_name = match.group(3) attachment_title = match.group(2) attachment_url = self.url_writer(attachment_name) image = etree.Element('img') image.set('src', attachment_url) image.set('title', attachment_title) return image