def stylesheet_filter_func(tag: bs4.element.Tag) -> bool: """ Filter function for stylesheet tags only """ if tag.has_attr("rel"): is_css = "stylesheet" in tag.get("rel") enabled = not tag.has_attr("disabled") if is_css and enabled: return True return False
def _render_code(code_elem: bs4.element.Tag) -> str: """Render custom <x-code> element into an HTML string.""" language = code_elem['language'] if code_elem.has_attr( 'language') else None contents = code_elem.contents[0] try: # Use `TextLexer` as default if no language specified lexer = get_lexer_by_name(language if language else 'text') except pygments.util.ClassNotFound: raise ValueError( f'Invalid "language" parameter in <x-code> element {language}') # TODO: COULD PROVIDE A GLOBAL SETTING FOR THE 'STYLE' TO USE (see https://pygments.org/styles/) # https://pygments.org/docs/formatters/#HtmlFormatter formatter = HtmlFormatter(noclasses=True) return pygments.highlight(contents.strip(), lexer, formatter)
def _parse_link(el: bs4.element.Tag) -> Link: onclick = el.get('onclick') if el.has_attr('onclick') else None return Link(text=el.text, href=None, onclick=onclick)
def meets_condition(self, tag: bs4.element.Tag): return tag.has_attr(self.kwargs['condition'])