Skip to content

Commit

Permalink
Fix double load warnings by using require_relative
Browse files Browse the repository at this point in the history
load will load, always. Don't use it unless you really really mean it.
  • Loading branch information
zenspider authored and tancnle committed Mar 18, 2024
1 parent c0e5f08 commit b595d66
Show file tree
Hide file tree
Showing 46 changed files with 95 additions and 157 deletions.
89 changes: 45 additions & 44 deletions lib/rouge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def highlight(text, lexer, formatter, &b)
#
# @api private
def load_file(path)
Kernel::load File.join(LIB_DIR, "rouge/#{path}.rb")
warn "just use require_relative %p" % ["rouge/#{path}"]
require_relative "rouge/#{path}"
end

# Load the lexers in the `lib/rouge/lexers` directory.
Expand All @@ -55,52 +56,52 @@ def load_lexers
end
end

Rouge.load_file 'version'
Rouge.load_file 'util'
Rouge.load_file 'text_analyzer'
Rouge.load_file 'token'
require_relative 'rouge/version'
require_relative 'rouge/util'
require_relative 'rouge/text_analyzer'
require_relative 'rouge/token'

Rouge.load_file 'lexer'
Rouge.load_file 'regex_lexer'
Rouge.load_file 'template_lexer'
require_relative 'rouge/lexer'
require_relative 'rouge/regex_lexer'
require_relative 'rouge/template_lexer'

Rouge.load_lexers

Rouge.load_file 'guesser'
Rouge.load_file 'guessers/util'
Rouge.load_file 'guessers/glob_mapping'
Rouge.load_file 'guessers/modeline'
Rouge.load_file 'guessers/filename'
Rouge.load_file 'guessers/mimetype'
Rouge.load_file 'guessers/source'
Rouge.load_file 'guessers/disambiguation'
require_relative 'rouge/guesser'
require_relative 'rouge/guessers/util'
require_relative 'rouge/guessers/glob_mapping'
require_relative 'rouge/guessers/modeline'
require_relative 'rouge/guessers/filename'
require_relative 'rouge/guessers/mimetype'
require_relative 'rouge/guessers/source'
require_relative 'rouge/guessers/disambiguation'

Rouge.load_file 'formatter'
Rouge.load_file 'formatters/html'
Rouge.load_file 'formatters/html_table'
Rouge.load_file 'formatters/html_pygments'
Rouge.load_file 'formatters/html_legacy'
Rouge.load_file 'formatters/html_linewise'
Rouge.load_file 'formatters/html_line_highlighter'
Rouge.load_file 'formatters/html_line_table'
Rouge.load_file 'formatters/html_inline'
Rouge.load_file 'formatters/terminal256'
Rouge.load_file 'formatters/terminal_truecolor'
Rouge.load_file 'formatters/tex'
Rouge.load_file 'formatters/null'
require_relative 'rouge/formatter'
require_relative 'rouge/formatters/html'
require_relative 'rouge/formatters/html_table'
require_relative 'rouge/formatters/html_pygments'
require_relative 'rouge/formatters/html_legacy'
require_relative 'rouge/formatters/html_linewise'
require_relative 'rouge/formatters/html_line_highlighter'
require_relative 'rouge/formatters/html_line_table'
require_relative 'rouge/formatters/html_inline'
require_relative 'rouge/formatters/terminal256'
require_relative 'rouge/formatters/terminal_truecolor'
require_relative 'rouge/formatters/tex'
require_relative 'rouge/formatters/null'

Rouge.load_file 'theme'
Rouge.load_file 'tex_theme_renderer'
Rouge.load_file 'themes/thankful_eyes'
Rouge.load_file 'themes/colorful'
Rouge.load_file 'themes/base16'
Rouge.load_file 'themes/github'
Rouge.load_file 'themes/igor_pro'
Rouge.load_file 'themes/monokai'
Rouge.load_file 'themes/molokai'
Rouge.load_file 'themes/monokai_sublime'
Rouge.load_file 'themes/gruvbox'
Rouge.load_file 'themes/tulip'
Rouge.load_file 'themes/pastie'
Rouge.load_file 'themes/bw'
Rouge.load_file 'themes/magritte'
require_relative 'rouge/theme'
require_relative 'rouge/tex_theme_renderer'
require_relative 'rouge/themes/thankful_eyes'
require_relative 'rouge/themes/colorful'
require_relative 'rouge/themes/base16'
require_relative 'rouge/themes/github'
require_relative 'rouge/themes/igor_pro'
require_relative 'rouge/themes/monokai'
require_relative 'rouge/themes/molokai'
require_relative 'rouge/themes/monokai_sublime'
require_relative 'rouge/themes/gruvbox'
require_relative 'rouge/themes/tulip'
require_relative 'rouge/themes/pastie'
require_relative 'rouge/themes/bw'
require_relative 'rouge/themes/magritte'
7 changes: 1 addition & 6 deletions lib/rouge/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,8 @@ def self.detect?(text)
end

module Lexers
BASE_DIR = "#{__dir__}/lexers".freeze
@_loaded_lexers = {}

def self.load_lexer(relpath)
return if @_loaded_lexers.key?(relpath.to_s)
@_loaded_lexers[relpath.to_s] = true
Kernel::load File.join(BASE_DIR, relpath)
require_relative "lexers/#{relpath}"
end
end
end
16 changes: 1 addition & 15 deletions lib/rouge/lexers/apache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,7 @@ class Apache < RegexLexer
mimetypes 'text/x-httpd-conf', 'text/x-apache-conf'
filenames '.htaccess', 'httpd.conf'

# self-modifying method that loads the keywords file
def self.directives
Kernel::load File.join(Lexers::BASE_DIR, 'apache/keywords.rb')
directives
end

def self.sections
Kernel::load File.join(Lexers::BASE_DIR, 'apache/keywords.rb')
sections
end

def self.values
Kernel::load File.join(Lexers::BASE_DIR, 'apache/keywords.rb')
values
end
require_relative "apache/keywords"

def name_for_token(token, tktype)
if self.class.sections.include? token
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/apiblueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Rouge
module Lexers
load_lexer 'markdown.rb'
require_relative 'markdown'

class APIBlueprint < Markdown
title 'API Blueprint'
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/biml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Rouge
module Lexers
load_lexer 'xml.rb'
require_relative 'xml'

class BIML < XML
title "BIML"
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/cpp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'c.rb'
require_relative 'c'

class Cpp < C
title "C++"
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/cuda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Rouge
module Lexers
load_lexer 'cpp.rb'
require_relative 'cpp'

class CUDA < Cpp
title "CUDA"
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/cython.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'python.rb'
require_relative 'python'

class Cython < Python
title "Cython"
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/digdag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'set'
module Rouge
module Lexers
load_lexer 'yaml.rb'
require_relative 'yaml'

class Digdag < YAML
title 'digdag'
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/freefem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'cpp.rb'
require_relative 'cpp'

class FreeFEM < Cpp
title "FreeFEM"
Expand Down
6 changes: 1 addition & 5 deletions lib/rouge/lexers/gherkin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ def self.detect?(text)
return true if text.shebang? 'cucumber'
end

# self-modifying method that loads the keywords file
def self.keywords
Kernel::load File.join(Lexers::BASE_DIR, 'gherkin/keywords.rb')
keywords
end
require_relative "gherkin/keywords"

def self.step_regex
# in Gherkin's config, keywords that end in < don't
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/glsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'c.rb'
require_relative 'c'

# This file defines the GLSL language lexer to the Rouge
# syntax highlighter.
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/gradle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'groovy.rb'
require_relative 'groovy'

class Gradle < Groovy
title "Gradle"
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/hack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'php.rb'
require_relative 'php'

class Hack < PHP
title 'Hack'
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/hlsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'c.rb'
require_relative 'c'

class HLSL < C
title "HLSL"
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/hocon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'json.rb'
require_relative 'json'

class HOCON < JSON
title 'HOCON'
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/hql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Rouge
module Lexers
load_lexer 'sql.rb'
require_relative 'sql'

class HQL < SQL
title "HQL"
Expand Down
4 changes: 2 additions & 2 deletions lib/rouge/lexers/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'console.rb'
require_relative 'console'

class IRBLexer < ConsoleLexer
tag 'irb'
Expand Down Expand Up @@ -37,7 +37,7 @@ def allow_comments?
end
end

load_lexer 'ruby.rb'
require_relative 'ruby'
class IRBOutputLexer < Ruby
tag 'irb_output'

Expand Down
5 changes: 1 addition & 4 deletions lib/rouge/lexers/isbl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ class ISBL < RegexLexer
tag 'isbl'
filenames '*.isbl'

def self.builtins
Kernel::load File.join(Lexers::BASE_DIR, 'isbl/builtins.rb')
self.builtins
end
require_relative "isbl/builtins"

def self.constants
@constants ||= self.builtins["const"].merge(self.builtins["enum"]).collect!(&:downcase)
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/json_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Rouge
module Lexers
load_lexer 'json.rb'
require_relative 'json'

class JSONDOC < JSON
desc "JavaScript Object Notation with extensions for documentation"
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/jsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Rouge
module Lexers
load_lexer 'javascript.rb'
require_relative 'javascript'

class JSX < Javascript
title 'JSX'
Expand Down
6 changes: 1 addition & 5 deletions lib/rouge/lexers/lasso.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ def start_inline?
push :lasso if start_inline?
end

# self-modifying method that loads the keywords file
def self.keywords
Kernel::load File.join(Lexers::BASE_DIR, 'lasso/keywords.rb')
keywords
end
require_relative "lasso/keywords"

id = /[a-z_][\w.]*/i

Expand Down
15 changes: 1 addition & 14 deletions lib/rouge/lexers/llvm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,7 @@ class LLVM < RegexLexer
string = /"[^"]*?"/
identifier = /([-a-zA-Z$._][-a-zA-Z$._0-9]*|#{string})/

def self.keywords
Kernel::load File.join(Lexers::BASE_DIR, "llvm/keywords.rb")
keywords
end

def self.instructions
Kernel::load File.join(Lexers::BASE_DIR, "llvm/keywords.rb")
instructions
end

def self.types
Kernel::load File.join(Lexers::BASE_DIR, "llvm/keywords.rb")
types
end
require_relative "llvm/keywords"

state :basic do
rule %r/;.*?$/, Comment::Single
Expand Down
5 changes: 1 addition & 4 deletions lib/rouge/lexers/lua.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ def self.detect?(text)
return true if text.shebang? 'lua'
end

def self.builtins
Kernel::load File.join(Lexers::BASE_DIR, 'lua/keywords.rb')
builtins
end
require_relative "lua/keywords"

def builtins
return [] unless @function_highlighting
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/lutin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# adapted from lustre.rf (adapted from ocaml.rb), hence some ocaml-ism migth remains
module Rouge
module Lexers
load_lexer 'lustre.rb'
require_relative 'lustre'

class Lutin < Lustre
title "Lutin"
Expand Down
6 changes: 1 addition & 5 deletions lib/rouge/lexers/mathematica.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ def self.keywords
)
end

# The list of built-in symbols comes from a wolfram server and is created automatically by rake
def self.builtins
Kernel::load File.join(Lexers::BASE_DIR, 'mathematica/keywords.rb')
builtins
end
require_relative "mathematica/keywords"

state :root do
rule %r/\s+/, Text::Whitespace
Expand Down
Loading

0 comments on commit b595d66

Please sign in to comment.