smarky 0.0.3 → 0.0.4

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: fa9f31c6225a86adf76e593a27e76edabcd516ee
4
- data.tar.gz: e99740f11285229385a36a7e9f9a096e9ab68865
3
+ metadata.gz: d05d56103e40567d3fcce54bcb24aed503602374
4
+ data.tar.gz: 06b7fc4c4085fedba1c436c21a6101a0bc915282
5
5
  SHA512:
6
- metadata.gz: a21a8033ed7410766a7c1b7e10f3b53a5548a71cdc9b7514a770c4918332fc15011ebe093edbb9d5f8ab479ae6bc9348e53c17aeb09f555ff01977454d6fc7d0
7
- data.tar.gz: e290a87f88a69bd672aaac30968053d55f2819896ac31bad146b5004545d4ac70e8a91022a37ee6c0f85d9b2d05e3d049159a65d72e2633d2bc93b2c8f4948be
6
+ metadata.gz: 1a921f9d2d5677922e4d519641c185843570c2829e422316facf6233143727e3fe9d70a6ba936a4d3c97c201a1b78b2675b26544a74c44c830cc203075de6cac
7
+ data.tar.gz: a8786f43275b7c9d91a1ff72e5467e4b66c4d91373fd4e52c1f40096d09475809bf2cc88680f17652f9d865706bd97a415a0f2ffa1c1201d1b82d36837fa5a87
data/README.md CHANGED
@@ -59,6 +59,9 @@ article.sections
59
59
  article.to_html
60
60
  # => the structured HTML
61
61
 
62
+ article.to_html(:omit_titles => true)
63
+ # => if you want to exclude the <h1>, <h2>, etc. tags from the HTML output
64
+
62
65
  article.inner_html
63
66
  # => if you want to inject the HTML into an existing container
64
67
  ```
data/bin/smarky CHANGED
@@ -14,8 +14,5 @@ output_file = ARGV[1] || File.basename(input_file, '.*') + '.html'
14
14
  document = Smarky.parse(File.read(input_file))
15
15
 
16
16
  File.open(output_file, 'w') do |f|
17
- # Hacky way of getting nicely indented HTML:
18
- # http://stackoverflow.com/questions/1898829/how-do-i-pretty-print-html-with-nokogiri
19
- # This might be a huge mistake.
20
- f.write(Nokogiri::XML(document.to_html, &:noblanks).to_xhtml(:indent => 4))
17
+ document.to_html
21
18
  end
@@ -69,8 +69,20 @@ module Smarky
69
69
  add_child(Element.new('section'))
70
70
  end
71
71
 
72
- def to_html
73
- @node.to_html
72
+ def to_html(options={})
73
+ node = @node
74
+
75
+ if options[:omit_titles]
76
+ node = node.clone
77
+ node.css('h1,h2,h3,h4,h5,h6').each do |child|
78
+ child.remove()
79
+ end
80
+ end
81
+
82
+ # Getting nicely indented HTML:
83
+ # http://stackoverflow.com/questions/1898829/how-do-i-pretty-print-html-with-nokogiri
84
+ # This might be a huge mistake?
85
+ node.to_xhtml(:indent => 2, :indent_text => ' ')
74
86
  end
75
87
 
76
88
  def inner_html
@@ -1,3 +1,3 @@
1
1
  module Smarky
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -65,4 +65,24 @@ describe Smarky::Element do
65
65
  result.title.should be_nil
66
66
  end
67
67
  end
68
+
69
+ describe 'to_html' do
70
+ it 'can omit section titles if desired' do
71
+ input <<-EOMARKDOWN
72
+ This is a title
73
+ ===============
74
+
75
+ Content content content.
76
+ EOMARKDOWN
77
+
78
+ # TODO: This still sucks :(
79
+ verify_result(:html_options => { :omit_titles => true }) do
80
+ <<-EOHTML
81
+ <article id="this-is-a-title">
82
+
83
+ <p>Content content content.</p></article>
84
+ EOHTML
85
+ end
86
+ end
87
+ end
68
88
  end
@@ -1,22 +1,9 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
3
  describe Smarky do
4
- let(:result) { Smarky.parse(@input, @options || {}) }
4
+ let(:result) { Smarky.parse(@input, @parse_options || {}) }
5
5
 
6
6
  describe 'parse' do
7
- def verify_result(*args)
8
- expected, @options = [args.pop, args.pop]
9
-
10
- case expected
11
- when Array
12
- node_to_array(result)[1..-1].should == expected
13
- when String
14
- result.to_html.should == expected
15
- else
16
- raise 'Unable to verify against a #{expected.class}.'
17
- end
18
- end
19
-
20
7
  it 'returns a Smarky::Element wrapper around an <article>' do
21
8
  result.should be_a(Smarky::Element)
22
9
  result.name.should == 'article'
@@ -24,7 +11,12 @@ describe Smarky do
24
11
 
25
12
  it 'works in the simplest case: rendering a single paragraph' do
26
13
  input 'Why *hello* there.'
27
- verify_result '<article><p>Why <em>hello</em> there.</p></article>'
14
+
15
+ verify_result <<-EOHTML
16
+ <article>
17
+ <p>Why <em>hello</em> there.</p>
18
+ </article>
19
+ EOHTML
28
20
  end
29
21
 
30
22
  it 'uses RedCarpet for rendering Markdown by default' do
@@ -32,7 +24,13 @@ describe Smarky do
32
24
  This should look weird.
33
25
  {: .weird }
34
26
  EOMARKDOWN
35
- verify_result "<article><p>This should look weird.\n{: .weird }</p></article>"
27
+
28
+ verify_result <<-EOHTML
29
+ <article>
30
+ <p>This should look weird.
31
+ {: .weird }</p>
32
+ </article>
33
+ EOHTML
36
34
  end
37
35
 
38
36
  it 'can use Maruku, if specified' do
@@ -40,7 +38,14 @@ describe Smarky do
40
38
  This should look normal.
41
39
  {: .normal }
42
40
  EOMARKDOWN
43
- verify_result({ :markdown_renderer => :maruku }, '<article><p class="normal">This should look normal.</p></article>')
41
+
42
+ verify_result(:parse_options => { :markdown_renderer => :maruku }) do
43
+ <<-EOHTML
44
+ <article>
45
+ <p class="normal">This should look normal.</p>
46
+ </article>
47
+ EOHTML
48
+ end
44
49
  end
45
50
 
46
51
  it 'can also use Kramdown' do
@@ -49,7 +54,14 @@ describe Smarky do
49
54
  This should also look normal.
50
55
  {: .normal }
51
56
  EOMARKDOWN
52
- verify_result({ :markdown_renderer => :kramdown }, '<article><p class="normal">This should also look normal.</p></article>')
57
+
58
+ verify_result(:parse_options => { :markdown_renderer => :kramdown }) do
59
+ <<-EOHTML
60
+ <article>
61
+ <p class="normal">This should also look normal.</p>
62
+ </article>
63
+ EOHTML
64
+ end
53
65
  end
54
66
 
55
67
  it 'renders sibling sections for progressive heading elements' do
@@ -65,7 +77,7 @@ describe Smarky do
65
77
  This is section 2.
66
78
  EOMARKDOWN
67
79
 
68
- verify_result [
80
+ verify_result([
69
81
  [:section,
70
82
  [:h1, 'Section 1'],
71
83
  [:p, 'This is section 1.']
@@ -74,7 +86,7 @@ describe Smarky do
74
86
  [:h1, 'Section 2'],
75
87
  [:p, 'This is section 2.']
76
88
  ]
77
- ]
89
+ ])
78
90
  end
79
91
 
80
92
  it 'attaches IDs to HTML sections' do
@@ -86,11 +98,16 @@ describe Smarky do
86
98
  =========
87
99
  EOMARKDOWN
88
100
 
89
- # TODO: This is fragile :( Make it not!
90
- verify_result '<article>' +
91
- '<section id="section-1"><h1>Section 1</h1>' + "\n\n" + '</section>' +
92
- '<section id="section-2"><h1>Section 2</h1></section>' +
93
- '</article>'
101
+ verify_result(:array_options => { :include_attributes => true }) do
102
+ [
103
+ [:section, { :id => 'section-1' },
104
+ [:h1, 'Section 1'],
105
+ ],
106
+ [:section, { :id => 'section-2' },
107
+ [:h1, 'Section 2']
108
+ ]
109
+ ]
110
+ end
94
111
  end
95
112
 
96
113
  it 'adds content directly to the root article if there is only one top-level section' do
@@ -7,12 +7,14 @@ def input(markdown)
7
7
  @input = markdown.unindent
8
8
  end
9
9
 
10
- def node_to_array(node)
10
+ def node_to_array(node, options={})
11
+ options ||= {}
12
+
11
13
  array = [node.name.to_sym]
12
14
 
13
- # if node.attributes.any?
14
- # array << attribute_hash(node.attributes)
15
- # end
15
+ if options[:include_attributes] && node.attributes.any?
16
+ array << attribute_hash(node.attributes)
17
+ end
16
18
 
17
19
  if node.children.empty?
18
20
  array << node.content
@@ -22,7 +24,7 @@ def node_to_array(node)
22
24
 
23
25
  else
24
26
  node.children.each do |node|
25
- array << node_to_array(node) unless node.name == 'text' && node.content =~ /^\s*$/
27
+ array << node_to_array(node, options) unless node.name == 'text' && node.content =~ /^\s*$/
26
28
  end
27
29
  end
28
30
 
@@ -40,4 +42,23 @@ RSpec.configure do |config|
40
42
  config.before :each do
41
43
  @input = ''
42
44
  end
45
+
46
+ def verify_result(*args)
47
+ # Allow expected HTML to be specified either as a last parameter or via a block.
48
+ expected, options = block_given? && [yield, args.pop] || [args.pop, args.pop]
49
+
50
+ options ||= {}
51
+ @parse_options = options[:parse_options]
52
+ array_options = options[:array_options]
53
+ html_options = options[:html_options]
54
+
55
+ case expected
56
+ when Array
57
+ node_to_array(result, array_options)[1..-1].should == expected
58
+ when String
59
+ result.to_html(html_options || {}).should == expected.unindent.strip
60
+ else
61
+ raise "Unable to verify against a #{expected.class}."
62
+ end
63
+ end
43
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smarky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Tao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-03 00:00:00.000000000 Z
11
+ date: 2013-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri