hemingway 0.0.0 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +297 -0
  7. data/Rakefile +1 -0
  8. data/hemingway.gemspec +23 -0
  9. data/lib/hemingway/block/block.rb +378 -0
  10. data/lib/hemingway/block/block.treetop +36 -0
  11. data/lib/hemingway/block/block_nodes.rb +9 -0
  12. data/lib/hemingway/block/list/list.rb +412 -0
  13. data/lib/hemingway/block/list/list.treetop +51 -0
  14. data/lib/hemingway/block/list/list_nodes.rb +39 -0
  15. data/lib/hemingway/block/quote/quote.rb +192 -0
  16. data/lib/hemingway/block/quote/quote.treetop +27 -0
  17. data/lib/hemingway/block/quote/quote_nodes.rb +25 -0
  18. data/lib/hemingway/block/verbatim/verbatim.rb +159 -0
  19. data/lib/hemingway/block/verbatim/verbatim.treetop +21 -0
  20. data/lib/hemingway/block/verbatim/verbatim_nodes.rb +9 -0
  21. data/lib/hemingway/build.rb +65 -0
  22. data/lib/hemingway/footnote/footnote.rb +83 -0
  23. data/lib/hemingway/footnote/footnote.treetop +11 -0
  24. data/lib/hemingway/footnote/footnote_nodes.rb +21 -0
  25. data/lib/hemingway/latex.rb +409 -0
  26. data/lib/hemingway/latex.treetop +81 -0
  27. data/lib/hemingway/latex_nodes.rb +45 -0
  28. data/lib/hemingway/math/math.rb +135 -0
  29. data/lib/hemingway/math/math.treetop +29 -0
  30. data/lib/hemingway/math/math_nodes.rb +7 -0
  31. data/lib/hemingway/special/special.rb +164 -0
  32. data/lib/hemingway/special/special.treetop +17 -0
  33. data/lib/hemingway/special/special_nodes.rb +7 -0
  34. data/lib/hemingway/symbol/symbol.rb +460 -0
  35. data/lib/hemingway/symbol/symbol.treetop +47 -0
  36. data/lib/hemingway/symbol/symbol_nodes.rb +7 -0
  37. data/lib/hemingway/tag/tag.rb +538 -0
  38. data/lib/hemingway/tag/tag.treetop +63 -0
  39. data/lib/hemingway/tag/tag_nodes.rb +49 -0
  40. data/lib/hemingway/text/text.rb +121 -0
  41. data/lib/hemingway/text/text.treetop +35 -0
  42. data/lib/hemingway/text/text_nodes.rb +7 -0
  43. data/lib/hemingway/version.rb +3 -0
  44. data/lib/hemingway.rb +7 -0
  45. data/script/build +22 -0
  46. data/script/test +2 -0
  47. data/spec/build_spec.rb +65 -0
  48. data/spec/nodes/block/list_spec.rb +91 -0
  49. data/spec/nodes/block/quote_spec.rb +31 -0
  50. data/spec/nodes/block/verbatim_spec.rb +27 -0
  51. data/spec/nodes/block_spec.rb +21 -0
  52. data/spec/nodes/footnote_spec.rb +42 -0
  53. data/spec/nodes/math_spec.rb +31 -0
  54. data/spec/nodes/special_spec.rb +27 -0
  55. data/spec/nodes/tag_spec.rb +98 -0
  56. data/spec/parser_spec.rb +48 -0
  57. data/spec/spec_helper.rb +5 -0
  58. metadata +110 -18
@@ -0,0 +1,25 @@
1
+ module Hemingway
2
+
3
+ module QuoteNode
4
+ def html(block_content)
5
+ Build.tag("blockquote", block_content.html)
6
+ end
7
+ end
8
+
9
+ module QuoteEntryNode
10
+ def html
11
+ elements.map { |e| e.html }.join
12
+ end
13
+ end
14
+
15
+ module QuoteParagraphNode
16
+ def html
17
+ paragraph_content = sequence.elements.map do |element|
18
+ element.html
19
+ end.join
20
+
21
+ Build.tag("p", paragraph_content)
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,159 @@
1
+ # Autogenerated from a Treetop grammar. Edits may be lost.
2
+
3
+
4
+ require "hemingway/block/verbatim/verbatim_nodes"
5
+
6
+ module Hemingway
7
+ module Block
8
+ module Verbatim
9
+ include Treetop::Runtime
10
+
11
+ def root
12
+ @root ||= :verbatim
13
+ end
14
+
15
+ def _nt_verbatim
16
+ start_index = index
17
+ if node_cache[:verbatim].has_key?(index)
18
+ cached = node_cache[:verbatim][index]
19
+ if cached
20
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
21
+ @index = cached.interval.end
22
+ end
23
+ return cached
24
+ end
25
+
26
+ r0 = _nt_verbatim_text
27
+
28
+ node_cache[:verbatim][start_index] = r0
29
+
30
+ r0
31
+ end
32
+
33
+ def _nt_verbatim_type
34
+ start_index = index
35
+ if node_cache[:verbatim_type].has_key?(index)
36
+ cached = node_cache[:verbatim_type][index]
37
+ if cached
38
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
39
+ @index = cached.interval.end
40
+ end
41
+ return cached
42
+ end
43
+
44
+ if has_terminal?("verbatim", false, index)
45
+ r0 = instantiate_node(VerbatimNode,input, index...(index + 8))
46
+ @index += 8
47
+ else
48
+ terminal_parse_failure("verbatim")
49
+ r0 = nil
50
+ end
51
+
52
+ node_cache[:verbatim_type][start_index] = r0
53
+
54
+ r0
55
+ end
56
+
57
+ module VerbatimText0
58
+ def block_end
59
+ elements[0]
60
+ end
61
+
62
+ def verbatim_type
63
+ elements[1]
64
+ end
65
+
66
+ end
67
+
68
+ module VerbatimText1
69
+ end
70
+
71
+ def _nt_verbatim_text
72
+ start_index = index
73
+ if node_cache[:verbatim_text].has_key?(index)
74
+ cached = node_cache[:verbatim_text][index]
75
+ if cached
76
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
77
+ @index = cached.interval.end
78
+ end
79
+ return cached
80
+ end
81
+
82
+ s0, i0 = [], index
83
+ loop do
84
+ i1, s1 = index, []
85
+ i2 = index
86
+ i3, s3 = index, []
87
+ r4 = _nt_block_end
88
+ s3 << r4
89
+ if r4
90
+ r5 = _nt_verbatim_type
91
+ s3 << r5
92
+ if r5
93
+ if has_terminal?("}", false, index)
94
+ r6 = instantiate_node(SyntaxNode,input, index...(index + 1))
95
+ @index += 1
96
+ else
97
+ terminal_parse_failure("}")
98
+ r6 = nil
99
+ end
100
+ s3 << r6
101
+ end
102
+ end
103
+ if s3.last
104
+ r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
105
+ r3.extend(VerbatimText0)
106
+ else
107
+ @index = i3
108
+ r3 = nil
109
+ end
110
+ if r3
111
+ r2 = nil
112
+ else
113
+ @index = i2
114
+ r2 = instantiate_node(SyntaxNode,input, index...index)
115
+ end
116
+ s1 << r2
117
+ if r2
118
+ if index < input_length
119
+ r7 = instantiate_node(SyntaxNode,input, index...(index + 1))
120
+ @index += 1
121
+ else
122
+ terminal_parse_failure("any character")
123
+ r7 = nil
124
+ end
125
+ s1 << r7
126
+ end
127
+ if s1.last
128
+ r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
129
+ r1.extend(VerbatimText1)
130
+ else
131
+ @index = i1
132
+ r1 = nil
133
+ end
134
+ if r1
135
+ s0 << r1
136
+ else
137
+ break
138
+ end
139
+ end
140
+ if s0.empty?
141
+ @index = i0
142
+ r0 = nil
143
+ else
144
+ r0 = instantiate_node(TextNode,input, i0...index, s0)
145
+ end
146
+
147
+ node_cache[:verbatim_text][start_index] = r0
148
+
149
+ r0
150
+ end
151
+
152
+ end
153
+
154
+ class VerbatimParser < Treetop::Runtime::CompiledParser
155
+ include Verbatim
156
+ end
157
+
158
+ end
159
+ end
@@ -0,0 +1,21 @@
1
+ require "hemingway/block/verbatim/verbatim_nodes"
2
+
3
+ module Hemingway
4
+ module Block
5
+ grammar Verbatim
6
+
7
+ rule verbatim
8
+ verbatim_text
9
+ end
10
+
11
+ rule verbatim_type
12
+ "verbatim" <VerbatimNode>
13
+ end
14
+
15
+ rule verbatim_text
16
+ ( !( block_end verbatim_type "}" ) . )+ <TextNode>
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module Hemingway
2
+
3
+ module VerbatimNode
4
+ def html(block_content)
5
+ Build.tag("pre", block_content.html)
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,65 @@
1
+ module Hemingway
2
+
3
+ # This is a moronic little class to generate some HTML Tags around
4
+ # some content.
5
+ class Build
6
+ def self.tag(html_attribute, html_content=nil, options={})
7
+ String.new.tap do |t|
8
+ t << "<" + html_attribute
9
+ t << " id='" + options[:id] + "'" if options[:id]
10
+ t << " class='" + options[:class] + "'" if options[:class]
11
+ t << " href='" + options[:href] + "'" if options[:href]
12
+ t << ">"
13
+ t << html_content if html_content
14
+ t << "</" + html_attribute + ">" unless options[:close_tag] == false
15
+ end
16
+ end
17
+
18
+ def self.symbol(symbol)
19
+ latex_sym_to_html[symbol]
20
+ end
21
+
22
+ private
23
+ def self.latex_sym_to_html
24
+ {
25
+ "\\Gamma" => "&Gamma;",
26
+ "\\Delta" => "&Delta;",
27
+ "\\Theta" => "&Theta;",
28
+ "\\Lambda" => "&Lambda;",
29
+ "\\Xi" => "&Xi;",
30
+ "\\Pi" => "&Pi;",
31
+ "\\Sigma" => "&Sigma;",
32
+ "\\Upsilon" => "&Upsilon;",
33
+ "\\Phi" => "&Phi;",
34
+ "\\Psi" => "&Psi;",
35
+ "\\Omega" => "&Omega;",
36
+ "\\alpha" => "&alpha;",
37
+ "\\beta" => "&beta;",
38
+ "\\gamma" => "&gamma;",
39
+ "\\delta" => "&delta;",
40
+ "\\epsilon" => "&epsilon;",
41
+ "\\zeta" => "&zeta;",
42
+ "\\eta" => "&eta;",
43
+ "\\theta" => "&theta;",
44
+ "\\iota" => "&iota;",
45
+ "\\kappa" => "&kappa;",
46
+ "\\lambda" => "&lambda;",
47
+ "\\mu" => "&mu;",
48
+ "\\nu" => "&nu;",
49
+ "\\xi" => "&xi;",
50
+ "\\pi" => "&pi;",
51
+ "\\rho" => "&rho;",
52
+ "\\varsigma" => "&sigmaf;",
53
+ "\\sigma" => "&sigma;",
54
+ "\\tau" => "&tau;",
55
+ "\\upsilon" => "&upsilon;",
56
+ "\\phi" => "&phi;",
57
+ "\\chi" => "&chi;",
58
+ "\\psi" => "&psi;",
59
+ "\\omega" => "&omega;"
60
+ }
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,83 @@
1
+ # Autogenerated from a Treetop grammar. Edits may be lost.
2
+
3
+
4
+ require "hemingway/footnote/footnote_nodes"
5
+
6
+ module Hemingway
7
+ module Footnote
8
+ include Treetop::Runtime
9
+
10
+ def root
11
+ @root ||= :footnote
12
+ end
13
+
14
+ module Footnote0
15
+ def sequence
16
+ elements[1]
17
+ end
18
+
19
+ end
20
+
21
+ def _nt_footnote
22
+ start_index = index
23
+ if node_cache[:footnote].has_key?(index)
24
+ cached = node_cache[:footnote][index]
25
+ if cached
26
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
27
+ @index = cached.interval.end
28
+ end
29
+ return cached
30
+ end
31
+
32
+ i0, s0 = index, []
33
+ if has_terminal?("\\footnote{", false, index)
34
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 10))
35
+ @index += 10
36
+ else
37
+ terminal_parse_failure("\\footnote{")
38
+ r1 = nil
39
+ end
40
+ s0 << r1
41
+ if r1
42
+ s2, i2 = [], index
43
+ loop do
44
+ r3 = _nt_content
45
+ if r3
46
+ s2 << r3
47
+ else
48
+ break
49
+ end
50
+ end
51
+ r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
52
+ s0 << r2
53
+ if r2
54
+ if has_terminal?("}", false, index)
55
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
56
+ @index += 1
57
+ else
58
+ terminal_parse_failure("}")
59
+ r4 = nil
60
+ end
61
+ s0 << r4
62
+ end
63
+ end
64
+ if s0.last
65
+ r0 = instantiate_node(FootnoteNode,input, i0...index, s0)
66
+ r0.extend(Footnote0)
67
+ else
68
+ @index = i0
69
+ r0 = nil
70
+ end
71
+
72
+ node_cache[:footnote][start_index] = r0
73
+
74
+ r0
75
+ end
76
+
77
+ end
78
+
79
+ class FootnoteParser < Treetop::Runtime::CompiledParser
80
+ include Footnote
81
+ end
82
+
83
+ end
@@ -0,0 +1,11 @@
1
+ require "hemingway/footnote/footnote_nodes"
2
+
3
+ module Hemingway
4
+ grammar Footnote
5
+
6
+ rule footnote
7
+ "\\footnote{" sequence:content* "}" <FootnoteNode>
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ module Hemingway
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)
6
+ 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}")
8
+ end
9
+
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
12
+ # 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)
16
+ footnote_label = Build.tag("span", Build.tag("sup", id.to_s), :class => "footnote-number")
17
+ footnote_content = sequence.elements.map { |s| s.html }.join
18
+ Build.tag("div", footnote_label + footnote_content, :id => "footnote#{id}", :class => "footnote")
19
+ end
20
+ end
21
+ end