Mercurial > libervia-desktop-kivy
diff cagou/core/simple_xhtml.py @ 491:203755bbe0fe
massive refactoring from camelCase -> snake_case. See backend commit log for more details
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:44:32 +0200 |
parents | 3c9ba4a694ef |
children |
line wrap: on
line diff
--- a/cagou/core/simple_xhtml.py Sat Apr 08 13:34:55 2023 +0200 +++ b/cagou/core/simple_xhtml.py Sat Apr 08 13:44:32 2023 +0200 @@ -43,7 +43,7 @@ if parent is not None: self.font_size = parent.font_size - def _addUrlMarkup(self, text): + def _add_url_markup(self, text): text_elts = [] idx = 0 links = 0 @@ -74,7 +74,7 @@ # this would result in infinite loop (because self.text # is changed if an URL is found, and in this case markup too) if text and not self.markup: - self._addUrlMarkup(text) + self._add_url_markup(text) def on_ref_press(self, ref): url = self.ref_urls[ref] @@ -115,7 +115,7 @@ xhtml = ET.fromstring(xhtml.encode()) self.current_wid = None self.styles = [] - self._callParseMethod(xhtml) + self._call_parse_method(xhtml) if len(self.children) > 1: self._do_split_labels() @@ -137,7 +137,7 @@ new_text = [] current_tag = [] current_value = [] - current_wid = self._createText() + current_wid = self._create_text() value = False close = False # we will parse the text and create a new widget @@ -184,7 +184,7 @@ new_text = [] self.add_widget(current_wid) log.debug("new widget: {}".format(current_wid.text)) - current_wid = self._createText() + current_wid = self._create_text() for t, v in styles: new_text.append('[{tag}{value}]'.format( tag = t, @@ -207,7 +207,7 @@ # XHTML parsing methods - def _callParseMethod(self, e): + def _call_parse_method(self, e): """Call the suitable method to parse the element self.xhtml_[tag] will be called if it exists, else @@ -221,7 +221,7 @@ method = self.xhtml_generic method(e) - def _addStyle(self, tag, value=None, append_to_list=True): + def _add_style(self, tag, value=None, append_to_list=True): """add a markup style to label @param tag(unicode): markup tag @@ -230,7 +230,7 @@ self.styles is needed to keep track of styles to remove should most probably be set to True """ - label = self._getLabel() + label = self._get_label() label.text += '[{tag}{value}]'.format( tag = tag, value = '={}'.format(value) if value else '' @@ -238,14 +238,14 @@ if append_to_list: self.styles.append((tag, value)) - def _removeStyle(self, tag, remove_from_list=True): + def _remove_style(self, tag, remove_from_list=True): """remove a markup style from the label @param tag(unicode): markup tag to remove @param remove_from_list(bool): if True, remove from self.styles too should most probably be set to True """ - label = self._getLabel() + label = self._get_label() label.text += '[/{tag}]'.format( tag = tag ) @@ -256,30 +256,30 @@ del self.styles[tag_idx] break - def _getLabel(self): + def _get_label(self): """get current Label if it exists, or create a new one""" if not isinstance(self.current_wid, Label): - self._addLabel() + self._add_label() return self.current_wid - def _addLabel(self): + def _add_label(self): """add a new Label current styles will be closed and reopened if needed """ - self._closeLabel() - self.current_wid = self._createText() + self._close_label() + self.current_wid = self._create_text() for tag, value in self.styles: - self._addStyle(tag, value, append_to_list=False) + self._add_style(tag, value, append_to_list=False) self.add_widget(self.current_wid) - def _createText(self): + def _create_text(self): label = SimpleXHTMLWidgetText(color=self.color, markup=True) self.bind(color=label.setter('color')) label.bind(texture_size=label.setter('size')) return label - def _closeLabel(self): + def _close_label(self): """close current style tags in current label needed when you change label to keep style between @@ -287,9 +287,9 @@ """ if isinstance(self.current_wid, Label): for tag, value in reversed(self.styles): - self._removeStyle(tag, remove_from_list=False) + self._remove_style(tag, remove_from_list=False) - def _parseCSS(self, e): + def _parse_css(self, e): """parse CSS found in "style" attribute of element self._css_styles will be created and contained markup styles added by this method @@ -313,14 +313,14 @@ method(e, value) self._css_styles = self.styles[styles_limit:] - def _closeCSS(self): + def _close_css(self): """removed CSS styles styles in self._css_styles will be removed and the attribute will be deleted """ for tag, __ in reversed(self._css_styles): - self._removeStyle(tag) + self._remove_style(tag) del self._css_styles def xhtml_generic(self, elem, style=True, markup=None): @@ -337,33 +337,33 @@ tag, value = markup, None else: tag, value = markup - self._addStyle(tag, value) + self._add_style(tag, value) style_ = 'style' in elem.attrib and style if style_: - self._parseCSS(elem) + self._parse_css(elem) # then content if elem.text: - self._getLabel().text += escape_markup(elem.text) + self._get_label().text += escape_markup(elem.text) # we parse the children for child in elem: - self._callParseMethod(child) + self._call_parse_method(child) # closing CSS style and markup if style_: - self._closeCSS() + self._close_css() if markup is not None: - self._removeStyle(tag) + self._remove_style(tag) # and the tail, which is regular text if elem.tail: - self._getLabel().text += escape_markup(elem.tail) + self._get_label().text += escape_markup(elem.tail) # method handling XHTML elements def xhtml_br(self, elem): - label = self._getLabel() + label = self._get_label() label.text+='\n' self.xhtml_generic(elem, style=False) @@ -406,12 +406,12 @@ # methods handling CSS properties def css_color(self, elem, value): - self._addStyle("color", css_color.parse(value)) + self._add_style("color", css_color.parse(value)) def css_text_decoration(self, elem, value): if value == 'underline': - self._addStyle('u') + self._add_style('u') elif value == 'line-through': - self._addStyle('s') + self._add_style('s') else: log.warning("unhandled text decoration: {}".format(value))