Butcher whitespace to appease tab-hating heretics

This commit is contained in:
Alhadis
2016-09-07 00:23:31 +10:00
parent 09612ae42e
commit 2a4150b104

View File

@@ -4,106 +4,98 @@ require "linguist"
require "json" require "json"
require "yaml" require "yaml"
class GrammarList class GrammarList
ROOT = File.expand_path "../../", __FILE__ ROOT = File.expand_path "../../", __FILE__
def initialize def initialize
@submodules = load_submodules() @submodules = load_submodules()
@sources = load_sources() @sources = load_sources()
@language_names = load_languages() @language_names = load_languages()
end end
# Load .gitmodules
# Load .gitmodules def load_submodules
def load_submodules submodules = {}
submodules = {} submodule_file = File.read("#{ROOT}/.gitmodules")
submodule_file = File.read("#{ROOT}/.gitmodules") pattern = /^\[submodule\s*"([^"]+)"\]$\n((?:^(?!\[).+(?:\n|$))+)/is
pattern = /^\[submodule\s*"([^"]+)"\]$\n((?:^(?!\[).+(?:\n|$))+)/is submodule_file.scan(pattern) do |id, attr|
submodule_file.scan(pattern) do |id, attr| submod = {}
submod = {} submod[:path] = $1 if attr =~ /^\s*path\s*=\s*(.+)$/
submod[:path] = $1 if attr =~ /^\s*path\s*=\s*(.+)$/ submod[:url] = $1 if attr =~ /^\s*url\s*=\s*(.+)$/
submod[:url] = $1 if attr =~ /^\s*url\s*=\s*(.+)$/ submod[:url].gsub!(/\.git$/, "")
submod[:url].gsub!(/\.git$/, "") submod[:short] = shorten(submod[:url])
submod[:short] = shorten(submod[:url]) submodules["#{id}"] = submod
submodules["#{id}"] = submod end
end submodules
submodules end
end
# Grab the name of each language, sorted case-insensitively
def load_languages
# Grab the name of each language, sorted case-insensitively Linguist::Language.all.map(&:name).sort do |a, b|
def load_languages a.downcase() <=> b.downcase()
Linguist::Language.all.map(&:name).sort do |a, b| end
a.downcase() <=> b.downcase() end
end
end # Load grammars.yml
def load_sources
sources = {}
# Load grammars.yml grammars = YAML.load_file("#{ROOT}/grammars.yml")
def load_sources grammars.each do |path, scopes|
sources = {} scopes.each { |scope| sources[scope] = path }
grammars = YAML.load_file("#{ROOT}/grammars.yml") end
grammars.each do |path, scopes| sources
scopes.each { |scope| sources[scope] = path } end
end
sources # Shorten a repository URL
end def shorten(url)
if url =~ /^https?:\/\/(?:www\.)?github\.com\/([^\/]+\/[^\/]+)/i
$1
# Shorten a repository URL elsif url =~ /^https?:\/\/(?:www\.)?(bitbucket|gitlab)\.(?:com|org)\/([^\/]+\/[^\/]+)/i
def shorten(url) "#{$1.downcase()}:#{$2}"
if url =~ /^https?:\/\/(?:www\.)?github\.com\/([^\/]+\/[^\/]+)/i else
$1 url.replace(/^https?:\/\/(?:www\.)?/i, "")
elsif url =~ /^https?:\/\/(?:www\.)?(bitbucket|gitlab)\.(?:com|org)\/([^\/]+\/[^\/]+)/i end
"#{$1.downcase()}:#{$2}" end
else
url.replace(/^https?:\/\/(?:www\.)?/i, "") # Markdown: Generate grammar list
end def to_markdown
end markdown = ""
@language_names.each do |item|
lang = Linguist::Language["#{item}"]
# Markdown: Generate grammar list scope = lang.tm_scope
def to_markdown next if scope == "none"
markdown = "" path = @sources[scope] || scope
@language_names.each do |item| case path
lang = Linguist::Language["#{item}"] when "https://bitbucket.org/Clams/sublimesystemverilog/get/default.tar.gz"
scope = lang.tm_scope short_url = "bitbucket:Clams/sublimesystemverilog"
next if scope == "none" long_url = "https://bitbucket.org/Clams/sublimesystemverilog"
path = @sources[scope] || scope when "http://svn.edgewall.org/repos/genshi/contrib/textmate/Genshi.tmbundle/Syntaxes/Markup%20Template%20%28XML%29.tmLanguage"
case path short_url = "genshi.edgewall.org/query"
when "https://bitbucket.org/Clams/sublimesystemverilog/get/default.tar.gz" long_url = "https://genshi.edgewall.org/query"
short_url = "bitbucket:Clams/sublimesystemverilog" when "vendor/grammars/oz-tmbundle/Syntaxes/Oz.tmLanguage"
long_url = "https://bitbucket.org/Clams/sublimesystemverilog" short_url = "eregon/oz-tmbundle"
when "http://svn.edgewall.org/repos/genshi/contrib/textmate/Genshi.tmbundle/Syntaxes/Markup%20Template%20%28XML%29.tmLanguage" long_url = "https://github.com/eregon/oz-tmbundle"
short_url = "genshi.edgewall.org/query" else
long_url = "https://genshi.edgewall.org/query" submodule = @submodules[@sources[scope]]
when "vendor/grammars/oz-tmbundle/Syntaxes/Oz.tmLanguage" next unless submodule
short_url = "eregon/oz-tmbundle" short_url = submodule[:short]
long_url = "https://github.com/eregon/oz-tmbundle" long_url = submodule[:url]
else end
submodule = @submodules[@sources[scope]] markdown += "- **#{item}:** [#{short_url}](#{long_url})\n"
next unless submodule end
short_url = submodule[:short]
long_url = submodule[:url]
end
markdown += "- **#{item}:** [#{short_url}](#{long_url})\n"
end
markdown markdown
end end
# Update the file displaying the reader-friendly list of grammar repos
# Update the file displaying the reader-friendly list of grammar repos def update_readme
def update_readme readme = "#{ROOT}/vendor/README.md"
readme = "#{ROOT}/vendor/README.md" preamble = File.read(readme).match(/\A.+?<!--.+?-->\n/ms)
preamble = File.read(readme).match(/\A.+?<!--.+?-->\n/ms) list = self.to_markdown
list = self.to_markdown File.write(readme, preamble.to_s + list)
File.write(readme, preamble.to_s + list) end
end
end end
list = GrammarList.new list = GrammarList.new
puts list.update_readme() puts list.update_readme()