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
data/lib/hemingway/latex.treetop
CHANGED
@@ -34,7 +34,7 @@ module Hemingway
|
|
34
34
|
# have to dig past to get at the list. elements[0] refers to the array of
|
35
35
|
# tags or texts here.
|
36
36
|
rule paragraph
|
37
|
-
sequence:( content / footnote )* eop <ParagraphNode>
|
37
|
+
sequence:( content / footnote / !eop newline )* eop <ParagraphNode>
|
38
38
|
end
|
39
39
|
|
40
40
|
# The last paragraph in an entry need not end with \n\n. Because this
|
@@ -43,7 +43,7 @@ module Hemingway
|
|
43
43
|
# to consume. The only time I can imagine this happening would be at
|
44
44
|
# the end of an entry.
|
45
45
|
rule last_paragraph
|
46
|
-
sequence:( content / footnote )+ eop? <ParagraphNode>
|
46
|
+
sequence:( content / footnote / !eop newline )+ eop? <ParagraphNode>
|
47
47
|
end
|
48
48
|
|
49
49
|
# Example: \tag{text} or just text or $\Delta$
|
@@ -58,7 +58,7 @@ module Hemingway
|
|
58
58
|
|
59
59
|
# Treetop does not separate lexing from parsing. Must consume all input.
|
60
60
|
rule whitespace
|
61
|
-
( " " / newline )
|
61
|
+
( " " / newline )+ <WhitespaceNode>
|
62
62
|
end
|
63
63
|
|
64
64
|
rule spaces
|
@@ -74,7 +74,7 @@ module Hemingway
|
|
74
74
|
end
|
75
75
|
|
76
76
|
rule newline
|
77
|
-
"\n"
|
77
|
+
"\n" <NewlineNode>
|
78
78
|
end
|
79
79
|
|
80
80
|
end
|
@@ -22,6 +22,8 @@ module Hemingway
|
|
22
22
|
if element.respond_to?(:footnote_html)
|
23
23
|
footnote_seed += 1
|
24
24
|
element.html(footnote_seed)
|
25
|
+
elsif element.respond_to?(:newline)
|
26
|
+
element.newline.html
|
25
27
|
else
|
26
28
|
element.html
|
27
29
|
end
|
@@ -40,6 +42,20 @@ module Hemingway
|
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module NewlineNode
|
48
|
+
def html
|
49
|
+
end
|
50
|
+
|
51
|
+
def visible_html
|
52
|
+
"\n"
|
53
|
+
end
|
54
|
+
end
|
43
55
|
|
56
|
+
module WhitespaceNode
|
57
|
+
def html
|
58
|
+
end
|
44
59
|
end
|
60
|
+
|
45
61
|
end
|
data/lib/hemingway/math/math.rb
CHANGED
@@ -16,7 +16,7 @@ module Hemingway
|
|
16
16
|
elements[0]
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def content
|
20
20
|
elements[1]
|
21
21
|
end
|
22
22
|
|
@@ -40,11 +40,23 @@ module Hemingway
|
|
40
40
|
r1 = _nt_math_start
|
41
41
|
s0 << r1
|
42
42
|
if r1
|
43
|
-
|
43
|
+
i2 = index
|
44
|
+
r3 = _nt_symbol
|
45
|
+
if r3
|
46
|
+
r2 = r3
|
47
|
+
else
|
48
|
+
r4 = _nt_exponent
|
49
|
+
if r4
|
50
|
+
r2 = r4
|
51
|
+
else
|
52
|
+
@index = i2
|
53
|
+
r2 = nil
|
54
|
+
end
|
55
|
+
end
|
44
56
|
s0 << r2
|
45
57
|
if r2
|
46
|
-
|
47
|
-
s0 <<
|
58
|
+
r5 = _nt_math_end
|
59
|
+
s0 << r5
|
48
60
|
end
|
49
61
|
end
|
50
62
|
if s0.last
|
@@ -78,6 +90,78 @@ module Hemingway
|
|
78
90
|
r0
|
79
91
|
end
|
80
92
|
|
93
|
+
module Exponent0
|
94
|
+
def value
|
95
|
+
elements[1]
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
def _nt_exponent
|
101
|
+
start_index = index
|
102
|
+
if node_cache[:exponent].has_key?(index)
|
103
|
+
cached = node_cache[:exponent][index]
|
104
|
+
if cached
|
105
|
+
cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
|
106
|
+
@index = cached.interval.end
|
107
|
+
end
|
108
|
+
return cached
|
109
|
+
end
|
110
|
+
|
111
|
+
i0, s0 = index, []
|
112
|
+
if has_terminal?("^{", false, index)
|
113
|
+
r1 = instantiate_node(SyntaxNode,input, index...(index + 2))
|
114
|
+
@index += 2
|
115
|
+
else
|
116
|
+
terminal_parse_failure("^{")
|
117
|
+
r1 = nil
|
118
|
+
end
|
119
|
+
s0 << r1
|
120
|
+
if r1
|
121
|
+
i2 = index
|
122
|
+
if has_terminal?("\\circ", false, index)
|
123
|
+
r3 = instantiate_node(SyntaxNode,input, index...(index + 5))
|
124
|
+
@index += 5
|
125
|
+
else
|
126
|
+
terminal_parse_failure("\\circ")
|
127
|
+
r3 = nil
|
128
|
+
end
|
129
|
+
if r3
|
130
|
+
r2 = r3
|
131
|
+
else
|
132
|
+
r4 = _nt_text
|
133
|
+
if r4
|
134
|
+
r2 = r4
|
135
|
+
else
|
136
|
+
@index = i2
|
137
|
+
r2 = nil
|
138
|
+
end
|
139
|
+
end
|
140
|
+
s0 << r2
|
141
|
+
if r2
|
142
|
+
if has_terminal?("}", false, index)
|
143
|
+
r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
144
|
+
@index += 1
|
145
|
+
else
|
146
|
+
terminal_parse_failure("}")
|
147
|
+
r5 = nil
|
148
|
+
end
|
149
|
+
s0 << r5
|
150
|
+
end
|
151
|
+
end
|
152
|
+
if s0.last
|
153
|
+
r0 = instantiate_node(ExponentNode,input, i0...index, s0)
|
154
|
+
r0.extend(Exponent0)
|
155
|
+
else
|
156
|
+
@index = i0
|
157
|
+
r0 = nil
|
158
|
+
end
|
159
|
+
|
160
|
+
node_cache[:exponent][start_index] = r0
|
161
|
+
|
162
|
+
r0
|
163
|
+
end
|
164
|
+
|
81
165
|
def _nt_math_start
|
82
166
|
start_index = index
|
83
167
|
if node_cache[:math_start].has_key?(index)
|
@@ -5,7 +5,7 @@ module Hemingway
|
|
5
5
|
|
6
6
|
# Example: $ \Pi = 3.14159 $
|
7
7
|
rule math
|
8
|
-
math_start symbol math_end <MathNode>
|
8
|
+
math_start content:( symbol / exponent ) math_end <MathNode>
|
9
9
|
end
|
10
10
|
|
11
11
|
# Example : \Pi
|
@@ -17,6 +17,10 @@ module Hemingway
|
|
17
17
|
math_symbol
|
18
18
|
end
|
19
19
|
|
20
|
+
rule exponent
|
21
|
+
"^{" value:( "\\circ" / text ) "}" <ExponentNode>
|
22
|
+
end
|
23
|
+
|
20
24
|
rule math_start
|
21
25
|
"$"
|
22
26
|
end
|
@@ -1,7 +1,20 @@
|
|
1
1
|
module Hemingway
|
2
2
|
module MathNode
|
3
3
|
def html
|
4
|
-
|
4
|
+
content.html
|
5
5
|
end
|
6
6
|
end
|
7
|
+
|
8
|
+
module ExponentNode
|
9
|
+
def html
|
10
|
+
# Pretty bad hack, but I figure it ain't a big deal until I want to
|
11
|
+
# support more rebust syntax.
|
12
|
+
if value.text_value == "\\circ"
|
13
|
+
"°"
|
14
|
+
else
|
15
|
+
Build.tag("sup", value.html)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
7
20
|
end
|
@@ -107,8 +107,19 @@ module Hemingway
|
|
107
107
|
if r9
|
108
108
|
r2 = r9
|
109
109
|
else
|
110
|
-
|
111
|
-
|
110
|
+
if has_terminal?("textbackslash{}", false, index)
|
111
|
+
r10 = instantiate_node(SyntaxNode,input, index...(index + 15))
|
112
|
+
@index += 15
|
113
|
+
else
|
114
|
+
terminal_parse_failure("textbackslash{}")
|
115
|
+
r10 = nil
|
116
|
+
end
|
117
|
+
if r10
|
118
|
+
r2 = r10
|
119
|
+
else
|
120
|
+
@index = i2
|
121
|
+
r2 = nil
|
122
|
+
end
|
112
123
|
end
|
113
124
|
end
|
114
125
|
end
|
@@ -6,7 +6,7 @@ module Hemingway
|
|
6
6
|
# Special characters that need to be escaped in Latex.
|
7
7
|
# Example: War \& Peace
|
8
8
|
rule special
|
9
|
-
escape character:( "#" / "$" / "%" / "&" / "_" / "{" / "}" ) <SpecialNode>
|
9
|
+
escape character:( "#" / "$" / "%" / "&" / "_" / "{" / "}" / "textbackslash{}" ) <SpecialNode>
|
10
10
|
end
|
11
11
|
|
12
12
|
rule escape
|
@@ -408,8 +408,20 @@ module Hemingway
|
|
408
408
|
r0 = r35
|
409
409
|
r0.extend(SymbolNode)
|
410
410
|
else
|
411
|
-
|
412
|
-
|
411
|
+
if has_terminal?("\\rightarrow", false, index)
|
412
|
+
r36 = instantiate_node(SyntaxNode,input, index...(index + 11))
|
413
|
+
@index += 11
|
414
|
+
else
|
415
|
+
terminal_parse_failure("\\rightarrow")
|
416
|
+
r36 = nil
|
417
|
+
end
|
418
|
+
if r36
|
419
|
+
r0 = r36
|
420
|
+
r0.extend(SymbolNode)
|
421
|
+
else
|
422
|
+
@index = i0
|
423
|
+
r0 = nil
|
424
|
+
end
|
413
425
|
end
|
414
426
|
end
|
415
427
|
end
|
data/lib/hemingway/tag/tag.rb
CHANGED
@@ -75,6 +75,21 @@ module Hemingway
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
+
module Tag5
|
79
|
+
def tag_start
|
80
|
+
elements[0]
|
81
|
+
end
|
82
|
+
|
83
|
+
def accent
|
84
|
+
elements[1]
|
85
|
+
end
|
86
|
+
|
87
|
+
def character
|
88
|
+
elements[3]
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
78
93
|
def _nt_tag
|
79
94
|
start_index = index
|
80
95
|
if node_cache[:tag].has_key?(index)
|
@@ -249,8 +264,56 @@ module Hemingway
|
|
249
264
|
if r23
|
250
265
|
r0 = r23
|
251
266
|
else
|
252
|
-
|
253
|
-
|
267
|
+
i26, s26 = index, []
|
268
|
+
r27 = _nt_tag_start
|
269
|
+
s26 << r27
|
270
|
+
if r27
|
271
|
+
r28 = _nt_accent
|
272
|
+
s26 << r28
|
273
|
+
if r28
|
274
|
+
if has_terminal?("{", false, index)
|
275
|
+
r29 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
276
|
+
@index += 1
|
277
|
+
else
|
278
|
+
terminal_parse_failure("{")
|
279
|
+
r29 = nil
|
280
|
+
end
|
281
|
+
s26 << r29
|
282
|
+
if r29
|
283
|
+
if index < input_length
|
284
|
+
r30 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
285
|
+
@index += 1
|
286
|
+
else
|
287
|
+
terminal_parse_failure("any character")
|
288
|
+
r30 = nil
|
289
|
+
end
|
290
|
+
s26 << r30
|
291
|
+
if r30
|
292
|
+
if has_terminal?("}", false, index)
|
293
|
+
r31 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
294
|
+
@index += 1
|
295
|
+
else
|
296
|
+
terminal_parse_failure("}")
|
297
|
+
r31 = nil
|
298
|
+
end
|
299
|
+
s26 << r31
|
300
|
+
end
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
if s26.last
|
305
|
+
r26 = instantiate_node(AccentNode,input, i26...index, s26)
|
306
|
+
r26.extend(Tag5)
|
307
|
+
else
|
308
|
+
@index = i26
|
309
|
+
r26 = nil
|
310
|
+
end
|
311
|
+
if r26
|
312
|
+
r0 = r26
|
313
|
+
else
|
314
|
+
@index = i0
|
315
|
+
r0 = nil
|
316
|
+
end
|
254
317
|
end
|
255
318
|
end
|
256
319
|
end
|
@@ -481,6 +544,103 @@ module Hemingway
|
|
481
544
|
r0
|
482
545
|
end
|
483
546
|
|
547
|
+
def _nt_accent
|
548
|
+
start_index = index
|
549
|
+
if node_cache[:accent].has_key?(index)
|
550
|
+
cached = node_cache[:accent][index]
|
551
|
+
if cached
|
552
|
+
cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
|
553
|
+
@index = cached.interval.end
|
554
|
+
end
|
555
|
+
return cached
|
556
|
+
end
|
557
|
+
|
558
|
+
i0 = index
|
559
|
+
if has_terminal?('`', false, index)
|
560
|
+
r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
561
|
+
@index += 1
|
562
|
+
else
|
563
|
+
terminal_parse_failure('`')
|
564
|
+
r1 = nil
|
565
|
+
end
|
566
|
+
if r1
|
567
|
+
r0 = r1
|
568
|
+
else
|
569
|
+
if has_terminal?("'", false, index)
|
570
|
+
r2 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
571
|
+
@index += 1
|
572
|
+
else
|
573
|
+
terminal_parse_failure("'")
|
574
|
+
r2 = nil
|
575
|
+
end
|
576
|
+
if r2
|
577
|
+
r0 = r2
|
578
|
+
else
|
579
|
+
if has_terminal?('^', false, index)
|
580
|
+
r3 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
581
|
+
@index += 1
|
582
|
+
else
|
583
|
+
terminal_parse_failure('^')
|
584
|
+
r3 = nil
|
585
|
+
end
|
586
|
+
if r3
|
587
|
+
r0 = r3
|
588
|
+
else
|
589
|
+
if has_terminal?('"', false, index)
|
590
|
+
r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
591
|
+
@index += 1
|
592
|
+
else
|
593
|
+
terminal_parse_failure('"')
|
594
|
+
r4 = nil
|
595
|
+
end
|
596
|
+
if r4
|
597
|
+
r0 = r4
|
598
|
+
else
|
599
|
+
if has_terminal?("c", false, index)
|
600
|
+
r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
601
|
+
@index += 1
|
602
|
+
else
|
603
|
+
terminal_parse_failure("c")
|
604
|
+
r5 = nil
|
605
|
+
end
|
606
|
+
if r5
|
607
|
+
r0 = r5
|
608
|
+
else
|
609
|
+
if has_terminal?("~", false, index)
|
610
|
+
r6 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
611
|
+
@index += 1
|
612
|
+
else
|
613
|
+
terminal_parse_failure("~")
|
614
|
+
r6 = nil
|
615
|
+
end
|
616
|
+
if r6
|
617
|
+
r0 = r6
|
618
|
+
else
|
619
|
+
if has_terminal?("r", false, index)
|
620
|
+
r7 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
621
|
+
@index += 1
|
622
|
+
else
|
623
|
+
terminal_parse_failure("r")
|
624
|
+
r7 = nil
|
625
|
+
end
|
626
|
+
if r7
|
627
|
+
r0 = r7
|
628
|
+
else
|
629
|
+
@index = i0
|
630
|
+
r0 = nil
|
631
|
+
end
|
632
|
+
end
|
633
|
+
end
|
634
|
+
end
|
635
|
+
end
|
636
|
+
end
|
637
|
+
end
|
638
|
+
|
639
|
+
node_cache[:accent][start_index] = r0
|
640
|
+
|
641
|
+
r0
|
642
|
+
end
|
643
|
+
|
484
644
|
def _nt_tag_start
|
485
645
|
start_index = index
|
486
646
|
if node_cache[:tag_start].has_key?(index)
|