import re from flask_logic.registry import COMPONENTS # ------------------------ # Content renderer (blocks) # ------------------------ def render_blocks(blocks, context=None): html_output = [] print(COMPONENTS) for block in blocks: block_type = block.get("type") if block_type == "text": html_output.append(block.get("value", "")) elif block_type in COMPONENTS: render_func = COMPONENTS[block_type]["render"] # remove "type" before passing kwargs kwargs = {k: v for k, v in block.items() if k != "type"} html_output.append( render_func(**kwargs, context=context) ) return "\n".join(html_output) # ------------------------ # Old string renderer (tokens) # ------------------------ def parse_options(option_string): options = {} if not option_string: return options parts = option_string.split("|") for part in parts: if "=" in part: key, value = part.split("=", 1) options[key.strip()] = value.strip() return options def render_content(content, context=None): if not content: return "" def replace_image(match): src = match.group("src").strip() options = parse_options(match.group("options")) render_func = COMPONENTS["image"]["render"] return render_func( src=src, caption=options.get("caption"), css_class=options.get("class"), context=context ) pattern = r"\[image:(?P[^\|\]]+)(?:\|(?P[^\]]+))?\]" return re.sub(pattern, replace_image, content) # ------------------------ # Unified entry # ------------------------ def process_post_content(content, context=None): if isinstance(content, str): return render_content(content, context=context) elif isinstance(content, list): return render_blocks(content, context=context) return "" # ------------------------ # CSS collector # ------------------------ def collect_css(context): css_files = [] for comp in context["used_components"]: css = COMPONENTS.get(comp, {}).get("css") if css: css_files.append(css) return css_files