Skip to content

Commit

Permalink
Extends support back to Ruby 2.0
Browse files Browse the repository at this point in the history
This monkeypatches some core libraries.... I don't know that I'm ok with
it, but at the same time, I don't want to build in big icky hacks to
make it work on old Ruby. With this approach, when we decide to drop
2.0, we can just yank the monkeypatches.rb file and be done with it.
  • Loading branch information
binford2k committed Feb 10, 2020
1 parent 7a13cc0 commit 81da8c3
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ matrix:
include:
- rvm: 2.0
env: "CHECK=spec"
gemfile: Gemfile.ruby-2.0.x

- rvm: 2.3
env: "CHECK=spec"
Expand Down
25 changes: 25 additions & 0 deletions Gemfile.ruby-2.0.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
source 'https://rubygems.org'

gemspec

gem 'public_suffix', '2.0.5'
gem 'i18n', '1.2.0'
gem 'nokogiri', '1.6.8.1'

group :development do
gem "turn"
gem "rack-test"
gem "pdf-inspector"
end

group :optional do
gem "pdfkit"
end

group :test do
gem 'rake'
gem 'rspec'
gem 'pry'
end

gem 'rack-contrib'
9 changes: 6 additions & 3 deletions lib/showoff/compiler/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ def self.render!(doc, options={})
return unless title

begin
tools = Nokogiri::XML::Node.new('div', doc).add_class('tools')
tools = Nokogiri::XML::Node.new('div', doc)
tools.add_class('tools')
doc.add_child(tools)

button = Nokogiri::XML::Node.new('input', doc).add_class('display')
button = Nokogiri::XML::Node.new('input', doc)
button.add_class('display')
button.set_attribute('type', 'button')
button.set_attribute('value', I18n.t('forms.display'))
tools.add_child(button)

submit = Nokogiri::XML::Node.new('input', doc).add_class('save')
submit = Nokogiri::XML::Node.new('input', doc)
submit.add_class('save')
submit.set_attribute('type', 'submit')
submit.set_attribute('value', I18n.t('forms.save'))
submit.set_attribute('disabled', 'disabled')
Expand Down
15 changes: 10 additions & 5 deletions lib/showoff/compiler/glossary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def self.render!(doc)
glossary = (item.attr('class').split - ['callout', 'glossary']).first
address = glossary ? "#{glossary}/#{$2}" : $2

link = Nokogiri::XML::Node.new('a', doc).add_class('processed label')
link = Nokogiri::XML::Node.new('a', doc)
link.add_class('processed label')
link.set_attribute('href', "glossary://#{address}")
link.content = $1

Expand All @@ -51,7 +52,8 @@ def self.render!(doc)
label = link.clone
label.add_class('label processed')

definition = Nokogiri::XML::Node.new('p', doc).add_class("callout glossary #{name}")
definition = Nokogiri::XML::Node.new('p', doc)
definition.add_class("callout glossary #{name}")
definition.set_attribute('data-term', term)
definition.set_attribute('data-text', text)
definition.set_attribute('data-target', target)
Expand Down Expand Up @@ -84,7 +86,8 @@ def self.render!(doc)
def self.generatePage!(doc)
doc.search('.slide.glossary .content').each do |glossary|
name = (glossary.attr('class').split - ['content', 'glossary']).first
list = Nokogiri::XML::Node.new('ul', doc).add_class('glossary terms')
list = Nokogiri::XML::Node.new('ul', doc)
list.add_class('glossary terms')
seen = []

doc.search('.callout.glossary').each do |item|
Expand Down Expand Up @@ -117,11 +120,13 @@ def self.generatePage!(doc)

entry = Nokogiri::XML::Node.new('li', doc)

label = Nokogiri::XML::Node.new('a', doc).add_class('label')
label = Nokogiri::XML::Node.new('a', doc)
label.add_class('label')
label.set_attribute('id', anchor)
label.content = term

link = Nokogiri::XML::Node.new('a', doc).add_class('return')
link = Nokogiri::XML::Node.new('a', doc)
label.add_class('return')
link.set_attribute('href', "##{href}")
link.content = '↩'

Expand Down
3 changes: 2 additions & 1 deletion lib/showoff/compiler/notes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def self.render!(doc, profile, options = {})
# Don't bother creating this if we don't want to use it
next unless Showoff::Config.includeNotes?(klass)

notes = Nokogiri::XML::Node.new('div', doc).add_class("notes-section #{klass}")
notes = Nokogiri::XML::Node.new('div', doc)
notes.add_class("notes-section #{klass}")
nodes = []
iter = p.next_sibling
until iter.text == '~~~ENDSECTION~~~' do
Expand Down
6 changes: 4 additions & 2 deletions lib/showoff/compiler/table_of_contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def self.generate!(doc)
title = heads.empty? ? slide['data-title'] : heads.first.text
href = "##{slide['id']}"

entry = Nokogiri::XML::Node.new('li', doc).add_class('tocentry')
entry = Nokogiri::XML::Node.new('li', doc)
entry.add_class('tocentry')
link = Nokogiri::XML::Node.new('a', doc)
link.set_attribute('href', href)
link.content = title
Expand All @@ -35,7 +36,8 @@ def self.generate!(doc)
if (section and slide['data-section'] == section['data-section'])
section.add_child(entry)
else
section = Nokogiri::XML::Node.new('ol', doc).add_class('major')
section = Nokogiri::XML::Node.new('ol', doc)
section.add_class('major')
section.set_attribute('data-section', slide['data-section'])
entry.add_child(section)
toc.add_child(entry)
Expand Down
28 changes: 28 additions & 0 deletions lib/showoff/monkeypatches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Yay for Ruby 2.0!
class Hash
unless Hash.method_defined? :dig
def dig(*args)
args.reduce(self) do |iter, arg|
break nil unless iter.is_a? Enumerable
break nil unless iter.include? arg
iter[arg]
end
end
end

end

class Nokogiri::XML::Element
unless Nokogiri::XML::Element.method_defined? :add_class
def add_class(classlist)
self[:class] = [self[:class], classlist].join(' ')
end
end

unless Nokogiri::XML::Element.method_defined? :classes
def classes
self[:class] ? self[:class].split(' ') : []
end
end

end
3 changes: 3 additions & 0 deletions lib/showoff_ng.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Showoff
require 'showoff/locale'
require 'showoff/logger'

# @todo: Do we really need Ruby 2.0 support?
require 'showoff/monkeypatches'

GEMROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

def self.do_static(args, options)
Expand Down

0 comments on commit 81da8c3

Please sign in to comment.