lib/rouge.rb in rouge-3.17.0 vs lib/rouge.rb in rouge-3.18.0

- old
+ new

@@ -4,10 +4,14 @@ # stdlib require 'pathname' # 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 load __FILE__ end @@ -29,65 +33,75 @@ formatter = Formatter.find(formatter) unless formatter.respond_to? :format raise "unknown formatter #{formatter}" unless formatter formatter.format(lexer.lex(text), &b) end - end -end -# mimic Kernel#require_relative API -def load_relative(path) - load File.join(__dir__, "#{path}.rb") -end + # Load a file relative to the `lib/rouge` path. + # + # @api private + def load_file(path) + load File.join(LIB_DIR, "rouge/#{path}.rb") + end -def lexer_dir(path = '') - File.join(__dir__, 'rouge', 'lexers', path) + # Load the lexers in the `lib/rouge/lexers` directory. + # + # @api private + def load_lexers + # The trailing slash is necessary to avoid lexers being loaded multiple + # times by `Lexers.load_lexer` + lexer_dir = File.join(LIB_DIR, "rouge/lexers/") + Dir.glob(File.join(lexer_dir, '*.rb')).each do |f| + Lexers.load_lexer(f.sub(lexer_dir, '')) + end + end + end end -load_relative 'rouge/version' -load_relative 'rouge/util' -load_relative 'rouge/text_analyzer' -load_relative 'rouge/token' +Rouge.load_file 'version' +Rouge.load_file 'util' +Rouge.load_file 'text_analyzer' +Rouge.load_file 'token' -load_relative 'rouge/lexer' -load_relative 'rouge/regex_lexer' -load_relative 'rouge/template_lexer' +Rouge.load_file 'lexer' +Rouge.load_file 'regex_lexer' +Rouge.load_file 'template_lexer' -Dir.glob(lexer_dir('*rb')).each { |f| Rouge::Lexers.load_lexer(f.sub(lexer_dir, '')) } +Rouge.load_lexers -load_relative 'rouge/guesser' -load_relative 'rouge/guessers/util' -load_relative 'rouge/guessers/glob_mapping' -load_relative 'rouge/guessers/modeline' -load_relative 'rouge/guessers/filename' -load_relative 'rouge/guessers/mimetype' -load_relative 'rouge/guessers/source' -load_relative 'rouge/guessers/disambiguation' +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' -load_relative 'rouge/formatter' -load_relative 'rouge/formatters/html' -load_relative 'rouge/formatters/html_table' -load_relative 'rouge/formatters/html_pygments' -load_relative 'rouge/formatters/html_legacy' -load_relative 'rouge/formatters/html_linewise' -load_relative 'rouge/formatters/html_line_table' -load_relative 'rouge/formatters/html_inline' -load_relative 'rouge/formatters/terminal256' -load_relative 'rouge/formatters/terminal_truecolor' -load_relative 'rouge/formatters/tex' -load_relative 'rouge/formatters/null' +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_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' -load_relative 'rouge/theme' -load_relative 'rouge/tex_theme_renderer' -load_relative 'rouge/themes/thankful_eyes' -load_relative 'rouge/themes/colorful' -load_relative 'rouge/themes/base16' -load_relative 'rouge/themes/github' -load_relative 'rouge/themes/igor_pro' -load_relative 'rouge/themes/monokai' -load_relative 'rouge/themes/molokai' -load_relative 'rouge/themes/monokai_sublime' -load_relative 'rouge/themes/gruvbox' -load_relative 'rouge/themes/tulip' -load_relative 'rouge/themes/pastie' -load_relative 'rouge/themes/bw' -load_relative 'rouge/themes/magritte' +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'