bookingit 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +4 -0
- data/Gemfile.lock +7 -1
- data/README.rdoc +86 -37
- data/TODO.md +7 -0
- data/bin/bookingit +19 -58
- data/bookingit.gemspec +2 -0
- data/features/bookingit.feature +149 -16
- data/features/step_definitions/bookinggit_steps.rb +18 -0
- data/features/support/env.rb +5 -1
- data/lib/bookingit.rb +5 -0
- data/lib/bookingit/book.rb +94 -0
- data/lib/bookingit/code_block_interpreter.rb +114 -0
- data/lib/bookingit/config.rb +62 -28
- data/lib/bookingit/errors.rb +16 -0
- data/lib/bookingit/renderer.rb +173 -39
- data/lib/bookingit/shell_command.rb +30 -0
- data/lib/bookingit/version.rb +1 -1
- data/lib/bookingit/views.rb +8 -0
- data/lib/bookingit/views/base_view.rb +12 -0
- data/lib/bookingit/views/code_view.rb +16 -0
- data/lib/bookingit/views/footer_view.rb +14 -0
- data/lib/bookingit/views/header_view.rb +17 -0
- data/lib/bookingit/views/index_view.rb +20 -0
- data/templates/block_code.html.mustache +1 -0
- data/templates/footer.html.mustache +37 -0
- data/templates/header.html.mustache +16 -0
- data/templates/index.html.mustache +27 -0
- data/templates/stylesheets.html.mustache +3 -0
- data/templates/syntax_highlighting.html.mustache +3 -0
- data/test/book_test.rb +20 -0
- data/test/config_test.rb +43 -35
- data/test/renderer_test.rb +173 -23
- data/test/test_helper.rb +2 -0
- metadata +50 -2
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
module Bookingit
|
5
|
+
class ShellCommand
|
6
|
+
include FileUtils
|
7
|
+
|
8
|
+
attr_reader :stdout, :stderr, :exit_code, :command, :expected_exit_status
|
9
|
+
|
10
|
+
def initialize(command: nil,path: '.',expected_exit_status: 0, &block)
|
11
|
+
@command = command
|
12
|
+
@path = path
|
13
|
+
@exit_status_checker = if block.nil?
|
14
|
+
->(exit_code) { exit_code == expected_exit_status }
|
15
|
+
else
|
16
|
+
block
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def run!
|
21
|
+
chdir @path do
|
22
|
+
@stdout, @stderr, status = Open3.capture3(@command)
|
23
|
+
@exit_code = status.exitstatus
|
24
|
+
end
|
25
|
+
unless @exit_status_checker.(@exit_code)
|
26
|
+
raise UnexpectedShellCommandExit.new(@command,@stdout,@stderr)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/bookingit/version.rb
CHANGED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Bookingit
|
2
|
+
module Views
|
3
|
+
class BaseView < Mustache
|
4
|
+
attr_reader :config
|
5
|
+
self.template_path = File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','templates'))
|
6
|
+
|
7
|
+
def initialize(config)
|
8
|
+
@config = config.options
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Bookingit
|
2
|
+
module Views
|
3
|
+
class CodeView < BaseView
|
4
|
+
self.template_name = 'block_code.html'
|
5
|
+
|
6
|
+
attr_reader :code, :filename, :css_class
|
7
|
+
|
8
|
+
def initialize(code, filename, language, config)
|
9
|
+
@code = CGI.escapeHTML(code)
|
10
|
+
@filename = String(filename).strip == '' ? nil : filename.strip
|
11
|
+
@css_class = language ? "language-#{language}" : ""
|
12
|
+
super(config)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Bookingit
|
2
|
+
module Views
|
3
|
+
class HeaderView < BaseView
|
4
|
+
self.template_name = 'header.html'
|
5
|
+
|
6
|
+
attr_reader :stylesheets, :theme
|
7
|
+
|
8
|
+
def initialize(stylesheets, theme, config)
|
9
|
+
@stylesheets = stylesheets.map { |stylesheet|
|
10
|
+
OpenStruct.new(path: stylesheet, media: "all")
|
11
|
+
}
|
12
|
+
@theme = theme
|
13
|
+
super(config)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
module Bookingit
|
3
|
+
module Views
|
4
|
+
class IndexView < BaseView
|
5
|
+
extend Forwardable
|
6
|
+
self.template_name = 'index.html'
|
7
|
+
|
8
|
+
attr_reader :front_matter, :main_matter, :back_matter, :config
|
9
|
+
def_delegators :@header_view, :stylesheets, :theme
|
10
|
+
|
11
|
+
def initialize(stylesheets,theme,front_matter,main_matter,back_matter,config)
|
12
|
+
@header_view = HeaderView.new(stylesheets,theme,config)
|
13
|
+
@front_matter = front_matter
|
14
|
+
@main_matter = main_matter
|
15
|
+
@back_matter = back_matter
|
16
|
+
super(config)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<article class='code-listing'><pre><code class="{{css_class}}">{{{code}}}</code></pre>{{#filename}}<footer><h1>{{filename}}</h1></footer>{{/filename}}</article>
|
@@ -0,0 +1,37 @@
|
|
1
|
+
<footer>
|
2
|
+
{{#chapter}}
|
3
|
+
<ol>
|
4
|
+
{{#previous_chapter}}
|
5
|
+
<li class="previous"><a href="{{relative_url}}">←
|
6
|
+
{{#title}}
|
7
|
+
{{{title}}}
|
8
|
+
{{/title}}
|
9
|
+
{{^title}}
|
10
|
+
Previous
|
11
|
+
{{/title}}
|
12
|
+
</a></li>
|
13
|
+
{{/previous_chapter}}
|
14
|
+
{{#next_chapter}}
|
15
|
+
<li class="next"><a href="{{relative_url}}">
|
16
|
+
{{#title}}
|
17
|
+
{{{title}}}
|
18
|
+
{{/title}}
|
19
|
+
{{^title}}
|
20
|
+
Next
|
21
|
+
{{/title}}
|
22
|
+
→</a></li>
|
23
|
+
{{/next_chapter}}
|
24
|
+
</ol>
|
25
|
+
<div class="clearfix"></div>
|
26
|
+
{{/chapter}}
|
27
|
+
{{#config}}
|
28
|
+
{{#license}}
|
29
|
+
<p class="copyright">
|
30
|
+
<a rel="license" href="http://creativecommons.org/licenses/{{type}}/4.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/{{type}}/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">{{title}}</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="{{url}}" property="cc:attributionName" rel="cc:attributionURL">{{author}}</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/{{type}}/4.0/">{{typeDescription}}
|
31
|
+
</a>.
|
32
|
+
</p>
|
33
|
+
{{/license}}
|
34
|
+
{{/config}}
|
35
|
+
</footer>
|
36
|
+
</body>
|
37
|
+
</html>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
{{> stylesheets.html }}
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
|
6
|
+
<meta charset="utf-8">
|
7
|
+
<title>{{config.title}}: {{config.subtitle}}</title>
|
8
|
+
{{> syntax_highlighting.html }}
|
9
|
+
</head>
|
10
|
+
<body>
|
11
|
+
<h1><a href="index.html">{{config.title}}</a></h1>
|
12
|
+
{{#config}}
|
13
|
+
{{#author}}
|
14
|
+
<p class="byline">by {{config.author}}</p>
|
15
|
+
{{/author}}
|
16
|
+
{{/config}}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
{{> header.html}}
|
2
|
+
{{#config}}
|
3
|
+
<h2 class="subtitle">{{subtitle}}</h2>
|
4
|
+
{{/config}}
|
5
|
+
<ol class="front-matter">
|
6
|
+
{{#front_matter}}
|
7
|
+
<li class='chapter'><a href="{{relative_url}}">{{{title}}}</a></li>
|
8
|
+
{{/front_matter}}
|
9
|
+
</ol>
|
10
|
+
<ol class="main-matter">
|
11
|
+
{{#main_matter}}
|
12
|
+
<li class='chapter'>
|
13
|
+
<a href="{{relative_url}}">{{{title}}}</a>
|
14
|
+
<ol>
|
15
|
+
{{#sections}}
|
16
|
+
<li class='section'><a href="{{relative_url}}">{{{title}}}</a></li>
|
17
|
+
{{/sections}}
|
18
|
+
</ol>
|
19
|
+
</li class='chapter'>
|
20
|
+
{{/main_matter}}
|
21
|
+
</ol>
|
22
|
+
<ol class="back-matter">
|
23
|
+
{{#back_matter}}
|
24
|
+
<li class='chapter'><a href="{{relative_url}}">{{{title}}}</a></li>
|
25
|
+
{{/back_matter}}
|
26
|
+
</ol>
|
27
|
+
{{> footer.html}}
|
data/test/book_test.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
include FileUtils
|
5
|
+
|
6
|
+
class Bookingit::BookTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@tempdir = Dir.mktmpdir
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
remove_entry @tempdir
|
14
|
+
end
|
15
|
+
|
16
|
+
test_that "we blow up if our markdown doesn't have any H1's" do
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
end
|
data/test/config_test.rb
CHANGED
@@ -17,6 +17,13 @@ class Bookingit::ConfigTest < Test::Unit::TestCase
|
|
17
17
|
Given :some_markdown_files
|
18
18
|
And {
|
19
19
|
@config = {
|
20
|
+
title: "blah town",
|
21
|
+
subtitle: "the town of blah",
|
22
|
+
whatever: "foobar",
|
23
|
+
authors: [
|
24
|
+
"Shane Vendrel",
|
25
|
+
"Ronnie Gardocki",
|
26
|
+
],
|
20
27
|
front_matter: "foo.md",
|
21
28
|
main_matter: "bar.md",
|
22
29
|
back_matter: "baz.md",
|
@@ -26,13 +33,16 @@ class Bookingit::ConfigTest < Test::Unit::TestCase
|
|
26
33
|
@normalized_config = Bookingit::Config.new(@config.to_json,@tempdir)
|
27
34
|
}
|
28
35
|
Then {
|
36
|
+
assert_equal @config[:title], @normalized_config.options['title']
|
37
|
+
assert_equal @config[:subtitle], @normalized_config.options['subtitle']
|
38
|
+
assert_equal @config[:whatever], @normalized_config.options['whatever']
|
39
|
+
assert_equal @config[:authors], @normalized_config.options['authors']
|
29
40
|
assert_equal 1,@normalized_config.front_matter.chapters.size
|
30
|
-
assert_equal File.join(@tempdir,'foo.md'),@normalized_config.front_matter.chapters[0].
|
41
|
+
assert_equal File.join(@tempdir,'foo.md'),@normalized_config.front_matter.chapters[0].markdown_path
|
31
42
|
assert_equal 1,@normalized_config.main_matter.chapters.size
|
32
|
-
assert_equal File.join(@tempdir,'bar.md'),@normalized_config.main_matter.chapters[0].
|
33
|
-
assert_equal 0,@normalized_config.main_matter.chapters[0].sections.size
|
43
|
+
assert_equal File.join(@tempdir,'bar.md'),@normalized_config.main_matter.chapters[0].markdown_path
|
34
44
|
assert_equal 1,@normalized_config.back_matter.chapters.size
|
35
|
-
assert_equal File.join(@tempdir,'baz.md'),@normalized_config.back_matter.chapters[0].
|
45
|
+
assert_equal File.join(@tempdir,'baz.md'),@normalized_config.back_matter.chapters[0].markdown_path
|
36
46
|
}
|
37
47
|
end
|
38
48
|
|
@@ -50,57 +60,55 @@ class Bookingit::ConfigTest < Test::Unit::TestCase
|
|
50
60
|
}
|
51
61
|
Then {
|
52
62
|
assert_equal 1,@normalized_config.front_matter.chapters.size
|
53
|
-
assert_equal File.join(@tempdir,'foo.md'),@normalized_config.front_matter.chapters[0].
|
63
|
+
assert_equal File.join(@tempdir,'foo.md'),@normalized_config.front_matter.chapters[0].markdown_path
|
54
64
|
assert_equal 1,@normalized_config.main_matter.chapters.size
|
55
|
-
assert_equal File.join(@tempdir,'bar.md'),@normalized_config.main_matter.chapters[0].
|
56
|
-
assert_equal 0,@normalized_config.main_matter.chapters[0].sections.size
|
65
|
+
assert_equal File.join(@tempdir,'bar.md'),@normalized_config.main_matter.chapters[0].markdown_path
|
57
66
|
assert_equal 1,@normalized_config.back_matter.chapters.size
|
58
|
-
assert_equal File.join(@tempdir,'baz.md'),@normalized_config.back_matter.chapters[0].
|
67
|
+
assert_equal File.join(@tempdir,'baz.md'),@normalized_config.back_matter.chapters[0].markdown_path
|
59
68
|
}
|
60
69
|
end
|
61
70
|
|
62
|
-
test_that "
|
63
|
-
Given
|
64
|
-
And {
|
71
|
+
test_that "converts rendering config" do
|
72
|
+
Given {
|
65
73
|
@config = {
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
74
|
+
rendering: {
|
75
|
+
stylesheets: 'blah.css',
|
76
|
+
languages: {
|
77
|
+
".coffee" => "coffeescript",
|
78
|
+
"/^Bowerfile$/" => "ruby",
|
79
|
+
},
|
80
|
+
git_repos_basedir: "/tmp",
|
81
|
+
syntax_theme: "solarized",
|
82
|
+
}
|
72
83
|
}
|
73
84
|
}
|
74
85
|
When {
|
75
86
|
@normalized_config = Bookingit::Config.new(@config.to_json,@tempdir)
|
76
87
|
}
|
77
|
-
Then
|
78
|
-
assert_equal
|
79
|
-
assert_equal
|
88
|
+
Then {
|
89
|
+
assert_equal "/tmp",@normalized_config.rendering_config[:basedir]
|
90
|
+
assert_equal({ ".coffee" => "coffeescript", /^Bowerfile$/ => "ruby" },@normalized_config.rendering_config[:languages])
|
91
|
+
assert_equal ['blah.css'],@normalized_config.rendering_config[:stylesheets]
|
92
|
+
assert_equal 'solarized',@normalized_config.rendering_config[:theme]
|
93
|
+
refute @normalized_config.cache
|
80
94
|
}
|
81
95
|
end
|
82
96
|
|
83
|
-
test_that "
|
84
|
-
Given
|
85
|
-
And {
|
97
|
+
test_that "we can mutate the config" do
|
98
|
+
Given {
|
86
99
|
@config = {
|
87
|
-
|
88
|
-
|
89
|
-
"
|
90
|
-
|
91
|
-
],
|
92
|
-
back_matter: ["baz.md"],
|
100
|
+
rendering: {
|
101
|
+
stylesheets: 'blah.css',
|
102
|
+
syntax_theme: "solarized",
|
103
|
+
}
|
93
104
|
}
|
94
105
|
}
|
95
106
|
When {
|
96
107
|
@normalized_config = Bookingit::Config.new(@config.to_json,@tempdir)
|
108
|
+
@normalized_config.cache = true
|
97
109
|
}
|
98
|
-
Then
|
99
|
-
|
100
|
-
assert_equal 3,@normalized_config.main_matter.chapters[1].sections.size
|
101
|
-
assert_equal File.join(@tempdir,'blah1.md'),@normalized_config.main_matter.chapters[1].sections[0].path
|
102
|
-
assert_equal File.join(@tempdir,'blah2.md'),@normalized_config.main_matter.chapters[1].sections[1].path
|
103
|
-
assert_equal File.join(@tempdir,'blah3.md'),@normalized_config.main_matter.chapters[1].sections[2].path
|
110
|
+
Then {
|
111
|
+
assert @normalized_config.cache
|
104
112
|
}
|
105
113
|
end
|
106
114
|
|
data/test/renderer_test.rb
CHANGED
@@ -14,11 +14,40 @@ class RendererTest < Test::Unit::TestCase
|
|
14
14
|
remove_entry @tempdir
|
15
15
|
end
|
16
16
|
|
17
|
+
def renderer(options={})
|
18
|
+
options = OpenStruct.new(:rendering_config => { basedir: @tempdir }.merge(options))
|
19
|
+
Bookingit::Renderer.new(options)
|
20
|
+
end
|
21
|
+
|
17
22
|
test_that "block_code can read a file URL and guess ruby" do
|
18
23
|
Given a_file_with_extension(".rb")
|
19
24
|
When render_file_url_code_block
|
20
25
|
Then {
|
21
|
-
assert_equal %{<pre><code class="language-ruby">#{@code}</code></pre>},@html
|
26
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-ruby">#{@code}</code></pre><footer><h1>#{@relative_path}</h1></footer></article>},@html
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
test_that "block_code can read a file URL and guess ruby from Gemfile" do
|
31
|
+
Given a_file_named("Gemfile")
|
32
|
+
When render_file_url_code_block
|
33
|
+
Then {
|
34
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-ruby">#{@code}</code></pre><footer><h1>Gemfile</h1></footer></article>},@html
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
test_that "block_code can read a file URL and be OK if it cannot guess" do
|
39
|
+
Given a_file_with_extension(".blah")
|
40
|
+
When render_file_url_code_block
|
41
|
+
Then {
|
42
|
+
assert_equal %{<article class='code-listing'><pre><code class="">#{@code}</code></pre><footer><h1>#{@relative_path}</h1></footer></article>},@html
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
test_that "we can tell the renderer about other languages and extensions" do
|
47
|
+
Given a_file_with_extension(".blah")
|
48
|
+
When render_file_url_code_block(languages: { '.blah' => 'blahscript' })
|
49
|
+
Then {
|
50
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-blahscript">#{@code}</code></pre><footer><h1>#{@relative_path}</h1></footer></article>},@html
|
22
51
|
}
|
23
52
|
end
|
24
53
|
|
@@ -26,7 +55,7 @@ class RendererTest < Test::Unit::TestCase
|
|
26
55
|
Given a_file_with_extension(".scala")
|
27
56
|
When render_file_url_code_block
|
28
57
|
Then {
|
29
|
-
assert_equal %{<pre><code class="language-scala">#{@code}</code></pre>},@html
|
58
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-scala">#{@code}</code></pre><footer><h1>#{@relative_path}</h1></footer></article>},@html
|
30
59
|
}
|
31
60
|
end
|
32
61
|
|
@@ -41,14 +70,14 @@ class RendererTest < Test::Unit::TestCase
|
|
41
70
|
})
|
42
71
|
When render_file_url_code_block
|
43
72
|
Then {
|
44
|
-
assert_equal %{<pre><code class="language-html"><!DOCTYPE html>
|
73
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-html"><!DOCTYPE html>
|
45
74
|
<html>
|
46
75
|
<body>
|
47
76
|
<h1>HELLO!</h1>
|
48
77
|
<h2>&amp; Goodbye</h2>
|
49
78
|
</body>
|
50
79
|
</html>
|
51
|
-
</code></pre>},@html
|
80
|
+
</code></pre><footer><h1>#{@relative_path}</h1></footer></article>},@html
|
52
81
|
}
|
53
82
|
end
|
54
83
|
|
@@ -56,7 +85,7 @@ class RendererTest < Test::Unit::TestCase
|
|
56
85
|
Given a_git_repo_with_file("foo.rb")
|
57
86
|
When {
|
58
87
|
@code = -> {
|
59
|
-
|
88
|
+
renderer.block_code(@file_git_url,nil)
|
60
89
|
}
|
61
90
|
}
|
62
91
|
Then {
|
@@ -68,12 +97,12 @@ class RendererTest < Test::Unit::TestCase
|
|
68
97
|
Given a_git_repo_with_two_verions_of_file("foo.rb")
|
69
98
|
When {
|
70
99
|
@parsed_versions = Hash[@versions.map { |sha1,_|
|
71
|
-
[sha1,
|
100
|
+
[sha1, renderer.block_code(@file_git_url + "##{sha1}",nil)]
|
72
101
|
}]
|
73
102
|
}
|
74
103
|
Then {
|
75
104
|
@parsed_versions.each do |sha1,code|
|
76
|
-
assert_equal %{<pre><code class="language-ruby">#{@versions[sha1]}\n</code></pre>},code,"For SHA: #{sha1}"
|
105
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-ruby">#{@versions[sha1]}\n</code></pre><footer><h1>foo.rb</h1></footer></article>},code,"For SHA: #{sha1}"
|
77
106
|
end
|
78
107
|
}
|
79
108
|
end
|
@@ -82,12 +111,12 @@ class RendererTest < Test::Unit::TestCase
|
|
82
111
|
Given a_git_repo_with_two_tagged_verions_of_file("foo.rb")
|
83
112
|
When {
|
84
113
|
@parsed_versions = Hash[@versions.map { |tagname,_|
|
85
|
-
[tagname,
|
114
|
+
[tagname, renderer.block_code(@file_git_url + "##{tagname}",nil)]
|
86
115
|
}]
|
87
116
|
}
|
88
117
|
Then {
|
89
118
|
@parsed_versions.each do |tagname,code|
|
90
|
-
assert_equal %{<pre><code class="language-ruby">#{@versions[tagname]}\n</code></pre>},code,"For Tag: #{tagname}"
|
119
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-ruby">#{@versions[tagname]}\n</code></pre><footer><h1>foo.rb</h1></footer></article>},code,"For Tag: #{tagname}"
|
91
120
|
end
|
92
121
|
}
|
93
122
|
end
|
@@ -96,10 +125,25 @@ class RendererTest < Test::Unit::TestCase
|
|
96
125
|
Given a_git_repo_with_two_tagged_verions_of_file("foo.rb")
|
97
126
|
When {
|
98
127
|
url = @file_git_url + "#" + @versions.keys[0] + ".." + @versions.keys[1]
|
99
|
-
@html =
|
128
|
+
@html = renderer.block_code(url,nil)
|
129
|
+
}
|
130
|
+
Then {
|
131
|
+
assert_match /<article class=\'code-listing\'><pre><code class=\"language-diff\">diff --git/,@html
|
132
|
+
assert_match /a\/foo.rb b\/foo.rb/,@html
|
133
|
+
assert_match /index [a-z0-9]+..[a-z0-9]+ 100644/,@html
|
134
|
+
assert_match /\-\-\- a\/foo.rb/,@html
|
135
|
+
assert_match /\+\+\+ b\/foo.rb/,@html
|
136
|
+
}
|
137
|
+
end
|
138
|
+
|
139
|
+
test_that "a git url with a compact diff spec shows the diff, too" do
|
140
|
+
Given a_git_repo_with_two_tagged_verions_of_file("foo.rb")
|
141
|
+
When {
|
142
|
+
url = @file_git_url + "#.." + @versions.keys[1]
|
143
|
+
@html = renderer.block_code(url,nil)
|
100
144
|
}
|
101
145
|
Then {
|
102
|
-
assert_match /<pre><code class=\"language-diff\">diff --git/,@html
|
146
|
+
assert_match /<article class=\'code-listing\'><pre><code class=\"language-diff\">diff --git/,@html
|
103
147
|
assert_match /a\/foo.rb b\/foo.rb/,@html
|
104
148
|
assert_match /index [a-z0-9]+..[a-z0-9]+ 100644/,@html
|
105
149
|
assert_match /\-\-\- a\/foo.rb/,@html
|
@@ -107,6 +151,44 @@ class RendererTest < Test::Unit::TestCase
|
|
107
151
|
}
|
108
152
|
end
|
109
153
|
|
154
|
+
test_that "a git url with a shell command runs that command on that version of the repo" do
|
155
|
+
Given a_git_repo_with_two_tagged_verions_of_file("foo.rb")
|
156
|
+
When {
|
157
|
+
@version = @versions.keys[0]
|
158
|
+
git_url = @file_git_url.gsub(/\/foo.rb/,'/')
|
159
|
+
@html = renderer.block_code("#{git_url}##{@version}!cat foo.rb",nil)
|
160
|
+
}
|
161
|
+
Then {
|
162
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-shell">> cat foo.rb\n#{@versions[@version]}\n</code></pre></article>},@html
|
163
|
+
}
|
164
|
+
end
|
165
|
+
|
166
|
+
test_that "a git url with a shell command that exits nonzero raises an error" do
|
167
|
+
Given a_git_repo_with_two_tagged_verions_of_file("foo.rb")
|
168
|
+
When {
|
169
|
+
@version = @versions.keys[0]
|
170
|
+
git_url = @file_git_url.gsub(/\/foo.rb/,'/')
|
171
|
+
@executable = -> {
|
172
|
+
renderer.block_code("#{git_url}##{@version}!cat blah.rb",nil)
|
173
|
+
}
|
174
|
+
}
|
175
|
+
Then {
|
176
|
+
assert_raise Bookingit::UnexpectedShellCommandExit,&@executable
|
177
|
+
}
|
178
|
+
end
|
179
|
+
|
180
|
+
test_that "a git url with a shell command that exits nonzero doesn't raise an error if we indicate as such" do
|
181
|
+
Given a_git_repo_with_two_tagged_verions_of_file("foo.rb")
|
182
|
+
When {
|
183
|
+
@version = @versions.keys[0]
|
184
|
+
git_url = @file_git_url.gsub(/\/foo.rb/,'/')
|
185
|
+
@html = renderer.block_code("#{git_url}##{@version}!cat blah.rb!nonzero",nil)
|
186
|
+
}
|
187
|
+
Then {
|
188
|
+
refute_nil @html
|
189
|
+
}
|
190
|
+
end
|
191
|
+
|
110
192
|
test_that "an sh url will run the given command and put the contents into the output" do
|
111
193
|
Given {
|
112
194
|
chdir @tempdir do
|
@@ -119,32 +201,88 @@ class RendererTest < Test::Unit::TestCase
|
|
119
201
|
end
|
120
202
|
}
|
121
203
|
When {
|
122
|
-
@html =
|
204
|
+
@html = renderer.block_code("sh://play#ls -1",nil)
|
123
205
|
}
|
124
206
|
Then {
|
125
|
-
assert_equal %{<pre><code class="language-shell">> ls -1
|
207
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-shell">> ls -1
|
126
208
|
bar
|
127
209
|
blah
|
128
210
|
quux
|
129
|
-
</code></pre>},@html
|
211
|
+
</code></pre></article>},@html
|
212
|
+
}
|
213
|
+
end
|
214
|
+
|
215
|
+
test_that "an sh url that exits nonzero will raise" do
|
216
|
+
Given {
|
217
|
+
chdir @tempdir do
|
218
|
+
mkdir "play"
|
219
|
+
chdir "play" do
|
220
|
+
system "touch blah"
|
221
|
+
system "touch bar"
|
222
|
+
system "touch quux"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
}
|
226
|
+
When {
|
227
|
+
@executable = -> { renderer.block_code("sh://play#cat bleorgh",nil) }
|
228
|
+
}
|
229
|
+
Then {
|
230
|
+
assert_raise Bookingit::UnexpectedShellCommandExit, &@executable
|
231
|
+
}
|
232
|
+
end
|
233
|
+
|
234
|
+
test_that "an sh url that exits nonzero but we expect it to will not raise" do
|
235
|
+
Given {
|
236
|
+
chdir @tempdir do
|
237
|
+
mkdir "play"
|
238
|
+
chdir "play" do
|
239
|
+
system "touch blah"
|
240
|
+
system "touch bar"
|
241
|
+
system "touch quux"
|
242
|
+
end
|
243
|
+
end
|
244
|
+
}
|
245
|
+
When {
|
246
|
+
@html = renderer.block_code("sh://play#cat bleorgh!nonzero",nil)
|
247
|
+
}
|
248
|
+
Then {
|
249
|
+
refute_nil @html
|
130
250
|
}
|
131
251
|
end
|
132
252
|
|
133
253
|
test_that "we pass through inline code blocks" do
|
134
254
|
When {
|
135
|
-
@html =
|
255
|
+
@html = renderer.block_code("class Foo", 'coffeescript')
|
136
256
|
}
|
137
257
|
Then {
|
138
|
-
assert_equal %{<pre><code class="language-coffeescript">class Foo</code></pre>},@html
|
258
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-coffeescript">class Foo</code></pre></article>},@html
|
139
259
|
}
|
140
260
|
end
|
141
261
|
|
142
262
|
test_that "we omit the language class if it's not provided in inline code" do
|
143
263
|
When {
|
144
|
-
@html =
|
264
|
+
@html = renderer.block_code("class Foo", nil)
|
265
|
+
}
|
266
|
+
Then {
|
267
|
+
assert_equal %{<article class='code-listing'><pre><code class="">class Foo</code></pre></article>},@html
|
268
|
+
}
|
269
|
+
end
|
270
|
+
|
271
|
+
test_that "we can cache results between calls" do
|
272
|
+
Given a_git_repo_with_two_tagged_verions_of_file("foo.rb")
|
273
|
+
And {
|
274
|
+
@cachedir = File.join(@tempdir,'cache')
|
275
|
+
@renderer = renderer(cache: @cachedir)
|
276
|
+
@version = @versions.keys[0]
|
277
|
+
@url = "#{@file_git_url.gsub(/\/foo.rb/,'/')}##{@version}!cat foo.rb"
|
278
|
+
@html = @renderer.block_code(@url,nil)
|
279
|
+
}
|
280
|
+
When {
|
281
|
+
@html2 = @renderer.block_code(@url,nil)
|
145
282
|
}
|
146
283
|
Then {
|
147
|
-
assert_equal %{<pre><code
|
284
|
+
assert_equal %{<article class='code-listing'><pre><code class="language-shell">> cat foo.rb\n#{@versions[@version]}\n</code></pre></article>},@html
|
285
|
+
assert_equal @html,@html2
|
148
286
|
}
|
149
287
|
end
|
150
288
|
|
@@ -179,7 +317,7 @@ end}
|
|
179
317
|
create_and_commit_file(file,code)
|
180
318
|
# make sure test doesn't get the HEAD version
|
181
319
|
end
|
182
|
-
@file_git_url = "git
|
320
|
+
@file_git_url = "git://git_repo.git/#{file}"
|
183
321
|
}
|
184
322
|
end
|
185
323
|
|
@@ -207,7 +345,7 @@ end}
|
|
207
345
|
create_and_commit_file(file,code)
|
208
346
|
# make sure test doesn't get the HEAD version
|
209
347
|
end
|
210
|
-
@file_git_url = "git
|
348
|
+
@file_git_url = "git://git_repo.git/#{file}"
|
211
349
|
}
|
212
350
|
end
|
213
351
|
|
@@ -232,13 +370,24 @@ end}
|
|
232
370
|
end}
|
233
371
|
create_and_commit_file(file,@code)
|
234
372
|
end
|
235
|
-
@file_git_url = "git
|
373
|
+
@file_git_url = "git://git_repo.git/#{file}"
|
374
|
+
}
|
375
|
+
end
|
376
|
+
|
377
|
+
def render_file_url_code_block(options={})
|
378
|
+
-> {
|
379
|
+
@html = renderer(options).block_code("file://#{@path.gsub(/#{Regexp.escape(@tempdir)}\/?/,'')}",nil)
|
236
380
|
}
|
237
381
|
end
|
238
382
|
|
239
|
-
def
|
383
|
+
def a_file_named(filename)
|
240
384
|
-> {
|
241
|
-
@
|
385
|
+
@code = %{#{any_string}
|
386
|
+
}
|
387
|
+
@path = File.join(@tempdir,filename)
|
388
|
+
File.open(@path,'w') do |file|
|
389
|
+
file.puts @code
|
390
|
+
end
|
242
391
|
}
|
243
392
|
end
|
244
393
|
|
@@ -246,6 +395,7 @@ end}
|
|
246
395
|
-> {
|
247
396
|
file = Tempfile.new(['foo',extension])
|
248
397
|
@path = file.path
|
398
|
+
@relative_path = @path.gsub(Regexp.escape(@tempdir),'')
|
249
399
|
@code = contents
|
250
400
|
File.open(@path,'w') do |file|
|
251
401
|
file.puts @code
|