Quickstart

Highlight code in 2 minutes

1 min read 206 words

Highlight your first code block in under 2 minutes.

1. Install

pip install rosettes

2. Highlight Code

from rosettes import highlight

code = '''
def greet(name: str) -> str:
    """Return a greeting."""
    return f"Hello, {name}!"
'''

html = highlight(code, "python")
print(html)

Output:

<div class="rosettes" data-language="python">
  <pre><code><span class="syntax-keyword">def</span> <span class="syntax-function">greet</span>...

3. Try Different Languages

from rosettes import highlight

# JavaScript
js_html = highlight("const x = 42;", "javascript")

# Rust
rust_html = highlight("fn main() { println!(\"Hello\"); }", "rust")

# JSON
json_html = highlight('{"key": "value"}', "json")

4. Check Available Languages

from rosettes import list_languages, supports_language

# List all 55 supported languages
print(list_languages())
# ['bash', 'c', 'clojure', 'cpp', 'css', ...]

# Check if a language is supported
print(supports_language("python"))  # True
print(supports_language("cobol"))   # False

5. Add Line Numbers

html = highlight(code, "python", show_linenos=True)

6. Highlight Specific Lines

html = highlight(code, "python", hl_lines={2, 3})

Lines 2 and 3 receive the.hllCSS class for highlighting.

Next Steps