Skip to content

Commit

Permalink
Linguist optional; fallback to filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki Izumi committed Mar 27, 2017
1 parent a970742 commit 665ab4d
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 27 deletions.
2 changes: 1 addition & 1 deletion github-markup.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
22 changes: 15 additions & 7 deletions lib/github/markup.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
begin
require "linguist"
rescue LoadError
# Rely on extensions instead.
end

require "github/markup/command_implementation"
require "github/markup/gem_implementation"

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/github/markup/command_implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/github/markup/gem_implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions lib/github/markup/implementation.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/github/markup/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/github/markup/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module GitHub
module Markup
class RDoc < Implementation
def initialize
super([Linguist::Language["RDoc"]])
super(/rdoc/, ["RDoc"])
end

def render(content)
Expand Down
16 changes: 8 additions & 8 deletions lib/github/markups.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
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

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")
2 changes: 1 addition & 1 deletion test/markup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 665ab4d

Please sign in to comment.