hemingway 0.0.3 → 1.0.0

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: 2a574830a78bd7ac89da2dcd37f90b1515c265bd
4
- data.tar.gz: c27b6b9773bc3768aac752831fa25190d5b2895e
3
+ metadata.gz: 6be07dbe35ddfe1b586683f1c0b22794e7224eb6
4
+ data.tar.gz: f2289732a6e1250a13d475a4301caa3ff575c397
5
5
  SHA512:
6
- metadata.gz: 8815d0ebdd41a282ed8f1003a05e0748a8fa4112d8ccd59f0b8c0da040d04e374f09f5ae26002501520aa8eeb3548eb18f50b753e170960725c8f0b9c5255fd9
7
- data.tar.gz: 3305dd31396f382c5304d407d6b43bfac01224bb5f05f4bf55e4d5bcd7d2fc5e027fa1bdb7f71c06fd4824030c52ddbab21426631c9f29a30f8ab95bb1d46c7a
6
+ metadata.gz: 9c39102a201abc6ae4a602b91a0cbdbcebfb8c53d2022912688af99a9f973ebc129f12be862392acf1ab1d9a8d801fa17ea8b8ca879c7009e5088b833928b371
7
+ data.tar.gz: bf781c65504fbf222fcd6a9cb041a22645625818139cbe4cd2f835389215a6bfb2b4fc8f6849cf2df48ac80ef0a08e0843e67c84d61c22653f63f65a6aef0a34
data/README.md CHANGED
@@ -253,6 +253,9 @@ And looks like this:
253
253
 
254
254
  </div>
255
255
 
256
+ I've added a timestamp onto footnote hrefs and ids to make the unique incase
257
+ you are parsing multiple entries on a given HTML document.
258
+
256
259
  ###### Block Quotes
257
260
 
258
261
  Pretty standard. You can put anything in there except a footnote. That seemed
@@ -304,6 +307,16 @@ The love, <span class="pull-right">Will</span>
304
307
  Charm\'{e}e de vous voir. Je suis tr\`{e} contente de vous voir. : Charm&eacute;e de vous voir. Je suis tr&egrave; contente de vous voir.
305
308
  ```
306
309
 
310
+ ###### Overrides
311
+
312
+ Some characters need a little help.
313
+
314
+ ```html
315
+ --- : "&mdash;"
316
+ `` : "&#8220;"
317
+ '' : "&#8221;"
318
+ ```
319
+
307
320
  ## Contributing
308
321
 
309
322
  1. Fork it
@@ -1,4 +1,4 @@
1
- module Hemingway
1
+ module Hemingway
2
2
 
3
3
  module ItemizeListNode
4
4
  def html(block_content)
@@ -26,7 +26,7 @@ module Hemingway
26
26
 
27
27
  def description_html
28
28
  label_tag = label.empty? ? "" : Build.tag("dt", label.html)
29
- Build.tag("dd", label_tag + sequence.elements.map { |e| e.html }.join)
29
+ label_tag + Build.tag("dd", sequence.elements.map { |e| e.html }.join)
30
30
  end
31
31
  end
32
32
 
@@ -1,21 +1,31 @@
1
1
  module Hemingway
2
2
  module FootnoteNode
3
- # This is the method that will place the anchor tag and id of the
4
- # footnote within the paragraph body itself.
5
- def html(id)
3
+ # This is the method that will place the anchor tag and id of the
4
+ # footnote within the paragraph body itself.
5
+ #
6
+ # I'm passing in a time variable here to make links unique. You see,
7
+ # if you parse many of these entries on a single HTML page you'll end up
8
+ # with multiple #footnote1 divs. To make them unique, we'll pass down
9
+ # a time variable from above to seed them.
10
+ def html(id, time)
6
11
  inline_footnote_label = Build.tag("span", Build.tag("sup", id.to_s), :class => "inline-footnote-number")
7
- Build.tag("a", inline_footnote_label, :href => "#footnote#{id}")
12
+ Build.tag("a", inline_footnote_label, :href => "#footnote#{id}#{time}")
8
13
  end
9
14
 
10
- # This is the method that will actually spit out the div that the
11
- # footnote's content is in. This will generally be called after all of the
15
+ # This is the method that will actually spit out the div that the
16
+ # footnote's content is in. This will generally be called after all of the
12
17
  # paragraph's text has been spit out so that the footnotes can be appended
13
- # after. Note that it needs to be passed an id from the caller so that it
14
- # can be linked to corretly with an anchor tag in the body of the main text.
15
- def footnote_html(id)
18
+ # after. Note that it needs to be passed an id from the caller so that it
19
+ # can be linked to corretly with an anchor tag in the body of the main text.
20
+ #
21
+ # I'm passing in a time variable here to make links unique. You see,
22
+ # if you parse many of these entries on a single HTML page you'll end up
23
+ # with multiple #footnote1 divs. To make them unique, we'll pass down
24
+ # a time variable from above to seed them.
25
+ def footnote_html(id, time)
16
26
  footnote_label = Build.tag("span", Build.tag("sup", id.to_s), :class => "footnote-number")
17
27
  footnote_content = sequence.elements.map { |s| s.html }.join
18
- Build.tag("div", footnote_label + footnote_content, :id => "footnote#{id}", :class => "footnote")
28
+ Build.tag("div", footnote_label + footnote_content, :id => "footnote#{id}#{time}", :class => "footnote")
19
29
  end
20
30
  end
21
31
  end
@@ -6,6 +6,7 @@ require 'hemingway/latex_nodes'
6
6
  require "hemingway/block/block"
7
7
  require "hemingway/footnote/footnote"
8
8
  require "hemingway/math/math"
9
+ require "hemingway/override/override"
9
10
  require "hemingway/special/special"
10
11
  require "hemingway/symbol/symbol"
11
12
  require "hemingway/tag/tag"
@@ -25,6 +26,8 @@ module Hemingway
25
26
 
26
27
  include Math
27
28
 
29
+ include Override
30
+
28
31
  include Special
29
32
 
30
33
  include Symbol
@@ -311,12 +314,17 @@ module Hemingway
311
314
  if r4
312
315
  r0 = r4
313
316
  else
314
- r5 = _nt_text
317
+ r5 = _nt_override
315
318
  if r5
316
319
  r0 = r5
317
320
  else
318
- @index = i0
319
- r0 = nil
321
+ r6 = _nt_text
322
+ if r6
323
+ r0 = r6
324
+ else
325
+ @index = i0
326
+ r0 = nil
327
+ end
320
328
  end
321
329
  end
322
330
  end
@@ -3,6 +3,7 @@ require 'hemingway/latex_nodes'
3
3
  require "hemingway/block/block"
4
4
  require "hemingway/footnote/footnote"
5
5
  require "hemingway/math/math"
6
+ require "hemingway/override/override"
6
7
  require "hemingway/special/special"
7
8
  require "hemingway/symbol/symbol"
8
9
  require "hemingway/tag/tag"
@@ -14,6 +15,7 @@ module Hemingway
14
15
  include Block
15
16
  include Footnote
16
17
  include Math
18
+ include Override
17
19
  include Special
18
20
  include Symbol
19
21
  include Tag
@@ -53,7 +55,7 @@ module Hemingway
53
55
  # method need not be defined on it because alas, it already exists as
54
56
  # defined in the text rule itself!
55
57
  rule content
56
- special / tag / block / math / text
58
+ special / tag / block / math / override / text
57
59
  end
58
60
 
59
61
  # Treetop does not separate lexing from parsing. Must consume all input.
@@ -6,8 +6,10 @@ module Hemingway
6
6
  paragraph_html = ""
7
7
 
8
8
  elements.each do |e|
9
- paragraph_html += e.html(footnote_content.size)
10
- footnote_content += e.footnote_html(footnote_content.size)
9
+ # Time.now.to_f => 123123.1231231
10
+ time = Time.now.to_f.to_s.gsub(".", "-")
11
+ paragraph_html += e.html(footnote_content.size, time)
12
+ footnote_content += e.footnote_html(footnote_content.size, time)
11
13
  end
12
14
 
13
15
  footnote_html = footnote_content.join
@@ -17,11 +19,16 @@ module Hemingway
17
19
  end
18
20
 
19
21
  module ParagraphNode
20
- def html(footnote_seed)
22
+
23
+ # I'm passing in a time variable here to make links unique. You see,
24
+ # if you parse many of these entries on a single HTML page you'll end up
25
+ # with multiple #footnote1 divs. To make them unique, we'll pass down
26
+ # a time variable from above to seed them.
27
+ def html(footnote_seed, time)
21
28
  paragraph_content = sequence.elements.map do |element|
22
29
  if element.respond_to?(:footnote_html)
23
30
  footnote_seed += 1
24
- element.html(footnote_seed)
31
+ element.html(footnote_seed, time)
25
32
  elsif element.respond_to?(:newline)
26
33
  element.newline.html
27
34
  else
@@ -32,11 +39,15 @@ module Hemingway
32
39
  Build.tag("p", paragraph_content)
33
40
  end
34
41
 
35
- def footnote_html(footnote_seed)
42
+ # I'm passing in a time variable here to make links unique. You see,
43
+ # if you parse many of these entries on a single HTML page you'll end up
44
+ # with multiple #footnote1 divs. To make them unique, we'll pass down
45
+ # a time variable from above to seed them.
46
+ def footnote_html(footnote_seed, time)
36
47
  footnote_content = sequence.elements.reduce([]) do |memo, element|
37
48
  if element.respond_to?(:footnote_html)
38
49
  footnote_seed += 1
39
- memo + [element.footnote_html(footnote_seed)]
50
+ memo + [element.footnote_html(footnote_seed, time)]
40
51
  else
41
52
  memo
42
53
  end
@@ -0,0 +1,73 @@
1
+ # Autogenerated from a Treetop grammar. Edits may be lost.
2
+
3
+
4
+ require "hemingway/override/override_nodes"
5
+
6
+ module Hemingway
7
+ module Override
8
+ include Treetop::Runtime
9
+
10
+ def root
11
+ @root ||= :override
12
+ end
13
+
14
+ def _nt_override
15
+ start_index = index
16
+ if node_cache[:override].has_key?(index)
17
+ cached = node_cache[:override][index]
18
+ if cached
19
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
20
+ @index = cached.interval.end
21
+ end
22
+ return cached
23
+ end
24
+
25
+ i0 = index
26
+ if has_terminal?("---", false, index)
27
+ r1 = instantiate_node(MDashNode,input, index...(index + 3))
28
+ @index += 3
29
+ else
30
+ terminal_parse_failure("---")
31
+ r1 = nil
32
+ end
33
+ if r1
34
+ r0 = r1
35
+ else
36
+ if has_terminal?("``", false, index)
37
+ r2 = instantiate_node(LeftQuoteNode,input, index...(index + 2))
38
+ @index += 2
39
+ else
40
+ terminal_parse_failure("``")
41
+ r2 = nil
42
+ end
43
+ if r2
44
+ r0 = r2
45
+ else
46
+ if has_terminal?("''", false, index)
47
+ r3 = instantiate_node(RightQuoteNode,input, index...(index + 2))
48
+ @index += 2
49
+ else
50
+ terminal_parse_failure("''")
51
+ r3 = nil
52
+ end
53
+ if r3
54
+ r0 = r3
55
+ else
56
+ @index = i0
57
+ r0 = nil
58
+ end
59
+ end
60
+ end
61
+
62
+ node_cache[:override][start_index] = r0
63
+
64
+ r0
65
+ end
66
+
67
+ end
68
+
69
+ class OverrideParser < Treetop::Runtime::CompiledParser
70
+ include Override
71
+ end
72
+
73
+ end
@@ -0,0 +1,16 @@
1
+ require "hemingway/override/override_nodes"
2
+
3
+ module Hemingway
4
+ grammar Override
5
+
6
+ # Plain text to be overridden with more appropriate characters
7
+ rule override
8
+ "---" <MDashNode>
9
+ /
10
+ "``" <LeftQuoteNode>
11
+ /
12
+ "''" <RightQuoteNode>
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ module Hemingway
2
+
3
+ module MDashNode
4
+ def html
5
+ "&mdash;"
6
+ end
7
+ end
8
+
9
+ module LeftQuoteNode
10
+ def html
11
+ "&#8220;"
12
+ end
13
+ end
14
+
15
+ module RightQuoteNode
16
+ def html
17
+ "&#8221;"
18
+ end
19
+ end
20
+
21
+ end
@@ -100,8 +100,13 @@ module Hemingway
100
100
  if r4
101
101
  r0 = r4
102
102
  else
103
- @index = i0
104
- r0 = nil
103
+ r5 = _nt_override
104
+ if r5
105
+ r0 = r5
106
+ else
107
+ @index = i0
108
+ r0 = nil
109
+ end
105
110
  end
106
111
  end
107
112
  end
@@ -28,7 +28,7 @@ module Hemingway
28
28
 
29
29
  # If you hit any of these while chomping on text you should pop out
30
30
  rule non_text
31
- escape / tag_end / math_start / newline
31
+ escape / tag_end / math_start / newline / override
32
32
  end
33
33
 
34
34
  end
@@ -1,3 +1,3 @@
1
1
  module Hemingway
2
- VERSION = "0.0.3"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -87,7 +87,7 @@ module Hemingway
87
87
 
88
88
  it 'should wrap labels in the dictionary label tag' do
89
89
  html = @parser.parse("\\begin{description} \\item [Frodo] Adventuerer \\item [Sam] Faithful Companion \\end{description}").html
90
- html.should == "<div class='entry'><p><dl><dd><dt>Frodo</dt> Adventuerer </dd><dd><dt>Sam</dt> Faithful Companion </dd></dl></p></div>"
90
+ html.should == "<div class='entry'><p><dl><dt>Frodo</dt><dd> Adventuerer </dd><dt>Sam</dt><dd> Faithful Companion </dd></dl></p></div>"
91
91
  end
92
92
  end
93
93
 
@@ -6,33 +6,36 @@ module Hemingway
6
6
 
7
7
  before do
8
8
  @parser = Parser.new
9
+ time = double()
10
+ Time.stub(:now) { time }
11
+ time.stub(:to_f) { 100.01 }
9
12
  end
10
13
 
11
14
  describe "#footnote" do
12
15
 
13
16
  it "should put a footnote at the end of a paragraph" do
14
17
  html = @parser.parse("You are what you redpoint\\footnote{sending a lead route}, after all.").html
15
- html.should == "<div class='entry'><p>You are what you redpoint<a href='#footnote1'><span class='inline-footnote-number'><sup>1</sup></span></a>, after all.</p><div id='footnote1' class='footnote'><span class='footnote-number'><sup>1</sup></span>sending a lead route</div></div>"
18
+ html.should == "<div class='entry'><p>You are what you redpoint<a href='#footnote1100-01'><span class='inline-footnote-number'><sup>1</sup></span></a>, after all.</p><div id='footnote1100-01' class='footnote'><span class='footnote-number'><sup>1</sup></span>sending a lead route</div></div>"
16
19
  end
17
20
 
18
21
  it "should put properly increment the footnote count per entry" do
19
22
  html = @parser.parse("Slowly, Vader\\footnote{thug} approached Luke\\footnote{homeboy}").html
20
- html.should == "<div class='entry'><p>Slowly, Vader<a href='#footnote1'><span class='inline-footnote-number'><sup>1</sup></span></a> approached Luke<a href='#footnote2'><span class='inline-footnote-number'><sup>2</sup></span></a></p><div id='footnote1' class='footnote'><span class='footnote-number'><sup>1</sup></span>thug</div><div id='footnote2' class='footnote'><span class='footnote-number'><sup>2</sup></span>homeboy</div></div>"
23
+ html.should == "<div class='entry'><p>Slowly, Vader<a href='#footnote1100-01'><span class='inline-footnote-number'><sup>1</sup></span></a> approached Luke<a href='#footnote2100-01'><span class='inline-footnote-number'><sup>2</sup></span></a></p><div id='footnote1100-01' class='footnote'><span class='footnote-number'><sup>1</sup></span>thug</div><div id='footnote2100-01' class='footnote'><span class='footnote-number'><sup>2</sup></span>homeboy</div></div>"
21
24
  end
22
25
 
23
26
  it "should support any type of content in a footnote" do
24
27
  html = @parser.parse("Dave Graham\\footnote{\\emph{Boulderer}, and \& all around \\textsc{good} guy $\\gamma$} makes the best mixtapes").html
25
- html.should == "<div class='entry'><p>Dave Graham<a href='#footnote1'><span class='inline-footnote-number'><sup>1</sup></span></a> makes the best mixtapes</p><div id='footnote1' class='footnote'><span class='footnote-number'><sup>1</sup></span><em>Boulderer</em>, and & all around <span class='textsc'>good</span> guy &gamma;</div></div>"
28
+ html.should == "<div class='entry'><p>Dave Graham<a href='#footnote1100-01'><span class='inline-footnote-number'><sup>1</sup></span></a> makes the best mixtapes</p><div id='footnote1100-01' class='footnote'><span class='footnote-number'><sup>1</sup></span><em>Boulderer</em>, and & all around <span class='textsc'>good</span> guy &gamma;</div></div>"
26
29
  end
27
30
 
28
31
  it "should properly place footnotes from all paragraphs after all paragraphs an increment properly" do
29
32
  html = @parser.parse("Freddie Gibbs\\footnote{from Gary, Indiana} is hood\\footnote{thug} as all hell.\n\n John Russel\\footnote{of Short Bus fame} almost got raped in Gary, ID\\footnote{but not by Freddie Gibbs}").html
30
- html.should == "<div class='entry'><p>Freddie Gibbs<a href='#footnote1'><span class='inline-footnote-number'><sup>1</sup></span></a> is hood<a href='#footnote2'><span class='inline-footnote-number'><sup>2</sup></span></a> as all hell.</p><p> John Russel<a href='#footnote3'><span class='inline-footnote-number'><sup>3</sup></span></a> almost got raped in Gary, ID<a href='#footnote4'><span class='inline-footnote-number'><sup>4</sup></span></a></p><div id='footnote1' class='footnote'><span class='footnote-number'><sup>1</sup></span>from Gary, Indiana</div><div id='footnote2' class='footnote'><span class='footnote-number'><sup>2</sup></span>thug</div><div id='footnote3' class='footnote'><span class='footnote-number'><sup>3</sup></span>of Short Bus fame</div><div id='footnote4' class='footnote'><span class='footnote-number'><sup>4</sup></span>but not by Freddie Gibbs</div></div>"
33
+ html.should == "<div class='entry'><p>Freddie Gibbs<a href='#footnote1100-01'><span class='inline-footnote-number'><sup>1</sup></span></a> is hood<a href='#footnote2100-01'><span class='inline-footnote-number'><sup>2</sup></span></a> as all hell.</p><p> John Russel<a href='#footnote3100-01'><span class='inline-footnote-number'><sup>3</sup></span></a> almost got raped in Gary, ID<a href='#footnote4100-01'><span class='inline-footnote-number'><sup>4</sup></span></a></p><div id='footnote1100-01' class='footnote'><span class='footnote-number'><sup>1</sup></span>from Gary, Indiana</div><div id='footnote2100-01' class='footnote'><span class='footnote-number'><sup>2</sup></span>thug</div><div id='footnote3100-01' class='footnote'><span class='footnote-number'><sup>3</sup></span>of Short Bus fame</div><div id='footnote4100-01' class='footnote'><span class='footnote-number'><sup>4</sup></span>but not by Freddie Gibbs</div></div>"
31
34
  end
32
35
 
33
36
  it "will obviously allow me to drop a list in a footnote (this is just for my own satisfaction mostly)" do
34
37
  html = @parser.parse("There are some rules\\footnote{\\begin{itemize} \\item Steezy socks always \\item Brosend often \\item Everything for the ladies \\end{itemize}} that we all must live by.").html
35
- html.should == "<div class='entry'><p>There are some rules<a href='#footnote1'><span class='inline-footnote-number'><sup>1</sup></span></a> that we all must live by.</p><div id='footnote1' class='footnote'><span class='footnote-number'><sup>1</sup></span><ul><li>Steezy socks always </li><li>Brosend often </li><li>Everything for the ladies </li></ul></div></div>"
38
+ html.should == "<div class='entry'><p>There are some rules<a href='#footnote1100-01'><span class='inline-footnote-number'><sup>1</sup></span></a> that we all must live by.</p><div id='footnote1100-01' class='footnote'><span class='footnote-number'><sup>1</sup></span><ul><li>Steezy socks always </li><li>Brosend often </li><li>Everything for the ladies </li></ul></div></div>"
36
39
  end
37
40
 
38
41
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ module Hemingway
4
+
5
+ describe Parser do
6
+
7
+ before do
8
+ @parser = Parser.new
9
+ end
10
+
11
+ describe "#override" do
12
+
13
+ it 'should override a dash character' do
14
+ html = @parser.parse("Please--- wait for me.").html
15
+ html.should == "<div class='entry'><p>Please&mdash; wait for me.</p></div>"
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hemingway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - William Myers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-15 00:00:00.000000000 Z
11
+ date: 2013-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -78,6 +78,9 @@ files:
78
78
  - lib/hemingway/math/math.rb
79
79
  - lib/hemingway/math/math.treetop
80
80
  - lib/hemingway/math/math_nodes.rb
81
+ - lib/hemingway/override/override.rb
82
+ - lib/hemingway/override/override.treetop
83
+ - lib/hemingway/override/override_nodes.rb
81
84
  - lib/hemingway/special/special.rb
82
85
  - lib/hemingway/special/special.treetop
83
86
  - lib/hemingway/special/special_nodes.rb
@@ -101,6 +104,7 @@ files:
101
104
  - spec/nodes/block_spec.rb
102
105
  - spec/nodes/footnote_spec.rb
103
106
  - spec/nodes/math_spec.rb
107
+ - spec/nodes/override_spec.rb
104
108
  - spec/nodes/special_spec.rb
105
109
  - spec/nodes/tag_spec.rb
106
110
  - spec/parser_spec.rb
@@ -138,6 +142,7 @@ test_files:
138
142
  - spec/nodes/block_spec.rb
139
143
  - spec/nodes/footnote_spec.rb
140
144
  - spec/nodes/math_spec.rb
145
+ - spec/nodes/override_spec.rb
141
146
  - spec/nodes/special_spec.rb
142
147
  - spec/nodes/tag_spec.rb
143
148
  - spec/parser_spec.rb