From a970742bd4aaba60aa8b6b42609b9ebb38d15ca9 Mon Sep 17 00:00:00 2001 From: Yuki Izumi Date: Mon, 27 Mar 2017 13:09:05 +1100 Subject: [PATCH 1/2] Revert "Revert #537 temporarily for final 1.4 release" This reverts commit bbee330095529e0a428b42b01a68fbefed89c7e8. --- github-markup.gemspec | 1 + lib/github/markup.rb | 26 +++++++++------ lib/github/markup/command_implementation.rb | 4 +-- lib/github/markup/gem_implementation.rb | 4 +-- lib/github/markup/implementation.rb | 15 +++------ lib/github/markup/markdown.rb | 2 +- lib/github/markup/rdoc.rb | 2 +- lib/github/markups.rb | 15 +++++---- test/markup_test.rb | 36 ++++++++++----------- 9 files changed, 54 insertions(+), 51 deletions(-) diff --git a/github-markup.gemspec b/github-markup.gemspec index 6320a489..ebec6a19 100644 --- a/github-markup.gemspec +++ b/github-markup.gemspec @@ -16,6 +16,7 @@ Gem::Specification.new do |s| s.test_files = s.files.grep(%r{^(test|spec|features)/}) s.require_paths = %w[lib] + s.add_dependency "github-linguist", "~> 5.0", ">= 5.0.8" s.add_dependency "rinku" s.add_development_dependency 'rake', '~> 12' s.add_development_dependency 'activesupport', '~> 4.0' diff --git a/lib/github/markup.rb b/lib/github/markup.rb index ddc7312f..712ef7c7 100644 --- a/lib/github/markup.rb +++ b/lib/github/markup.rb @@ -37,7 +37,7 @@ def preload! def render(filename, content = nil) content ||= File.read(filename) - if impl = renderer(filename) + if impl = renderer(filename, content) impl.render(content) else content @@ -53,9 +53,9 @@ def render_s(symbol, content) content end end - - def markup(symbol, file, pattern, opts = {}, &block) - markup_impl(symbol, GemImplementation.new(pattern, file, &block)) + + def markup(symbol, gem_name, pattern, opts = {}, &block) + markup_impl(symbol, GemImplementation.new(pattern, gem_name, &block)) end def markup_impl(symbol, impl) @@ -65,24 +65,30 @@ def markup_impl(symbol, impl) markups[symbol] = impl end - def command(symbol, command, regexp, name, &block) + def command(symbol, command, languages, name, &block) if File.exist?(file = File.dirname(__FILE__) + "/commands/#{command}") command = file end - markup_impl(symbol, CommandImplementation.new(regexp, command, name, &block)) + markup_impl(symbol, CommandImplementation.new(languages, command, name, &block)) end - def can_render?(filename) - !!renderer(filename) + def can_render?(filename, content) + !!renderer(filename, content) end - def renderer(filename) + def renderer(filename, content) + language = language(filename, content) markup_impls.find { |impl| - impl.match?(filename) + impl.match?(language) } end + def language(filename, content) + blob = Linguist::Blob.new(filename, content) + return Linguist.detect(blob, allow_empty: true) + end + # Define markups markups_rb = File.dirname(__FILE__) + '/markups.rb' instance_eval File.read(markups_rb), markups_rb diff --git a/lib/github/markup/command_implementation.rb b/lib/github/markup/command_implementation.rb index be3f6b46..d0f2fc47 100644 --- a/lib/github/markup/command_implementation.rb +++ b/lib/github/markup/command_implementation.rb @@ -15,8 +15,8 @@ class CommandError < RuntimeError class CommandImplementation < Implementation attr_reader :command, :block, :name - def initialize(regexp, command, name, &block) - super regexp + def initialize(languages, command, name, &block) + super languages @command = command.to_s @block = block @name = name diff --git a/lib/github/markup/gem_implementation.rb b/lib/github/markup/gem_implementation.rb index 270f5a88..08383f52 100644 --- a/lib/github/markup/gem_implementation.rb +++ b/lib/github/markup/gem_implementation.rb @@ -5,8 +5,8 @@ module Markup class GemImplementation < Implementation attr_reader :gem_name, :renderer - def initialize(regexp, gem_name, &renderer) - super regexp + def initialize(languages, gem_name, &renderer) + super languages @gem_name = gem_name.to_s @renderer = renderer end diff --git a/lib/github/markup/implementation.rb b/lib/github/markup/implementation.rb index 463a39d4..46df1510 100644 --- a/lib/github/markup/implementation.rb +++ b/lib/github/markup/implementation.rb @@ -1,10 +1,10 @@ module GitHub module Markup class Implementation - attr_reader :regexp + attr_reader :languages - def initialize(regexp) - @regexp = regexp + def initialize(languages) + @languages = languages end def load @@ -15,13 +15,8 @@ def render(content) raise NotImplementedError, "subclasses of GitHub::Markup::Implementation must define #render" end - def match?(filename) - file_ext_regexp =~ filename - end - - private - def file_ext_regexp - @file_ext_regexp ||= /\.(#{regexp})\z/ + def match?(language) + languages.include? language end end end diff --git a/lib/github/markup/markdown.rb b/lib/github/markup/markdown.rb index e3a9b77c..0d8555c3 100644 --- a/lib/github/markup/markdown.rb +++ b/lib/github/markup/markdown.rb @@ -28,7 +28,7 @@ class Markdown < Implementation } def initialize - super(/md|rmd|mkdn?|mdwn|mdown|markdown|litcoffee/i) + super([Linguist::Language["Markdown"], Linguist::Language["RMarkdown"], Linguist::Language["Literate CoffeeScript"]]) end def load diff --git a/lib/github/markup/rdoc.rb b/lib/github/markup/rdoc.rb index bc008cb4..e96ddf07 100644 --- a/lib/github/markup/rdoc.rb +++ b/lib/github/markup/rdoc.rb @@ -6,7 +6,7 @@ module GitHub module Markup class RDoc < Implementation def initialize - super(/rdoc/) + super([Linguist::Language["RDoc"]]) end def render(content) diff --git a/lib/github/markups.rb b/lib/github/markups.rb index e951fb16..24ecaffa 100644 --- a/lib/github/markups.rb +++ b/lib/github/markups.rb @@ -1,33 +1,34 @@ require "github/markup/markdown" require "github/markup/rdoc" require "shellwords" +require "linguist" markup_impl(::GitHub::Markups::MARKUP_MARKDOWN, ::GitHub::Markup::Markdown.new) -markup(::GitHub::Markups::MARKUP_TEXTILE, :redcloth, /textile/) do |content| +markup(::GitHub::Markups::MARKUP_TEXTILE, :redcloth, [Linguist::Language["Textile"]]) do |content| RedCloth.new(content).to_html end markup_impl(::GitHub::Markups::MARKUP_RDOC, GitHub::Markup::RDoc.new) -markup(::GitHub::Markups::MARKUP_ORG, 'org-ruby', /org/) do |content| +markup(::GitHub::Markups::MARKUP_ORG, 'org-ruby', [Linguist::Language["Org"]]) do |content| Orgmode::Parser.new(content, { :allow_include_files => false, :skip_syntax_highlight => true }).to_html end -markup(::GitHub::Markups::MARKUP_CREOLE, :creole, /creole/) do |content| +markup(::GitHub::Markups::MARKUP_CREOLE, :creole, [Linguist::Language["Creole"]]) do |content| Creole.creolize(content) end -markup(::GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, /mediawiki|wiki/) do |content| +markup(::GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, [Linguist::Language["MediaWiki"]]) do |content| wikicloth = WikiCloth::WikiCloth.new(:data => content) WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS << 'tt' unless WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS.include?('tt') wikicloth.to_html(:noedit => true) end -markup(::GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, /adoc|asc(iidoc)?/) do |content| +markup(::GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, [Linguist::Language["AsciiDoc"]]) do |content| Asciidoctor::Compliance.unique_id_start_index = 1 Asciidoctor.convert(content, :safe => :secure, :attributes => %w(showtitle=@ idprefix idseparator=- env=github env-github source-highlighter=html-pipeline)) end @@ -35,8 +36,8 @@ command( ::GitHub::Markups::MARKUP_RST, "python2 -S #{Shellwords.escape(File.dirname(__FILE__))}/commands/rest2html", - /re?st(\.txt)?/, + [Linguist::Language["reStructuredText"]], "restructuredtext" ) -command(::GitHub::Markups::MARKUP_POD, :pod2html, /pod/, "pod") +command(::GitHub::Markups::MARKUP_POD, :pod2html, [Linguist::Language["Pod"]], "pod") diff --git a/test/markup_test.rb b/test/markup_test.rb index d0b6d09a..6c8365d4 100644 --- a/test/markup_test.rb +++ b/test/markup_test.rb @@ -77,24 +77,24 @@ def call end def test_knows_what_it_can_and_cannot_render - assert_equal false, GitHub::Markup.can_render?('README.html') - assert_equal true, GitHub::Markup.can_render?('README.markdown') - assert_equal true, GitHub::Markup.can_render?('README.rmd') - assert_equal true, GitHub::Markup.can_render?('README.Rmd') - assert_equal false, GitHub::Markup.can_render?('README.cmd') - assert_equal true, GitHub::Markup.can_render?('README.litcoffee') + assert_equal false, GitHub::Markup.can_render?('README.html', '

Title

') + assert_equal true, GitHub::Markup.can_render?('README.markdown', '=== Title') + assert_equal true, GitHub::Markup.can_render?('README.rmd', '=== Title') + assert_equal true, GitHub::Markup.can_render?('README.Rmd', '=== Title') + assert_equal false, GitHub::Markup.can_render?('README.cmd', 'echo 1') + assert_equal true, GitHub::Markup.can_render?('README.litcoffee', 'Title') end def test_each_render_has_a_name - assert_equal "markdown", GitHub::Markup.renderer('README.md').name - assert_equal "redcloth", GitHub::Markup.renderer('README.textile').name - assert_equal "rdoc", GitHub::Markup.renderer('README.rdoc').name - assert_equal "org-ruby", GitHub::Markup.renderer('README.org').name - assert_equal "creole", GitHub::Markup.renderer('README.creole').name - assert_equal "wikicloth", GitHub::Markup.renderer('README.wiki').name - assert_equal "asciidoctor", GitHub::Markup.renderer('README.adoc').name - assert_equal "restructuredtext", GitHub::Markup.renderer('README.rst').name - assert_equal "pod", GitHub::Markup.renderer('README.pod').name + assert_equal "markdown", GitHub::Markup.renderer('README.md', '=== Title').name + assert_equal "redcloth", GitHub::Markup.renderer('README.textile', '* One').name + assert_equal "rdoc", GitHub::Markup.renderer('README.rdoc', '* One').name + assert_equal "org-ruby", GitHub::Markup.renderer('README.org', '* Title').name + assert_equal "creole", GitHub::Markup.renderer('README.creole', '= Title =').name + assert_equal "wikicloth", GitHub::Markup.renderer('README.wiki', '

Title

').name + assert_equal "asciidoctor", GitHub::Markup.renderer('README.adoc', '== Title').name + assert_equal "restructuredtext", GitHub::Markup.renderer('README.rst', 'Title').name + assert_equal "pod", GitHub::Markup.renderer('README.pod', '=begin').name end def test_rendering_by_symbol @@ -102,10 +102,10 @@ def test_rendering_by_symbol end def test_raises_error_if_command_exits_non_zero - GitHub::Markup.command(:doesntmatter, 'test/fixtures/fail.sh', /fail/, 'fail') - assert GitHub::Markup.can_render?('README.fail') + GitHub::Markup.command(:doesntmatter, 'test/fixtures/fail.sh', [Linguist::Language['Java']], 'fail') + assert GitHub::Markup.can_render?('README.java', 'stop swallowing errors') begin - GitHub::Markup.render('README.fail', "stop swallowing errors") + GitHub::Markup.render('README.java', "stop swallowing errors") rescue GitHub::Markup::CommandError => e assert_equal "failure message", e.message else From 665ab4d007ad0831098a269fe876a89995b88200 Mon Sep 17 00:00:00 2001 From: Yuki Izumi Date: Mon, 27 Mar 2017 13:09:28 +1100 Subject: [PATCH 2/2] Linguist optional; fallback to filenames --- github-markup.gemspec | 2 +- lib/github/markup.rb | 22 +++++++++++++------- lib/github/markup/command_implementation.rb | 4 ++-- lib/github/markup/gem_implementation.rb | 4 ++-- lib/github/markup/implementation.rb | 23 +++++++++++++++++---- lib/github/markup/markdown.rb | 4 +++- lib/github/markup/rdoc.rb | 2 +- lib/github/markups.rb | 16 +++++++------- test/markup_test.rb | 2 +- 9 files changed, 52 insertions(+), 27 deletions(-) diff --git a/github-markup.gemspec b/github-markup.gemspec index ebec6a19..69e10b45 100644 --- a/github-markup.gemspec +++ b/github-markup.gemspec @@ -16,7 +16,6 @@ Gem::Specification.new do |s| s.test_files = s.files.grep(%r{^(test|spec|features)/}) s.require_paths = %w[lib] - s.add_dependency "github-linguist", "~> 5.0", ">= 5.0.8" s.add_dependency "rinku" s.add_development_dependency 'rake', '~> 12' s.add_development_dependency 'activesupport', '~> 4.0' @@ -25,4 +24,5 @@ Gem::Specification.new do |s| s.add_development_dependency 'sanitize', '~> 2.1', '>= 2.1.0' s.add_development_dependency 'nokogiri', '1.6.8.1' s.add_development_dependency 'nokogiri-diff', '~> 0.2.0' + s.add_development_dependency "github-linguist", "~> 5.0", ">= 5.0.8" end diff --git a/lib/github/markup.rb b/lib/github/markup.rb index 712ef7c7..f7befbe0 100644 --- a/lib/github/markup.rb +++ b/lib/github/markup.rb @@ -1,3 +1,9 @@ +begin + require "linguist" +rescue LoadError + # Rely on extensions instead. +end + require "github/markup/command_implementation" require "github/markup/gem_implementation" @@ -54,8 +60,8 @@ def render_s(symbol, content) end end - def markup(symbol, gem_name, pattern, opts = {}, &block) - markup_impl(symbol, GemImplementation.new(pattern, gem_name, &block)) + def markup(symbol, gem_name, regexp, languages, opts = {}, &block) + markup_impl(symbol, GemImplementation.new(regexp, languages, gem_name, &block)) end def markup_impl(symbol, impl) @@ -65,12 +71,12 @@ def markup_impl(symbol, impl) markups[symbol] = impl end - def command(symbol, command, languages, name, &block) + def command(symbol, command, regexp, languages, name, &block) if File.exist?(file = File.dirname(__FILE__) + "/commands/#{command}") command = file end - markup_impl(symbol, CommandImplementation.new(languages, command, name, &block)) + markup_impl(symbol, CommandImplementation.new(regexp, languages, command, name, &block)) end def can_render?(filename, content) @@ -80,13 +86,15 @@ def can_render?(filename, content) def renderer(filename, content) language = language(filename, content) markup_impls.find { |impl| - impl.match?(language) + impl.match?(filename, language) } end def language(filename, content) - blob = Linguist::Blob.new(filename, content) - return Linguist.detect(blob, allow_empty: true) + if defined?(::Linguist) + blob = Linguist::Blob.new(filename, content) + return Linguist.detect(blob, allow_empty: true) + end end # Define markups diff --git a/lib/github/markup/command_implementation.rb b/lib/github/markup/command_implementation.rb index d0f2fc47..439ed293 100644 --- a/lib/github/markup/command_implementation.rb +++ b/lib/github/markup/command_implementation.rb @@ -15,8 +15,8 @@ class CommandError < RuntimeError class CommandImplementation < Implementation attr_reader :command, :block, :name - def initialize(languages, command, name, &block) - super languages + def initialize(regexp, languages, command, name, &block) + super(regexp, languages) @command = command.to_s @block = block @name = name diff --git a/lib/github/markup/gem_implementation.rb b/lib/github/markup/gem_implementation.rb index 08383f52..1391de97 100644 --- a/lib/github/markup/gem_implementation.rb +++ b/lib/github/markup/gem_implementation.rb @@ -5,8 +5,8 @@ module Markup class GemImplementation < Implementation attr_reader :gem_name, :renderer - def initialize(languages, gem_name, &renderer) - super languages + def initialize(regexp, languages, gem_name, &renderer) + super(regexp, languages) @gem_name = gem_name.to_s @renderer = renderer end diff --git a/lib/github/markup/implementation.rb b/lib/github/markup/implementation.rb index 46df1510..fb31165b 100644 --- a/lib/github/markup/implementation.rb +++ b/lib/github/markup/implementation.rb @@ -1,10 +1,15 @@ module GitHub module Markup class Implementation + attr_reader :regexp attr_reader :languages - def initialize(languages) - @languages = languages + def initialize(regexp, languages) + @regexp = regexp + + if defined?(::Linguist) + @languages = languages.map {|l| Linguist::Language[l]} + end end def load @@ -15,8 +20,18 @@ def render(content) raise NotImplementedError, "subclasses of GitHub::Markup::Implementation must define #render" end - def match?(language) - languages.include? language + def match?(filename, language) + if defined?(::Linguist) + languages.include? language + else + file_ext_regexp =~ filename + end + end + + private + + def file_ext_regexp + @file_ext_regexp ||= /\.(#{regexp})\z/ end end end diff --git a/lib/github/markup/markdown.rb b/lib/github/markup/markdown.rb index 0d8555c3..0894fa03 100644 --- a/lib/github/markup/markdown.rb +++ b/lib/github/markup/markdown.rb @@ -28,7 +28,9 @@ class Markdown < Implementation } def initialize - super([Linguist::Language["Markdown"], Linguist::Language["RMarkdown"], Linguist::Language["Literate CoffeeScript"]]) + super( + /md|rmd|mkdn?|mdwn|mdown|markdown|litcoffee/i, + ["Markdown", "RMarkdown", "Literate CoffeeScript"]) end def load diff --git a/lib/github/markup/rdoc.rb b/lib/github/markup/rdoc.rb index e96ddf07..1064f24b 100644 --- a/lib/github/markup/rdoc.rb +++ b/lib/github/markup/rdoc.rb @@ -6,7 +6,7 @@ module GitHub module Markup class RDoc < Implementation def initialize - super([Linguist::Language["RDoc"]]) + super(/rdoc/, ["RDoc"]) end def render(content) diff --git a/lib/github/markups.rb b/lib/github/markups.rb index 24ecaffa..538ee7d0 100644 --- a/lib/github/markups.rb +++ b/lib/github/markups.rb @@ -1,34 +1,33 @@ require "github/markup/markdown" require "github/markup/rdoc" require "shellwords" -require "linguist" markup_impl(::GitHub::Markups::MARKUP_MARKDOWN, ::GitHub::Markup::Markdown.new) -markup(::GitHub::Markups::MARKUP_TEXTILE, :redcloth, [Linguist::Language["Textile"]]) do |content| +markup(::GitHub::Markups::MARKUP_TEXTILE, :redcloth, /textile/, ["Textile"]) do |content| RedCloth.new(content).to_html end markup_impl(::GitHub::Markups::MARKUP_RDOC, GitHub::Markup::RDoc.new) -markup(::GitHub::Markups::MARKUP_ORG, 'org-ruby', [Linguist::Language["Org"]]) do |content| +markup(::GitHub::Markups::MARKUP_ORG, 'org-ruby', /org/, ["Org"]) do |content| Orgmode::Parser.new(content, { :allow_include_files => false, :skip_syntax_highlight => true }).to_html end -markup(::GitHub::Markups::MARKUP_CREOLE, :creole, [Linguist::Language["Creole"]]) do |content| +markup(::GitHub::Markups::MARKUP_CREOLE, :creole, /creole/, ["Creole"]) do |content| Creole.creolize(content) end -markup(::GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, [Linguist::Language["MediaWiki"]]) do |content| +markup(::GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, /mediawiki|wiki/, ["MediaWiki"]) do |content| wikicloth = WikiCloth::WikiCloth.new(:data => content) WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS << 'tt' unless WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS.include?('tt') wikicloth.to_html(:noedit => true) end -markup(::GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, [Linguist::Language["AsciiDoc"]]) do |content| +markup(::GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, /adoc|asc(iidoc)?/, ["AsciiDoc"]) do |content| Asciidoctor::Compliance.unique_id_start_index = 1 Asciidoctor.convert(content, :safe => :secure, :attributes => %w(showtitle=@ idprefix idseparator=- env=github env-github source-highlighter=html-pipeline)) end @@ -36,8 +35,9 @@ command( ::GitHub::Markups::MARKUP_RST, "python2 -S #{Shellwords.escape(File.dirname(__FILE__))}/commands/rest2html", - [Linguist::Language["reStructuredText"]], + /re?st(\.txt)?/, + ["reStructuredText"], "restructuredtext" ) -command(::GitHub::Markups::MARKUP_POD, :pod2html, [Linguist::Language["Pod"]], "pod") +command(::GitHub::Markups::MARKUP_POD, :pod2html, /pod/, ["Pod"], "pod") diff --git a/test/markup_test.rb b/test/markup_test.rb index 6c8365d4..ccd1818e 100644 --- a/test/markup_test.rb +++ b/test/markup_test.rb @@ -102,7 +102,7 @@ def test_rendering_by_symbol end def test_raises_error_if_command_exits_non_zero - GitHub::Markup.command(:doesntmatter, 'test/fixtures/fail.sh', [Linguist::Language['Java']], 'fail') + GitHub::Markup.command(:doesntmatter, 'test/fixtures/fail.sh', /fail/, ['Java'], 'fail') assert GitHub::Markup.can_render?('README.java', 'stop swallowing errors') begin GitHub::Markup.render('README.java', "stop swallowing errors")