hemingway 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/lib/hemingway/block/block.rb +222 -121
- data/lib/hemingway/block/block.treetop +6 -3
- data/lib/hemingway/block/center/center.rb +134 -0
- data/lib/hemingway/block/center/center.treetop +17 -0
- data/lib/hemingway/block/center/center_nodes.rb +17 -0
- data/lib/hemingway/block/list/list.rb +30 -25
- data/lib/hemingway/block/list/list.treetop +1 -1
- data/lib/hemingway/build.rb +104 -6
- data/lib/hemingway/latex.rb +82 -15
- data/lib/hemingway/latex.treetop +4 -4
- data/lib/hemingway/latex_nodes.rb +16 -0
- data/lib/hemingway/math/math.rb +88 -4
- data/lib/hemingway/math/math.treetop +5 -1
- data/lib/hemingway/math/math_nodes.rb +14 -1
- data/lib/hemingway/special/special.rb +13 -2
- data/lib/hemingway/special/special.treetop +1 -1
- data/lib/hemingway/special/special_nodes.rb +9 -5
- data/lib/hemingway/symbol/symbol.rb +14 -2
- data/lib/hemingway/symbol/symbol.treetop +2 -1
- data/lib/hemingway/tag/tag.rb +162 -2
- data/lib/hemingway/tag/tag.treetop +6 -0
- data/lib/hemingway/tag/tag_nodes.rb +6 -0
- data/lib/hemingway/version.rb +1 -1
- data/spec/build_spec.rb +17 -0
- data/spec/nodes/block/center_spec.rb +27 -0
- data/spec/nodes/block/list_spec.rb +5 -0
- data/spec/nodes/block_spec.rb +5 -1
- data/spec/nodes/math_spec.rb +10 -0
- data/spec/nodes/special_spec.rb +6 -0
- data/spec/nodes/tag_spec.rb +9 -0
- metadata +7 -2
@@ -2,6 +2,7 @@ require "hemingway/block/block_nodes"
|
|
2
2
|
require "hemingway/block/list/list"
|
3
3
|
require "hemingway/block/verbatim/verbatim"
|
4
4
|
require "hemingway/block/quote/quote"
|
5
|
+
require "hemingway/block/center/center"
|
5
6
|
|
6
7
|
module Hemingway
|
7
8
|
grammar Block
|
@@ -9,6 +10,7 @@ module Hemingway
|
|
9
10
|
include List
|
10
11
|
include Verbatim
|
11
12
|
include Quote
|
13
|
+
include Center
|
12
14
|
|
13
15
|
# Example:
|
14
16
|
# \begin{itemize}
|
@@ -18,9 +20,10 @@ module Hemingway
|
|
18
20
|
# \end{itemize}
|
19
21
|
rule block
|
20
22
|
(
|
21
|
-
block_start type:list_type "}" whitespace content:list whitespace block_end list_type "}" &{ |seq| seq[1].text_value == seq[7].text_value } /
|
22
|
-
block_start type:verbatim_type "}" whitespace content:verbatim whitespace block_end verbatim_type "}" /
|
23
|
-
block_start type:quote_type "}" whitespace content:quote_entry whitespace block_end quote_type "}"
|
23
|
+
block_start type:list_type "}" whitespace? content:list whitespace? block_end list_type "}" &{ |seq| seq[1].text_value == seq[7].text_value } /
|
24
|
+
block_start type:verbatim_type "}" whitespace? content:verbatim whitespace? block_end verbatim_type "}" /
|
25
|
+
block_start type:quote_type "}" whitespace? content:quote_entry whitespace? block_end quote_type "}" /
|
26
|
+
block_start type:center_type "}" whitespace? content:center whitespace? block_end center_type "}"
|
24
27
|
) <BlockNode>
|
25
28
|
end
|
26
29
|
|
@@ -0,0 +1,134 @@
|
|
1
|
+
# Autogenerated from a Treetop grammar. Edits may be lost.
|
2
|
+
|
3
|
+
|
4
|
+
require "hemingway/block/center/center_nodes"
|
5
|
+
|
6
|
+
module Hemingway
|
7
|
+
module Block
|
8
|
+
module Center
|
9
|
+
include Treetop::Runtime
|
10
|
+
|
11
|
+
def root
|
12
|
+
@root ||= :center
|
13
|
+
end
|
14
|
+
|
15
|
+
module Center0
|
16
|
+
def block_end
|
17
|
+
elements[0]
|
18
|
+
end
|
19
|
+
|
20
|
+
def center_type
|
21
|
+
elements[1]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module Center1
|
26
|
+
def content
|
27
|
+
elements[1]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def _nt_center
|
32
|
+
start_index = index
|
33
|
+
if node_cache[:center].has_key?(index)
|
34
|
+
cached = node_cache[:center][index]
|
35
|
+
if cached
|
36
|
+
cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
|
37
|
+
@index = cached.interval.end
|
38
|
+
end
|
39
|
+
return cached
|
40
|
+
end
|
41
|
+
|
42
|
+
s0, i0 = [], index
|
43
|
+
loop do
|
44
|
+
i1 = index
|
45
|
+
i2, s2 = index, []
|
46
|
+
i3 = index
|
47
|
+
i4, s4 = index, []
|
48
|
+
r5 = _nt_block_end
|
49
|
+
s4 << r5
|
50
|
+
if r5
|
51
|
+
r6 = _nt_center_type
|
52
|
+
s4 << r6
|
53
|
+
end
|
54
|
+
if s4.last
|
55
|
+
r4 = instantiate_node(SyntaxNode,input, i4...index, s4)
|
56
|
+
r4.extend(Center0)
|
57
|
+
else
|
58
|
+
@index = i4
|
59
|
+
r4 = nil
|
60
|
+
end
|
61
|
+
if r4
|
62
|
+
r3 = nil
|
63
|
+
else
|
64
|
+
@index = i3
|
65
|
+
r3 = instantiate_node(SyntaxNode,input, index...index)
|
66
|
+
end
|
67
|
+
s2 << r3
|
68
|
+
if r3
|
69
|
+
r7 = _nt_content
|
70
|
+
s2 << r7
|
71
|
+
end
|
72
|
+
if s2.last
|
73
|
+
r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
|
74
|
+
r2.extend(Center1)
|
75
|
+
else
|
76
|
+
@index = i2
|
77
|
+
r2 = nil
|
78
|
+
end
|
79
|
+
if r2
|
80
|
+
r1 = r2
|
81
|
+
else
|
82
|
+
r8 = _nt_newline
|
83
|
+
if r8
|
84
|
+
r1 = r8
|
85
|
+
else
|
86
|
+
@index = i1
|
87
|
+
r1 = nil
|
88
|
+
end
|
89
|
+
end
|
90
|
+
if r1
|
91
|
+
s0 << r1
|
92
|
+
else
|
93
|
+
break
|
94
|
+
end
|
95
|
+
end
|
96
|
+
r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
|
97
|
+
|
98
|
+
node_cache[:center][start_index] = r0
|
99
|
+
|
100
|
+
r0
|
101
|
+
end
|
102
|
+
|
103
|
+
def _nt_center_type
|
104
|
+
start_index = index
|
105
|
+
if node_cache[:center_type].has_key?(index)
|
106
|
+
cached = node_cache[:center_type][index]
|
107
|
+
if cached
|
108
|
+
cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
|
109
|
+
@index = cached.interval.end
|
110
|
+
end
|
111
|
+
return cached
|
112
|
+
end
|
113
|
+
|
114
|
+
if has_terminal?("center", false, index)
|
115
|
+
r0 = instantiate_node(CenterNode,input, index...(index + 6))
|
116
|
+
@index += 6
|
117
|
+
else
|
118
|
+
terminal_parse_failure("center")
|
119
|
+
r0 = nil
|
120
|
+
end
|
121
|
+
|
122
|
+
node_cache[:center_type][start_index] = r0
|
123
|
+
|
124
|
+
r0
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
class CenterParser < Treetop::Runtime::CompiledParser
|
130
|
+
include Center
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "hemingway/block/center/center_nodes"
|
2
|
+
|
3
|
+
module Hemingway
|
4
|
+
module Block
|
5
|
+
grammar Center
|
6
|
+
|
7
|
+
rule center
|
8
|
+
( !( block_end center_type ) content / newline )*
|
9
|
+
end
|
10
|
+
|
11
|
+
rule center_type
|
12
|
+
"center" <CenterNode>
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Hemingway
|
2
|
+
|
3
|
+
module CenterNode
|
4
|
+
def html(block_content)
|
5
|
+
content = block_content.elements.map do |e|
|
6
|
+
if e.respond_to?(:content)
|
7
|
+
e.content.html
|
8
|
+
else
|
9
|
+
e.visible_html
|
10
|
+
end
|
11
|
+
end.join
|
12
|
+
|
13
|
+
Build.tag("div", content , class: "center")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -98,10 +98,6 @@ module Hemingway
|
|
98
98
|
end
|
99
99
|
|
100
100
|
module Item0
|
101
|
-
def whitespace1
|
102
|
-
elements[1]
|
103
|
-
end
|
104
|
-
|
105
101
|
def label
|
106
102
|
elements[2]
|
107
103
|
end
|
@@ -109,10 +105,6 @@ module Hemingway
|
|
109
105
|
def sequence
|
110
106
|
elements[3]
|
111
107
|
end
|
112
|
-
|
113
|
-
def whitespace2
|
114
|
-
elements[4]
|
115
|
-
end
|
116
108
|
end
|
117
109
|
|
118
110
|
def _nt_item
|
@@ -136,32 +128,45 @@ module Hemingway
|
|
136
128
|
end
|
137
129
|
s0 << r1
|
138
130
|
if r1
|
139
|
-
|
131
|
+
r3 = _nt_whitespace
|
132
|
+
if r3
|
133
|
+
r2 = r3
|
134
|
+
else
|
135
|
+
r2 = instantiate_node(SyntaxNode,input, index...index)
|
136
|
+
end
|
140
137
|
s0 << r2
|
141
138
|
if r2
|
142
|
-
|
143
|
-
if
|
144
|
-
|
139
|
+
r5 = _nt_label
|
140
|
+
if r5
|
141
|
+
r4 = r5
|
145
142
|
else
|
146
|
-
|
143
|
+
r4 = instantiate_node(SyntaxNode,input, index...index)
|
147
144
|
end
|
148
|
-
s0 <<
|
149
|
-
if
|
150
|
-
|
145
|
+
s0 << r4
|
146
|
+
if r4
|
147
|
+
s6, i6 = [], index
|
151
148
|
loop do
|
152
|
-
|
153
|
-
|
154
|
-
|
149
|
+
i7 = index
|
150
|
+
r8 = _nt_content
|
151
|
+
if r8
|
152
|
+
r7 = r8
|
153
|
+
else
|
154
|
+
r9 = _nt_whitespace
|
155
|
+
if r9
|
156
|
+
r7 = r9
|
157
|
+
else
|
158
|
+
@index = i7
|
159
|
+
r7 = nil
|
160
|
+
end
|
161
|
+
end
|
162
|
+
if r7
|
163
|
+
s6 << r7
|
155
164
|
else
|
156
165
|
break
|
157
166
|
end
|
158
167
|
end
|
159
|
-
|
160
|
-
s0 <<
|
161
|
-
if r5
|
162
|
-
r7 = _nt_whitespace
|
163
|
-
s0 << r7
|
164
|
-
end
|
168
|
+
r6 = instantiate_node(SyntaxNode,input, i6...index, s6)
|
169
|
+
s0 << r6
|
165
170
|
end
|
166
171
|
end
|
167
172
|
end
|
data/lib/hemingway/build.rb
CHANGED
@@ -5,13 +5,15 @@ module Hemingway
|
|
5
5
|
class Build
|
6
6
|
def self.tag(html_attribute, html_content=nil, options={})
|
7
7
|
String.new.tap do |t|
|
8
|
-
t << "
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
t << "<#{html_attribute}"
|
9
|
+
|
10
|
+
options.select { |k, v| k != :close_tag }.each do |k, v|
|
11
|
+
t << " #{k}='#{v}'"
|
12
|
+
end
|
13
|
+
|
12
14
|
t << ">"
|
13
15
|
t << html_content if html_content
|
14
|
-
t << "
|
16
|
+
t << "</#{html_attribute}>" unless options[:close_tag] == false
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
@@ -19,6 +21,11 @@ module Hemingway
|
|
19
21
|
latex_sym_to_html[symbol]
|
20
22
|
end
|
21
23
|
|
24
|
+
# return the accented character or just the character if no match.
|
25
|
+
def self.accent(character, accent)
|
26
|
+
character_accent_to_html[character] and character_accent_to_html[character][accent] or character
|
27
|
+
end
|
28
|
+
|
22
29
|
private
|
23
30
|
def self.latex_sym_to_html
|
24
31
|
{
|
@@ -56,7 +63,98 @@ module Hemingway
|
|
56
63
|
"\\phi" => "φ",
|
57
64
|
"\\chi" => "χ",
|
58
65
|
"\\psi" => "ψ",
|
59
|
-
"\\omega" => "ω"
|
66
|
+
"\\omega" => "ω",
|
67
|
+
"\\rightarrow" => "⇒"
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.character_accent_to_html
|
72
|
+
{
|
73
|
+
"A" => {
|
74
|
+
"`" => "À",
|
75
|
+
"'" => "Á",
|
76
|
+
"c" => "Â",
|
77
|
+
"~" => "Ã",
|
78
|
+
'"' => "Ä",
|
79
|
+
"r" => "Å"
|
80
|
+
},
|
81
|
+
"C" => {
|
82
|
+
"c" => "Ç"
|
83
|
+
},
|
84
|
+
"E" => {
|
85
|
+
"`" => "È",
|
86
|
+
"'" => "É",
|
87
|
+
"c" => "Ê",
|
88
|
+
'"' => "Ë"
|
89
|
+
},
|
90
|
+
"I" => {
|
91
|
+
"`" => "Ì",
|
92
|
+
"'" => "Í",
|
93
|
+
"c" => "Î",
|
94
|
+
'"' => "Ï"
|
95
|
+
},
|
96
|
+
"N" => {
|
97
|
+
"~" => "Ñ"
|
98
|
+
},
|
99
|
+
"O" => {
|
100
|
+
"`" => "Ò",
|
101
|
+
"'" => "Ó",
|
102
|
+
"c" => "Ô",
|
103
|
+
"~" => "Õ",
|
104
|
+
'"' => "Ö"
|
105
|
+
},
|
106
|
+
"U" => {
|
107
|
+
"`" => "Ù",
|
108
|
+
"'" => "Ú",
|
109
|
+
"c" => "Û",
|
110
|
+
'"' => "Ü"
|
111
|
+
},
|
112
|
+
"Y" => {
|
113
|
+
"'" => "Ý"
|
114
|
+
},
|
115
|
+
"a" => {
|
116
|
+
"`" => "à",
|
117
|
+
"'" => "á",
|
118
|
+
"c" => "â",
|
119
|
+
"~" => "ã",
|
120
|
+
'"' => "ä",
|
121
|
+
"r" => "å"
|
122
|
+
},
|
123
|
+
"c" => {
|
124
|
+
"c" => "ç"
|
125
|
+
},
|
126
|
+
"e" => {
|
127
|
+
"`" => "è",
|
128
|
+
"'" => "é",
|
129
|
+
"c" => "ê",
|
130
|
+
'"' => "ë"
|
131
|
+
},
|
132
|
+
"i" => {
|
133
|
+
"`" => "ì",
|
134
|
+
"'" => "í",
|
135
|
+
"c" => "î",
|
136
|
+
'"' => "ï"
|
137
|
+
},
|
138
|
+
"n" => {
|
139
|
+
"~" => "ñ"
|
140
|
+
},
|
141
|
+
"o" => {
|
142
|
+
"`" => "ò",
|
143
|
+
"'" => "ó",
|
144
|
+
"c" => "ô",
|
145
|
+
"~" => "õ",
|
146
|
+
'"' => "ö"
|
147
|
+
},
|
148
|
+
"u" => {
|
149
|
+
"`" => "ù",
|
150
|
+
"'" => "ú",
|
151
|
+
"c" => "û",
|
152
|
+
'"' => "ü"
|
153
|
+
},
|
154
|
+
"y" => {
|
155
|
+
"'" => "ý",
|
156
|
+
'"' => "ÿ"
|
157
|
+
}
|
60
158
|
}
|
61
159
|
end
|
62
160
|
|
data/lib/hemingway/latex.rb
CHANGED
@@ -91,6 +91,12 @@ module Hemingway
|
|
91
91
|
end
|
92
92
|
|
93
93
|
module Paragraph0
|
94
|
+
def newline
|
95
|
+
elements[1]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
module Paragraph1
|
94
100
|
def sequence
|
95
101
|
elements[0]
|
96
102
|
end
|
@@ -123,8 +129,33 @@ module Hemingway
|
|
123
129
|
if r4
|
124
130
|
r2 = r4
|
125
131
|
else
|
126
|
-
|
127
|
-
|
132
|
+
i5, s5 = index, []
|
133
|
+
i6 = index
|
134
|
+
r7 = _nt_eop
|
135
|
+
if r7
|
136
|
+
r6 = nil
|
137
|
+
else
|
138
|
+
@index = i6
|
139
|
+
r6 = instantiate_node(SyntaxNode,input, index...index)
|
140
|
+
end
|
141
|
+
s5 << r6
|
142
|
+
if r6
|
143
|
+
r8 = _nt_newline
|
144
|
+
s5 << r8
|
145
|
+
end
|
146
|
+
if s5.last
|
147
|
+
r5 = instantiate_node(SyntaxNode,input, i5...index, s5)
|
148
|
+
r5.extend(Paragraph0)
|
149
|
+
else
|
150
|
+
@index = i5
|
151
|
+
r5 = nil
|
152
|
+
end
|
153
|
+
if r5
|
154
|
+
r2 = r5
|
155
|
+
else
|
156
|
+
@index = i2
|
157
|
+
r2 = nil
|
158
|
+
end
|
128
159
|
end
|
129
160
|
end
|
130
161
|
if r2
|
@@ -136,12 +167,12 @@ module Hemingway
|
|
136
167
|
r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
|
137
168
|
s0 << r1
|
138
169
|
if r1
|
139
|
-
|
140
|
-
s0 <<
|
170
|
+
r9 = _nt_eop
|
171
|
+
s0 << r9
|
141
172
|
end
|
142
173
|
if s0.last
|
143
174
|
r0 = instantiate_node(ParagraphNode,input, i0...index, s0)
|
144
|
-
r0.extend(
|
175
|
+
r0.extend(Paragraph1)
|
145
176
|
else
|
146
177
|
@index = i0
|
147
178
|
r0 = nil
|
@@ -153,6 +184,12 @@ module Hemingway
|
|
153
184
|
end
|
154
185
|
|
155
186
|
module LastParagraph0
|
187
|
+
def newline
|
188
|
+
elements[1]
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
module LastParagraph1
|
156
193
|
def sequence
|
157
194
|
elements[0]
|
158
195
|
end
|
@@ -182,8 +219,33 @@ module Hemingway
|
|
182
219
|
if r4
|
183
220
|
r2 = r4
|
184
221
|
else
|
185
|
-
|
186
|
-
|
222
|
+
i5, s5 = index, []
|
223
|
+
i6 = index
|
224
|
+
r7 = _nt_eop
|
225
|
+
if r7
|
226
|
+
r6 = nil
|
227
|
+
else
|
228
|
+
@index = i6
|
229
|
+
r6 = instantiate_node(SyntaxNode,input, index...index)
|
230
|
+
end
|
231
|
+
s5 << r6
|
232
|
+
if r6
|
233
|
+
r8 = _nt_newline
|
234
|
+
s5 << r8
|
235
|
+
end
|
236
|
+
if s5.last
|
237
|
+
r5 = instantiate_node(SyntaxNode,input, i5...index, s5)
|
238
|
+
r5.extend(LastParagraph0)
|
239
|
+
else
|
240
|
+
@index = i5
|
241
|
+
r5 = nil
|
242
|
+
end
|
243
|
+
if r5
|
244
|
+
r2 = r5
|
245
|
+
else
|
246
|
+
@index = i2
|
247
|
+
r2 = nil
|
248
|
+
end
|
187
249
|
end
|
188
250
|
end
|
189
251
|
if r2
|
@@ -200,17 +262,17 @@ module Hemingway
|
|
200
262
|
end
|
201
263
|
s0 << r1
|
202
264
|
if r1
|
203
|
-
|
204
|
-
if
|
205
|
-
|
265
|
+
r10 = _nt_eop
|
266
|
+
if r10
|
267
|
+
r9 = r10
|
206
268
|
else
|
207
|
-
|
269
|
+
r9 = instantiate_node(SyntaxNode,input, index...index)
|
208
270
|
end
|
209
|
-
s0 <<
|
271
|
+
s0 << r9
|
210
272
|
end
|
211
273
|
if s0.last
|
212
274
|
r0 = instantiate_node(ParagraphNode,input, i0...index, s0)
|
213
|
-
r0.extend(
|
275
|
+
r0.extend(LastParagraph1)
|
214
276
|
else
|
215
277
|
@index = i0
|
216
278
|
r0 = nil
|
@@ -304,7 +366,12 @@ module Hemingway
|
|
304
366
|
break
|
305
367
|
end
|
306
368
|
end
|
307
|
-
|
369
|
+
if s0.empty?
|
370
|
+
@index = i0
|
371
|
+
r0 = nil
|
372
|
+
else
|
373
|
+
r0 = instantiate_node(WhitespaceNode,input, i0...index, s0)
|
374
|
+
end
|
308
375
|
|
309
376
|
node_cache[:whitespace][start_index] = r0
|
310
377
|
|
@@ -388,7 +455,7 @@ module Hemingway
|
|
388
455
|
end
|
389
456
|
|
390
457
|
if has_terminal?("\n", false, index)
|
391
|
-
r0 = instantiate_node(
|
458
|
+
r0 = instantiate_node(NewlineNode,input, index...(index + 1))
|
392
459
|
@index += 1
|
393
460
|
else
|
394
461
|
terminal_parse_failure("\n")
|