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