Skip to content

Commit a59d414

Browse files
authored
Replace usage of cgi for ruby 3.5 compatibility (#2131)
In Ruby 3.5, most of the cgi gem is removed. That includes `CGI.parse`, which `rouge` uses. But, we can subsistute it with `URI.decode_www_form`. The data is returned in a slightly different format (array of arrays instead of a hash), so do some processing to get back into the expected structure. https://bugs.ruby-lang.org/issues/21258
1 parent 327071f commit a59d414

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

lib/rouge/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# to use this module, require 'rouge/cli'.
66

77
require 'rbconfig'
8+
require 'uri'
89

910
module Rouge
1011
class FileReader
@@ -348,7 +349,7 @@ def run
348349
end
349350

350351
private_class_method def self.parse_cgi(str)
351-
pairs = CGI.parse(str).map { |k, v| [k.to_sym, v.first] }
352+
pairs = URI.decode_www_form(str).map { |k, v| [k.to_sym, v] }
352353
Hash[pairs]
353354
end
354355
end

lib/rouge/formatters/html_legacy.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# -*- coding: utf-8 -*- #
22
# frozen_string_literal: true
33

4-
# stdlib
5-
require 'cgi'
6-
74
module Rouge
85
module Formatters
96
# Transforms a token stream into HTML output.

lib/rouge/lexer.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
# stdlib
55
require 'strscan'
6-
require 'cgi'
76
require 'set'
7+
require 'uri'
88

99
module Rouge
1010
# @abstract
@@ -52,17 +52,19 @@ def lookup_fancy(str, code=nil, default_options={})
5252
name, opts = str ? str.split('?', 2) : [nil, '']
5353

5454
# parse the options hash from a cgi-style string
55-
opts = CGI.parse(opts || '').map do |k, vals|
56-
val = case vals.size
55+
cgi_opts = Hash.new { |hash, key| hash[key] = [] }
56+
URI.decode_www_form(opts || '').each do |k, val|
57+
cgi_opts[k] << val
58+
end
59+
cgi_opts.transform_values! do |vals|
60+
case vals.size
5761
when 0 then true
5862
when 1 then vals[0]
5963
else vals
6064
end
61-
62-
[ k.to_s, val ]
6365
end
6466

65-
opts = default_options.merge(Hash[opts])
67+
opts = default_options.merge(cgi_opts)
6668

6769
lexer_class = case name
6870
when 'guess', nil

0 commit comments

Comments
 (0)