transmuter 0.0.0.1 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +17 -0
- data/Guardfile +13 -0
- data/README.md +35 -1
- data/Rakefile +7 -0
- data/bin/transmute +7 -0
- data/lib/transmuter/cli/thor.rb +99 -0
- data/lib/transmuter/cli/transmute.rb +67 -0
- data/lib/transmuter/cli.rb +13 -0
- data/lib/transmuter/core_ext.rb +4 -0
- data/lib/transmuter/format/html.rb +81 -0
- data/lib/transmuter/format/markdown.rb +45 -0
- data/lib/transmuter/format/pdf.rb +25 -0
- data/lib/transmuter/format.rb +9 -0
- data/lib/transmuter/version.rb +3 -4
- data/lib/transmuter.rb +5 -4
- data/spec/spec_helper.rb +25 -0
- data/spec/transmuter/cli/thor_spec.rb +175 -0
- data/spec/transmuter/cli/transmute_spec.rb +138 -0
- data/spec/transmuter/cli_spec.rb +4 -0
- data/spec/transmuter/format/html_spec.rb +159 -0
- data/spec/transmuter/format/markdown_spec.rb +150 -0
- data/spec/transmuter/format/pdf_spec.rb +49 -0
- data/spec/transmuter/format_spec.rb +4 -0
- data/stylesheets/default.css +67 -0
- data/transmuter.gemspec +26 -3
- metadata +180 -8
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CLI do
|
4
|
+
describe "Thor" do
|
5
|
+
before(:all) do
|
6
|
+
@valid_initialize_options = ['README.md']
|
7
|
+
end
|
8
|
+
|
9
|
+
subject { CLI::Runner.new(@valid_initialize_options) }
|
10
|
+
|
11
|
+
describe "Thor group definition" do
|
12
|
+
subject { CLI::Runner }
|
13
|
+
it { should respond_to(:desc) }
|
14
|
+
it { should respond_to(:class_option) }
|
15
|
+
it { should respond_to(:argument) }
|
16
|
+
its (:desc) { should_not be_empty }
|
17
|
+
its (:arguments) { should_not be_empty }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "input" do
|
21
|
+
it "should have an arguments :input defined" do
|
22
|
+
CLI::Runner.arguments.any? { |arg| arg.name == 'input' }.should be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be required" do
|
26
|
+
-> { CLI::Runner.new }.should raise_error Thor::RequiredArgumentMissingError,
|
27
|
+
"No value provided for required arguments 'input'"
|
28
|
+
end
|
29
|
+
|
30
|
+
it { should respond_to(:set_input_filename) }
|
31
|
+
|
32
|
+
it "should set @input_filename" do
|
33
|
+
subject.set_input_filename
|
34
|
+
subject.instance_variable_get('@input_filename').should == 'README.md'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "input format" do
|
39
|
+
it "should have a class_option input_format defined" do
|
40
|
+
CLI::Runner.class_options.should have_key(:input_format)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not be required" do
|
44
|
+
-> { CLI::Runner.new @valid_initialize_options }.should_not
|
45
|
+
raise_error Thor::RequiredArgumentMissingError
|
46
|
+
end
|
47
|
+
|
48
|
+
it { should respond_to(:set_input_fileformat) }
|
49
|
+
|
50
|
+
it "should set @input_fileformat with --input_format" do
|
51
|
+
cli = CLI::Runner.new @valid_initialize_options, input_format: 'format1'
|
52
|
+
cli.set_input_fileformat
|
53
|
+
cli.instance_variable_get('@input_fileformat').should == "format1"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should be markdown if the input file extensions is .md" do
|
57
|
+
cli = CLI::Runner.new ['README.md']
|
58
|
+
cli.set_input_fileformat
|
59
|
+
cli.instance_variable_get('@input_fileformat').should == "markdown"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should be markdown if the input file extensions is .markdown" do
|
63
|
+
cli = CLI::Runner.new ['README.markdown']
|
64
|
+
cli.set_input_fileformat
|
65
|
+
cli.instance_variable_get('@input_fileformat').should == "markdown"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should be html if the input file extensions is .htm" do
|
69
|
+
cli = CLI::Runner.new ['README.htm']
|
70
|
+
cli.set_input_fileformat
|
71
|
+
cli.instance_variable_get('@input_fileformat').should == "html"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should be html if the input file extensions is .html" do
|
75
|
+
cli = CLI::Runner.new ['README.html']
|
76
|
+
cli.set_input_fileformat
|
77
|
+
cli.instance_variable_get('@input_fileformat').should == "html"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "output format" do
|
82
|
+
|
83
|
+
it "should have a class_option output_format defined" do
|
84
|
+
CLI::Runner.class_options.should have_key(:output_format)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should not be required" do
|
88
|
+
-> { CLI::Runner.new @valid_initialize_options }.should_not
|
89
|
+
raise_error Thor::RequiredArgumentMissingError
|
90
|
+
end
|
91
|
+
|
92
|
+
it { should respond_to(:set_output_fileformat) }
|
93
|
+
|
94
|
+
it "should set @output_fileformat with --output_format" do
|
95
|
+
cli = CLI::Runner.new @valid_initialize_options, output_format: 'format1'
|
96
|
+
cli.set_output_fileformat
|
97
|
+
cli.instance_variable_get('@output_fileformat').should == "format1"
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should default to pdf" do
|
101
|
+
subject.set_output_fileformat
|
102
|
+
subject.instance_variable_get('@output_fileformat').should == "pdf"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "output" do
|
107
|
+
before(:each) do
|
108
|
+
subject.set_input_filename
|
109
|
+
subject.set_input_fileformat
|
110
|
+
subject.set_output_fileformat
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should have an arguments :input defined" do
|
114
|
+
CLI::Runner.arguments.any? { |arg| arg.name == 'output' }.should be_true
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should not be required" do
|
118
|
+
-> { CLI::Runner.new @valid_initialize_options }.should_not
|
119
|
+
raise_error Thor::RequiredArgumentMissingError
|
120
|
+
end
|
121
|
+
|
122
|
+
it { should respond_to(:set_output_filename) }
|
123
|
+
|
124
|
+
it "should raise an exception of both output_format and output are empty" do
|
125
|
+
cli = CLI::Runner.new @valid_initialize_options, output_format: ''
|
126
|
+
cli.set_input_filename
|
127
|
+
cli.set_input_fileformat
|
128
|
+
cli.set_output_fileformat
|
129
|
+
|
130
|
+
-> { cli.set_output_filename }.should raise_error ArgumentError,
|
131
|
+
"Either output or output_format should be given,"
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should set @output_filename" do
|
135
|
+
subject.set_output_filename
|
136
|
+
subject.instance_variable_get('@output_filename').should == 'README.pdf'
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "stylesheets" do
|
141
|
+
|
142
|
+
it "should have a class_option stylesheets defined" do
|
143
|
+
CLI::Runner.class_options.should have_key(:stylesheets)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should not be required" do
|
147
|
+
-> { CLI::Runner.new @valid_initialize_options }.should_not
|
148
|
+
raise_error Thor::RequiredArgumentMissingError
|
149
|
+
end
|
150
|
+
|
151
|
+
it { should respond_to(:set_stylesheets) }
|
152
|
+
|
153
|
+
it "should set @stylesheets with --stylesheets" do
|
154
|
+
cli = CLI::Runner.new @valid_initialize_options, stylesheets: 'stylesheets/test.css'
|
155
|
+
cli.set_stylesheets
|
156
|
+
cli.instance_variable_get('@stylesheets').should == 'stylesheets/test.css'
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should default to [#{ROOT_PATH}/stylesheets/default.css]" do
|
160
|
+
subject.set_stylesheets
|
161
|
+
subject.instance_variable_get('@stylesheets').should == ["#{ROOT_PATH}/stylesheets/default.css"]
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe "#start" do
|
166
|
+
it "should call transmute" do
|
167
|
+
CLI::Runner.any_instance.expects(:transmute).returns(true).at_least(1)
|
168
|
+
CLI::Runner.any_instance.expects(:transmute!).returns(true).at_least(1)
|
169
|
+
|
170
|
+
CLI::Runner.start ["README.md"]
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pdfkit'
|
3
|
+
|
4
|
+
describe CLI do
|
5
|
+
describe "Transmute" do
|
6
|
+
|
7
|
+
let(:markdown_h1) { '# Heading 1' }
|
8
|
+
let(:html_h1) { '<h1>Heading 1</h1>' }
|
9
|
+
|
10
|
+
before(:all) do
|
11
|
+
@valid_arguments = ['README.md']
|
12
|
+
@valid_html_options = { output_format: 'html' }
|
13
|
+
@valid_pdf_options = { output_format: 'pdf' }
|
14
|
+
@valid_html_start_args = begin
|
15
|
+
@valid_arguments + begin
|
16
|
+
@valid_html_options.collect do |k, v|
|
17
|
+
"--#{k.to_s.gsub(/_/, '-')}=#{v}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@valid_pdf_start_args = begin
|
22
|
+
@valid_arguments + begin
|
23
|
+
@valid_pdf_options.collect do |k, v|
|
24
|
+
"--#{k.to_s.gsub(/_/, '-')}=#{v}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
subject { CLI::Runner.new(@valid_arguments, @valid_html_options) }
|
31
|
+
|
32
|
+
before(:each) do
|
33
|
+
File.stubs(:read).with('README.md').returns(markdown_h1)
|
34
|
+
File.stubs(:read).with(::Transmuter::CLI::Runner::DEFAULT_THEME).returns('h1 { color: #000; }')
|
35
|
+
File.any_instance.stubs(:write).returns(true)
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "Definitions" do
|
39
|
+
it { should respond_to(:transmute) }
|
40
|
+
it { should respond_to(:transmute!) }
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#transmute!" do
|
44
|
+
describe "Errors" do
|
45
|
+
it "should raise a NameError exception if input is invalid" do
|
46
|
+
subject.instance_variable_set("@input_fileformat", 'Invalid')
|
47
|
+
-> { subject.transmute! }.should raise_error NameError
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should raise a NameError exception if output is invalid" do
|
51
|
+
subject.instance_variable_set("@output_fileformat", 'Invalid')
|
52
|
+
-> { subject.transmute! }.should raise_error NameError
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should raise an NotImplementedError if we don't know how to transmute from input to output" do
|
56
|
+
subject.instance_variable_set("@output_fileformat", "invalid")
|
57
|
+
subject.instance_variable_set("@destination_klass", "set to bypass errors")
|
58
|
+
-> { subject.transmute! }.should raise_error NotImplementedError
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should raise an NotImplementedError if we don't know how to process output" do
|
62
|
+
not_output = Class.new
|
63
|
+
subject.instance_variable_set("@output_fileformat", "invalid")
|
64
|
+
subject.instance_variable_set("@destination_klass", not_output)
|
65
|
+
-> { subject.transmute! }.should raise_error NotImplementedError
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "Transmuting from markdown to HTML" do
|
70
|
+
it "should invoke #transmute!" do
|
71
|
+
CLI::Runner.any_instance.expects(:transmute!).at_least(1)
|
72
|
+
|
73
|
+
CLI::Runner.start @valid_html_start_args
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should read the source file" do
|
77
|
+
File.expects(:read).with('README.md').returns(markdown_h1).once
|
78
|
+
|
79
|
+
CLI::Runner.start @valid_html_start_args
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should write the source file" do
|
83
|
+
File.any_instance.expects(:write).once
|
84
|
+
|
85
|
+
CLI::Runner.start @valid_html_start_args
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "Transmuting from markdown to PDF" do
|
91
|
+
before(:each) do
|
92
|
+
pdfkit_instance = mock()
|
93
|
+
pdfkit_instance.stubs(:to_pdf).returns true
|
94
|
+
PDFKit.stubs(:new).returns(pdfkit_instance)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should invoke #transmute!" do
|
98
|
+
CLI::Runner.any_instance.expects(:transmute!).at_least(1)
|
99
|
+
|
100
|
+
CLI::Runner.start @valid_pdf_start_args
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should read the source file" do
|
104
|
+
File.expects(:read).with('README.md').returns(markdown_h1).once
|
105
|
+
|
106
|
+
CLI::Runner.start @valid_pdf_start_args
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should write the source file" do
|
110
|
+
File.any_instance.expects(:write).once
|
111
|
+
|
112
|
+
CLI::Runner.start @valid_pdf_start_args
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "stylesheets" do
|
118
|
+
it "should set a default stylesheets" # do
|
119
|
+
# Format::Markdown.any_instance.expects(:parse_options).
|
120
|
+
# with(stylesheets: "#{ROOT_PATH}/stylesheets/default.css")
|
121
|
+
#
|
122
|
+
# CLI::Runner.start @valid_html_start_args
|
123
|
+
# end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "#transmute" do
|
128
|
+
describe "Errors" do
|
129
|
+
it "should not raise a NameError exception" do
|
130
|
+
cli = subject.dup
|
131
|
+
cli.instance_variable_set("@input_fileformat", 'Invalid')
|
132
|
+
-> { cli.transmute }.should_not raise_error NameError
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Format
|
4
|
+
describe Html do
|
5
|
+
let(:html_h1) { '<h1>Heading 1</h1>' }
|
6
|
+
let(:html_ruby) { %(<pre lang=\"ruby\"><code>def say_hi\n "Hello, world!"\nend\n</code></pre>\n) }
|
7
|
+
|
8
|
+
subject { Html.new(html_h1, stylesheets: "/path/to/file.css") }
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
File.stubs(:read).with('/path/to/file.css').returns('h1 { color: #000; }')
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#new" do
|
15
|
+
it "should require html" do
|
16
|
+
-> { Html.new }.should raise_error ArgumentError, "wrong number of arguments (0 for 1)"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should set @html" do
|
20
|
+
h = Html.new(html_h1)
|
21
|
+
h.instance_variable_get("@html").should == html_h1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#parse_options" do
|
26
|
+
it "should have parse_options as a protected method" do
|
27
|
+
Html.protected_instance_methods.should include(:parse_options)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should set @options" do
|
31
|
+
subject.instance_variable_get('@options').should_not be_empty
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#read_stylesheet_files" do
|
36
|
+
describe "as an Array" do
|
37
|
+
it "should have read_stylesheet_files as a protected method" do
|
38
|
+
Html.protected_instance_methods.should include(:read_stylesheet_files)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should reads the stylesheets from the specified files" do
|
42
|
+
File.expects(:read).with('/path/to/file.css').once
|
43
|
+
subject.send :read_stylesheet_files
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "as a String" do
|
48
|
+
it "should have read_stylesheet_files as a protected method" do
|
49
|
+
Html.protected_instance_methods.should include(:read_stylesheet_files)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should reads the stylesheets from the specified files" do
|
53
|
+
File.expects(:read).with('/path/to/file.css').once
|
54
|
+
subject.send :read_stylesheet_files
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#include_inline_stylesheets" do
|
60
|
+
it "should render html with stylsheets" do
|
61
|
+
require 'nokogiri'
|
62
|
+
html = "<html><body>#{html_h1}</body></html>"
|
63
|
+
styled_html = subject.send(:include_inline_stylesheets, html)
|
64
|
+
|
65
|
+
doc = Nokogiri::HTML(styled_html)
|
66
|
+
doc.search('/html/head').size.should == 1
|
67
|
+
|
68
|
+
styled_html.should
|
69
|
+
match(%r(<html>[^<head>]*<head><style [^>]*>h1 { color: #000; }.*</style>.*</head>[^<head>]*<body>)m)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should render html with stylsheets even if there's already head" do
|
73
|
+
require 'nokogiri'
|
74
|
+
html = %(<html><head><link rel="stylesheet" href="styles.css" type="text/css" /></head><body>#{html_h1}</body></html>)
|
75
|
+
styled_html = subject.send(:include_inline_stylesheets, html)
|
76
|
+
|
77
|
+
doc = Nokogiri::HTML(styled_html)
|
78
|
+
doc.search('/html/head').size.should == 1
|
79
|
+
|
80
|
+
styled_html.should
|
81
|
+
match(%r(<html>[^<head>]*<head><style [^>]*>h1 { color: #000; }.*</style>.*</head>[^<head>]*<body>)m)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#syntax_highlighter" do
|
86
|
+
it "should have syntax_highlighter as a protected method" do
|
87
|
+
Html.protected_instance_methods.should include(:syntax_highlighter)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should call Nokogiri::HTML" do
|
91
|
+
nokogiri_document = mock()
|
92
|
+
nokogiri_document.stubs(:search).returns([])
|
93
|
+
Nokogiri.expects(:HTML).with(html_h1).once.returns(nokogiri_document)
|
94
|
+
|
95
|
+
subject.send :syntax_highlighter
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should call Albino.colorize" do
|
99
|
+
pre = mock
|
100
|
+
pre.stubs(:text).returns("some html")
|
101
|
+
pre.stubs(:[]).with(:lang).returns(:ruby)
|
102
|
+
pre.stubs(:replace).returns(true)
|
103
|
+
nokogiri_document = mock()
|
104
|
+
nokogiri_document.stubs(:search).returns([pre])
|
105
|
+
Nokogiri.expects(:HTML).with(html_h1).once.returns(nokogiri_document)
|
106
|
+
|
107
|
+
Albino.expects(:colorize).once.returns("")
|
108
|
+
subject.send(:syntax_highlighter)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "#process" do
|
113
|
+
it { should respond_to :process }
|
114
|
+
|
115
|
+
describe "call stack" do
|
116
|
+
it "should call syntax_highlighter" do
|
117
|
+
Html.any_instance.expects(:syntax_highlighter).once.returns(html_h1)
|
118
|
+
|
119
|
+
subject.process
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should call include_inline_stylesheets" do
|
123
|
+
Html.any_instance.expects(:include_inline_stylesheets).once.returns(html_h1)
|
124
|
+
|
125
|
+
subject.process
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "#to_pdf" do
|
132
|
+
before(:each) do
|
133
|
+
pdfkit_instance = mock()
|
134
|
+
pdfkit_instance.stubs(:to_pdf).returns true
|
135
|
+
PDFKit = mock() unless defined?(PDFKit)
|
136
|
+
PDFKit.stubs(:new).returns(pdfkit_instance)
|
137
|
+
end
|
138
|
+
|
139
|
+
it { should respond_to :to_pdf }
|
140
|
+
|
141
|
+
describe "call stack" do
|
142
|
+
it "should call process" do
|
143
|
+
Html.any_instance.expects(:process).returns(html_h1).once
|
144
|
+
|
145
|
+
subject.to_pdf
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should create a new Pdf object" do
|
149
|
+
pdf = mock
|
150
|
+
pdf.stubs(:process).returns(true)
|
151
|
+
Pdf.expects(:new).returns(pdf).once
|
152
|
+
|
153
|
+
subject.to_pdf
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Format
|
4
|
+
describe Markdown do
|
5
|
+
|
6
|
+
let(:markdown_h1) { '# Heading 1' }
|
7
|
+
let(:html_h1) { '<h1>Heading 1</h1>' }
|
8
|
+
let(:markdown_ruby) { %(```ruby\ndef say_hi\n "Hello, world!"\nend\n```) }
|
9
|
+
let(:html_ruby) { %(<pre lang=\"ruby\"><code>def say_hi\n "Hello, world!"\nend\n</code></pre>\n) }
|
10
|
+
|
11
|
+
subject { Markdown.new(markdown_h1, stylesheets: "/path/to/file.css") }
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
File.stubs(:read).with('/path/to/file.css').returns('h1 { color: #000; }')
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "REDCARPET_OPTIONS" do
|
18
|
+
it "shoudle have defined REDCARPET_OPTIONS" do
|
19
|
+
Markdown.constants.should include(:REDCARPET_OPTIONS)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be equal to [:autolink, :no_intraemphasis, :fenced_code, :gh_blockcode]" do
|
23
|
+
Markdown::REDCARPET_OPTIONS.should == [:autolink, :no_intraemphasis, :fenced_code, :gh_blockcode]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#new" do
|
28
|
+
it "should require markdown" do
|
29
|
+
-> { Markdown.new }.should raise_error ArgumentError, "wrong number of arguments (0 for 1)"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should set @markdown" do
|
33
|
+
m = Markdown.new(markdown_h1)
|
34
|
+
m.instance_variable_get("@markdown").should == markdown_h1
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#parse_options" do
|
39
|
+
it "should have parse_options as a protected method" do
|
40
|
+
Markdown.protected_instance_methods.should include(:parse_options)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should set @options" do
|
44
|
+
subject.instance_variable_get('@options').should_not be_empty
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should include :redcarpet_options in the options" do
|
48
|
+
redcarpet_options = subject.instance_variable_get('@options')[:redcarpet_options]
|
49
|
+
redcarpet_options.should_not be_empty
|
50
|
+
redcarpet_options.should == Markdown::REDCARPET_OPTIONS
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#create_markdown" do
|
55
|
+
it "should have create_markdown as a protected method" do
|
56
|
+
Markdown.protected_instance_methods.should include(:create_markdown)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should create a new Redcarpet object" do
|
60
|
+
m = subject.send :create_markdown
|
61
|
+
m.should be_instance_of Redcarpet
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#parse_markdown" do
|
66
|
+
it "should have parse_markdown as a protected method" do
|
67
|
+
Markdown.protected_instance_methods.should include(:parse_markdown)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should call create_markdown" do
|
71
|
+
markdown = mock()
|
72
|
+
markdown.stubs(:to_html).returns(true)
|
73
|
+
Markdown.any_instance.expects(:create_markdown).once.returns(markdown)
|
74
|
+
|
75
|
+
subject.send(:parse_markdown)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should call Redcarpet.to_html" do
|
79
|
+
Redcarpet.any_instance.expects(:to_html).returns(true)
|
80
|
+
|
81
|
+
subject.send(:parse_markdown)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should render simple markdown" do
|
85
|
+
subject.send(:parse_markdown).should match(%r(#{html_h1}))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "#to_html" do
|
90
|
+
describe "call stach" do
|
91
|
+
it "should call parse_markdown" do
|
92
|
+
Markdown.any_instance.expects(:parse_markdown).once.returns(html_h1)
|
93
|
+
|
94
|
+
subject.to_html
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should call process on the Html object" do
|
98
|
+
Html.any_instance.expects(:process).once.returns(html_h1)
|
99
|
+
|
100
|
+
subject.to_html
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it { should respond_to :to_html }
|
105
|
+
|
106
|
+
it "should call Redcarpet.to_html" do
|
107
|
+
Redcarpet.any_instance.expects(:to_html).returns(html_h1)
|
108
|
+
|
109
|
+
subject.to_html
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should render simple markdown" do
|
113
|
+
subject.to_html.should match(%r(#{html_h1}))
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should render simple markdown and include stylesheets" do
|
117
|
+
subject.to_html.should
|
118
|
+
match(%r(<html>.*<head>.*<style [^>]*>h1 { color: #000; }.*</style>.*</head>.*<body>.*#{html_h1}.*</body>.*</html>)m)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "#to_pdf" do
|
123
|
+
before(:each) do
|
124
|
+
pdfkit_instance = mock()
|
125
|
+
pdfkit_instance.stubs(:to_pdf).returns true
|
126
|
+
PDFKit = mock() unless defined?(PDFKit)
|
127
|
+
PDFKit.stubs(:new).returns(pdfkit_instance)
|
128
|
+
end
|
129
|
+
|
130
|
+
it { should respond_to :to_pdf }
|
131
|
+
|
132
|
+
describe "call stack" do
|
133
|
+
|
134
|
+
it "should call to_html" do
|
135
|
+
Html.any_instance.expects(:process).returns(html_h1).once
|
136
|
+
|
137
|
+
subject.to_pdf
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should create a new Pdf object" do
|
141
|
+
pdf = mock
|
142
|
+
pdf.stubs(:process).returns(true)
|
143
|
+
Pdf.expects(:new).returns(pdf).once
|
144
|
+
|
145
|
+
subject.to_pdf
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Format
|
4
|
+
describe Pdf do
|
5
|
+
let(:html_h1) { '<h1>Heading 1</h1>' }
|
6
|
+
let(:html_ruby) { %(<pre lang=\"ruby\"><code>def say_hi\n "Hello, world!"\nend\n</code></pre>\n) }
|
7
|
+
|
8
|
+
subject { Pdf.new(html_h1, stylesheets: "/path/to/file.css") }
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
File.stubs(:read).with('/path/to/file.css').returns('h1 { color: #000; }')
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#new" do
|
15
|
+
it "should require html" do
|
16
|
+
-> { Pdf.new }.should raise_error ArgumentError, "wrong number of arguments (0 for 1)"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should set @html" do
|
20
|
+
h = Pdf.new(html_h1)
|
21
|
+
h.instance_variable_get("@html").should == html_h1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#process" do
|
26
|
+
it { should respond_to :process }
|
27
|
+
|
28
|
+
describe "call stack" do
|
29
|
+
it "should create a new PDFKit object" do
|
30
|
+
pdf = mock
|
31
|
+
pdf.stubs(:to_pdf).returns(true)
|
32
|
+
PDFKit.expects(:new).with(html_h1, page_size: 'Letter').returns(pdf).once
|
33
|
+
|
34
|
+
subject.process
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should call to_pdf on the created PDFKit object" do
|
38
|
+
pdf = mock
|
39
|
+
pdf.expects(:to_pdf).returns(true).once
|
40
|
+
PDFKit.expects(:new).with(html_h1, page_size: 'Letter').returns(pdf).once
|
41
|
+
|
42
|
+
subject.process
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|