nora_mark 0.2beta15 → 0.2beta16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cab3cab407b40a37ddb78f770011aabd71768727
4
- data.tar.gz: 88374067b24427c73c945f0be3fc5654875779f8
3
+ metadata.gz: d90789f0a20df993346e159dd0b37cdd1f664f9c
4
+ data.tar.gz: 4c373042623881fcc820041c5454219d12c2f2ca
5
5
  SHA512:
6
- metadata.gz: e2d49d88eb73c4d933be68140e0cd6dd0df0336aad343e6cd2dfb32ff5c03e18db38df803c4c4833d5608a57ee891f897c6f41b66997c26253c9a9ce6f4bd25d
7
- data.tar.gz: e61233e4d76f037e19e786341f31faefed03a36cc6c0b0b8a8a85529b175ce96fc0028cae3e38517f64f9f32516ad1adaf65ad626451c48b9242e084a057d836
6
+ metadata.gz: eb21f8791bccb24784e2761a4aa3bbe92c77723f83643acf5a60a33e84db5715b99b824b0b77e331c91d83600286d7b87198fff6fce101ca1fbe34d01367e0e5
7
+ data.tar.gz: 8797e08d2c24486f596028f6b65f036c4097f5126ae058b29feae8dc7a859c4267a250ebeab02b6dc86f4930256f41363e620b1d600a02a94cdbb894533e3068
@@ -50,13 +50,7 @@ module NoraMark
50
50
  @root = parser.result
51
51
  @root.document_name ||= @document_name
52
52
  @root.reparent
53
- @root.first_child.inject(1) do |page_no, node|
54
- if node.kind_of? Page
55
- node.page_no = page_no
56
- page_no = page_no + 1
57
- end
58
- page_no
59
- end
53
+ @root.assign_pageno
60
54
  end
61
55
  frontmatter = instance.root.find_node :type => :Frontmatter
62
56
  if (frontmatter)
@@ -93,7 +93,7 @@ module NoraMark
93
93
  end
94
94
 
95
95
  # save metadata written with Frontmatter Writer
96
- def save_metas
96
+ def save_default_metas
97
97
  @default_param[:styleheets] ||= @stylesheets if !@stylesheets.nil? && @stylesheets.size > 0
98
98
  @default_param[:title] ||= @title
99
99
  @default_param[:lang] ||= @lang
@@ -26,10 +26,10 @@ module NoraMark
26
26
  node_preprocessor: proc do |node|
27
27
  @context.end_html
28
28
  if node.first_child.class == Frontmatter
29
- @context.save_metas
30
29
  frontmatter = node.first_child
31
30
  frontmatter_writer.write frontmatter
32
31
  frontmatter.remove
32
+ @context.save_default_metas
33
33
  end
34
34
  node
35
35
  end);
@@ -57,6 +57,16 @@ module NoraMark
57
57
  end
58
58
  end
59
59
  end
60
+ def write_toc_as_file(directory: nil)
61
+ return if @toc.nil?
62
+ dir = directory || Dir.pwd
63
+ Dir.chdir(dir) do
64
+ File.open("#{@file_basename}.yaml", 'w+') do
65
+ |file|
66
+ file << YAML.dump(@toc)
67
+ end
68
+ end
69
+ end
60
70
  end
61
71
  end
62
72
  end
@@ -36,8 +36,8 @@ module NoraMark
36
36
  def each
37
37
  node = self
38
38
  while !node.nil?
39
- yield node
40
- node = node.next
39
+ node, node_old = node.next, node
40
+ yield node_old
41
41
  end
42
42
  end
43
43
 
@@ -151,16 +151,31 @@ module NoraMark
151
151
  @next = nil
152
152
  end
153
153
 
154
- def remove
154
+ def _remove_internal
155
155
  @parent.first_child = @next if !@parent.nil? && @parent.first_child == self
156
156
  @parent.last_child = @prev if !@parent.nil? && @parent.last_child == self
157
157
  @next.prev = @prev unless @next.nil?
158
158
  @prev.next = @next unless @prev.nil?
159
+ end
160
+
161
+ def remove
162
+ _remove_internal
159
163
  @parent.children_replaced unless @parent.nil?
160
164
  unlink
161
165
  self
162
166
  end
163
167
 
168
+ def remove_following
169
+ parent = @parent
170
+ r = self.map do |node|
171
+ node._remove_internal
172
+ node.unlink
173
+ node
174
+ end
175
+ parent.children_replaced
176
+ r
177
+ end
178
+
164
179
  def after(node)
165
180
  node.remove
166
181
  node.parent = @parent
@@ -309,6 +324,16 @@ module NoraMark
309
324
 
310
325
  class Root < Node
311
326
  attr_accessor :document_name
327
+
328
+ def assign_pageno
329
+ @first_child.inject(1) do |page_no, node|
330
+ if node.kind_of? Page
331
+ node.page_no = page_no
332
+ page_no = page_no + 1
333
+ end
334
+ page_no
335
+ end
336
+ end
312
337
  end
313
338
 
314
339
  class Page < Node
@@ -3,7 +3,7 @@ require 'rake/tasklib'
3
3
 
4
4
  module NoraMark
5
5
  class RakeTask < ::Rake::TaskLib
6
- attr_accessor :lang, :page_number_digits
6
+ attr_accessor :lang, :page_number_digits, :write_toc_file
7
7
  def initialize(lang: 'en')
8
8
  @preprocessors = []
9
9
  @transformers = []
@@ -21,6 +21,14 @@ module NoraMark
21
21
  @transformers << block;
22
22
  end
23
23
 
24
+ def html
25
+ return @nora.html if !@nora.nil?
26
+ end
27
+
28
+ def toc
29
+ return @nora.html.toc if !@nora.nil? && !@nora.html.nil?
30
+ end
31
+
24
32
  def define
25
33
  desc "rule for *-nora.txt to *-nora_xxx.html. Use *-nora-transform.rb on same directory as transformer"
26
34
  rule( /-((nora)|(arti))_[0-9]{#{page_number_digits}}\.xhtml/ =>
@@ -30,8 +38,9 @@ module NoraMark
30
38
  |t|
31
39
 
32
40
  dir = File.dirname File.expand_path(t.source)
33
- transformer_name = File.join dir, File.basename(t.source, '.txt') + '-transform.rb'
34
- nora =
41
+ basename = File.basename(t.source, '.txt')
42
+ transformer_name = File.join dir, basename + '-transform.rb'
43
+ @nora =
35
44
  NoraMark::Document.parse(
36
45
  File.open(t.source),
37
46
  :lang => @lang.to_s,
@@ -50,7 +59,10 @@ module NoraMark
50
59
  doc.add_transformer(text: File.open(transformer_name).read)
51
60
  end
52
61
  end
53
- nora.html.write_as_files
62
+ @nora.html.write_as_files
63
+ if (@write_toc_file)
64
+ @nora.html.write_toc_as_file
65
+ end
54
66
  end
55
67
 
56
68
  end
@@ -1,3 +1,3 @@
1
1
  module NoraMark
2
- VERSION = "0.2beta15"
2
+ VERSION = "0.2beta16"
3
3
  end
@@ -129,6 +129,39 @@ EOF
129
129
  ['div.the_class', ['p', '3rd line.']])
130
130
  end
131
131
 
132
+ it 'removes node' do
133
+ noramark = NoraMark::Document.parse(@text)
134
+ page = noramark.root.children[0]
135
+ second_div = page.children[1]
136
+ second_div.remove
137
+ body = Nokogiri::XML::Document.parse(noramark.html[0]).root.at_xpath('xmlns:body')
138
+ expect(body.selector_and_children(remove_id: false))
139
+ .to eq(["body", ["p", "1st line."], ["p", "5th line."]])
140
+ end
141
+
142
+ it 'removes all nodes after specified node' do
143
+ noramark = NoraMark::Document.parse(@text)
144
+ page = noramark.root.children[0]
145
+ second_div = page.children[1]
146
+ second_div.remove_following
147
+ body = Nokogiri::XML::Document.parse(noramark.html[0]).root.at_xpath('xmlns:body')
148
+ expect(body.selector_and_children(remove_id: false))
149
+ .to eq(["body", ["p", "1st line."]])
150
+ end
151
+
152
+ it 'replace with removed nodes' do
153
+ noramark = NoraMark::Document.parse(@text)
154
+ page = noramark.root.children[0]
155
+ first_p = page.children[0]
156
+ second_div = page.children[1]
157
+ removed = second_div.remove_following
158
+ first_p.replace(removed)
159
+ body = Nokogiri::XML::Document.parse(noramark.html[0]).root.at_xpath('xmlns:body')
160
+ expect(body.selector_and_children(remove_id: false))
161
+ .to eq(["body", ["div.the_class", ["p", "3rd line."]], ["p", "5th line."]])
162
+ end
163
+
164
+
132
165
  it 'modify existing node by DSL' do
133
166
  text = "1st line.\nfoobar(title)[level: 3] {\n in the section.\n}\n# section 2."
134
167
  noramark = NoraMark::Document.parse(text, lang: 'ja')
@@ -1388,6 +1388,7 @@ EOF
1388
1388
  expect(files).to include 'nora-test-file_001.xhtml'
1389
1389
  expect(files).to include 'nora-test-file_002.xhtml'
1390
1390
  end
1391
+
1391
1392
  end
1392
1393
  describe 'parse and create manual' do
1393
1394
  before {
@@ -1421,6 +1422,7 @@ EOF
1421
1422
  end
1422
1423
  describe 'table of contents' do
1423
1424
  before do
1425
+ @basedir = File.join(File.dirname(__FILE__), 'created_files')
1424
1426
  @text = <<EOF
1425
1427
  ---
1426
1428
  lang: ja
@@ -1436,8 +1438,9 @@ h6: [strong{some column}]
1436
1438
  text
1437
1439
  }
1438
1440
  EOF
1439
- @noramark = NoraMark::Document.parse(@text, document_name: 'nora')
1441
+ @noramark = NoraMark::Document.parse(@text, document_name: 'nora-with-toc')
1440
1442
  end
1443
+ after { Dir.glob(File.join(@basedir, '*.yaml')) { |file| File.delete file } }
1441
1444
  it 'should assign ids to headers' do
1442
1445
  body = Nokogiri::XML::Document.parse(@noramark.html[0]).root.at_xpath('xmlns:body')
1443
1446
  h1 = body.at_xpath('//xmlns:h1')
@@ -1452,12 +1455,27 @@ EOF
1452
1455
  toc = @noramark.html.toc
1453
1456
  expect(toc.size).to eq 3
1454
1457
  expect(toc[0])
1455
- .to eq({link: "nora_00001.xhtml#heading_index_1", level: 1, text: "chapter 1"})
1458
+ .to eq({link: "nora-with-toc_00001.xhtml#heading_index_1", level: 1, text: "chapter 1"})
1456
1459
  expect(toc[1])
1457
- .to eq({link: "nora_00001.xhtml#heading_index_2", level: 2, text: "section 1-1"})
1460
+ .to eq({link: "nora-with-toc_00001.xhtml#heading_index_2", level: 2, text: "section 1-1"})
1458
1461
  expect(toc[2])
1459
- .to eq({link: "nora_00002.xhtml#heading_index_3", level: 6, text: "some column"})
1462
+ .to eq({link: "nora-with-toc_00002.xhtml#heading_index_3", level: 6, text: "some column"})
1463
+ end
1464
+ it 'should generate and write tocs' do
1465
+ @noramark.html.write_toc_as_file(directory: @basedir)
1466
+ expect(File.exist?(File.join(@basedir, 'nora-with-toc.yaml')))
1467
+ File.open(File.join(@basedir, 'nora-with-toc.yaml')) do
1468
+ |file|
1469
+ toc = YAML.load(file.read)
1470
+ expect(toc[0])
1471
+ .to eq({link: "nora-with-toc_00001.xhtml#heading_index_1", level: 1, text: "chapter 1"})
1472
+ expect(toc[1])
1473
+ .to eq({link: "nora-with-toc_00001.xhtml#heading_index_2", level: 2, text: "section 1-1"})
1474
+ expect(toc[2])
1475
+ .to eq({link: "nora-with-toc_00002.xhtml#heading_index_3", level: 6, text: "some column"})
1476
+ end
1460
1477
  end
1478
+
1461
1479
  end
1462
1480
  it 'should raise error' do
1463
1481
  text = "d {\n block is\nd {\n nested but\nd {\n not terminated }"
@@ -18,9 +18,15 @@ d.class-1 {
18
18
  text is here
19
19
  }
20
20
 
21
+ #(split-me) section 2
22
+
23
+ next text.
24
+
25
+ # section 3
21
26
  EOF
22
27
  @parsed = NoraMark::Document.parse(@text)
23
28
  end
29
+
24
30
  it 'can access frontmatter' do
25
31
  @parsed.add_transformer(generator: :html) do
26
32
  modify 'd' do
@@ -34,4 +40,100 @@ EOF
34
40
  ['div.custom1', ['div.pgroup', ['p', 'text is here']]]]
35
41
  )
36
42
  end
43
+ it 'can split page' do
44
+ @parsed.add_transformer(generator: :html) do
45
+ modify type: :Root do
46
+ def split page
47
+ splitted = page.children.inject([]) do |result, node|
48
+ if node.params && node.params.select { |param| param.text == 'split-me' }.size > 0
49
+ rest = node.remove_following
50
+ as_page = rest.shift
51
+ result << NoraMark::Page.new([as_page], as_page.line_no)
52
+ result = result + split(NoraMark::Page.new(rest, rest[0].line_no)) if rest.size > 0
53
+ end
54
+ result
55
+ end
56
+ splitted.unshift page
57
+ end
58
+ @node.children.each do |page|
59
+ splitted = split page
60
+ if (splitted.size > 1)
61
+ page = splitted.shift
62
+ splitted.inject(page) do |page_before, page_after|
63
+ page_before.after(page_after)
64
+ page_after
65
+ end
66
+ @node.assign_pageno
67
+ end
68
+ end
69
+ end
70
+ end
71
+ converted = @parsed.html
72
+ expect(converted.size).to eq(3)
73
+ body = Nokogiri::XML::Document.parse(@parsed.html[0]).root.at_xpath('xmlns:body')
74
+ expect(body.element_children[0].selector_and_children())
75
+ .to eq(
76
+ ['section', ['h1', 'section 1'],
77
+ ['div.class-1', ['div.pgroup', ['p', 'text is here']]]]
78
+ )
79
+
80
+ body = Nokogiri::XML::Document.parse(@parsed.html[1]).root.at_xpath('xmlns:body')
81
+ expect(body.element_children[0].selector_and_children())
82
+ .to eq(
83
+ ["section", ["h1", "section 2"],
84
+ ["div.pgroup", ["p", "next text."]]]
85
+ )
86
+ end
87
+
88
+ it 'can add frontmatter' do
89
+ @parsed.add_transformer(generator: :html) do
90
+ modify type: :Root do
91
+ def split page
92
+ splitted = page.children.inject([]) do |result, node|
93
+ if node.params && node.params.select { |param| param.text == 'split-me' }.size > 0
94
+ rest = node.remove_following
95
+ as_page = rest.shift
96
+ new_frontmatter = NoraMark::Frontmatter.new(['stylesheets: new_stylesheet.css'], as_page.line_no)
97
+
98
+ result << NoraMark::Page.new([new_frontmatter, as_page], as_page.line_no)
99
+ result = result + split(NoraMark::Page.new(rest, rest[0].line_no)) if rest.size > 0
100
+ end
101
+ result
102
+ end
103
+ splitted.unshift page
104
+ end
105
+
106
+ @node.children.each do |page|
107
+ splitted = split page
108
+ if (splitted.size > 1)
109
+ page = splitted.shift
110
+ splitted.inject(page) do |page_before, page_after|
111
+ page_before.after(page_after)
112
+ page_after
113
+ end
114
+ @node.assign_pageno
115
+ end
116
+ end
117
+
118
+ end
119
+ end
120
+ html = @parsed.html
121
+ body1 = Nokogiri::XML::Document.parse(html[0]).root.at_xpath('xmlns:body')
122
+ expect(body1.element_children[0].selector_and_children())
123
+ .to eq(
124
+ ['section', ['h1', 'section 1'],
125
+ ['div.class-1', ['div.pgroup', ['p', 'text is here']]]]
126
+ )
127
+ head2 = Nokogiri::XML::Document.parse(html[1]).root.at_xpath('xmlns:head')
128
+ expect(head2.element_children[1].a).to eq ["link[rel='stylesheet'][type='text/css'][href='new_stylesheet.css']", '']
129
+
130
+ body2 = Nokogiri::XML::Document.parse(@parsed.html[1]).root.at_xpath('xmlns:body')
131
+ expect(body2.element_children[0].selector_and_children())
132
+ .to eq(
133
+ ["section", ["h1", "section 2"],
134
+ ["div.pgroup", ["p", "next text."]]]
135
+ )
136
+
137
+
138
+ end
37
139
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nora_mark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2beta15
4
+ version: 0.2beta16
5
5
  platform: ruby
6
6
  authors:
7
7
  - KOJIMA Satoshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-02 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kpeg