Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stash base dir paths in module constants #1416

Merged
merged 3 commits into from Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/rouge.rb
Expand Up @@ -6,6 +6,10 @@

# The containing module for Rouge
module Rouge
# cache value in a constant since `__dir__` allocates a new string
# on every call.
LIB_DIR = __dir__.freeze

class << self
def reload!
Object.send :remove_const, :Rouge
Expand Down Expand Up @@ -36,11 +40,11 @@ def highlight(text, lexer, formatter, &b)

# mimic Kernel#require_relative API
def load_relative(path)
load File.join(__dir__, "#{path}.rb")
load File.join(Rouge::LIB_DIR, "#{path}.rb")
end

def lexer_dir(path = '')
File.join(__dir__, 'rouge', 'lexers', path)
File.join(Rouge::LIB_DIR, 'rouge/lexers', path)
end

load_relative 'rouge/version'
Expand Down
3 changes: 2 additions & 1 deletion lib/rouge/lexer.rb
Expand Up @@ -505,12 +505,13 @@ 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)
@_loaded_lexers[relpath] = true
load File.join(__dir__, 'lexers', relpath)
load File.join(BASE_DIR, relpath)
end
end
end
2 changes: 1 addition & 1 deletion lib/rouge/lexers/apache.rb
Expand Up @@ -15,7 +15,7 @@ class << self
attr_reader :keywords
end
# Load Apache keywords from separate YML file
@keywords = ::YAML.load_file(File.join(__dir__, 'apache/keywords.yml')).tap do |h|
@keywords = ::YAML.load_file(File.join(Lexers::BASE_DIR, 'apache/keywords.yml')).tap do |h|
h.each do |k,v|
h[k] = Set.new v
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/gherkin.rb
Expand Up @@ -19,7 +19,7 @@ def self.detect?(text)

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

Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/isbl.rb
Expand Up @@ -10,7 +10,7 @@ class ISBL < RegexLexer
filenames '*.isbl'

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

Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/lasso.rb
Expand Up @@ -39,7 +39,7 @@ class << self
end

# Load Lasso keywords from separate YML file
@keywords = ::YAML.load_file(File.join(__dir__, 'lasso/keywords.yml')).tap do |h|
@keywords = ::YAML.load_file(File.join(Lexers::BASE_DIR, 'lasso/keywords.yml')).tap do |h|
h.each do |k,v|
h[k] = Set.new v
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/lua.rb
Expand Up @@ -25,7 +25,7 @@ def self.detect?(text)
end

def self.builtins
load File.join(__dir__, 'lua/builtins.rb')
load File.join(Lexers::BASE_DIR, 'lua/builtins.rb')
self.builtins
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/mathematica.rb
Expand Up @@ -56,7 +56,7 @@ def self.keywords

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

Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/matlab.rb
Expand Up @@ -20,7 +20,7 @@ def self.keywords

def self.builtins
# Load Matlab keywords from separate YML file
@builtins ||= ::YAML.load_file(File.join(__dir__, 'matlab/builtins.yml')).tap do |a|
@builtins ||= ::YAML.load_file(File.join(Lexers::BASE_DIR, 'matlab/builtins.yml')).tap do |a|
Set.new a
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/php.rb
Expand Up @@ -29,7 +29,7 @@ def initialize(*)
end

def self.builtins
load File.join(__dir__, 'php/builtins.rb')
load File.join(Lexers::BASE_DIR, 'php/builtins.rb')
self.builtins
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/sqf.rb
Expand Up @@ -55,7 +55,7 @@ def self.diag_commands
end

def self.commands
load File.join(__dir__, "sqf/commands.rb")
load File.join(Lexers::BASE_DIR, "sqf/commands.rb")
@commands = self.commands
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/viml.rb
Expand Up @@ -14,7 +14,7 @@ class VimL < RegexLexer
mimetypes 'text/x-vim'

def self.keywords
load File.join(__dir__, 'viml/keywords.rb')
load File.join(Lexers::BASE_DIR, 'viml/keywords.rb')
self.keywords
end

Expand Down