tartancloth 0.0.1 → 0.0.2

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.
@@ -0,0 +1,110 @@
1
+ require 'diff/lcs'
2
+ require 'diff/lcs/callbacks'
3
+ require 'pathname'
4
+
5
+ # Borrowed most of this from the BlueCloth gem at
6
+ # https://github.com/ged/bluecloth/blob/master/spec/lib/matchers.rb
7
+
8
+
9
+ module TartanCloth::Matchers
10
+
11
+ class TransformMatcher
12
+
13
+ def initialize( html )
14
+ @html = html
15
+ end
16
+
17
+ ### Strip tab indentation from the expected HTML output.
18
+ def without_indentation
19
+ if indent = @html[/\A\s+/]
20
+ indent.gsub!( /\A\n/m, '' )
21
+ @html.gsub!( /^#{indent}/m, '' )
22
+ end
23
+
24
+ return self
25
+ end
26
+
27
+ ### Returns true if the HTML generated by the given +tartancloth+ object matches the
28
+ ### expected HTML, comparing only the salient document structures.
29
+ def matches?( tartancloth )
30
+ @tartancloth = tartancloth
31
+ @output_html = tartancloth.body_html.gsub( /\n\n\n/, "\n\n" )
32
+ return @output_html.strip == @html.strip
33
+ end
34
+
35
+ def failure_message_for_should
36
+ patch = self.make_patch( @html, @output_html )
37
+ return ("Expected the generated html:\n\n %p\n\nto be the same as:\n\n" +
38
+ " %p\n\nDiffs:\n\n%s") % [ @output_html, @html, patch ]
39
+ end
40
+
41
+ def failure_message_for_should_not
42
+ return "Expected the generated html:\n\n %p\n\nnot to be the same as:\n\n %p\n\n" %
43
+ [ @output_html, @html ]
44
+ end
45
+
46
+ ### Compute a patch between the given +expected+ output and the +actual+ output
47
+ ### and return it as a string.
48
+ def make_patch( expected, actual )
49
+ diffs = Diff::LCS.sdiff( expected.split("\n"), actual.split("\n"),
50
+ Diff::LCS::ContextDiffCallbacks )
51
+
52
+ maxcol = diffs.flatten.
53
+ collect {|d| [d.old_element.to_s.length, d.new_element.to_s.length ] }.
54
+ flatten.max || 0
55
+ maxcol += 4
56
+
57
+ patch = " %#{maxcol}s | %s\n" % [ "Expected", "Actual" ]
58
+ patch << diffs.collect do |changeset|
59
+ changeset.collect do |change|
60
+ "%s [%03d, %03d]: %#{maxcol}s | %-#{maxcol}s" % [
61
+ change.action,
62
+ change.old_position,
63
+ change.new_position,
64
+ change.old_element.inspect,
65
+ change.new_element.inspect,
66
+ ]
67
+ end.join("\n")
68
+ end.join("\n---\n")
69
+ end
70
+
71
+ end # class TransformMatcher
72
+
73
+
74
+ ### Write markup to temp file
75
+ def write_tmp_file( string )
76
+ tmpfile = Pathname('tmp')
77
+ tmpfile.mkpath
78
+ tmpfile += 'markdown.md'
79
+ File.open( tmpfile.to_s, 'w') do |f|
80
+ f << string
81
+ end
82
+ tmpfile.to_s
83
+ end
84
+
85
+ ### Create a new TartanCloth object out of the given +string+ and +options+ and
86
+ ### return it.
87
+ def the_markdown( string )
88
+ return TartanCloth.new( write_tmp_file(string) )
89
+ end
90
+
91
+
92
+ ### Strip indentation from the given +string+, create a new TartanCloth object
93
+ ### out of the result and any +options+, and return it.
94
+ def the_indented_markdown( string )
95
+ if indent = string[/\A\s+/]
96
+ indent.gsub!( /\A\n/m, '' )
97
+ $stderr.puts "Source indent is: %p" % [ indent ] if $DEBUG
98
+ string.gsub!( /^#{indent}/m, '' )
99
+ end
100
+
101
+ return TartanCloth.new( write_tmp_file(string) )
102
+ end
103
+
104
+
105
+ ### Generate a matcher that expects to equal the given +html+.
106
+ def be_transformed_into( html )
107
+ return TartanCloth::Matchers::TransformMatcher.new( html )
108
+ end
109
+
110
+ end # module
@@ -0,0 +1,24 @@
1
+ require_relative '../lib/tartancloth'
2
+ require_relative './lib/matchers'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # Require this file using `require "spec_helper"` to ensure that it is only
7
+ # loaded once.
8
+ #
9
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+
21
+ # Include custom matchers
22
+ config.include(TartanCloth::Matchers)
23
+ end
24
+
data/spec/toc_spec.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe "TOC" do
4
+
5
+ context "headers" do
6
+
7
+ it "identical headers have unique anchors" do
8
+ the_indented_markdown( <<-"---" ).should be_transformed_into(<<-"---").without_indentation
9
+ # TOC
10
+
11
+ # Header 1
12
+
13
+ # Header 1
14
+
15
+ # Header 1
16
+ ---
17
+ <a name="TOC"></a>
18
+ <h2>Table of Contents</h2>
19
+
20
+ <ul class="toc">
21
+ <li><span class="h1toc"><a href="#Header.1">Header 1</a></span></li>
22
+ <li><span class="h1toc"><a href="#Header.1.0">Header 1</a></span></li>
23
+ <li><span class="h1toc"><a href="#Header.1.1">Header 1</a></span></li>
24
+ </ul>
25
+
26
+ <a name="Header.1"></a>
27
+ <h1>Header 1</h1>
28
+
29
+ <a name="Header.1.0"></a>
30
+ <h1>Header 1</h1>
31
+
32
+ <a name="Header.1.1"></a>
33
+ <h1>Header 1</h1>
34
+ ---
35
+ end
36
+ end # context "headers"
37
+ end # describe "TOC"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tartancloth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-21 00:00:00.000000000 Z
12
+ date: 2013-04-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -96,11 +96,16 @@ extensions: []
96
96
  extra_rdoc_files: []
97
97
  files:
98
98
  - .gitignore
99
+ - .rspec
100
+ - CHANGELOG.txt
99
101
  - Gemfile
100
102
  - LICENSE.txt
101
103
  - README.md
102
104
  - Rakefile
103
105
  - lib/tartancloth.rb
106
+ - spec/lib/matchers.rb
107
+ - spec/spec_helper.rb
108
+ - spec/toc_spec.rb
104
109
  - tartancloth.gemspec
105
110
  homepage: ''
106
111
  licenses:
@@ -127,5 +132,8 @@ rubygems_version: 1.8.24
127
132
  signing_key:
128
133
  specification_version: 3
129
134
  summary: Generate nice HTML with a table of contents
130
- test_files: []
135
+ test_files:
136
+ - spec/lib/matchers.rb
137
+ - spec/spec_helper.rb
138
+ - spec/toc_spec.rb
131
139
  has_rdoc: