Skip to content

Add Code mobject with syntax highlighting#1625

Merged
3b1b merged 4 commits into
3b1b:masterfrom
YishiMichael:master
Sep 15, 2021
Merged

Add Code mobject with syntax highlighting#1625
3b1b merged 4 commits into
3b1b:masterfrom
YishiMichael:master

Conversation

@YishiMichael
Copy link
Copy Markdown
Contributor

Motivation

Add Code mobject with syntax highlighting. It's inherited from Text with the help of pygments, which is a python site-package and should be additionally installed.

Proposed changes

Test

Code:

from manimlib import *


EXAMPLE_HELLO_WORLD_CODE_DICT = {
"python": """
print("Hello World!")
""",

"C++": """
# include <iostream>

using namespace std;

int main() {
    cout << "Hello World!" << endl;
    return 0;
}
""",

"Java": """
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
""",

"C": """
# include <stdio.h>

int main() {
    printf("%s\\n", "Hello World!");
    return 0;
}
"""
}


class CodeExampleScene(Scene):
    def construct(self):
        code_mobs = VGroup(*[
            Code(EXAMPLE_HELLO_WORLD_CODE_DICT[lang], language=lang, code_style=code_style)
            for lang, code_style in zip(
                ["python", "C++", "Java", "C"],
                ["zenburn", "monokai", "algol", "emacs"]
            )
        ]).arrange_in_grid(2, 2, h_buff=0.0, v_buff=0.5)
        self.add(code_mobs)

Result:
CodeExampleScene

@TonyCrane TonyCrane requested a review from 3b1b September 15, 2021 12:08
@3b1b 3b1b merged commit e359f52 into 3b1b:master Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants