def convert(self, context): children = [] lastchild = None for child in self.find(): if child.name() == 'dt': if lastchild is not None and lastchild.name() == 'dt': children.append(html.dd(Text(' '))) children.append(html.dt(child.content.convert(context))) lastchild = child elif child.name() == 'dd': if lastchild is not None and lastchild.name() == 'dd': children.append(html.dt(Text(' '))) children.append(html.dd(child.content.convert(context))) lastchild = child return html.dl(Frag(children))
def convert(self, context): name = self.attr.name title = self.attr.title if title: title = unicode(self.attr.title.asBytes('utf-8'), 'utf-8') else: title = '' if title: text = Text('[#%s: %s]' % (name, title)) else: text = Text('[#%s]' % name) return html.a( text, name=name, title=title, class_='index',)
def convert(self, context): if hasattr(self, 'should_be_removed') and self.should_be_removed: return Frag() children = [] lastchild = None for child in self.find(): if child.name() == 'dt': if lastchild is not None and lastchild.name() == 'dt': children.append(silva.dd(Text(' '))) children.append(silva.dt(child.content.convert(context))) lastchild = child elif child.name() == 'dd': if lastchild is not None and lastchild.name() == 'dd': children.append(silva.dt(Text(' '))) children.append(silva.dd(child.content.convert(context))) lastchild = child if lastchild is not None and lastchild.name() == 'dt': children.append(silva.dd(Text(' '))) return silva.dlist(Frag(children))
def extract_texts(node, context, allow_indexes=0): """extract all text content from a tag """ result = [] for child in node.find(): if child.name() == 'br': result.append(Text(u'\n')) elif (allow_indexes and child.name() == 'a' and not child.getattr('href', None)): result.append(child.convert(context)) elif child.name() != 'Text': result += extract_texts(child, context, allow_indexes) else: result.append(child.convert(context)) return result
def convert(self, context): title = self.getattr('title', default='') name = self.getattr('name', default=None) href = self.getattr('href', default='#') anchor = self.getattr('_silva_anchor', default=None) window_target = self.getattr('target', default='') link_attributes = {'target': window_target, 'title': title, 'anchor': anchor} if name is not None and href == '#': # Case one, we are an anchor if self.getattr('class', None) == 'index': # index item text = ''.join([t.convert(context).asBytes('UTF-8') for t in extract_texts(self, context)]) textnode = Frag() if text and (text[0] != '[' or text[-1] != ']'): textnode = Text(text) return Frag( textnode, silva.index(name=name, title=title)) else: # named anchor, probably pasted from some other page return Frag(self.content.convert(context)) elif self.hasattr('_silva_reference'): # Case of a Silva reference used reference_name = str(self.getattr('_silva_reference')) reference_name, reference = context.get_reference(reference_name) if reference is not None: target_id = self.getattr('_silva_target', '0') try: target_id = int(str(target_id)) assert get_content_from_id(target_id) is not None except (ValueError, AssertionError): # Invalid target id, set it as zero (broken) target_id = 0 else: # Invalid reference. We create a new one and mark the # target as broken reference_name, reference = context.get_reference('new') target_id = 0 # If the target changed, update it if target_id != reference.target_id: reference.set_target_id(target_id) link_attributes['reference'] = reference_name return silva.link( self.content.convert(context), **link_attributes) elif self.hasattr('href'): # External links url = self.getattr('_silva_href', None) if url is None: url = self.getattr('href', '') if unicode(url).startswith('/'): # convert to physical path before storing url = Text(IPath(context.request).urlToPath(unicode(url))) if url: link_attributes['url'] = url return silva.link( self.content.convert(context), **link_attributes) elif anchor is not None: # Link to an anchor on the same page return silva.link( self.content.convert(context), **link_attributes) else: return Frag()
def convert(self, context): # external source element id = self.attr.id params = {} attrparams = {} divcontent = [] source = getSourceForId(context.model, str(id)) if source is not None: meta_type = source.meta_type source_title = source.get_title() or id source_form = source.get_parameters_form() header = html.h4(Text(u'%s \xab%s\xbb' % (meta_type, source_title)), title=u'source id: %s'%id) description = source.get_description() if description: divcontent.append( html.p(description, class_="externalsource-description")) else: source_title = '' source_form = None header = html.h4( Text('[%s]' % _('external source element is broken'))) for child in self.find(): if child.name() == 'parameter': vtype = child.getattr('type', 'string').convert(context).extract_text() value = child.content.convert(context).asBytes('utf-8') key = child.attr.key.convert(context).extract_text() attrkey = '%s__type__%s' % (key,vtype) if vtype == 'list': value = [unicode(x, 'utf-8') for x in eval(value)] else: value = unicode(value, 'utf-8') params[key] = (value,attrkey) divpar = [] for key in params: value, attrkey = params[key] display_key = key if source_form is not None: try: display_key = source_form.get_field(key).title() except AttributeError: pass # Field is gone divpar.append(html.strong("%s: " % display_key)) if '__type__list' in attrkey: for v in value: divpar.append(html.span( Text(xml_unescape(v)), {'key': attrkey})) divpar.append(Text(', ')) divpar.pop() else: divpar.append(html.span( Text(xml_unescape(value)), {'key': attrkey})) divpar.append(html.br()); par = html.div(Frag(divpar), {'class': 'parameters'}) divcontent.append(par) content = Frag(header, divcontent); return html.div(content, source_id=id, source_title = source_title, class_='externalsource', **attrparams)