hemingway 0.0.0 → 0.0.2
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 +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +297 -0
- data/Rakefile +1 -0
- data/hemingway.gemspec +23 -0
- data/lib/hemingway/block/block.rb +378 -0
- data/lib/hemingway/block/block.treetop +36 -0
- data/lib/hemingway/block/block_nodes.rb +9 -0
- data/lib/hemingway/block/list/list.rb +412 -0
- data/lib/hemingway/block/list/list.treetop +51 -0
- data/lib/hemingway/block/list/list_nodes.rb +39 -0
- data/lib/hemingway/block/quote/quote.rb +192 -0
- data/lib/hemingway/block/quote/quote.treetop +27 -0
- data/lib/hemingway/block/quote/quote_nodes.rb +25 -0
- data/lib/hemingway/block/verbatim/verbatim.rb +159 -0
- data/lib/hemingway/block/verbatim/verbatim.treetop +21 -0
- data/lib/hemingway/block/verbatim/verbatim_nodes.rb +9 -0
- data/lib/hemingway/build.rb +65 -0
- data/lib/hemingway/footnote/footnote.rb +83 -0
- data/lib/hemingway/footnote/footnote.treetop +11 -0
- data/lib/hemingway/footnote/footnote_nodes.rb +21 -0
- data/lib/hemingway/latex.rb +409 -0
- data/lib/hemingway/latex.treetop +81 -0
- data/lib/hemingway/latex_nodes.rb +45 -0
- data/lib/hemingway/math/math.rb +135 -0
- data/lib/hemingway/math/math.treetop +29 -0
- data/lib/hemingway/math/math_nodes.rb +7 -0
- data/lib/hemingway/special/special.rb +164 -0
- data/lib/hemingway/special/special.treetop +17 -0
- data/lib/hemingway/special/special_nodes.rb +7 -0
- data/lib/hemingway/symbol/symbol.rb +460 -0
- data/lib/hemingway/symbol/symbol.treetop +47 -0
- data/lib/hemingway/symbol/symbol_nodes.rb +7 -0
- data/lib/hemingway/tag/tag.rb +538 -0
- data/lib/hemingway/tag/tag.treetop +63 -0
- data/lib/hemingway/tag/tag_nodes.rb +49 -0
- data/lib/hemingway/text/text.rb +121 -0
- data/lib/hemingway/text/text.treetop +35 -0
- data/lib/hemingway/text/text_nodes.rb +7 -0
- data/lib/hemingway/version.rb +3 -0
- data/lib/hemingway.rb +7 -0
- data/script/build +22 -0
- data/script/test +2 -0
- data/spec/build_spec.rb +65 -0
- data/spec/nodes/block/list_spec.rb +91 -0
- data/spec/nodes/block/quote_spec.rb +31 -0
- data/spec/nodes/block/verbatim_spec.rb +27 -0
- data/spec/nodes/block_spec.rb +21 -0
- data/spec/nodes/footnote_spec.rb +42 -0
- data/spec/nodes/math_spec.rb +31 -0
- data/spec/nodes/special_spec.rb +27 -0
- data/spec/nodes/tag_spec.rb +98 -0
- data/spec/parser_spec.rb +48 -0
- data/spec/spec_helper.rb +5 -0
- 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,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" => "Γ",
|
26
|
+
"\\Delta" => "Δ",
|
27
|
+
"\\Theta" => "Θ",
|
28
|
+
"\\Lambda" => "Λ",
|
29
|
+
"\\Xi" => "Ξ",
|
30
|
+
"\\Pi" => "Π",
|
31
|
+
"\\Sigma" => "Σ",
|
32
|
+
"\\Upsilon" => "Υ",
|
33
|
+
"\\Phi" => "Φ",
|
34
|
+
"\\Psi" => "Ψ",
|
35
|
+
"\\Omega" => "Ω",
|
36
|
+
"\\alpha" => "α",
|
37
|
+
"\\beta" => "β",
|
38
|
+
"\\gamma" => "γ",
|
39
|
+
"\\delta" => "δ",
|
40
|
+
"\\epsilon" => "ε",
|
41
|
+
"\\zeta" => "ζ",
|
42
|
+
"\\eta" => "η",
|
43
|
+
"\\theta" => "θ",
|
44
|
+
"\\iota" => "ι",
|
45
|
+
"\\kappa" => "κ",
|
46
|
+
"\\lambda" => "λ",
|
47
|
+
"\\mu" => "μ",
|
48
|
+
"\\nu" => "ν",
|
49
|
+
"\\xi" => "ξ",
|
50
|
+
"\\pi" => "π",
|
51
|
+
"\\rho" => "ρ",
|
52
|
+
"\\varsigma" => "ς",
|
53
|
+
"\\sigma" => "σ",
|
54
|
+
"\\tau" => "τ",
|
55
|
+
"\\upsilon" => "υ",
|
56
|
+
"\\phi" => "φ",
|
57
|
+
"\\chi" => "χ",
|
58
|
+
"\\psi" => "ψ",
|
59
|
+
"\\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,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
|