nora_mark 0.2beta7 → 0.2beta8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +149 -73
- data/Rakefile +1 -3
- data/bin/nora2html +1 -1
- data/emacs-mode/noramark-mode.el +497 -0
- data/example/koujin.nora +13 -0
- data/example/nora-simple.css +27 -2
- data/example/noramark-reference-ja.nora +446 -64
- data/example/noramark-reference-ja_00001.xhtml +485 -85
- data/lib/nora_mark.rb +5 -67
- data/lib/nora_mark/document.rb +89 -0
- data/lib/nora_mark/extensions.rb +26 -0
- data/lib/nora_mark/html/context.rb +2 -3
- data/lib/nora_mark/html/default_transformer.rb +151 -0
- data/lib/nora_mark/html/generator.rb +25 -149
- data/lib/nora_mark/html/paragraph_writer.rb +4 -4
- data/lib/nora_mark/html/tag_writer.rb +13 -10
- data/lib/nora_mark/node.rb +196 -72
- data/lib/nora_mark/node_set.rb +13 -0
- data/lib/nora_mark/node_util.rb +29 -16
- data/lib/nora_mark/parser.kpeg +210 -74
- data/lib/nora_mark/parser.kpeg.rb +1353 -401
- data/lib/nora_mark/parser.rb +1 -1
- data/lib/nora_mark/transformer.rb +35 -10
- data/lib/nora_mark/version.rb +1 -1
- data/nora_mark.gemspec +3 -2
- data/spec/node_spec.rb +280 -0
- data/spec/nora_mark_spec.rb +1189 -1025
- data/spec/spec_helper.rb +2 -0
- data/spec/transformer_spec.rb +37 -0
- metadata +27 -6
- data/lib/nora_mark/html/writer_selector.rb +0 -24
- data/lib/nora_mark/node_builder.rb +0 -18
@@ -36,9 +36,9 @@ module NoraMark
|
|
36
36
|
ParagraphGroup =>
|
37
37
|
TagWriter.create("p", @generator,
|
38
38
|
node_preprocessor: proc do |node|
|
39
|
-
node.children = node.children.inject([]) do |memo,
|
40
|
-
memo <<
|
41
|
-
memo <<
|
39
|
+
node.children = node.children.inject([]) do |memo, n|
|
40
|
+
memo << inline('br', body_empty: true, line_no: memo.last.line_no) if !memo.last.nil? && memo.last.kind_of?(Paragraph) && n.kind_of?(Paragraph)
|
41
|
+
memo << n
|
42
42
|
end
|
43
43
|
node
|
44
44
|
end
|
@@ -48,7 +48,7 @@ module NoraMark
|
|
48
48
|
end
|
49
49
|
def write(node)
|
50
50
|
writer_set = @writer_set[@context.paragraph_style]
|
51
|
-
writer_set = @writer_set[
|
51
|
+
writer_set = @writer_set[:default] if writer_set.nil?
|
52
52
|
writer_set[node.class].write(node)
|
53
53
|
end
|
54
54
|
end
|
@@ -2,6 +2,7 @@ module NoraMark
|
|
2
2
|
module Html
|
3
3
|
class TagWriter
|
4
4
|
include Util
|
5
|
+
include NodeUtil
|
5
6
|
attr_accessor :trailer, :node_preprocessors, :write_body_preprocessors
|
6
7
|
|
7
8
|
def self.create(tag_name, generator, node_preprocessor: nil, write_body_preprocessor: nil, trailer: "\n", chop_last_space: false)
|
@@ -27,7 +28,11 @@ module NoraMark
|
|
27
28
|
return '' if attrs.nil?
|
28
29
|
attrs.map do
|
29
30
|
|name, vals|
|
30
|
-
if vals.nil?
|
31
|
+
if vals.nil?
|
32
|
+
''
|
33
|
+
elsif !vals.is_a? Array
|
34
|
+
" #{name}='#{name}'"
|
35
|
+
elsif vals.size == 0
|
31
36
|
''
|
32
37
|
else
|
33
38
|
" #{name}='#{vals.join(' ')}'"
|
@@ -47,10 +52,6 @@ module NoraMark
|
|
47
52
|
(node.classes ||= []) << cls
|
48
53
|
end
|
49
54
|
|
50
|
-
def add_class_if_empty(node, cls)
|
51
|
-
add_class(node, cls) if node.classes.nil? || node.classes.size == 0
|
52
|
-
end
|
53
|
-
|
54
55
|
def tag_start(node)
|
55
56
|
return if node.no_tag
|
56
57
|
ids = node.ids || []
|
@@ -77,7 +78,7 @@ module NoraMark
|
|
77
78
|
|
78
79
|
def write(node)
|
79
80
|
@node_preprocessors.each { |x| node = instance_exec node.dup, &x }
|
80
|
-
@context.enable_pgroup, saved_ep = !(
|
81
|
+
@context.enable_pgroup, saved_ep = !(node.params.map(&:text).include?('wo-pgroup') || !@context.enable_pgroup), @context.enable_pgroup
|
81
82
|
tag_start node
|
82
83
|
write_body node unless node.body_empty
|
83
84
|
tag_end node unless node.body_empty
|
@@ -90,10 +91,15 @@ module NoraMark
|
|
90
91
|
return if instance_exec(node, &x) == :done
|
91
92
|
}
|
92
93
|
write_children node
|
94
|
+
@generator.context.chop_last_space if node.n[:chop_last_space]
|
93
95
|
end
|
94
96
|
|
95
97
|
def write_children(node)
|
96
|
-
|
98
|
+
if node.raw_text?
|
99
|
+
output escape_html(node.content.join "\n")
|
100
|
+
else
|
101
|
+
write_nodeset(node.children)
|
102
|
+
end
|
97
103
|
end
|
98
104
|
|
99
105
|
def write_nodeset(nodeset)
|
@@ -102,9 +108,6 @@ module NoraMark
|
|
102
108
|
@generator.context.chop_last_space if (@param[:chop_last_space])
|
103
109
|
end
|
104
110
|
|
105
|
-
def children_not_empty(node)
|
106
|
-
!node.children.nil? && node.children.size > 0 && node.children.reject { |x| x.nil? }.size > 0
|
107
|
-
end
|
108
111
|
end
|
109
112
|
end
|
110
113
|
end
|
data/lib/nora_mark/node.rb
CHANGED
@@ -3,25 +3,36 @@ require 'yaml'
|
|
3
3
|
module NoraMark
|
4
4
|
class Node
|
5
5
|
include Enumerable
|
6
|
-
attr_accessor :content, :ids, :classes, :no_tag, :attrs, :name, :body_empty, :line_no
|
6
|
+
attr_accessor :raw_content, :content, :ids, :classes, :no_tag, :attrs, :name, :body_empty, :line_no, :raw_text
|
7
7
|
attr_accessor :parent, :first_child, :last_child, :prev, :next, :holders
|
8
8
|
|
9
|
-
def
|
10
|
-
@
|
9
|
+
def raw_text?
|
10
|
+
@raw_text
|
11
|
+
end
|
12
|
+
|
13
|
+
def named_params=(named_params)
|
14
|
+
@named_params = named_params
|
11
15
|
end
|
12
16
|
|
13
|
-
def
|
14
|
-
@
|
17
|
+
def named_params
|
18
|
+
@named_params ||= {}
|
15
19
|
end
|
16
20
|
|
17
|
-
def
|
18
|
-
@
|
21
|
+
def params=(params)
|
22
|
+
@params = params.map { |param| NodeSet.new param }
|
19
23
|
end
|
20
24
|
|
21
|
-
def
|
22
|
-
@
|
25
|
+
def params
|
26
|
+
@params
|
23
27
|
end
|
24
28
|
|
29
|
+
alias p params
|
30
|
+
alias n named_params
|
31
|
+
|
32
|
+
def add_attr attr
|
33
|
+
(@attrs ||= {}).merge! attr
|
34
|
+
end
|
35
|
+
|
25
36
|
def each
|
26
37
|
node = self
|
27
38
|
while !node.nil?
|
@@ -31,10 +42,13 @@ module NoraMark
|
|
31
42
|
end
|
32
43
|
|
33
44
|
def match?(selector)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
45
|
+
_match? build_selector(selector)
|
46
|
+
end
|
47
|
+
|
48
|
+
def _match?(raw_selector)
|
49
|
+
raw_selector.inject(true) {
|
50
|
+
|result, s|
|
51
|
+
result && s.call(self)
|
38
52
|
}
|
39
53
|
end
|
40
54
|
|
@@ -56,52 +70,80 @@ module NoraMark
|
|
56
70
|
end
|
57
71
|
|
58
72
|
def build_selector(selector)
|
59
|
-
|
60
|
-
|
73
|
+
original_selector = selector
|
74
|
+
case selector
|
75
|
+
when String
|
76
|
+
selector = { name: original_selector }
|
77
|
+
when Regexp
|
78
|
+
selector = { proc: proc { |node| original_selector =~ node.name }}
|
61
79
|
end
|
62
80
|
selector.map { |k,v| modify_selector(k,v) }
|
63
81
|
end
|
64
82
|
|
65
83
|
def ancestors(selector = {})
|
66
84
|
result = []
|
85
|
+
raw_selector = build_selector selector
|
67
86
|
node = parent
|
68
87
|
while !node.nil?
|
69
|
-
result << node if node.
|
88
|
+
result << node if node._match? raw_selector
|
70
89
|
node = node.parent
|
71
90
|
end
|
72
91
|
result
|
73
92
|
end
|
74
93
|
|
94
|
+
def children_empty?
|
95
|
+
children.nil? || children.size == 0 || children.reject { |x| x.nil? }.size == 0
|
96
|
+
end
|
97
|
+
|
75
98
|
def reparent
|
76
|
-
|
99
|
+
@params ||= []
|
100
|
+
@params = @params.map do
|
101
|
+
|node_array|
|
102
|
+
node_array.inject(nil) do
|
103
|
+
|prev, child_node|
|
104
|
+
child_node.prev = prev
|
105
|
+
prev.next = child_node if !prev.nil?
|
106
|
+
child_node.parent = self
|
107
|
+
child_node.reparent
|
108
|
+
child_node
|
109
|
+
end
|
110
|
+
NodeSet.new node_array
|
111
|
+
end
|
77
112
|
|
78
|
-
@
|
113
|
+
return if @raw_content.nil? || raw_text
|
79
114
|
|
80
|
-
@
|
81
|
-
@
|
82
|
-
@
|
115
|
+
@raw_content.each { |node| node.remove }
|
116
|
+
@first_child = @raw_content.first
|
117
|
+
@last_child = @raw_content.last
|
118
|
+
@raw_content.inject(nil) do |prev, child_node|
|
83
119
|
child_node.prev = prev
|
84
120
|
prev.next = child_node if !prev.nil?
|
85
121
|
child_node.parent = self
|
86
122
|
child_node.reparent
|
87
123
|
child_node
|
88
124
|
end
|
89
|
-
@
|
125
|
+
@raw_content = nil
|
90
126
|
@children = nil
|
127
|
+
rebuild_children
|
128
|
+
end
|
129
|
+
|
130
|
+
def rebuild_children
|
131
|
+
@children = @first_child.nil? ? [] : NodeSet.new(@first_child.collect { |node| node })
|
132
|
+
@children
|
91
133
|
end
|
92
134
|
|
93
135
|
def children
|
94
136
|
return [] if @first_child.nil?
|
95
|
-
|
137
|
+
@children ||= rebuild_children
|
96
138
|
end
|
97
139
|
|
98
140
|
def children=(x)
|
99
|
-
@
|
141
|
+
@raw_content = x.to_ary
|
100
142
|
reparent
|
101
143
|
end
|
102
144
|
|
103
145
|
def children_replaced
|
104
|
-
|
146
|
+
rebuild_children
|
105
147
|
end
|
106
148
|
|
107
149
|
def unlink
|
@@ -117,30 +159,80 @@ module NoraMark
|
|
117
159
|
@prev.next = @next unless @prev.nil?
|
118
160
|
@parent.children_replaced unless @parent.nil?
|
119
161
|
unlink
|
162
|
+
self
|
120
163
|
end
|
121
|
-
|
122
|
-
def replace(node)
|
123
|
-
node.parent = @parent
|
124
|
-
@parent.first_child = node if (@parent.first_child == self)
|
125
|
-
@parent.last_child = node if (@parent.last_child == self)
|
126
164
|
|
127
|
-
|
165
|
+
def after(node)
|
166
|
+
node.remove
|
167
|
+
node.parent = @parent
|
168
|
+
node.prev = self
|
128
169
|
node.next = @next
|
129
|
-
|
130
|
-
@prev.next = node unless @prev.nil?
|
131
170
|
@next.prev = node unless @next.nil?
|
171
|
+
@next = node
|
172
|
+
if !@parent.nil? && @parent.last_child == self
|
173
|
+
@parent.last_child = node
|
174
|
+
end
|
175
|
+
node.reparent
|
176
|
+
@parent.children_replaced unless @parent.nil?
|
177
|
+
end
|
132
178
|
|
179
|
+
def before(node)
|
180
|
+
node.remove
|
181
|
+
node.parent = @parent
|
182
|
+
node.next = self
|
183
|
+
node.prev = @prev
|
184
|
+
@prev.next = node unless @prev.nil?
|
185
|
+
@prev = node
|
186
|
+
if !@parent.nil? && @parent.first_child == self
|
187
|
+
@parent.first_child = node
|
188
|
+
end
|
133
189
|
node.reparent
|
134
|
-
|
190
|
+
@parent.children_replaced unless @parent.nil?
|
191
|
+
end
|
192
|
+
|
193
|
+
def replace(node)
|
194
|
+
node = [node] if !node.is_a? Array
|
195
|
+
|
196
|
+
first_node = node.shift
|
197
|
+
rest_nodes = node
|
198
|
+
|
199
|
+
first_node.parent = @parent
|
200
|
+
if !@parent.nil?
|
201
|
+
@parent.first_child = first_node if (@parent.first_child == self)
|
202
|
+
@parent.last_child = first_node if (@parent.last_child == self)
|
203
|
+
end
|
204
|
+
|
205
|
+
first_node.prev = @prev
|
206
|
+
first_node.next = @next
|
207
|
+
|
208
|
+
@prev.next = first_node unless @prev.nil?
|
209
|
+
@next.prev = first_node unless @next.nil?
|
135
210
|
|
211
|
+
first_node.reparent
|
212
|
+
first_node.parent.children_replaced unless first_node.parent.nil?
|
136
213
|
unlink
|
214
|
+
rest_nodes.inject(first_node) do
|
215
|
+
|prev, rest_node|
|
216
|
+
prev.after rest_node
|
217
|
+
rest_node
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def wrap(node, method = :prepend)
|
222
|
+
replace(node)
|
223
|
+
if (method == :prepend)
|
224
|
+
node.prepend_child(self)
|
225
|
+
else
|
226
|
+
node.append_child(self)
|
227
|
+
end
|
228
|
+
node
|
137
229
|
end
|
138
230
|
|
139
231
|
def prepend_child(node)
|
140
232
|
node.remove
|
141
233
|
node.reparent
|
142
234
|
if self.children.size == 0
|
143
|
-
@
|
235
|
+
@raw_content = [ node ]
|
144
236
|
reparent
|
145
237
|
else
|
146
238
|
@first_child.prev = node
|
@@ -155,7 +247,7 @@ module NoraMark
|
|
155
247
|
node.remove
|
156
248
|
node.reparent
|
157
249
|
if self.children.size == 0
|
158
|
-
@
|
250
|
+
@raw_content = [ node ]
|
159
251
|
reparent
|
160
252
|
else
|
161
253
|
@last_child.next = node
|
@@ -167,26 +259,53 @@ module NoraMark
|
|
167
259
|
end
|
168
260
|
|
169
261
|
def all_nodes
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
262
|
+
r = []
|
263
|
+
if !@params.nil?
|
264
|
+
@params.each do
|
265
|
+
|node_array|
|
266
|
+
r = node_array[0].inject([]) do
|
267
|
+
|result, node|
|
268
|
+
result << node
|
269
|
+
result + node.all_nodes
|
270
|
+
end
|
271
|
+
end
|
175
272
|
end
|
273
|
+
if !@first_child.nil?
|
274
|
+
r = @first_child.inject(r) do
|
275
|
+
|result, node|
|
276
|
+
result << node
|
277
|
+
result + node.all_nodes
|
278
|
+
end
|
279
|
+
end
|
280
|
+
r
|
281
|
+
end
|
282
|
+
|
283
|
+
def find_node selector
|
284
|
+
_find_node(build_selector selector)
|
285
|
+
end
|
286
|
+
|
287
|
+
def _find_node raw_selector
|
288
|
+
return self if _match? raw_selector
|
289
|
+
return nil unless @first_child
|
290
|
+
return (@first_child.find { |n| n._match? raw_selector } ||
|
291
|
+
@first_child.inject(nil) do
|
292
|
+
|r, n| r or n._find_node raw_selector
|
293
|
+
end)
|
176
294
|
end
|
177
295
|
|
178
296
|
def clone
|
179
|
-
@
|
180
|
-
all_nodes.each { |node| @
|
297
|
+
@raw_content = nil
|
298
|
+
all_nodes.each { |node| node.instance_eval { @raw_content = nil } }
|
181
299
|
Marshal.restore Marshal.dump self
|
182
300
|
end
|
183
301
|
|
184
|
-
def
|
302
|
+
def text
|
185
303
|
children.inject("") do
|
186
304
|
|result, node|
|
187
|
-
result << node.
|
305
|
+
result << node.text
|
188
306
|
end
|
189
307
|
end
|
308
|
+
|
190
309
|
end
|
191
310
|
|
192
311
|
class Root < Node
|
@@ -198,21 +317,10 @@ module NoraMark
|
|
198
317
|
end
|
199
318
|
|
200
319
|
class DLItem < Node
|
201
|
-
def
|
202
|
-
|
203
|
-
@parameters[0].inject(nil) do
|
204
|
-
|prev, child_node|
|
205
|
-
child_node.prev = prev
|
206
|
-
prev.next = child_node if !prev.nil?
|
207
|
-
child_node.parent = self
|
208
|
-
child_node.reparent
|
209
|
-
child_node
|
210
|
-
end
|
211
|
-
end
|
212
|
-
def get_text
|
213
|
-
@parameters[0].inject('') do
|
320
|
+
def text
|
321
|
+
@params[0].inject('') do
|
214
322
|
|result, node|
|
215
|
-
result << node.
|
323
|
+
result << node.text
|
216
324
|
end << super
|
217
325
|
end
|
218
326
|
end
|
@@ -221,14 +329,12 @@ module NoraMark
|
|
221
329
|
def heading_info
|
222
330
|
@name =~ /h([1-6])/
|
223
331
|
return {} if $1.nil?
|
224
|
-
{level: $1.to_i, id: @ids[0], text:
|
332
|
+
{level: $1.to_i, id: @ids[0], text: text }
|
225
333
|
end
|
226
334
|
end
|
227
335
|
|
228
336
|
class HeadedSection < Node
|
229
|
-
|
230
|
-
{level: @level, id: (named_parameters[:heading_id] || [])[0], text: @heading.map(&:get_text).join('')}
|
231
|
-
end
|
337
|
+
attr_accessor :level
|
232
338
|
|
233
339
|
def reparent
|
234
340
|
super
|
@@ -242,40 +348,58 @@ module NoraMark
|
|
242
348
|
end
|
243
349
|
end
|
244
350
|
|
245
|
-
def
|
351
|
+
def text
|
246
352
|
@heading[0].inject('') do
|
247
353
|
|result, node|
|
248
|
-
result << node.
|
354
|
+
result << node.text
|
249
355
|
end << super
|
250
356
|
end
|
251
357
|
|
252
358
|
end
|
253
359
|
class Text < Node
|
254
360
|
def reparent
|
255
|
-
# do nothing
|
361
|
+
# do nothing
|
256
362
|
end
|
257
363
|
|
258
|
-
def
|
364
|
+
def text
|
259
365
|
@content
|
260
366
|
end
|
261
367
|
end
|
262
368
|
|
369
|
+
class CodeInline < Node
|
370
|
+
def raw_text
|
371
|
+
true
|
372
|
+
end
|
373
|
+
|
374
|
+
def raw_text?
|
375
|
+
true
|
376
|
+
end
|
377
|
+
|
378
|
+
def text
|
379
|
+
@content
|
380
|
+
end
|
381
|
+
end
|
263
382
|
|
264
383
|
class PreformattedBlock < Node
|
265
|
-
def
|
266
|
-
|
384
|
+
def raw_text
|
385
|
+
true
|
386
|
+
end
|
387
|
+
|
388
|
+
def raw_text?
|
389
|
+
true
|
267
390
|
end
|
268
|
-
|
391
|
+
|
392
|
+
def text
|
269
393
|
@content.join "\n"
|
270
394
|
end
|
271
395
|
end
|
272
396
|
|
273
397
|
class Frontmatter < Node
|
274
398
|
def reparent
|
275
|
-
# do nothing
|
399
|
+
# do nothing
|
276
400
|
end
|
277
401
|
|
278
|
-
def
|
402
|
+
def text
|
279
403
|
@content.join "\n"
|
280
404
|
end
|
281
405
|
|