Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/blocks/code_extraction_block.py (Python)

Code before:
                ("python", "print('Hello World')"),
                ("html", "<h1>Title</h1>"),
                ("remaining_text", "Here's a Python example:\nAnd some HTML:")
            ]
        )

    def run(self, input_data: Input, **kwargs) -> BlockOutput:
        # List of supported programming languages
        languages = [
            "html",
            "css",
            "javascript",
            "python",
            "sql",
            "java",
            "cpp",
            "csharp",
            "json",
            "bash",
            "php",
            "ruby",
            "yaml",
            "markdown",
            "typescript",
            "xml",
        ]

        # Extract code for each language
        for lang in languages:
            code = self.extract_code(input_data.text, lang)
            if code:  # Only yield if there's actual code content
                yield lang, code

        # Remove all code blocks from the text to get remaining text
        pattern = "|".join(languages)
        remaining_text = re.sub(
            rf"```({pattern})[\s\S]*?```", "", input_data.text
        ).strip()
        if remaining_text:  # Only yield if there's remaining text
            yield "remaining_text", remaining_text

    def extract_code(self, text: str, language: str) -> str:
        # Extract all code blocks enclosed in ```language``` blocks
        pattern = re.compile(rf"```{language}\n([\s\S]*?)```", re.IGNORECASE)
        matches = pattern.finditer(text)
        # Combine all code blocks for this language with newlines between them
        code_blocks = [match.group(1).strip() for match in matches]
        return "\n\n".join(code_blocks) if code_blocks else ""


Review comment (refactor):
Quite a few of these have valid aliases or acronyms, e.g. `ts` for `typescript`. These should also be extracted, but mapped to the same output.

Code after:
                ("html", "<h1>Title</h1>"),
                ("python", "print('Hello World')"),
                ("remaining_text", "Here's a Python example:\nAnd some HTML:"),
            ],
        )

    def run(self, input_data: Input, **kwargs) -> BlockOutput:
        # List of supported programming languages with mapped aliases
        language_aliases = {
            "html": ["html", "htm"],
            "css": ["css"],
            "javascript": ["javascript", "js"],
            "python": ["python", "py"],
            "sql": ["sql"],
            "java": ["java"],
            "cpp": ["cpp", "c++"],
            "csharp": ["csharp", "c#", "cs"],
            "json_code": ["json"],
            "bash": ["bash", "shell", "sh"],
            "php": ["php"],
            "ruby": ["ruby", "rb"],
            "yaml": ["yaml", "yml"],
            "markdown": ["markdown", "md"],
            "typescript": ["typescript", "ts"],
            "xml": ["xml"],
        }

        # Extract code for each language
        for canonical_name, aliases in language_aliases.items():
            code = ""
            # Try each alias for the language
            for alias in aliases:
                code_for_alias = self.extract_code(input_data.text, alias)
                if code_for_alias:
                    code = code + "\n\n" + code_for_alias if code else code_for_alias

            if code:  # Only yield if there's actual code content
                yield canonical_name, code

        # Remove all code blocks from the text to get remaining text
        pattern = (
            r"```(?:"
            + "|".join(
                re.escape(alias)
                for aliases in language_aliases.values()
                for alias in aliases
            )
            + r")\s+[\s\S]*?```"
        )

        remaining_text = re.sub(pattern, "", input_data.text).strip()