pygments / pygments/pygments

RTF formatter creates incomplete RTF code sequences

Open
#2,109 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.2k
Forks
885
PR merge metrics
No merged PRs in 30d

Description

Hi,

I use pygments for converting code sniplets to RTF. I observed a difference between the version 2.4.2 and 2.5.1+. In the newer versions some RTF codes are missing, especially round and curly brackets are ignored.

See the following Java code line which I want to highlight:

 for ( int i = 0; i < nl.getLength(); i++ ) {

Here, the Python code:

lexer = get_lexer_by_name("java") 
formatter = RtfFormatter(style = "xcode")
print(highlight(code, lexer, formatter))

Dependent on which version I install, I get two different results:

 2.4.2:   
{\cf7 for} {\cf6 (} {\cf7 int} {\cf6 i} {\cf6 =} {\cf3 0}{\cf6 ;} {\cf6 i} {\cf6 <} {\cf6 nl}{\cf6 .}{\cf8 getLength}{\cf6 (}{\cf6 )}{\cf6 ;} {\cf6 i}{\cf6 +}{\cf6 +} {\cf6 )} {\cf6 \{}\par

and

2.5.1 and higher :  
{\cf7 for} ( {\cf7 int} {\cf6 i} {\cf6 =} {\cf3 0}; {\cf6 i} {\cf6 <} {\cf6 nl}.{\cf8 getLength}(); {\cf6 i}{\cf6 +}{\cf6 +} ) \{\par

You see the newer version lacks some RTF color codes. In comparison to current SVG and JPEG and older RTF output the first round bracket in Java's for ( has the same color than the for statement, but it must be a different one (as in version 2.4.2).

The problem concerns other styles and languages, too.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the Java snippet, get_lexer_by_name("java"), and RtfFormatter(style="xcode"), comparing Pygments 2.4.2 with 2.5.1 or later. Trace how RtfFormatter emits color codes for parentheses and braces; done means punctuation retains the expected styling across the affected styles and languages.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.