def truncate_text(text): settings = get_feeds_settings() truncate_words = settings.getint("FEEDS_CONFIG_TRUNCATE_WORDS") if truncate_words and truncate_words > 0: tree = build_tree(text)[0] # Assume that a word has 5 characters on average. truncate_tree(tree, truncate_words * 5) text = serialize_tree(tree) return text
def remove_elems(tree, loader_context): remove_elems = [] settings = get_feeds_settings() remove_images = settings.getbool("FEEDS_CONFIG_REMOVE_IMAGES") if remove_images: remove_elems += ["img"] # Remove tags. for elem_sel in loader_context.get("remove_elems", []) + remove_elems: selector = CSSSelector(elem_sel) for elem in selector(tree): elem.drop_tree() for elem_sel in loader_context.get("remove_elems_xpath", []): for elem in tree.xpath(elem_sel): elem.drop_tree() return [tree]