fix: crashes in --latex --inline when there is no colour to apply by Thorsrud22 · Pull Request #275 · pycontribs/ansi2html · GitHub
Skip to content

fix: crashes in --latex --inline when there is no colour to apply - #275

Open
Thorsrud22 wants to merge 1 commit into
pycontribs:mainfrom
Thorsrud22:latex-inline-crashes
Open

Thorsrud22 wants to merge 1 commit into
pycontribs:mainfrom
Thorsrud22:latex-inline-crashes

Conversation

@Thorsrud22

Copy link
Copy Markdown

Fixes #127. Fixes #252. Supersedes #253.

The inline LaTeX branch in _handle_ansi_code assumed every active CSS class had a style rule and that at least one of them was a colour. Two inputs break that assumption:

#253 addressed the KeyError with a .get() guard, but as @hartwork noted in review it then falls through to the same IndexError as #127. This PR handles both in one place.

Change

  • Collect colours only from classes that actually have a rule, looking for a color property rather than assuming it is the first one.
  • When there is no colour, emit a plain { instead of \textcolor[HTML]{...}{. The closing } emitted later when the span ends keeps the output balanced, and a bare group is a no-op in LaTeX.

Coloured output is unchanged: ESC[33mYELLOW still renders as \textcolor[HTML]{aa5500}{YELLOW}.

Verification

🤖 Generated with Claude Code

@Thorsrud22 Thorsrud22 changed the title Fix --latex --inline crashes when there is no colour to apply fix: crashes in --latex --inline when there is no colour to apply Sep 11, 2026
@github-actions github-actions Bot added the fix This issue/PR relates to a bug. label Sep 11, 2026
The inline LaTeX branch assumed every active CSS class had a style rule
and that at least one of them was a colour. Two inputs broke that:

- italic-only text (ESC[3m): the class "ansi3" has a font-style rule
  but no colour, so indexing the first colour raised IndexError (pycontribs#127)
- a foreground code with no parameter (ESC[38m): the class "ansi38"
  has no rule at all, so the lookup raised KeyError (pycontribs#252)

Collect colours only from classes that have a rule, and when there is
no colour open a plain LaTeX group instead of \textcolor so the closing
brace emitted later stays balanced.

Fixes pycontribs#127
Fixes pycontribs#252

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix This issue/PR relates to a bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[>=1.7.0] Crash for latex inline mode Crash for --latex and italic ^[[3m

1 participant