markdown_presenter 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/markdown_presenter +1 -1
- data/lib/markdown_presenter.rb +42 -39
- data/test/markdown_presenter_test.rb +33 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 004b3ff802e7e64f9d3225d72dfc889d160b5a6c
|
4
|
+
data.tar.gz: 5e28db5ca85fb5b2408e37a6c408ae469d4cc367
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0441f2d0b29b822d44546a1d97078feba8d0aee752218de27fc61e4ce988ee7d827919ae6cc5ba60446c4d2d5d4462d3793a32898efead753267c67d5dbe4f7
|
7
|
+
data.tar.gz: ea745e2f751b498dbe038ec03250f89a37f9af15433685e4d0f5ea045e1034a39f654c26227d33d9fadfb76fb09bd5be5bb3ea2260bc34e4f233662d6cda387f
|
data/bin/markdown_presenter
CHANGED
data/lib/markdown_presenter.rb
CHANGED
@@ -7,59 +7,62 @@ require 'open-uri'
|
|
7
7
|
require 'base64'
|
8
8
|
require 'mime/types'
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
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.
|
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:
|