{"id":7243,"library":"fluent-pygments","title":"Pygments Lexer for Fluent","description":"The `fluent-pygments` library provides a Pygments lexer for Fluent (FTL), a powerful localization system designed for natural language. It enables syntax highlighting of Fluent files, particularly useful within documentation generators like Sphinx that leverage Pygments for code rendering. It is a component of the broader `python-fluent` project, which also includes `fluent.syntax` and `fluent.runtime`.","status":"active","version":"1.0","language":"en","source_language":"en","source_url":"https://github.com/projectfluent/python-fluent","tags":["pygments","syntax highlighting","localization","fluent","i18n","ftl"],"install":[{"cmd":"pip install fluent-pygments","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides the core syntax highlighting framework that fluent-pygments extends.","package":"Pygments","optional":false}],"imports":[{"note":"Custom lexers provided by third-party packages are typically imported directly from their package, not from `pygments.lexers`.","wrong":"from pygments.lexers import FluentLexer","symbol":"FluentLexer","correct":"from fluent_pygments.lexer import FluentLexer"}],"quickstart":{"code":"from pygments import highlight\nfrom pygments.formatters import HtmlFormatter, Terminal256Formatter\nfrom fluent_pygments.lexer import FluentLexer\n\nftl_code = \"\"\"\n### A resource comment for the whole file\nmy-key = Localize { -brand-name }\n-brand-name = Fluent\n# $num is the number of strings to localize\nplurals = { $num ->\n    [one] One string\n   *[other] {$num} strings\n}\n\"\"\"\n\n# Highlight to HTML\nhtml_formatter = HtmlFormatter(full=True, style='default')\nhighlighted_html = highlight(ftl_code, FluentLexer(), html_formatter)\n\n# To see output, you might write to a file or print the raw HTML\n# print(highlighted_html)\n\n# Highlight to terminal with 256 colors\nterminal_formatter = Terminal256Formatter(style='monokai')\nhighlighted_terminal = highlight(ftl_code, FluentLexer(), terminal_formatter)\n\nprint(\"\\n--- HTML Output Snippet (not runnable directly) ---\\n\")\nprint(highlighted_html[:500] + \"...\") # Print a snippet for brevity\nprint(\"\\n--- Terminal Output ---\\n\")\nprint(highlighted_terminal)\n","lang":"python","description":"This example demonstrates how to use the `FluentLexer` with Pygments to highlight Fluent (FTL) code. It shows how to output the highlighted code both as a complete HTML document and as colored text for a 256-color terminal. The HTML output typically requires a separate CSS stylesheet for proper rendering, which can be generated using `pygmentize -S <style_name> -f html > style.css`."},"warnings":[{"fix":"Generate a stylesheet using `pygmentize -S <style_name> -f html > style.css` and include it in your HTML, or initialize `HtmlFormatter(noclasses=True)` for inline styles.","message":"Pygments' `HtmlFormatter` (by default) generates HTML with CSS classes, but does not embed the actual CSS styles. For fully styled output, a separate CSS file must be generated and linked, or inline styles must be explicitly enabled (though not recommended for larger outputs).","severity":"gotcha","affected_versions":"All Pygments versions"},{"fix":"Implement strict input size limits and/or apply timeouts to the Pygments highlighting process when handling user-provided content. Limit the number of concurrent highlighting processes.","message":"Processing untrusted or very large Fluent (FTL) input with Pygments can lead to performance issues or potential Denial-of-Service (DoS) attacks if not properly constrained, due to the complexity of regular expression matching in lexers.","severity":"gotcha","affected_versions":"All Pygments versions, including fluent-pygments 1.0"},{"fix":"Upgrade to Python 3.6 or newer. For best compatibility with the `python-fluent` ecosystem, use Python 3.8+.","message":"The broader `python-fluent` ecosystem, of which `fluent-pygments` is a part, has dropped support for older Python versions (e.g., Python 2.7 and 3.5 in `fluent.runtime` and `fluent.syntax` releases after `fluent-pygments` 1.0). While `fluent-pygments` 1.0 itself requires Python >=3.6, ensure your environment aligns with the latest `fluent.*` components if using them together.","severity":"deprecated","affected_versions":"fluent-pygments 1.0 used with older Python versions, especially <3.6"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `fluent-pygments` is installed (`pip install fluent-pygments`). If using Pygments directly in Python, explicitly import `FluentLexer` and pass it to `highlight()`.","cause":"The `FluentLexer` is not properly registered or imported when Pygments attempts to find it by alias. This often happens if `fluent-pygments` is not installed or if the Pygments environment isn't aware of the installed lexer.","error":"pygments.util.ClassNotFound: no lexer for alias 'fluent'"},{"fix":"Install `fluent-pygments` using `pip install fluent-pygments`. Verify the package is in your environment by trying to import it interactively.","cause":"The Python interpreter cannot find the `fluent_pygments.lexer` module. This is typically due to `fluent-pygments` not being installed in the current environment or an incorrect import path.","error":"ModuleNotFoundError: No module named 'fluent_pygments.lexer'"},{"fix":"Generate the stylesheet for your chosen Pygments style (e.g., `pygmentize -S default -f html > default.css`) and link this CSS file in your HTML document. Alternatively, set `noclasses=True` on `HtmlFormatter` to embed inline styles, but this makes the HTML larger.","cause":"The default `HtmlFormatter` only adds CSS classes to the HTML elements, but does not embed the CSS definitions themselves. Without a linked stylesheet, the browser does not know how to style these classes.","error":"Highlighted HTML output is monochrome or lacks expected styling."}]}