2019-01-14 12:46:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-13 01:15:13 -04:00
|
|
|
module HighlightJs
|
2019-01-21 04:12:23 -05:00
|
|
|
HIGHLIGHTJS_DIR ||= "#{Rails.root}/vendor/assets/javascripts/highlightjs/"
|
2015-03-13 01:15:13 -04:00
|
|
|
|
|
|
|
def self.languages
|
2019-01-14 12:46:35 -05:00
|
|
|
langs = Dir.glob(HIGHLIGHTJS_DIR + "languages/*.js").map do |path|
|
|
|
|
File.basename(path)[0..-8]
|
2015-03-13 01:15:13 -04:00
|
|
|
end
|
2019-01-14 12:46:35 -05:00
|
|
|
|
|
|
|
langs.sort
|
2015-03-13 01:15:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle(langs)
|
2019-01-14 12:46:35 -05:00
|
|
|
result = File.read(HIGHLIGHTJS_DIR + "highlight.min.js")
|
2015-03-13 01:15:13 -04:00
|
|
|
langs.each do |lang|
|
|
|
|
begin
|
2019-01-14 12:46:35 -05:00
|
|
|
result << "\n" << File.read(HIGHLIGHTJS_DIR + "languages/#{lang}.min.js")
|
2015-03-13 01:15:13 -04:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
# no file, don't care
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.version(lang_string)
|
|
|
|
(@lang_string_cache ||= {})[lang_string] ||=
|
|
|
|
Digest::SHA1.hexdigest(bundle lang_string.split("|"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.path
|
|
|
|
"/highlight-js/#{Discourse.current_hostname}/#{version SiteSetting.highlighted_languages}.js"
|
|
|
|
end
|
|
|
|
end
|