markdown_presenter 0.7.2 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e25ffa8ff942aff9b88c88a5f6716699928dfbf6
4
- data.tar.gz: 8276ea1dfac4a6ccc30174239b17de1683f34faf
3
+ metadata.gz: 004b3ff802e7e64f9d3225d72dfc889d160b5a6c
4
+ data.tar.gz: 5e28db5ca85fb5b2408e37a6c408ae469d4cc367
5
5
  SHA512:
6
- metadata.gz: 925175ba26d08ff0e01c1b53916f378b2f6323e867f837415530a2cbc4c84030a2e0e07d08286f8b5e4a99b01e809afac9364cd3c14450afe5f9403a7e2cfb3a
7
- data.tar.gz: ab35cfaf7ca26e6287f290c272f4e63b4ef0ff0b2b913f06254ce7845e57bb0ac5b1283d97796ca91517c94abc135512abfaafcb28672e9443d8cce377372c85
6
+ metadata.gz: e0441f2d0b29b822d44546a1d97078feba8d0aee752218de27fc61e4ce988ee7d827919ae6cc5ba60446c4d2d5d4462d3793a32898efead753267c67d5dbe4f7
7
+ data.tar.gz: ea745e2f751b498dbe038ec03250f89a37f9af15433685e4d0f5ea045e1034a39f654c26227d33d9fadfb76fb09bd5be5bb3ea2260bc34e4f233662d6cda387f
@@ -3,4 +3,4 @@ require 'markdown_presenter'
3
3
 
4
4
  (puts "You need to specify a filename."; exit 1) if ARGV.empty?
5
5
 
6
- process ARGV[0]
6
+ MarkdownPresenter::process ARGV[0]
@@ -7,59 +7,62 @@ require 'open-uri'
7
7
  require 'base64'
8
8
  require 'mime/types'
9
9
 
10
- def present title, content
11
- template = File.read(File.join(File.dirname(__FILE__), "../presenter.html.haml"))
12
- haml_engine = Haml::Engine.new(template)
13
- haml_engine.render(Object.new, {slides: content.split(/(?=<h1)/).reject {|s| s.chomp.empty?}, title: title}) do |filename|
14
- File.read File.join(File.dirname(__FILE__), "../", filename)
10
+ module MarkdownPresenter
11
+
12
+ def self.present title, content
13
+ template = File.read(File.join(File.dirname(__FILE__), "../presenter.html.haml"))
14
+ haml_engine = Haml::Engine.new(template)
15
+ haml_engine.render(Object.new, {slides: content.split(/(?=<h1)/).reject {|s| s.chomp.empty?}, title: title}) do |filename|
16
+ File.read File.join(File.dirname(__FILE__), "../", filename)
17
+ end
15
18
  end
16
- end
17
19
 
18
- def content_of filename
19
- if filename.end_with? ".md"
20
- document = Kramdown::Document.new(File.read(filename), parse_block_html: true, parse_span_html: true)
21
- document.to_html
20
+ def self.content_of filename
21
+ if filename.end_with? ".md"
22
+ document = Kramdown::Document.new(File.read(filename), parse_block_html: true, parse_span_html: true)
23
+ document.to_html
22
24
 
23
- elsif filename.end_with? ".html"
24
- require 'nokogiri'
25
- doc = Nokogiri::HTML(File.read(filename))
26
- doc.search('body')[0].inner_html
25
+ elsif filename.end_with? ".html"
26
+ require 'nokogiri'
27
+ doc = Nokogiri::HTML(File.read(filename))
28
+ doc.search('body')[0].inner_html
27
29
 
28
- else
29
- $stderr.puts "Format not supported: #{File.extname(filename)}"
30
- exit 1
30
+ else
31
+ $stderr.puts "Format not supported: #{File.extname(filename)}"
32
+ exit 1
33
+ end
31
34
  end
32
- end
33
35
 
34
- def data_uri_for url
35
- begin
36
- response = open(url)
37
- encoded_image = Base64.encode64 response.read
38
- content_type = response.respond_to?(:meta) ? response.meta['content-type'] : MIME::Types.type_for(url[-3..url.size]).first.content_type
39
- "data:#{content_type};base64,#{encoded_image}"
40
- rescue
41
- $stderr.puts "WARNING: An exception occurred trying to construct a data URI. We will fall back to the original URL.", $!, $!.backtrace
42
- url
36
+ def self.data_uri_for url
37
+ begin
38
+ response = open(url)
39
+ encoded_image = Base64.encode64 response.read
40
+ content_type = response.respond_to?(:meta) ? response.meta['content-type'] : MIME::Types.type_for(url[-3..url.size]).first.content_type
41
+ "data:#{content_type};base64,#{encoded_image}"
42
+ rescue
43
+ $stderr.puts "WARNING: An exception occurred trying to construct a data URI. We will fall back to the original URL.", $!, $!.backtrace
44
+ url
45
+ end
43
46
  end
44
- end
45
47
 
46
- def embed content
47
- doc = Nokogiri::HTML(content)
48
- tags_and_attributes = [['img', 'src'], ['link','href'], ['script', 'src']]
49
- tags_and_attributes.each do |tag_name, src|
50
- doc.css(tag_name).each do |tag|
51
- tag[src] = data_uri_for tag[src]
48
+ def self.embed content
49
+ doc = Nokogiri::HTML(content)
50
+ tags_and_attributes = [['img', 'src'], ['link','href'], ['script', 'src']]
51
+ tags_and_attributes.each do |tag_name, src|
52
+ doc.css(tag_name).each do |tag|
53
+ tag[src] = data_uri_for tag[src]
54
+ end
52
55
  end
56
+ doc.css("body").inner_html
53
57
  end
54
- doc.css("body").inner_html
55
- end
56
58
 
57
- def process filename
58
- puts present(filename, embed(content_of(filename)))
59
+ def self.process filename
60
+ puts present(filename, embed(content_of(filename)))
61
+ end
59
62
  end
60
63
 
61
64
  if __FILE__ == $0
62
65
  ($stderr.puts "Specify a filename in argument."; exit 1) if ARGV.empty?
63
66
 
64
- process ARGV[0]
67
+ MarkdownPresenter::process ARGV[0]
65
68
  end
@@ -0,0 +1,33 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require_relative '../lib/markdown_presenter'
4
+
5
+
6
+ describe "MarkdownPresenter" do
7
+ def setup_response_with &block
8
+ MarkdownPresenter.class.send(:define_method, "open") do |url|
9
+ block.yield
10
+ end
11
+ end
12
+
13
+ it "falls back to original URL if it cannot fetch the file" do
14
+ setup_response_with {raise "this is an error raised willingly in a test"}
15
+
16
+ data_uri = MarkdownPresenter::data_uri_for "http://unreachable654987.io/image.png"
17
+ data_uri.must_equal "http://unreachable654987.io/image.png"
18
+ end
19
+
20
+ it 'encodes a url into a data uri' do
21
+ setup_response_with {
22
+ Struct.new(:read) do
23
+ def read
24
+ "test content"
25
+ end
26
+ end.new
27
+ }
28
+
29
+ path = '/fairyland/test-file.txt'
30
+ data_uri = MarkdownPresenter::data_uri_for "file://#{path}"
31
+ data_uri.must_equal "data:text/plain;base64,dGVzdCBjb250ZW50\n"
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu Tanguay-Carel
@@ -79,6 +79,7 @@ files:
79
79
  - js/hammer.min.js
80
80
  - js/jquery-1.10.2.min.js
81
81
  - presenter.html.haml
82
+ - test/markdown_presenter_test.rb
82
83
  - bin/markdown_presenter
83
84
  homepage: https://github.com/matstc/markdown_presenter
84
85
  licenses:
@@ -104,5 +105,6 @@ rubygems_version: 2.1.11
104
105
  signing_key:
105
106
  specification_version: 4
106
107
  summary: Transforms a markdown file into an HTML presentation
107
- test_files: []
108
+ test_files:
109
+ - test/markdown_presenter_test.rb
108
110
  has_rdoc: