### Manage Supported Languages Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/get-started/quickstart.md Provides methods to list all supported languages and verify if a specific language is available for highlighting. ```python from rosettes import list_languages, supports_language # List all 55 supported languages print(list_languages()) # Check if a language is supported print(supports_language("python")) # True print(supports_language("cobol")) # False ``` -------------------------------- ### Development Setup and Testing Source: https://github.com/lbliii/rosettes/blob/main/README.md Provides commands for cloning the Rosettes repository, installing development dependencies using `uv`, and running tests with `pytest`. This setup is essential for contributing to or testing the highlighter. ```bash git clone https://github.com/lbliii/rosettes.git cd rosettes uv sync --group dev pytest ``` -------------------------------- ### Configure Line Numbers and Highlights Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/get-started/quickstart.md Demonstrates advanced formatting options including enabling line numbers and applying CSS classes to specific lines of code. ```python # Add Line Numbers html = highlight(code, "python", show_linenos=True) # Highlight Specific Lines html = highlight(code, "python", hl_lines={2, 3}) ``` -------------------------------- ### Highlight Code with Rosettes Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/get-started/quickstart.md Demonstrates the basic usage of the highlight function to convert raw code strings into syntax-highlighted HTML. It accepts the source code and the language identifier as inputs. ```python from rosettes import highlight code = ''' def greet(name: str) -> str: """Return a greeting.""" return f"Hello, {name}!" ''' html = highlight(code, "python") print(html) ``` -------------------------------- ### Highlight Multiple Languages Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/get-started/quickstart.md Shows how to generate syntax-highlighted HTML for various programming languages including JavaScript, Rust, and JSON using the highlight function. ```python 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") ``` -------------------------------- ### Install Rosettes via package managers or source Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/get-started/installation.md Provides commands to install the Rosettes library using uv, pip, or by cloning the repository from source. Requires Python 3.14+ as a prerequisite. ```bash uv add rosettes ``` ```bash pip install rosettes ``` ```bash git clone https://github.com/lbliii/rosettes.git cd rosettes pip install -e . ``` -------------------------------- ### Setup and Execution of Rosettes Benchmarks Source: https://github.com/lbliii/rosettes/blob/main/benchmarks/README.md Commands to install necessary dependencies and execute performance benchmarks using either the CLI or pytest-benchmark. These commands facilitate measuring execution time for syntax highlighting tasks. ```bash # Install benchmark dependencies pip install pygments pytest-benchmark # Run CLI benchmark (no pytest-benchmark required) python -m benchmarks.benchmark_vs_pygments # Run with pytest-benchmark for detailed stats pytest benchmarks/ -v --benchmark-only ``` -------------------------------- ### Verify Rosettes installation Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/get-started/installation.md Methods to confirm the library is correctly installed by checking the version string via Python script or command line interface. ```python import rosettes print(rosettes.__version__) ``` ```bash python -c "import rosettes; print(rosettes.__version__)" ``` -------------------------------- ### Dockerfile: Basic Python Application Setup Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md A Dockerfile for a Python application, setting up a build environment, installing dependencies, copying application code, exposing a port, and defining health checks and entry points. It uses multi-stage builds implicitly. ```dockerfile FROM python:3.14-slim AS builder ARG VERSION=1.0 ENV APP_HOME=/app \ PYTHONUNBUFFERED=1 WORKDIR ${APP_HOME} COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 8080 HEALTHCHECK --interval=30s CMD curl -f http://localhost:8080/health ENTRYPOINT ["python"] CMD ["app.py"] ``` -------------------------------- ### Highlight Python Code with Rosettes Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/get-started/_index.md Demonstrates how to use the `highlight` function from the Rosettes library to syntax-highlight a Python code string. The function takes the code as a string and the language name, returning an HTML string with highlighted code. ```python from rosettes import highlight html = highlight("def hello(): print('world')", "python") print(html) ``` -------------------------------- ### High-Level API Default Configuration Example (Python) Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/reference/configuration.md Illustrates the default parameters for the `highlight()` function, showing how `css_class`, `css_class_style`, `show_linenos`, and `hl_lines` are handled. It demonstrates equivalent calls with explicit and default parameters. ```python # These are equivalent highlight(code, "python") highlight( code, "python", hl_lines=None, show_linenos=False, css_class=None, # Auto: "rosettes" css_class_style="semantic", ) ``` -------------------------------- ### Example: Apply HTML Formatting Configuration (Python) Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/reference/configuration.md Demonstrates how to create and use `FormatConfig` with `HtmlFormatter` to customize the output. It shows setting a custom CSS class and data language attribute for the generated HTML. ```python from rosettes.formatters import HtmlFormatter from rosettes import get_lexer, FormatConfig lexer = get_lexer("python") formatter = HtmlFormatter() config = FormatConfig( css_class="my-code-block", data_language="python", ) tokens = lexer.tokenize("x = 1") html = formatter.format_string(tokens, config) #
... ``` ```python from rosettes import HighlightConfig from rosettes.formatters import HtmlFormatter config = HighlightConfig( hl_lines=frozenset({2, 3}), show_linenos=True, ) formatter = HtmlFormatter(config=config) ``` -------------------------------- ### C and C++ Language Fixture Samples Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Example C and C++ code snippets covering basics, preprocessor directives, pointers, templates, and modern C++ features. ```cpp #include int main(int argc, char *argv[]) { printf("Hello, World!\n"); return 0; } #ifndef HEADER_H #define HEADER_H #define MAX(a, b) ((a) > (b) ? (a) : (b)) #endif template class Container { public: explicit Container(T value) : value_(std::move(value)) {} private: T value_; }; ``` -------------------------------- ### Execute parallel batch highlighting on Python 3.14t Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/get-started/installation.md Demonstrates the use of highlight_many for true parallel processing when running on a free-threaded Python 3.14t build. This leverages PEP 703 to achieve significant performance speedups. ```python from rosettes import highlight_many # On 3.14t, this runs with true parallelism blocks = [("code", "python") for _ in range(100)] results = highlight_many(blocks) ``` -------------------------------- ### INI Configuration File Example Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md A basic INI file structure showing sections and key-value pairs. This format is commonly used for configuration files, including settings for sections like 'section' and 'database'. ```ini [section] key = value number = 42 boolean = true [database] host = localhost port = 5432 ``` -------------------------------- ### Build-Time Configuration with Environment Variables (Python) Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/reference/configuration.md Provides an example of how to manage configuration, specifically `css_class_style`, using environment variables for build-time settings in static site generators. It defines a function `highlight_code` that utilizes this approach. ```python import os from rosettes import highlight CSS_STYLE = os.getenv("ROSETTES_CSS_STYLE", "semantic") def highlight_code(code: str, language: str) -> str: return highlight(code, language, css_class_style=CSS_STYLE) ``` -------------------------------- ### Java Language Fixture Samples Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Example Java code snippets covering basics, generics, keywords, annotations, and lambdas for test fixture generation. ```java public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } } public class Box> { private T value; public void process(List items) { } } @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; } ``` -------------------------------- ### Diff File Format Example Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Demonstrates the standard diff file format used to show changes between two versions of a file. It includes lines that are unchanged, removed, and added. ```diff --- +++ @@ -1,3 +1,4 @@ unchanged -removed line +added line +another added unchanged ``` -------------------------------- ### CSS: Styling Hybrid Classes Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/styling/css-classes.md Provides example CSS rules for styling elements highlighted with the `semantic-hybrid` class style. It shows how to target specific classes like `.syntax-name-builtin` and `.syntax-function` to apply custom colors, demonstrating the flexibility of the hybrid mode. ```css .syntax-name-builtin { color: var(--syntax-builtin); } .syntax-function { color: var(--syntax-function); } ``` -------------------------------- ### Install Rosettes via pip Source: https://github.com/lbliii/rosettes/blob/main/README.md The standard command to install the Rosettes package into your Python environment. ```bash pip install rosettes ``` -------------------------------- ### Complete Migration Example: CodeHighlighter Class Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/tutorials/migrate-from-pygments.md Presents a full refactoring of a `CodeHighlighter` class from Pygments to Rosettes. This includes updates to initialization, basic highlighting, line highlighting, and batch processing. ```python from pygments import highlight from pygments.lexers import get_lexer_by_name, TextLexer from pygments.lexers import ClassNotFound from pygments.formatters import HtmlFormatter class CodeHighlighter: def __init__(self): self.formatter = HtmlFormatter(cssclass="highlight") def highlight(self, code: str, language: str) -> str: try: lexer = get_lexer_by_name(language) except ClassNotFound: lexer = TextLexer() return highlight(code, lexer, self.formatter) def highlight_with_lines( self, code: str, language: str, hl_lines: list[int], ) -> str: try: lexer = get_lexer_by_name(language) except ClassNotFound: lexer = TextLexer() formatter = HtmlFormatter( cssclass="highlight", linenos=True, hl_lines=hl_lines, ) return highlight(code, lexer, formatter) ``` ```python from rosettes import highlight, highlight_many, supports_language class CodeHighlighter: def highlight(self, code: str, language: str) -> str: if not supports_language(language): language = "plaintext" return highlight(code, language, css_class_style="pygments") def highlight_with_lines( self, code: str, language: str, hl_lines: set[int], ) -> str: if not supports_language(language): language = "plaintext" return highlight( code, language, show_linenos=True, hl_lines=hl_lines, css_class_style="pygments", ) def highlight_batch( self, blocks: list[tuple[str, str]], ) -> list[str]: # Validate languages validated = [ (code, lang if supports_language(lang) else "plaintext") for code, lang in blocks ] return highlight_many(validated, css_class_style="pygments") ``` -------------------------------- ### Minimal Lexer Implementation Example (Python) Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/about/architecture.md Provides a minimal example of a custom lexer class inheriting from StateMachineLexer. It demonstrates the required methods `tokenize` and `tokenize_fast`, outlining the basic structure for tokenizing code. ```Python from collections.abc import Iterator from rosettes._config import LexerConfig from rosettes._types import Token, TokenType from rosettes.lexers._state_machine import StateMachineLexer class MyLangStateMachineLexer(StateMachineLexer): name = "mylang" aliases = ("ml",) def tokenize( self, code: str, config: LexerConfig | None = None, *, start: int = 0, end: int | None = None, ) -> Iterator[Token]: if end is None: end = len(code) pos = start line = 1 col = 1 while pos < end: char = code[pos] # State machine logic here yield Token(TokenType.TEXT, char, line, col) pos += 1 col += 1 def tokenize_fast( self, code: str, start: int = 0, end: int | None = None, ) -> Iterator[tuple[TokenType, str]]: # Yields (type, value) tuples without line/col tracking if end is None: end = len(code) pos = start while pos < end: yield (TokenType.TEXT, code[pos]) pos += 1 ``` -------------------------------- ### Python: Highlight with Hybrid CSS Classes Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/styling/css-classes.md Demonstrates how to use the `highlight` function in Python with the `semantic-hybrid` CSS class style. This emits both role and token-type classes, allowing themes to differentiate elements like built-in functions from user-defined ones. The output includes example HTML class attributes. ```python from rosettes import highlight html = highlight("print(x)", "python", css_class_style="semantic-hybrid") print("syntax-function" in html and "syntax-name-builtin" in html) ``` -------------------------------- ### Implement Custom Formatter Protocol in Python Source: https://context7.com/lbliii/rosettes/llms.txt Demonstrates how to create custom formatters by implementing the Formatter protocol. Includes examples for an ANSI terminal color formatter and a JSON token dump formatter. ```python from collections.abc import Iterator from dataclasses import dataclass from rosettes import highlight, tokenize, Token, TokenType, FormatConfig import json @dataclass(frozen=True, slots=True) class SimpleAnsiFormatter: def format(self, tokens: Iterator[Token], config: FormatConfig | None = None) -> Iterator[str]: for token in tokens: yield token.value @dataclass(frozen=True, slots=True) class JsonFormatter: def format(self, tokens: Iterator[Token], config: FormatConfig | None = None) -> Iterator[str]: token_list = [{"type": t.type.name, "value": t.value} for t in tokens] yield json.dumps(token_list, indent=2) output = highlight("def foo(): pass", "python", formatter=SimpleAnsiFormatter()) json_output = highlight("x = 1", "python", formatter=JsonFormatter()) ``` -------------------------------- ### Swift Property Wrappers Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Introduces Swift's property wrappers with the `Clamped` example, which constrains a value within a specified range. This demonstrates how to encapsulate reusable property logic. ```swift @propertyWrapper struct Clamped { var value: Value let range: ClosedRange var wrappedValue: Value { get { value } set { value = min(max(newValue, range.lowerBound), range.upperBound) } } } ``` -------------------------------- ### Styling highlighted lines with CSS Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/highlighting/line-highlighting.md Provides CSS examples to customize the appearance of highlighted lines using the .hll class. ```css /* Subtle background highlight */ .rosettes .hll { background-color: rgba(255, 255, 0, 0.1); display: block; } /* Or with a border */ .rosettes .hll { border-left: 3px solid #f1fa8c; padding-left: 0.5em; margin-left: -0.5em; } ``` -------------------------------- ### Profile Performance with cProfile and timeit Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/about/performance.md Provides examples for measuring the performance of Rosettes highlighting using standard Python profiling and benchmarking tools. ```python import cProfile from rosettes import highlight code = open("large_file.py").read() cProfile.run('highlight(code, "python")', sort="cumtime") import timeit code = "def foo(): pass\n" * 10000 time = timeit.timeit( lambda: highlight(code, "python"), number=100, ) print(f"Average: {time/100*1000:.2f}ms") ``` -------------------------------- ### GET /languages Source: https://context7.com/lbliii/rosettes/llms.txt Retrieves the registry of supported programming languages and verifies support for specific identifiers. ```APIDOC ## GET /languages ### Description Returns a list of all supported language names or checks if a specific language or alias is supported by the engine. ### Method GET ### Endpoint /languages ### Parameters #### Query Parameters - **check** (string) - Optional - Language name or alias to verify. ### Response #### Success Response (200) - **languages** (list[string]) - A sorted list of supported language names. - **supported** (boolean) - Returned if 'check' parameter is provided. #### Response Example { "languages": ["bash", "c", "python", "javascript"], "supported": true } ``` -------------------------------- ### Integrate parallel highlighting in a static site generator Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/highlighting/parallel.md A practical example showing how to collect code blocks from multiple pages, process them in parallel using highlight_many, and map the results back to the original data structure. ```python from rosettes import highlight_many from pathlib import Path def highlight_all_code_blocks(pages: list[dict]) -> list[dict]: """Highlight all code blocks across all pages.""" # Collect all code blocks blocks = [] block_locations = [] # Track which page/block each belongs to for page_idx, page in enumerate(pages): for block_idx, block in enumerate(page["code_blocks"]): blocks.append((block["code"], block["language"])) block_locations.append((page_idx, block_idx)) # Highlight in parallel results = highlight_many(blocks) # Assign results back to pages for (page_idx, block_idx), html in zip(block_locations, results): pages[page_idx]["code_blocks"][block_idx]["html"] = html return pages ``` -------------------------------- ### HCL (Terraform) Configuration Example Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md This snippet represents a basic HCL (HashiCorp Configuration Language) structure, commonly used with Terraform. It typically defines resources, variables, and outputs for infrastructure as code. ```hcl ``` -------------------------------- ### GET /profiling/summary Source: https://context7.com/lbliii/rosettes/llms.txt Provides performance metrics for highlighting operations, including execution time per language and per call. ```APIDOC ## GET /profiling/summary ### Description Retrieves detailed performance metrics for syntax highlighting operations, useful for identifying bottlenecks in processing. ### Method GET ### Endpoint /profiling/summary ### Response #### Success Response (200) - **total_ms** (float) - Total execution time in milliseconds. - **by_language** (object) - Breakdown of performance metrics by language. #### Response Example { "total_ms": 5.2, "by_language": { "python": {"count": 1, "total_ms": 1.8} } } ``` -------------------------------- ### JavaScript Keywords and Constructs Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-test-hardening.md Provides examples of core JavaScript syntax, including if statements, for...of loops with continue/break, function declarations, class definitions with inheritance, asynchronous functions using async/await, and import/export statements. ```javascript if (true) { const x = 1; let y = 2; var z = 3; } for (const item of items) { continue; break; } function greet(name) { return `Hello ${name}`; } class MyClass extends Base { constructor() { super(); } } async function fetchData() { await fetch(url); } import { x } from "module"; export default MyClass; ``` -------------------------------- ### CSV Data File Example Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md A simple CSV (Comma Separated Values) file containing header information and data rows. This format is widely used for tabular data exchange. ```csv name,age,email Alice,30,alice@example.com Bob,25,bob@example.com ``` -------------------------------- ### Highlight Code with Null Formatter (Python) Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/formatters/null.md Demonstrates how to use the Null Formatter in Python to get the raw, unformatted code string. This is useful for benchmarking lexer performance or when no styling is needed. ```python from rosettes import highlight code = "def hello(): pass" raw = highlight(code, "python", formatter="null") print(raw == code) # True ``` -------------------------------- ### Python Fixture vs. Property Test Example Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Illustrates the difference between property tests (verifying invariants) and fixture tests (verifying correct token classification) in Python. Property tests ensure no characters are dropped or crashes occur, while fixture tests confirm specific tokens are assigned the correct types. ```python # Property test: ✅ passes for "
" tokenized as anything assert "".join(t.value for t in tokens) == "
" # Fixture test: ❌ fails if "
" is tokenized as ERROR instead of NAME_TAG assert tokens[1].type == TokenType.NAME_TAG ``` -------------------------------- ### Define Complete SyntaxPalette Scheme in Python Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/extending/custom-palettes.md This example demonstrates the structure of the `SyntaxPalette` dataclass, defining colors for a comprehensive set of semantic roles including control flow, data literals, identifiers, documentation, and feedback. It also shows how to set style modifiers like bold and italic for specific elements. The `SyntaxPalette` class is imported from `rosettes.themes`. ```python from rosettes.themes import SyntaxPalette palette = SyntaxPalette( # Required fields name="ocean-dark", background="#0a192f", text="#8892b0", # Control & Structure control_flow="#ff79c6", # if, for, while, return declaration="#bd93f9", # def, class, let, const import_="#ff79c6", # import, from, use # Data & Literals string="#50fa7b", # "hello", 'world' number="#f1fa8c", # 42, 3.14 boolean="#bd93f9", # True, False # Identifiers type_="#8be9fd", # int, str, MyClass function="#50fa7b", # function names variable="#f8f8f2", # variable names constant="#bd93f9", # CONSTANTS # Documentation comment="#6272a4", # # comments docstring="#6272a4", # """docstrings""" # Feedback (diffs, errors) error="#ff5555", warning="#ffb86c", added="#50fa7b", removed="#ff5555", # Additional muted="#6272a4", # less important elements punctuation="#f8f8f2", # brackets, commas operator="#ff79c6", # +, -, *, / attribute="#50fa7b", # @decorator, .attribute namespace="#f8f8f2", # module.submodule tag="#ff79c6", # HTML/XML tags regex="#f1fa8c", # regular expressions escape="#bd93f9", # escape sequences # Style modifiers bold_control=True, # bold keywords bold_declaration=True, # bold def/class bold_type=False, # bold type names (optional) italic_comment=True, # italic comments italic_docstring=True, # italic docstrings italic_variable=False, # italic variables (optional) ) ``` -------------------------------- ### Python Language Keywords and Constructs Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-test-hardening.md Demonstrates fundamental Python keywords like if/elif/else, for loops with continue/break, function and class definitions, exception handling (try/except), and import statements. It also includes an example of an asynchronous function. ```python if True: pass elif False: pass else: pass for x in range(10): continue break def func(): return None class MyClass: pass try: raise Exception except Exception: pass import os from sys import path async def async_func(): await something() ``` -------------------------------- ### Install Rosettes using pip Source: https://github.com/lbliii/rosettes/blob/main/site/content/releases/0.2.0.md Installs the Rosettes package with a version greater than or equal to 0.2.0. Requires Python 3.14 or higher. ```bash pip install rosettes>=0.2.0 ``` -------------------------------- ### Basic Syntax Highlighting with Rosettes Source: https://github.com/lbliii/rosettes/blob/main/site/content/releases/0.1.0.md Demonstrates basic syntax highlighting using the `highlight` function from the Rosettes library. It shows how to generate HTML output by default and how to specify terminal output. ```python from rosettes import highlight # HTML output (default) html = highlight("def hello(): print('world')", "python") # Terminal output ansi = highlight("def hello(): print('world')", "python", formatter="terminal") ``` -------------------------------- ### Retrieve Formatter Instance (Python) Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/formatters/_index.md Demonstrates how to get an instance of a specific formatter using its name with the `get_formatter()` function and access its properties. ```python from rosettes import get_formatter formatter = get_formatter("terminal") print(formatter.name) # 'terminal' ``` -------------------------------- ### Dockerfile: Multi-Stage Build for Frontend and Nginx Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Demonstrates a multi-stage Docker build. The first stage builds a Node.js frontend application, and the second stage copies the built artifacts into an Nginx image for serving. ```dockerfile FROM node:20 AS frontend WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM nginx:alpine COPY --from=frontend /app/dist /usr/share/nginx/html ``` -------------------------------- ### Verify Migration and HTML Highlighting Source: https://context7.com/lbliii/rosettes/llms.txt Demonstrates how to generate HTML-formatted code snippets using the highlight function. It verifies that the output contains expected Pygments-compatible CSS classes. ```python html = highlight("def foo(): pass", "python", css_class_style="pygments") assert 'def' in html assert 'foo' in html ``` -------------------------------- ### Execute Fixture Generation Script Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Commands to run the fixture generation script for specific languages or to update the entire suite. Requires the uv package manager. ```bash uv run python scripts/generate_fixtures.py --language java uv run python scripts/generate_fixtures.py --update ``` -------------------------------- ### Kotlin Basics: Data Class and Main Function Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Defines a data class `User` in Kotlin and a `main` function to demonstrate its usage. It highlights Kotlin's concise syntax for properties and string templating. ```kotlin data class User( val id: Int, val name: String, val email: String? ) fun main() { val user = User(1, "Alice", "alice@example.com") println("Hello, ${user.name}!") } ``` -------------------------------- ### Running Parallel Highlighting Benchmark Source: https://github.com/lbliii/rosettes/blob/main/README.md Shows how to execute a Python script to benchmark the parallel highlighting capabilities of Rosettes. This helps in understanding the performance scaling on multi-core systems. ```bash python benchmarks/benchmark_parallel.py ``` -------------------------------- ### Swift Basics: User Struct and UserService Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Defines a Codable User struct and a UserService class in Swift for fetching user data from an API. It uses async/await for asynchronous operations and URLSession for network requests. ```swift import Foundation struct User: Codable { let id: Int var name: String var email: String? } class UserService { func fetchUser(id: Int) async throws -> User { let url = URL(string: "https://api.example.com/users/(id)")! let (data, _) = try await URLSession.shared.data(from: url) return try JSONDecoder().decode(User.self, from: data) } } ``` -------------------------------- ### Access lexers and formatters Source: https://context7.com/lbliii/rosettes/llms.txt Demonstrates how to retrieve specific lexer instances for granular tokenization control and how to manage formatter registries for different output formats. ```python from rosettes import get_lexer, get_formatter, list_formatters # Lexer usage lexer = get_lexer("python") for token in lexer.tokenize("x = 1"): print(f"{token.type}: {token.value!r}") # Formatter usage formatters = list_formatters() html_formatter = get_formatter("html") ``` -------------------------------- ### Adding and styling line numbers Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/highlighting/line-highlighting.md Shows how to enable line numbering using the show_linenos parameter and how to style the resulting line numbers using CSS. ```python html = highlight(code, "python", show_linenos=True) ``` ```css .rosettes .lineno { color: #6272a4; user-select: none; /* Don't include in copy */ padding-right: 1em; text-align: right; min-width: 2em; display: inline-block; } ``` -------------------------------- ### Configure Advanced HTML Output in Python Source: https://context7.com/lbliii/rosettes/llms.txt Shows how to use HtmlFormatter with custom CSS classes, line highlighting, and line numbering configurations. ```python from rosettes import highlight, HighlightConfig from rosettes.formatters import HtmlFormatter config = HighlightConfig( hl_lines=frozenset({1, 2}), show_linenos=True, css_class="my-code" ) formatter = HtmlFormatter(config=config, css_class_style="pygments") html = highlight("def foo():\n pass", "python", formatter=formatter) ``` -------------------------------- ### Elixir Pipe Operator for Data Transformation Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Illustrates the use of the Elixir pipe operator (|>) to chain function calls for data transformation. This example filters positive numbers, doubles them, and then reduces the result to a sum. ```elixir result = data |> Enum.filter(&(&1 > 0)) |> Enum.map(&(&1 * 2)) |> Enum.reduce(0, &+/2) ``` -------------------------------- ### Direct Lexer Access for Python Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/extending/raw-tokens.md Shows how to access the lexer directly using `get_lexer()` for more control. It demonstrates streaming tokenization with `lexer.tokenize()` and a faster method `lexer.tokenize_fast()` which returns only token types and values. ```python from rosettes import get_lexer lexer = get_lexer("python") # Streaming tokenization (iterator) for token in lexer.tokenize("x = 1"): print(token) # Fast path (no position tracking) for token_type, value in lexer.tokenize_fast("x = 1"): print(f"{token_type}: {value!r}") ``` -------------------------------- ### Perform Basic Syntax Highlighting Source: https://github.com/lbliii/rosettes/blob/main/README.md Demonstrates how to generate HTML output from source code strings. Supports optional features like line numbering and highlighting specific line ranges. ```python from rosettes import highlight # Basic highlighting html = highlight("def foo(): pass", "python") # With line numbers html = highlight(code, "python", show_linenos=True) # Highlight specific lines html = highlight(code, "python", hl_lines={2, 3, 4}) ``` -------------------------------- ### Tokenize Python Code Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/extending/raw-tokens.md Demonstrates how to use the `tokenize()` function to get a list of `Token` objects for Python code. It iterates through the tokens and prints their type, value, line, and column. This is useful for basic code inspection. ```python from rosettes import tokenize tokens = tokenize("x = 1 + 2", "python") for token in tokens: print(f"{token.type.name:20} {token.value!r:10} L{token.line}:C{token.column}") ``` -------------------------------- ### Use Custom Formatter Instance with Python Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/extending/custom-formatter.md Illustrates how to pass a custom formatter instance directly to the `highlight` function in Python. This allows for flexible output formatting. ```python from rosettes import highlight formatter = MarkdownFormatter() output = highlight("def foo(): pass", "python", formatter=formatter) ``` -------------------------------- ### Ruby Blocks and Iterators Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Demonstrates the use of blocks and iterators in Ruby, including .each for iteration, .map for transformation, and File.open with a block for file handling. These are common patterns for processing collections and files. ```ruby items.each do |item| puts item end items.map { |x| x * 2 } File.open("file.txt") do |f| f.each_line { |line| process(line) } end ``` -------------------------------- ### Migrate syntax highlighting from Pygments to Rosettes Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/about/comparison.md This snippet demonstrates the transition from the Pygments library to Rosettes. It highlights the simplified API of Rosettes while maintaining compatibility with existing Pygments CSS themes. ```python # Before (Pygments) from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter lexer = get_lexer_by_name("python") formatter = HtmlFormatter() html = highlight(code, lexer, formatter) # After (Rosettes) from rosettes import highlight html = highlight(code, "python", css_class_style="pygments") ``` -------------------------------- ### Get File Extension by Language in Python Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-test-hardening.md A Python utility function that maps a given programming language string to its corresponding file extension. It uses a dictionary lookup and provides a default '.txt' extension if the language is not found. ```python def get_extension(language: str) -> str: """Get file extension for language.""" extensions = { "python": ".py", "javascript": ".js", "typescript": ".ts", "rust": ".rs", "go": ".go", "yaml": ".yaml", "json": ".json", "php": ".php", # ... add others } return extensions.get(language, ".txt") ``` -------------------------------- ### CSS for Language-Specific Styling in Rosettes Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/styling/custom-themes.md Demonstrates how to apply language-specific styles in Rosettes using the `data-language` attribute. Examples show how to style decorators in Python, keyword types in Rust, and JSON keys, allowing for tailored highlighting based on the programming language. ```css /* Python-specific */ .rosettes[data-language="python"] .syntax-decorator { color: #f5c2e7; font-weight: bold; } /* Rust-specific */ .rosettes[data-language="rust"] .syntax-keyword-type { color: #fab387; } /* JSON keys */ .rosettes[data-language="json"] .syntax-attribute { color: #89b4fa; } ``` -------------------------------- ### Register, List, and Get Palettes in Python Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/extending/custom-palettes.md This snippet illustrates the core palette registry operations in Rosettes. It shows how to register a palette using `register_palette`, list all available palettes with `list_palettes`, and retrieve a specific palette by its name using `get_palette`. These functions are all part of the `rosettes.themes` module. ```python from rosettes.themes import register_palette, list_palettes, get_palette # Assuming my_palette is already defined # register_palette(my_palette) print(list_palettes()) # ['bengal-tiger', 'bengal-snow-lynx', 'dracula', 'monokai', ...] palette = get_palette("monokai") print(palette.background) # #272822 ``` -------------------------------- ### Ruby Symbols and Method Arguments Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Illustrates Ruby's symbol syntax for hash keys and method arguments, including splat operators (*args) for variable arguments and keyword arguments (**kwargs), along with block passing (&block). ```ruby options = { name: "test", :legacy => "value", enabled: true } def method(arg, *args, **kwargs, &block) yield if block_given? end ``` -------------------------------- ### Dark Theme CSS for Semantic Classes Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/styling/_index.md Provides a basic CSS stylesheet for styling Rosettes-generated HTML with semantic classes. This example includes styles for the main container, preformatted text, and common syntax elements like keywords, functions, strings, comments, and numbers, creating a dark theme appearance. ```css /* Dark theme basics */ .rosettes { background: #282a36; padding: 1em; border-radius: 4px; overflow-x: auto; } .rosettes pre { margin: 0; font-family: 'Fira Code', monospace; } .syntax-keyword { color: #ff79c6; } .syntax-function { color: #50fa7b; } .syntax-string { color: #f1fa8c; } .syntax-comment { color: #6272a4; } .syntax-number { color: #bd93f9; } ``` -------------------------------- ### Complete CSS Theme Example for Rosettes (Catppuccin Mocha) Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/styling/custom-themes.md Presents a full CSS theme for Rosettes inspired by the Catppuccin Mocha color scheme. It defines CSS variables for each color and then applies them to the core `.rosettes` container and various syntax elements like keywords, functions, and strings. This provides a comprehensive and visually appealing theme. ```css /* Catppuccin Mocha for Rosettes */ .rosettes { --ctp-rosewater: #f5e0dc; --ctp-flamingo: #f2cdcd; --ctp-pink: #f5c2e7; --ctp-mauve: #cba6f7; --ctp-red: #f38ba8; --ctp-maroon: #eba0ac; --ctp-peach: #fab387; --ctp-yellow: #f9e2af; --ctp-green: #a6e3a1; --ctp-teal: #94e2d5; --ctp-sky: #89dceb; --ctp-sapphire: #74c7ec; --ctp-blue: #89b4fa; --ctp-lavender: #b4befe; --ctp-text: #cdd6f4; --ctp-subtext1: #bac2de; --ctp-subtext0: #a6adc8; --ctp-overlay2: #9399b2; --ctp-overlay1: #7f849c; --ctp-overlay0: #6c7086; --ctp-surface2: #585b70; --ctp-surface1: #45475a; --ctp-surface0: #313244; --ctp-base: #1e1e2e; --ctp-mantle: #181825; --ctp-crust: #11111b; background: var(--ctp-base); color: var(--ctp-text); padding: 1em; border-radius: 8px; overflow-x: auto; } .syntax-keyword { color: var(--ctp-mauve); } .syntax-keyword-constant { color: var(--ctp-peach); } .syntax-function { color: var(--ctp-blue); } .syntax-class { color: var(--ctp-yellow); } .syntax-string { color: var(--ctp-green); } .syntax-number { color: var(--ctp-peach); } .syntax-comment { color: var(--ctp-overlay0); font-style: italic; } .syntax-operator { color: var(--ctp-sky); } .syntax-decorator { color: var(--ctp-pink); } .syntax-builtin { color: var(--ctp-red); } ``` -------------------------------- ### Highlight and Tokenize Code with Rosettes Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/highlighting/_index.md Demonstrates the basic usage of the highlight and tokenize functions to process source code. The highlight function returns an HTML string, while tokenize returns a list of token objects for custom analysis. ```python from rosettes import highlight, tokenize # Get HTML output html = highlight("def foo(): pass", "python") # Get raw tokens tokens = tokenize("def foo(): pass", "python") for token in tokens: print(f"{token.type}: {token.value!r}") ``` -------------------------------- ### Highlight code with HTML formatter Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/formatters/html.md Demonstrates the basic usage of the highlight function to generate HTML output. It shows both the default behavior and explicit formatter selection. ```python from rosettes import highlight html = highlight(code, "python") # or explicitly: html = highlight(code, "python", formatter="html") ``` -------------------------------- ### Highlight Code Snippet in Python Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/extending/custom-formatter.md Demonstrates basic code highlighting using the `highlight` function from the rosettes library. It takes the code string, language, and an optional formatter as input. ```python from rosettes import highlight output = highlight("x = 1", "python", formatter=MarkdownFormatter()) ``` -------------------------------- ### Parallel Syntax Highlighting with Rosettes Source: https://github.com/lbliii/rosettes/blob/main/site/content/releases/0.1.0.md Illustrates parallel syntax highlighting using the `highlight_many` function for processing multiple code blocks concurrently. This is useful for improving performance when dealing with numerous code snippets. ```python from rosettes import highlight_many blocks = [ ("def foo(): pass", "python"), ("const x = 1;", "javascript"), ] results = highlight_many(blocks) ``` -------------------------------- ### Running Mutation Tests Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-test-hardening.md Command to execute mutation tests using 'mutmut'. It targets the 'src/rosettes/lexers/' directory to assess the quality of tests specifically for the lexer components. ```bash uv run mutmut run --paths-to-mutate=src/rosettes/lexers/ ``` -------------------------------- ### Scala Functional Programming Patterns Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Demonstrates Scala features including case classes, Future-based asynchronous services, pattern matching, implicit classes for type enrichment, and for-comprehensions for monadic composition. ```scala case class User(id: Long, name: String, email: Option[String]) def process(value: Any): String = value match { case i: Int if i > 0 => s"Positive: $i" case _ => "Unknown" } implicit class RichString(s: String) { def isPalindrome: Boolean = s == s.reverse } for { user <- findUser(id) profile <- fetchProfile(user.id) } yield UserDetails(user, profile) ``` -------------------------------- ### Perl Scripting and Regex Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-fixture-expansion.md Covers basic Perl syntax, regular expression operations for matching and substitution, and the use of references for complex data structures like anonymous arrays and hashes. ```perl my $name = "World"; say "Hello, $name!"; if ($text =~ /pattern/) { print "Match!\n"; } $text =~ s/old/new/g; my $scalar_ref = \$scalar; my $anon_array = [1, 2, 3]; $array_ref->[0]; ``` -------------------------------- ### GitHub Actions Workflow for Testing Source: https://github.com/lbliii/rosettes/blob/main/plan/rfc-test-hardening.md This workflow defines the CI process for the Rosettes project, using GitHub Actions to checkout code, set up the 'uv' environment, and run different pytest suites. It includes steps for fast tests, property tests, and optional slow tests. ```yaml jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: astral-sh/setup-uv@v4 - name: Run fast tests run: uv run pytest tests/ -m "not slow and not property" - name: Run property tests run: uv run pytest tests/ -m property --hypothesis-seed=0 - name: Run slow tests (optional) if: github.event_name == 'push' && github.ref == 'refs/heads/main' run: uv run pytest tests/ -m slow ``` -------------------------------- ### Print Hello function in Python Source: https://github.com/lbliii/rosettes/blob/main/tests/fixtures/markdown/basics.md A simple function that prints the string 'Hello' to the console. This serves as a basic entry point or test function for the project. ```python def hello(): print("Hello") ``` -------------------------------- ### Create Markdown Fenced Formatter Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/extending/custom-formatter.md An implementation of the Formatter protocol that wraps token output within Markdown fenced code blocks. ```python @dataclass(frozen=True, slots=True) class MarkdownFormatter: @property def name(self) -> str: return "markdown" def format(self, tokens, config=None): lang = config.data_language if config else "" yield f"```{lang}\n" for token in tokens: yield token.value yield "\n```" def format_fast(self, tokens, config=None): lang = config.data_language if config else "" yield f"```{lang}\n" for _, value in tokens: yield value yield "\n```" def format_string(self, tokens, config=None): return "".join(self.format(tokens, config)) def format_string_fast(self, tokens, config=None): return "".join(self.format_fast(tokens, config)) ``` -------------------------------- ### Advanced HTML formatter configuration Source: https://github.com/lbliii/rosettes/blob/main/site/content/docs/formatters/html.md Illustrates how to instantiate a custom HtmlFormatter with specific HighlightConfig settings for line highlighting and custom CSS classes. ```python from rosettes import highlight from rosettes.formatters import HtmlFormatter from rosettes import HighlightConfig # Custom configuration config = HighlightConfig( hl_lines={1, 2}, hl_line_class="my-highlight", lineno_class="my-lineno" ) formatter = HtmlFormatter(config=config, css_class_style="pygments") html = highlight(code, "python", formatter=formatter) ```