pagehub-markdown 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56b56c658c3c84a4e2aeb30d61c51d9d7a31b7cf
4
+ data.tar.gz: bf8cd37f12f181b00cd54870bfd8b5f1909945c3
5
+ SHA512:
6
+ metadata.gz: 37d90d0c5c9e6e9ea77275196e6a8551a8b060a9859d4f71078df472c55b6e9e3adb39a61fe201413ee0ff25ebeb9799e5784e82921c33e0a66ef53bba74a00c
7
+ data.tar.gz: 4fd843a037d26d38e9bfa5453d4d6a83003018932b7cfd02a8f771d8e603fb2b32a0056ffab183f1fd1d124e0e5e9f95be62555b5e0251d705c31d8e07f430f3
@@ -1,5 +1,5 @@
1
1
  require 'redcarpet'
2
- require 'albino'
2
+ require 'pygments'
3
3
 
4
4
  require 'pagehub-markdown/markdown'
5
5
  require 'pagehub-markdown/processors/embedder'
@@ -33,13 +33,22 @@ module PageHub
33
33
  # PageHubOptions, RendererOptions, and RendererExtensions
34
34
  # for accepted values
35
35
  def configure(ph_options = {}, options = {}, extensions = {})
36
- @@options = PageHubOptions.merge(ph_options)
36
+ @@options ||= PageHubOptions.dup
37
+ @@options.merge!(ph_options)
37
38
 
38
39
  @@renderer = Redcarpet::Markdown.new(
39
40
  HTMLWithAlbino.new(RendererOptions.merge(options)),
40
41
  RendererExtensions.merge(extensions))
41
42
  end
42
43
 
44
+ def reset_config()
45
+ @@options = PageHubOptions.dup
46
+ end
47
+
48
+ def options
49
+ @@options
50
+ end
51
+
43
52
  def render!(str)
44
53
  configure
45
54
 
@@ -47,10 +56,7 @@ module PageHub
47
56
 
48
57
  # escape any JavaScript snippets
49
58
  if @@options[:escape_scripts]
50
- str.gsub!(/\<script(.*)\>/i) {
51
- mutated = true
52
- "&lt;script#{$1}&gt;"
53
- }
59
+ str.gsub!(%r{<([^>]*)script[^>]*>}, "&lt;\\1script&gt;")
54
60
  end
55
61
 
56
62
  str = @@renderer.render(str)
@@ -61,7 +67,9 @@ module PageHub
61
67
  end
62
68
 
63
69
  def render(str)
64
- o = str.dup; render!(o); o
70
+ str.dup.tap do |md|
71
+ render!(md)
72
+ end
65
73
  end
66
74
 
67
75
  def mutate!(str)
@@ -80,7 +88,8 @@ module PageHub
80
88
  @@options = { }
81
89
 
82
90
  PageHubOptions = {
83
- escape_scripts: true
91
+ escape_scripts: true,
92
+ plain_errors: true
84
93
  }
85
94
 
86
95
  RendererOptions = {
@@ -112,10 +121,11 @@ module PageHub
112
121
  def block_code(code, language)
113
122
  begin
114
123
  # TODO: try to figure out whether @language is valid
115
- out = Albino.colorize(code, language)
124
+ out = Pygments.highlight(code, { lexer: language.to_s, encoding: 'utf-8' })
116
125
  rescue Exception => e
117
- out = ""
118
126
  # return "-- INVALID CODE BLOCK, MAKE SURE YOU'VE SURROUNDED CODE WITH ```"
127
+ puts "[warn] pagehub-markdown: unable to highlight code block for language #{language}. Reason: #{e.message}"
128
+ out = ""
119
129
  end
120
130
 
121
131
  # just render the code as plain text if the language is invalid
@@ -9,7 +9,6 @@ module Markdown
9
9
  # and allows for content extraction from HTML pages
10
10
  # so it can be neatly embedded in another page.
11
11
  module Embedder
12
-
13
12
  class EmbeddingError < RuntimeError; end
14
13
  class InvalidSizeError < EmbeddingError; end
15
14
  class InvalidTypeError < EmbeddingError; end
@@ -54,9 +53,12 @@ module Markdown
54
53
  # reject if the host is banned
55
54
  return "" if FilteredHosts.include?(uri.host)
56
55
 
57
- Net::HTTP.start(uri.host, uri.port) do |http|
58
- http.open_timeout = Timeout
59
- http.read_timeout = Timeout
56
+ http = Net::HTTP.new(uri.host, uri.port)
57
+ http.open_timeout = Timeout
58
+ http.read_timeout = Timeout
59
+ http.use_ssl = (uri.scheme == 'https')
60
+
61
+ http.start do
60
62
 
61
63
  # get the content type and length
62
64
  ctype = ""
@@ -92,7 +94,7 @@ module Markdown
92
94
  raise e
93
95
  rescue Exception => e
94
96
  # mask as a generic EmbeddingError
95
- raise EmbeddingError.new e.message
97
+ raise EmbeddingError.new "generic: #{e.class}##{e.message}"
96
98
  end
97
99
 
98
100
  ""
@@ -183,7 +185,7 @@ module Markdown
183
185
  }
184
186
  end
185
187
 
186
- node
188
+ node.to_s
187
189
  end
188
190
 
189
191
  end
@@ -204,18 +206,19 @@ module Markdown
204
206
  node.xpath('div[@id="breadcrumbs"]').remove
205
207
  node.xpath('div[@id="bottom"]').remove
206
208
  stamp(node, uri, 'pagehub')
207
- node
209
+ node.to_s
208
210
  end
209
211
  end
210
212
 
211
213
  register_processor(GithubWikiProcessor.new)
212
214
  register_processor(PageHubProcessor.new)
213
215
 
216
+ MATCH = /^\B\[\![include|embed]\s?(.*)\!\]\((.*)\)/
214
217
  end # Embedder module
215
218
 
216
219
  add_processor :pre_render, lambda {|str|
217
220
  # Embed remote references, if any
218
- str.gsub!(/^\B\[\!include\s?(.*)\!\]\((.*)\)/) {
221
+ str.gsub!(Embedder::MATCH) {
219
222
  content = ""
220
223
 
221
224
  uri = $2
@@ -227,6 +230,15 @@ module Markdown
227
230
 
228
231
  begin
229
232
  content = Embedder.get_resource(uri, source, args)
233
+ content.gsub!(Embedder::MATCH) {
234
+ source, uri = ($1 || '').split.first, $2
235
+
236
+ s = "[See this indirectly embedded resource"
237
+ s << source.empty? ? '' : " from '#{source}'"
238
+ s << ": #{uri}]"
239
+ s << "(#{uri})"
240
+ s
241
+ }
230
242
  rescue Embedder::InvalidSizeError => e
231
243
  content << "**Embedding error**: the file you tried to embed is too big - #{e.message.to_i} bytes."
232
244
  content << " (**Source**: [#{$2}](#{$2}))\n\n"
@@ -1,6 +1,7 @@
1
1
  module PageHub
2
2
  module Markdown
3
3
  module ToC
4
+ FENCED_CODE_BLOCKS = /```[^`]+```/xm
4
5
 
5
6
  # Builds a tree of headings from a given block of Markdown
6
7
  # text, the returned list can be turned into HTML using
@@ -24,7 +25,7 @@ module Markdown
24
25
  headings = []
25
26
  current = []
26
27
  toc_index = 0
27
- content.scan(pattern).each { |l, t|
28
+ content.gsub(FENCED_CODE_BLOCKS, '').scan(pattern).each { |l, t|
28
29
  level,title = formatter.call(l, t)
29
30
 
30
31
  if level <= threshold
@@ -73,7 +74,7 @@ module Markdown
73
74
  def to_html()
74
75
  html = ""
75
76
  html << "<li>"
76
- html << "<a href=\"\#toc_#{index}\">" << title << "</a>"
77
+ html << "<a href=\"\##{header_anchor(title)}\">" << title << "</a>"
77
78
 
78
79
  if children.any? then
79
80
  html << "<ol>"
@@ -83,6 +84,12 @@ module Markdown
83
84
 
84
85
  html << "</li>"
85
86
  end
87
+
88
+ private
89
+
90
+ def header_anchor(title)
91
+ CGI.escapeHTML(title.downcase.gsub(/\s+/, '-'))
92
+ end
86
93
  end
87
94
  end
88
95
 
metadata CHANGED
@@ -1,94 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagehub-markdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ahmad Amireh
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-10-04 00:00:00.000000000 Z
11
+ date: 2014-08-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: redcarpet
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '='
20
18
  - !ruby/object:Gem::Version
21
- version: 2.1.1
19
+ version: 3.1.2
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '='
28
25
  - !ruby/object:Gem::Version
29
- version: 2.1.1
26
+ version: 3.1.2
30
27
  - !ruby/object:Gem::Dependency
31
- name: albino
28
+ name: json
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
- version: 1.3.3
33
+ version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
- version: 1.3.3
40
+ version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
- name: json
42
+ name: nokogiri
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '='
52
46
  - !ruby/object:Gem::Version
53
- version: 1.7.0
47
+ version: 1.6.3.1
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '='
60
53
  - !ruby/object:Gem::Version
61
- version: 1.7.0
54
+ version: 1.6.3.1
62
55
  - !ruby/object:Gem::Dependency
63
- name: nokogiri
56
+ name: pygments.rb
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - "~>"
68
60
  - !ruby/object:Gem::Version
69
- version: 1.5.5
61
+ version: 0.6.0
70
62
  type: :runtime
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - "~>"
76
67
  - !ruby/object:Gem::Version
77
- version: 1.5.5
68
+ version: 0.6.0
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: rspec
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - ">="
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - ">="
92
81
  - !ruby/object:Gem::Version
93
82
  version: '0'
94
83
  description: A bunch of extensions added to the Markdown renderer usable as pure Markdown
@@ -99,33 +88,33 @@ extensions: []
99
88
  extra_rdoc_files: []
100
89
  files:
101
90
  - lib/pagehub-markdown.rb
102
- - lib/pagehub-markdown/processors/toc_generator.rb
103
- - lib/pagehub-markdown/processors/pagehub_options.rb
104
- - lib/pagehub-markdown/processors/embedder.rb
105
91
  - lib/pagehub-markdown/markdown.rb
106
92
  - lib/pagehub-markdown/mutators/date_injector.rb
93
+ - lib/pagehub-markdown/processors/embedder.rb
94
+ - lib/pagehub-markdown/processors/pagehub_options.rb
95
+ - lib/pagehub-markdown/processors/toc_generator.rb
107
96
  homepage: https://github.com/amireh/pagehub-markdown
108
97
  licenses: []
98
+ metadata: {}
109
99
  post_install_message:
110
100
  rdoc_options: []
111
101
  require_paths:
112
102
  - lib
113
103
  required_ruby_version: !ruby/object:Gem::Requirement
114
- none: false
115
104
  requirements:
116
- - - ! '>='
105
+ - - ">="
117
106
  - !ruby/object:Gem::Version
118
107
  version: '0'
119
108
  required_rubygems_version: !ruby/object:Gem::Requirement
120
- none: false
121
109
  requirements:
122
- - - ! '>='
110
+ - - ">="
123
111
  - !ruby/object:Gem::Version
124
112
  version: '0'
125
113
  requirements: []
126
114
  rubyforge_project:
127
- rubygems_version: 1.8.23
115
+ rubygems_version: 2.2.0
128
116
  signing_key:
129
- specification_version: 3
117
+ specification_version: 4
130
118
  summary: PageHub's extensions of GitHub's Redcarpet Markdown renderer.
131
119
  test_files: []
120
+ has_rdoc: