texttube_baby 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3683d2f419a0d6dcf000e7529ea1ccc71c05c333
4
- data.tar.gz: bf7786e8ca629ac8198c0e3aa6b2abcea5d2b49f
3
+ metadata.gz: 3c03a1149bdebebe406574df28daf4535cfef08a
4
+ data.tar.gz: 663ed918d62ef4dd27291a0fba5e1be2f629cc12
5
5
  SHA512:
6
- metadata.gz: d49031f13f772f2aff66c3096ca1bd71999a6cd84005d67233d055d6cff920c0446f5c27810af5954d553b740ecd7e7e6a07e430b1dbdab9d4d084be5042a84c
7
- data.tar.gz: 94ae20332a207a82aa0e4cc957cc57ebb41003a2d4d2a2fea42c2b8040d1fbc7b3d0f8bb5e86cd43da018748791b20218964063ec8cbe2027945c123fc4e225f
6
+ metadata.gz: 24149e6b053b362a72c6bb380ffc9b303c848c7f6ee44b5964beb7c485c616097d1d03f35af62b9f82f1e337b27db6a47894e6d0b731b0f32ad45b61bf49c05d
7
+ data.tar.gz: 0f2b5720a574478efbb1a07a3c0b5bc06ec515ed91e2eebd27151698f63d6da2a8a540efe202c6407116b396da315ab289ab15e751140ee93b8f03d1855a0919
data/.gitignore CHANGED
@@ -19,6 +19,7 @@ rdoc
19
19
  Gemfile.lock
20
20
  bin/
21
21
  vendor/
22
+ vendor.noindex/
22
23
  coverage/
23
24
  docs/
24
25
  .yardoc/
data/CHANGES.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## CH CH CH CH CHANGES! ##
2
2
 
3
+
4
+ ### Monday the 8th of June 2015, v1.1.0 ###
5
+
6
+ * Changed from Nokogiri to Oga because Nokogiri is annoying to install. Having said that, getting a bugfix into Oga was annoying too, so for now it will rely on my patched library, Oga Without the Wimpiness. This will be replaced by Oga in the next release when *it* is next released.
7
+
8
+ ----
9
+
10
+
3
11
  ### Friday the 17th of October 2014, v1.0.0 ###
4
12
 
5
13
  * All specs passing, library now separated from TextTube.
data/Gemfile CHANGED
@@ -3,8 +3,11 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in TextTubeBaby.gemspec
4
4
  gemspec
5
5
 
6
+
6
7
  group :development do
7
8
  gem "pry"
9
+ gem "pry-byebug"
10
+ gem "rake"
8
11
  end
9
12
 
10
13
  group :test do
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
  module TextTube
3
3
 
4
- require 'nokogiri'
4
+ require 'oga'
5
5
  require_relative "../../ext/blank.rb"
6
6
  require 'coderay'
7
7
  require "texttube/filterable"
@@ -9,6 +9,8 @@ module TextTube
9
9
  module Baby
10
10
 
11
11
  # a filter for Coderay
12
+ # @note Thanks to Rob Emerson for sharing his nanoc filter which helped me write this.
13
+ # @see http://www.remerson.plus.com/articles/nanoc-coderay/
12
14
  module Coderay
13
15
  extend Filterable
14
16
 
@@ -21,37 +23,44 @@ module TextTube
21
23
  # @param [Hash] options
22
24
  # @return [String]
23
25
  def self.run(content, options={})
24
- options = {lang: :ruby } if options.blank?
25
- doc = Nokogiri::HTML::fragment(content)
26
+ options = {lang: :ruby } if options.blank?
27
+ doc = Oga.parse_html content
26
28
 
27
- code_blocks = doc.xpath("pre/code").map do |code_block|
28
- #un-escape as Coderay will escape it again
29
- inner_html = code_block.inner_html
30
-
31
- # following the convention of Rack::Codehighlighter
32
- if inner_html.start_with?("::::")
33
- lines = inner_html.split("\n")
34
- options[:lang] = lines.shift.match(%r{::::(\w+)})[1].to_sym
35
- inner_html = lines.join("\n")
36
- end
29
+ if (xpath = doc.xpath("pre/code"))
30
+ xpath.map do |codeblock|
31
+ #un-escape as Coderay will escape it again
32
+ inner_html = codeblock.inner_text
37
33
 
38
- if (options[:lang] == :skip) || (! options.has_key? :lang )
39
- code_block.inner_html = inner_html
40
- else
41
- code = Coderay.codify(Coderay.html_unescape(inner_html), options[:lang])
42
- code_block.inner_html = code
43
- code_block["class"] = "CodeRay"
44
- end
45
- end#block
34
+ # following the convention of Rack::Codehighlighter
35
+ if inner_html.start_with?("::::")
36
+ lines = inner_html.split("\n")
37
+ options[:lang] = lines.shift.match(%r{::::(\w+)})[1].to_sym
38
+ inner_html = lines.join("\n")
39
+ end
46
40
 
47
- doc.to_s
41
+ if (options[:lang] == :skip) || (! options.has_key? :lang )
42
+ codeblock.inner_text = inner_html
43
+ else
44
+ # html_unescape(inner_html)
45
+ # code = codify(html_unescape(inner_html), options[:lang])
46
+ code = Coderay.codify(Coderay.html_unescape(inner_html), options[:lang])
47
+ # It needs to be parsed back into Oga
48
+ # or the escaping goes wrong
49
+ codeblock.children= Oga.parse_html( code ).children
50
+ codeblock.set "class", "CodeRay"
51
+ end
52
+ end#block
53
+ doc.to_xml
54
+ else
55
+ content
56
+ end
48
57
  end#def
49
58
 
50
59
  # @private
51
60
  # Unescape the HTML as the Coderay scanner won't work otherwise.
52
- def self.html_unescape(a_string)
61
+ def self.html_unescape(a_string)
53
62
  a_string.gsub('&amp;', '&').gsub('&lt;', '<').gsub('&gt;',
54
- '>').gsub('&quot;', '"')
63
+ '>').gsub('&quot;', '"')
55
64
  end#def
56
65
 
57
66
  # Run the Coderay scanner.
@@ -60,11 +69,10 @@ module TextTube
60
69
  # @param [String] lang
61
70
  # @example
62
71
  # self.class.codify "x = 2", "ruby"
63
- def self.codify(str, lang)
72
+ def self.codify(str, lang)
64
73
  ::CodeRay.scan(str, lang).html
65
74
  end#def
66
75
 
67
- end#class
76
+ end#class
68
77
  end
69
-
70
78
  end#module
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
- require 'nokogiri'
3
2
  require "texttube/filterable"
3
+ require 'oga'
4
4
 
5
5
  module TextTube
6
6
  module Baby
@@ -19,17 +19,19 @@ module TextTube
19
19
  def self.run( content, options={})
20
20
  options ||= {}
21
21
  if options[:markdown_parser].nil?
22
- require 'rdiscount'
22
+ require 'rdiscount'
23
23
  markdown_parser=RDiscount
24
24
  end
25
- doc = Nokogiri::HTML::fragment(content)
25
+ doc = Oga.parse_html(content)
26
26
 
27
- doc.xpath("*[@markdown='1']").each do |ele|
28
- ele.inner_html = markdown_parser.new(ele.inner_html).to_html
29
- ele.remove_attribute("markdown")
27
+ doc.xpath("*[@markdown='1']").each do |ele|
28
+ inner_text = ele.children.inject(""){|mem,child| mem << child.to_xml }
29
+ html_fragment = markdown_parser.new(inner_text).to_html
30
+ ele.children= Oga.parse_html( html_fragment ).children
31
+ ele.unset "markdown"
30
32
  end
31
33
 
32
- doc.to_s
34
+ doc.to_xml
33
35
  end # run
34
36
 
35
37
  end
@@ -85,7 +85,7 @@ module TextTube
85
85
  has_reflinks = false
86
86
 
87
87
  links = [] #to store the matches
88
-
88
+
89
89
  text.gsub! Pattern do |md| #block to pass to gsub
90
90
  has_reflinks = true
91
91
  if kind == :inline
@@ -32,10 +32,9 @@ module TextTube
32
32
  # @param [Hash] options
33
33
  def self.run( content, options={})
34
34
  ugly_child = content.dup
35
- # warn ugly_child
35
+
36
36
  DICTIONARY.each do |english, ugly|
37
37
  ugly_child.sub! english, ugly
38
- # warn ugly_child
39
38
  end
40
39
  ugly_child
41
40
  end
@@ -1,5 +1,5 @@
1
1
  module TextTube
2
2
  module Baby
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
data/lib/texttube/baby.rb CHANGED
@@ -8,7 +8,6 @@ module TextTube
8
8
  # @return [Array<String,TrueClass>]
9
9
  def self.load_all_filters filters=nil
10
10
  filters ||= File.join __dir__, "baby/*.rb"
11
- warn "filters = #{filters}"
12
11
  Dir.glob( filters )
13
12
  .reject{|name| name.end_with? "version.rb" }
14
13
  .map{|filter|
@@ -15,8 +15,7 @@ Sometimes this is useful.
15
15
  HTML
16
16
  }
17
17
  let(:expected) { <<HTML
18
- <div class="wrapper" id="sidebar">
19
- <h2>This is a sidebar</h2>
18
+ <div class="wrapper" id="sidebar"><h2>This is a sidebar</h2>
20
19
 
21
20
  <ul>
22
21
  <li>Written</li>
@@ -14,44 +14,80 @@ module Baby # convience
14
14
  it { should == expected }
15
15
  end
16
16
 
17
- let(:content) { "The UtterFAIL website[[http://utterfail.info|UtterFAIL!]] is good. My blog[[http://iainbarnett.me.uk|My blog]] is also good." }
17
+ context "And markdown" do
18
+ require 'rdiscount'
19
+ let(:content) { <<CONTENT
20
+ # Some Example Links #
21
+
22
+ Example1[[http://example.org/abc/1234.5678|Example1 link]].
23
+
24
+ Example2[[http://example.com/ABC/0.1.2.3|Example2 link]].
25
+ CONTENT
26
+ }
27
+ let(:expected){<<HTML
28
+ <h1>Some Example Links</h1>
29
+
30
+ <p>Example1<a href="#0" title="Jump to reference">&#8304;</a>.</p>
31
+
32
+ <p>Example2<a href="#1" title="Jump to reference">&sup1;</a>.</p>
18
33
 
19
- context "and no options" do
20
- subject { TextTube::Baby::LinkReffing.run content }
21
- let(:expected) { s = <<HTML
22
- The UtterFAIL website[&#8304;](#0 "Jump to reference") is good. My blog[&sup1;](#1 "Jump to reference") is also good.
23
34
  <div markdown='1' id='reflinks'>
24
- <a name="0"></a>&#91;0&#93; [http://utterfail.info](http://utterfail.info "http://utterfail.info") UtterFAIL!
35
+ <a name="0"></a>&#91;0&#93; [http://example.org/abc/1234.5678](http://example.org/abc/1234.5678 "http://example.org/abc/1234.5678") Example1 link
25
36
 
26
37
 
27
- <a name="1"></a>&#91;1&#93; [http://iainbarnett.me.uk](http://iainbarnett.me.uk "http://iainbarnett.me.uk") My blog
38
+ <a name="1"></a>&#91;1&#93; [http://example.com/ABC/0.1.2.3](http://example.com/ABC/0.1.2.3 "http://example.com/ABC/0.1.2.3") Example2 link
28
39
 
29
40
  </div>
41
+
30
42
  HTML
31
- s.strip
32
- }
33
- include_examples "outputting links"
34
- end
35
- context "and an option not to ref the link (i.e. inline)" do
36
- let(:expected) {
37
- "The UtterFAIL website [UtterFAIL!](http://utterfail.info) is good. My blog [My blog](http://iainbarnett.me.uk) is also good."
38
43
  }
39
44
  subject {
40
- TextTube::Baby::LinkReffing.run content, kind: :inline
45
+ text = TextTube::Baby::LinkReffing.run content
46
+ RDiscount.new(text).to_html
41
47
  }
42
48
  include_examples "outputting links"
43
- context "and use HTML" do
49
+ end # context
50
+
51
+ context "Given blah" do
52
+
53
+ let(:content) { "The UtterFAIL website[[http://utterfail.info|UtterFAIL!]] is good. My blog[[http://iainbarnett.me.uk|My blog]] is also good." }
54
+
55
+ context "and no options" do
56
+ subject { TextTube::Baby::LinkReffing.run content }
57
+ let(:expected) { s = <<HTML
58
+ The UtterFAIL website[&#8304;](#0 "Jump to reference") is good. My blog[&sup1;](#1 "Jump to reference") is also good.
59
+ <div markdown='1' id='reflinks'>
60
+ <a name="0"></a>&#91;0&#93; [http://utterfail.info](http://utterfail.info "http://utterfail.info") UtterFAIL!
61
+
62
+
63
+ <a name="1"></a>&#91;1&#93; [http://iainbarnett.me.uk](http://iainbarnett.me.uk "http://iainbarnett.me.uk") My blog
64
+
65
+ </div>
66
+ HTML
67
+ s.strip
68
+ }
69
+ include_examples "outputting links"
70
+ end
71
+ context "and an option not to ref the link (i.e. inline)" do
44
72
  let(:expected) {
45
- %Q$The UtterFAIL website <a href="http://utterfail.info">UtterFAIL!</a> is good. My blog <a href="http://iainbarnett.me.uk">My blog</a> is also good.$
73
+ "The UtterFAIL website [UtterFAIL!](http://utterfail.info) is good. My blog [My blog](http://iainbarnett.me.uk) is also good."
46
74
  }
47
75
  subject {
48
- TextTube::Baby::LinkReffing.run content, kind: :inline, format: :html
76
+ TextTube::Baby::LinkReffing.run content, kind: :inline
49
77
  }
50
78
  include_examples "outputting links"
79
+ context "and use HTML" do
80
+ let(:expected) {
81
+ %Q$The UtterFAIL website <a href="http://utterfail.info">UtterFAIL!</a> is good. My blog <a href="http://iainbarnett.me.uk">My blog</a> is also good.$
82
+ }
83
+ subject {
84
+ TextTube::Baby::LinkReffing.run content, kind: :inline, format: :html
85
+ }
86
+ include_examples "outputting links"
87
+ end
51
88
  end
52
- end
53
- context "and an option to output a link as HTML" do
54
- let(:expected) { s = <<HTML
89
+ context "and an option to output a link as HTML" do
90
+ let(:expected) { s = <<HTML
55
91
  The UtterFAIL website<a href="#0" title="Jump to reference">&#8304;</a> is good. My blog<a href="#1" title="Jump to reference">&sup1;</a> is also good.
56
92
  <div markdown='1' id='reflinks'>
57
93
  <a name="0"></a>&#91;0&#93; [http://utterfail.info](http://utterfail.info "http://utterfail.info") UtterFAIL!
@@ -61,31 +97,32 @@ The UtterFAIL website<a href="#0" title="Jump to reference">&#8304;</a> is good.
61
97
 
62
98
  </div>
63
99
  HTML
64
- s.strip
65
- }
66
- subject { TextTube::Baby::LinkReffing.run content, format: :html }
67
- include_examples "outputting links"
68
- end
69
- context "and an option to not show the link at all" do
70
- let(:expected) { s = <<HTML
100
+ s.strip
101
+ }
102
+ subject { TextTube::Baby::LinkReffing.run content, format: :html }
103
+ include_examples "outputting links"
104
+ end
105
+ context "and an option to not show the link at all" do
106
+ let(:expected) { s = <<HTML
71
107
  The UtterFAIL website is good. My blog is also good.
72
108
  HTML
73
- s.strip
74
- }
75
- subject {
76
- TextTube::Baby::LinkReffing.run content, kind: :none
77
- }
78
- include_examples "outputting links"
79
- end
80
- end # context
109
+ s.strip
110
+ }
111
+ subject {
112
+ TextTube::Baby::LinkReffing.run content, kind: :none
113
+ }
114
+ include_examples "outputting links"
115
+ end
81
116
 
82
- context "With no link to be reffed in it" do
83
- let(:content) { %Q$The [UtterFAIL website](http://utterfail.info/ "UtterFAIL!") is good.$ }
84
- let(:expected) { content }
85
- subject { TextTube::Baby::LinkReffing.run content }
86
- it { should_not be_nil }
87
- it { should == expected }
88
- end # context
117
+ context "With no link to be reffed in it" do
118
+ let(:content) { %Q$The [UtterFAIL website](http://utterfail.info/ "UtterFAIL!") is good.$ }
119
+ let(:expected) { content }
120
+ subject { TextTube::Baby::LinkReffing.run content }
121
+ it { should_not be_nil }
122
+ it { should == expected }
123
+ end # context
124
+ end # context blah
125
+ end
89
126
  end # context
90
127
 
91
128
  context "Given no text" do
@@ -111,14 +111,12 @@ MARKDOWN
111
111
  context "An article that needs all the filters" do
112
112
  before :all do
113
113
  TextTube::Baby.load_all_filters
114
- warn "HERE"
115
- warn TextTube::Baby.constants
116
114
  class MyFilter < TextTube::Base
117
- register TextTube::Baby::Coderay
118
115
  register TextTube::Baby::LinkReffing
119
116
  register TextTube::Baby::EmbeddingAudio
120
117
  register TextTube::Baby::EmbeddingVideo
121
118
  register TextTube::Baby::InsideBlock
119
+ register TextTube::Baby::Coderay
122
120
  register do
123
121
  filter_with :rdiscount do |text|
124
122
  RDiscount.new(text).to_html
@@ -169,8 +167,7 @@ rainfall 99_998
169
167
 
170
168
  <p>Just to be clear, I ran this with Ruby 2.0.0-rc2 as well with the same results. It's a pity that Ruby fails at this kind of thing, because it is such an elegant and useful language that seems to have borrowed the best of many other niche languages. Recursion is often the most elegant <em>and</em> performant<a href="#1" title="Jump to reference">¹</a> solution.</p>
171
169
 
172
- <div id="reflinks">
173
- <p><a name="0"></a>[0] <a href="http://arxiv.org/abs/1304.5257" title="http://arxiv.org/abs/1304.5257">http://arxiv.org/abs/1304.5257</a> What Makes Code Hard to Understand? Michael Hansen, Robert L. Goldstone, Andrew Lumsdaine</p>
170
+ <div id="reflinks"><p><a name="0"></a>[0] <a href="http://arxiv.org/abs/1304.5257" title="http://arxiv.org/abs/1304.5257">http://arxiv.org/abs/1304.5257</a> What Makes Code Hard to Understand? Michael Hansen, Robert L. Goldstone, Andrew Lumsdaine</p>
174
171
 
175
172
  <p><a name="1"></a>[1] <a href="http://dictionary.cambridge.org/dictionary/british/pedant" title="http://dictionary.cambridge.org/dictionary/british/pedant">http://dictionary.cambridge.org/dictionary/br...</a> If you're one of these then you won't like that I made a word up. Unfortunately for you, I'm British, which means that I understand that English is a living language, it is my tool not my master, and it has a long history of being changed to suit the speaker or writer. This is one such case.</p>
176
173
  </div>
@@ -186,11 +183,14 @@ HTML
186
183
  before :all do
187
184
  MyFilter.options.merge! :linkreffing => {kind: :none}
188
185
  end
189
- subject { MyFilter.new(content).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount, :coderay, :insideblock }
186
+ subject { MyFilter.new(content).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount}#, :coderay, :insideblock }
190
187
  it { should_not == expected }
191
188
 
192
189
  context "and passed to filter" do
193
- subject { MyFilter.new(content).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount, :coderay, :insideblock, :linkreffing => {kind: :reference} }
190
+ subject {
191
+ r = MyFilter.new(content).filter :embeddingvideo, :embeddingaudio, :linkreffing, :rdiscount, :coderay, :insideblock, :linkreffing => {kind: :reference}
192
+ r
193
+ }
194
194
  it { should == expected }
195
195
  end
196
196
  end
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
  spec.add_dependency "texttube", ">= 6.0.0"
21
- spec.add_dependency "nokogiri"
21
+ spec.add_dependency "oga-without-the-wimpiness"
22
22
  spec.add_dependency "coderay"
23
23
  spec.add_development_dependency "bundler", "~> 1.7"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: texttube_baby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iain Barnett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-17 00:00:00.000000000 Z
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: texttube
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 6.0.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: nokogiri
28
+ name: oga-without-the-wimpiness
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  requirements: []
133
133
  rubyforge_project:
134
- rubygems_version: 2.2.2
134
+ rubygems_version: 2.4.5
135
135
  signing_key:
136
136
  specification_version: 4
137
137
  summary: Some useful filters for markdown that I use in my blogs