nora_mark 0.2beta19 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +5 -5
  2. data/.github/FUNDING.yml +3 -0
  3. data/.github/dependabot.yml +8 -0
  4. data/.github/workflows/test.yml +19 -0
  5. data/.rubocop.yml +3 -0
  6. data/.travis.yml +2 -2
  7. data/CHANGELOG.md +5 -0
  8. data/Gemfile +0 -1
  9. data/README.md +1 -2
  10. data/Rakefile +1 -4
  11. data/lib/nora_mark/document.rb +14 -14
  12. data/lib/nora_mark/extensions.rb +6 -6
  13. data/lib/nora_mark/html/abstract_node_writer.rb +2 -1
  14. data/lib/nora_mark/html/context.rb +16 -14
  15. data/lib/nora_mark/html/default_transformer.rb +36 -38
  16. data/lib/nora_mark/html/frontmatter_writer.rb +2 -2
  17. data/lib/nora_mark/html/generator.rb +19 -22
  18. data/lib/nora_mark/html/pages.rb +13 -14
  19. data/lib/nora_mark/html/paragraph_writer.rb +24 -26
  20. data/lib/nora_mark/html/raw_text_block_writer.rb +2 -1
  21. data/lib/nora_mark/html/tag_writer.rb +14 -13
  22. data/lib/nora_mark/html/util.rb +5 -5
  23. data/lib/nora_mark/node.rb +45 -55
  24. data/lib/nora_mark/node_set.rb +1 -1
  25. data/lib/nora_mark/node_util.rb +7 -8
  26. data/lib/nora_mark/parser.kpeg.rb +16 -16
  27. data/lib/nora_mark/parser.rb +1 -3
  28. data/lib/nora_mark/rake_task.rb +16 -20
  29. data/lib/nora_mark/transformer.rb +13 -15
  30. data/lib/nora_mark/version.rb +1 -1
  31. data/lib/nora_mark.rb +0 -4
  32. data/lib/tilt/nora_mark.rb +1 -1
  33. data/nora_mark.gemspec +6 -5
  34. data/spec/extensions_spec.rb +1 -2
  35. data/spec/fixtures/test-plugins/nora_mark_tester.rb +3 -1
  36. data/spec/node_spec.rb +69 -49
  37. data/spec/nokogiri_test_helper.rb +9 -7
  38. data/spec/nora_mark_spec.rb +437 -498
  39. data/spec/spec_helper.rb +1 -5
  40. data/spec/tilt_spec.rb +1 -1
  41. data/spec/transformer_spec.rb +16 -18
  42. metadata +36 -27
@@ -4,7 +4,7 @@ module NoraMark
4
4
  class Pages
5
5
  attr_reader :created_files
6
6
  attr_accessor :file_basename
7
- def initialize(sequence_format='%05d')
7
+ def initialize(sequence_format = '%05d')
8
8
  @sequence_format = sequence_format || '%05d'
9
9
  @result = []
10
10
  end
@@ -16,10 +16,10 @@ module NoraMark
16
16
  def size
17
17
  @result.size
18
18
  end
19
-
19
+
20
20
  def <<(page)
21
21
  seq = @result.size + 1
22
- @result << { content: page, filename: filename_for_page(seq)}
22
+ @result << { content: page, filename: filename_for_page(seq) }
23
23
  end
24
24
 
25
25
  def [](num)
@@ -32,37 +32,37 @@ module NoraMark
32
32
  end
33
33
 
34
34
  def filename_for_page n
35
- "#{@file_basename}_#{@sequence_format%(n)}.xhtml"
35
+ "#{@file_basename}_#{@sequence_format % (n)}.xhtml"
36
36
  end
37
37
 
38
38
  def set_toc toc_data
39
39
  @toc = toc_data.map do |toc_line|
40
40
  fi = toc_line[:id] ? "##{toc_line[:id]}" : ''
41
- {link: "#{filename_for_page(toc_line[:page])}#{fi}", level: toc_line[:level], text: toc_line[:text]}
41
+ { link: "#{filename_for_page(toc_line[:page])}#{fi}", level: toc_line[:level], text: toc_line[:text] }
42
42
  end
43
43
  end
44
+
44
45
  def toc
45
46
  @toc
46
47
  end
47
-
48
+
48
49
  def write_as_files(directory: nil)
49
50
  dir = directory || Dir.pwd
50
51
  Dir.chdir(dir) do
51
- @result.each do
52
- |page|
53
- File.open(page[:filename], 'w+') do
54
- |file|
52
+ @result.each do |page|
53
+ File.open(page[:filename], 'w+') do |file|
55
54
  file << page[:content]
56
55
  end
57
56
  end
58
57
  end
59
- end
58
+ end
59
+
60
60
  def write_toc_as_file(directory: nil)
61
61
  return if @toc.nil?
62
+
62
63
  dir = directory || Dir.pwd
63
64
  Dir.chdir(dir) do
64
- File.open("#{@file_basename}.yaml", 'w+') do
65
- |file|
65
+ File.open("#{@file_basename}.yaml", 'w+') do |file|
66
66
  file << YAML.dump(@toc)
67
67
  end
68
68
  end
@@ -70,4 +70,3 @@ module NoraMark
70
70
  end
71
71
  end
72
72
  end
73
-
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+
2
3
  module NoraMark
3
4
  module Html
4
5
  class ParagraphWriter
@@ -7,33 +8,31 @@ module NoraMark
7
8
  @context = generator.context
8
9
  @writer_set = { use_paragraph_group: {
9
10
  Paragraph =>
10
- TagWriter.create('p', @generator, chop_last_space: true,
11
- node_preprocessor: proc do |node|
12
- first = node.children[0]
13
- if first.kind_of? Text
14
- first.content.sub!(/^[[:space:]]+/, '')
15
- add_class(node, 'noindent') if first.content =~/^(「|『|()/ # TODO: should be plaggable
16
- end
17
- node
18
- end
19
- ),
11
+ TagWriter.create('p', @generator, chop_last_space: true,
12
+ node_preprocessor: proc do |node|
13
+ first = node.children[0]
14
+ if first.kind_of? Text
15
+ first.content.sub!(/^[[:space:]]+/, '')
16
+ add_class(node, 'noindent') if first.content =~ /^(「|『|()/ # TODO: should be plaggable
17
+ end
18
+ node
19
+ end),
20
20
  ParagraphGroup =>
21
21
  TagWriter.create("div", @generator,
22
22
  node_preprocessor: proc do |node|
23
23
  add_class node, 'pgroup'
24
24
  node.no_tag = true unless @context.enable_pgroup
25
25
  node
26
- end
27
- )
28
- },
29
- default: {
30
- Paragraph =>
31
- TagWriter.create(nil, @generator, chop_last_space: true,
32
- node_preprocessor: proc do |node|
33
- node.no_tag = true
34
- node
35
- end),
36
- ParagraphGroup =>
26
+ end)
27
+ },
28
+ default: {
29
+ Paragraph =>
30
+ TagWriter.create(nil, @generator, chop_last_space: true,
31
+ node_preprocessor: proc do |node|
32
+ node.no_tag = true
33
+ node
34
+ end),
35
+ ParagraphGroup =>
37
36
  TagWriter.create("p", @generator,
38
37
  node_preprocessor: proc do |node|
39
38
  node.children = node.children.inject([]) do |memo, n|
@@ -41,13 +40,12 @@ module NoraMark
41
40
  memo << n
42
41
  end
43
42
  node
44
- end
45
- )
46
- }
47
- }
43
+ end)
44
+ } }
48
45
  end
46
+
49
47
  def write(node)
50
- writer_set = @writer_set[@context.paragraph_style]
48
+ writer_set = @writer_set[@context.paragraph_style]
51
49
  writer_set = @writer_set[:default] if writer_set.nil?
52
50
  writer_set[node.class].write(node)
53
51
  end
@@ -4,9 +4,10 @@ module NoraMark
4
4
  def initialize(generator)
5
5
  @generator = generator
6
6
  end
7
+
7
8
  def write(node)
8
9
  @generator.to_html node.text
9
10
  end
10
11
  end
11
12
  end
12
- end
13
+ end
@@ -9,7 +9,7 @@ module NoraMark
9
9
  instance = TagWriter.new(tag_name, generator, chop_last_space: chop_last_space)
10
10
  instance.node_preprocessors << node_preprocessor unless node_preprocessor.nil?
11
11
  instance.write_body_preprocessors << write_body_preprocessor unless write_body_preprocessor.nil?
12
- instance.trailer = trailer
12
+ instance.trailer = trailer
13
13
  yield instance if block_given?
14
14
  instance
15
15
  end
@@ -26,8 +26,8 @@ module NoraMark
26
26
 
27
27
  def attr_string(attrs)
28
28
  return '' if attrs.nil?
29
- attrs.map do
30
- |name, vals|
29
+
30
+ attrs.map do |name, vals|
31
31
  if vals.nil?
32
32
  ''
33
33
  elsif !vals.is_a? Array
@@ -35,17 +35,17 @@ module NoraMark
35
35
  elsif vals.size == 0
36
36
  ''
37
37
  else
38
- " #{name}='#{escape_html(vals.join(' '))}'"
38
+ " #{name}='#{escape_html(vals.join(' '))}'"
39
39
  end
40
40
  end.join('')
41
41
  end
42
42
 
43
43
  def class_string(cls_array)
44
- attr_string({class: cls_array})
44
+ attr_string({ class: cls_array })
45
45
  end
46
46
 
47
47
  def ids_string(ids_array)
48
- attr_string({id: ids_array})
48
+ attr_string({ id: ids_array })
49
49
  end
50
50
 
51
51
  def add_class(node, cls)
@@ -54,12 +54,13 @@ module NoraMark
54
54
 
55
55
  def tag_start(node)
56
56
  return if node.no_tag
57
+
57
58
  ids = node.ids || []
58
59
  classes = node.classes || []
59
60
  attr = node.attrs || {}
60
- node.n.each { |k,v|
61
+ node.n.each { |k, v|
61
62
  if k.to_s.start_with? 'data-'
62
- attr.merge!({ k => [ v ] })
63
+ attr.merge!({ k => [v] })
63
64
  end
64
65
  }
65
66
  tag_name = @tag_name || node.name
@@ -74,9 +75,10 @@ module NoraMark
74
75
  def output(string)
75
76
  @context << string
76
77
  end
77
-
78
+
78
79
  def tag_end(node)
79
80
  return if node.no_tag
81
+
80
82
  tag_name = @tag_name || node.name
81
83
  @context << "</#{tag_name}>#{@trailer}"
82
84
  end
@@ -91,8 +93,7 @@ module NoraMark
91
93
  end
92
94
 
93
95
  def write_body(node)
94
- @write_body_preprocessors.each {
95
- |x|
96
+ @write_body_preprocessors.each { |x|
96
97
  return if instance_exec(node, &x) == :done
97
98
  }
98
99
  write_children node
@@ -109,10 +110,10 @@ module NoraMark
109
110
 
110
111
  def write_nodeset(nodeset)
111
112
  return if nodeset.nil? || nodeset.size == 0
113
+
112
114
  nodeset.each { |x| @generator.to_html x }
113
- @generator.context.chop_last_space if (@param[:chop_last_space])
115
+ @generator.context.chop_last_space if (@param[:chop_last_space])
114
116
  end
115
-
116
117
  end
117
118
  end
118
119
  end
@@ -2,11 +2,11 @@ module NoraMark
2
2
  module Html
3
3
  module Util
4
4
  def escape_html(string)
5
- string.to_s.gsub("&", "&amp;").
6
- gsub("<", "&lt;").
7
- gsub(">", "&gt;").
8
- gsub('"', "&quot;").
9
- gsub("'", "&#39;")
5
+ string.to_s.gsub("&", "&amp;")
6
+ .gsub("<", "&lt;")
7
+ .gsub(">", "&gt;")
8
+ .gsub('"', "&quot;")
9
+ .gsub("'", "&#39;")
10
10
  end
11
11
  end
12
12
  end
@@ -9,7 +9,7 @@ module NoraMark
9
9
  def raw_text?
10
10
  @raw_text
11
11
  end
12
-
12
+
13
13
  def named_params=(named_params)
14
14
  @named_params = named_params
15
15
  end
@@ -21,7 +21,7 @@ module NoraMark
21
21
  def params=(params)
22
22
  @params = params.map { |param| NodeSet.new param }
23
23
  end
24
-
24
+
25
25
  def params
26
26
  @params
27
27
  end
@@ -32,7 +32,7 @@ module NoraMark
32
32
  def add_attr attr
33
33
  (@attrs ||= {}).merge! attr
34
34
  end
35
-
35
+
36
36
  def each
37
37
  node = self
38
38
  while !node.nil?
@@ -46,22 +46,21 @@ module NoraMark
46
46
  end
47
47
 
48
48
  def _match?(raw_selector)
49
- raw_selector.inject(true) {
50
- |result, s|
49
+ raw_selector.inject(true) { |result, s|
51
50
  result && s.call(self)
52
51
  }
53
52
  end
54
53
 
55
- def modify_selector(k,v)
54
+ def modify_selector(k, v)
56
55
  case k
57
56
  when :type
58
- proc { | node | node.kind_of? NoraMark.const_get(v) }
57
+ proc { |node| node.kind_of? NoraMark.const_get(v) }
59
58
  when :name
60
- proc { | node | node.name == v }
59
+ proc { |node| node.name == v }
61
60
  when :id
62
- proc { | node | (node.ids || []).contain? v }
61
+ proc { |node| (node.ids || []).contain? v }
63
62
  when :class
64
- proc { | node | (node.class || []).contain? v }
63
+ proc { |node| (node.class || []).contain? v }
65
64
  when :proc
66
65
  v
67
66
  else
@@ -75,9 +74,9 @@ module NoraMark
75
74
  when String
76
75
  selector = { name: original_selector }
77
76
  when Regexp
78
- selector = { proc: proc { |node| original_selector =~ node.name }}
77
+ selector = { proc: proc { |node| original_selector =~ node.name } }
79
78
  end
80
- selector.map { |k,v| modify_selector(k,v) }
79
+ selector.map { |k, v| modify_selector(k, v) }
81
80
  end
82
81
 
83
82
  def ancestors(selector = {})
@@ -97,14 +96,12 @@ module NoraMark
97
96
 
98
97
  def reparent
99
98
  @params ||= []
100
- @params = @params.map do
101
- |node_array|
102
- node_array.inject(nil) do
103
- |prev, child_node|
99
+ @params = @params.map do |node_array|
100
+ node_array.inject(nil) do |prev, child_node|
104
101
  child_node.prev = prev
105
102
  prev.next = child_node if !prev.nil?
106
103
  child_node.parent = self
107
- child_node.reparent
104
+ child_node.reparent
108
105
  child_node
109
106
  end
110
107
  NodeSet.new node_array
@@ -119,7 +116,7 @@ module NoraMark
119
116
  child_node.prev = prev
120
117
  prev.next = child_node if !prev.nil?
121
118
  child_node.parent = self
122
- child_node.reparent
119
+ child_node.reparent
123
120
  child_node
124
121
  end
125
122
  @raw_content = nil
@@ -133,6 +130,7 @@ module NoraMark
133
130
 
134
131
  def children
135
132
  return [] if @first_child.nil?
133
+
136
134
  @children ||= rebuild_children
137
135
  end
138
136
 
@@ -152,12 +150,12 @@ module NoraMark
152
150
  end
153
151
 
154
152
  def _remove_internal
155
- @parent.first_child = @next if !@parent.nil? && @parent.first_child == self
156
- @parent.last_child = @prev if !@parent.nil? && @parent.last_child == self
153
+ @parent.first_child = @next if !@parent.nil? && @parent.first_child == self
154
+ @parent.last_child = @prev if !@parent.nil? && @parent.last_child == self
157
155
  @next.prev = @prev unless @next.nil?
158
156
  @prev.next = @next unless @prev.nil?
159
157
  end
160
-
158
+
161
159
  def remove
162
160
  _remove_internal
163
161
  @parent.children_replaced unless @parent.nil?
@@ -203,15 +201,15 @@ module NoraMark
203
201
  node.reparent
204
202
  @parent.children_replaced unless @parent.nil?
205
203
  end
206
-
204
+
207
205
  def replace(node)
208
206
  node = [node] if !node.is_a? Array
209
-
207
+
210
208
  first_node = node.shift
211
209
  rest_nodes = node
212
210
 
213
211
  first_node.parent = @parent
214
- if !@parent.nil?
212
+ if !@parent.nil?
215
213
  @parent.first_child = first_node if (@parent.first_child == self)
216
214
  @parent.last_child = first_node if (@parent.last_child == self)
217
215
  end
@@ -225,8 +223,7 @@ module NoraMark
225
223
  first_node.reparent
226
224
  first_node.parent.children_replaced unless first_node.parent.nil?
227
225
  unlink
228
- rest_nodes.inject(first_node) do
229
- |prev, rest_node|
226
+ rest_nodes.inject(first_node) do |prev, rest_node|
230
227
  prev.after rest_node
231
228
  rest_node
232
229
  end
@@ -246,7 +243,7 @@ module NoraMark
246
243
  node.remove
247
244
  node.reparent
248
245
  if self.children.size == 0
249
- @raw_content = [ node ]
246
+ @raw_content = [node]
250
247
  reparent
251
248
  else
252
249
  @first_child.prev = node
@@ -256,15 +253,15 @@ module NoraMark
256
253
  children_replaced
257
254
  end
258
255
  end
259
-
256
+
260
257
  def append_child(node)
261
258
  node.remove
262
259
  node.reparent
263
260
  if self.children.size == 0
264
- @raw_content = [ node ]
261
+ @raw_content = [node]
265
262
  reparent
266
263
  else
267
- @last_child.next = node
264
+ @last_child.next = node
268
265
  node.prev = @last_child
269
266
  node.parent = self
270
267
  @last_child = node
@@ -275,18 +272,15 @@ module NoraMark
275
272
  def all_nodes
276
273
  r = []
277
274
  if !@params.nil?
278
- @params.each do
279
- |node_array|
280
- r = node_array[0].inject([]) do
281
- |result, node|
275
+ @params.each do |node_array|
276
+ r = node_array[0].inject([]) do |result, node|
282
277
  result << node
283
278
  result + node.all_nodes
284
279
  end
285
280
  end
286
281
  end
287
282
  if !@first_child.nil?
288
- r = @first_child.inject(r) do
289
- |result, node|
283
+ r = @first_child.inject(r) do |result, node|
290
284
  result << node
291
285
  result + node.all_nodes
292
286
  end
@@ -301,12 +295,13 @@ module NoraMark
301
295
  def _find_node raw_selector
302
296
  return self if _match? raw_selector
303
297
  return nil unless @first_child
298
+
304
299
  return (@first_child.find { |n| n._match? raw_selector } ||
305
- @first_child.inject(nil) do
306
- |r, n| r or n._find_node raw_selector
300
+ @first_child.inject(nil) do |r, n|
301
+ r or n._find_node raw_selector
307
302
  end)
308
303
  end
309
-
304
+
310
305
  def clone
311
306
  @raw_content = nil
312
307
  all_nodes.each { |node| node.instance_eval { @raw_content = nil } }
@@ -314,12 +309,10 @@ module NoraMark
314
309
  end
315
310
 
316
311
  def text
317
- children.inject("") do
318
- |result, node|
312
+ children.inject("") do |result, node|
319
313
  result << node.text
320
314
  end
321
315
  end
322
-
323
316
  end
324
317
 
325
318
  class Root < Node
@@ -342,8 +335,7 @@ module NoraMark
342
335
 
343
336
  class DLItem < Node
344
337
  def text
345
- @params[0].inject('') do
346
- |result, node|
338
+ @params[0].inject('') do |result, node|
347
339
  result << node.text
348
340
  end << super
349
341
  end
@@ -353,7 +345,8 @@ module NoraMark
353
345
  def heading_info
354
346
  @name =~ /h([1-6])/
355
347
  return {} if $1.nil?
356
- {level: $1.to_i, id: @ids[0], text: text }
348
+
349
+ { level: $1.to_i, id: @ids[0], text: text }
357
350
  end
358
351
  end
359
352
 
@@ -362,23 +355,20 @@ module NoraMark
362
355
 
363
356
  def reparent
364
357
  super
365
- @heading.inject(nil) do
366
- |prev, child_node|
358
+ @heading.inject(nil) do |prev, child_node|
367
359
  child_node.prev = prev
368
360
  prev.next = child_node if !prev.nil?
369
361
  child_node.parent = self
370
- child_node.reparent
362
+ child_node.reparent
371
363
  child_node
372
364
  end
373
365
  end
374
366
 
375
367
  def text
376
- @heading[0].inject('') do
377
- |result, node|
368
+ @heading[0].inject('') do |result, node|
378
369
  result << node.text
379
370
  end << super
380
371
  end
381
-
382
372
  end
383
373
  class Text < Node
384
374
  attr_accessor :noescape
@@ -395,7 +385,7 @@ module NoraMark
395
385
  def raw_text
396
386
  true
397
387
  end
398
-
388
+
399
389
  def raw_text?
400
390
  true
401
391
  end
@@ -409,7 +399,7 @@ module NoraMark
409
399
  def raw_text
410
400
  true
411
401
  end
412
-
402
+
413
403
  def raw_text?
414
404
  true
415
405
  end
@@ -418,7 +408,7 @@ module NoraMark
418
408
  @content.join "\n"
419
409
  end
420
410
  end
421
-
411
+
422
412
  class Frontmatter < Node
423
413
  def reparent
424
414
  # do nothing
@@ -433,4 +423,4 @@ module NoraMark
433
423
  @yaml
434
424
  end
435
425
  end
436
- end
426
+ end
@@ -31,7 +31,7 @@ module NoraMark
31
31
  end
32
32
 
33
33
  def text
34
- @list.inject('') {|r,n| r << n.text }
34
+ @list.inject('') { |r, n| r << n.text }
35
35
  end
36
36
  end
37
37
  end
@@ -4,14 +4,14 @@ module NoraMark
4
4
  children_arg = children || children_
5
5
  if !children_arg.nil?
6
6
  children_arg = children_arg.to_ary if children_arg.kind_of? NodeSet
7
- children_arg = [ children_arg ] if !children_arg.kind_of? Array
7
+ children_arg = [children_arg] if !children_arg.kind_of? Array
8
8
  children_arg = children_arg.map { |node| (node.is_a? String) ? Text.new(node, 0) : node }
9
9
  end
10
10
  if !template.nil?
11
11
  node = klass.new(name, template.ids, template.classes, template.params, template.n, template.children, template.line_no)
12
- node.ids = (node.ids ||[] + ids) if ids.size > 0
12
+ node.ids = (node.ids || [] + ids) if ids.size > 0
13
13
  node.classes = (node.classes || [])
14
- node.classes = node.classes + classes
14
+ node.classes = node.classes + classes
15
15
  if node.classes.size == 0 && class_if_empty
16
16
  node.classes << class_if_empty
17
17
  end
@@ -33,12 +33,12 @@ module NoraMark
33
33
  node
34
34
  end
35
35
 
36
- def block(*args)
37
- _node(Block, *args)
36
+ def block(*args, **kwargs)
37
+ _node(Block, *args, **kwargs)
38
38
  end
39
39
 
40
- def inline(*args)
41
- _node(Inline, *args)
40
+ def inline(*args, **kwargs)
41
+ _node(Inline, *args, **kwargs)
42
42
  end
43
43
 
44
44
  def text value, raw_text: nil
@@ -46,6 +46,5 @@ module NoraMark
46
46
  text.raw_text = raw_text
47
47
  text
48
48
  end
49
-
50
49
  end
51
50
  end