-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathgenerate_function.rb
197 lines (178 loc) · 7.62 KB
/
generate_function.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require_relative '../lib/gepub/inspect_mixin.rb'
require_relative '../lib/gepub/item.rb'
attrs = GEPUB::Item::ATTRIBUTES.select do |attr|
attr != 'href'
end.map do |attr|
attr.sub('-', '_')
end
attrs << "toc_text"
attrs << "property"
attrs_arguments_string = attrs.map { |attr| "#{attr}: nil" }.join(',')
attrs_internal_string = "{ " + attrs.map { |attr| "#{attr}: #{attr}"}.join(',') + " }"
File.write(File.join(File.dirname(__FILE__), "../lib/gepub/book_add_item.rb"), <<EOF)
## this file is automatically generated by tools/generate_function.rb . do not edit this code directly.
module GEPUB
class Book
# add an item(i.e. html, images, audios, etc) to Book.
# the added item will be referenced by the first argument in the EPUB container.
def add_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content: nil,
#{attrs_arguments_string},
attributes: {})
content, id, attributes = handle_deprecated_add_item_arguments(deprecated_content, deprecated_id, deprecated_attributes, content, id, attributes)
add_item_internal(href, content: content, item_attributes: #{attrs_internal_string}, attributes: attributes, ordered: false)
end
# same as add_item, but the item will be added to spine of the EPUB.
def add_ordered_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content:nil,
#{attrs_arguments_string},
attributes: {})
content, id, attributes = handle_deprecated_add_item_arguments(deprecated_content, deprecated_id, deprecated_attributes, content, id, attributes)
add_item_internal(href, content: content, item_attributes: #{attrs_internal_string}, attributes: attributes, ordered: true)
end
end
end
EOF
require_relative '../lib/gepub/dsl_util.rb'
require_relative '../lib/gepub/meta.rb'
refiners = GEPUB::Meta::REFINERS.map do |refiner|
refiner.sub('-', '_')
end
refiners_arguments_string = refiners.map { |refiner| "#{refiner}: nil" }.join(',')
refiners_arguments_set_string = refiners.map { |refiner| "#{refiner}: #{refiner}" }.join(',')
refiners_string = "[" + GEPUB::Meta::REFINERS.map { |refiner| "{ value: #{refiner.sub('-', '_')}, name: '#{refiner}'}" }.join(",") + "]"
meta_attr_arguments_string = "lang: nil, alternates: {}"
meta_attr_arguments_set_string = "lang: lang, alternates: alternates"
File.write(File.join(File.dirname(__FILE__), "../lib/gepub/metadata_add.rb"), <<EOF)
## this file is automatically generated by tools/generate_function.rb . do not edit this code directly.
module GEPUB
class Metadata
CONTENT_NODE_LIST = ['identifier', 'title', 'language', 'contributor', 'creator', 'coverage', 'date','description','format','publisher','relation','rights','source','subject','type'].each {
|node|
define_method(node + '_list') { @content_nodes[node].dup.sort_as_meta }
define_method(node + '_clear') {
if !@content_nodes[node].nil?
@content_nodes[node].each { |x| unregister_meta(x) };
@content_nodes[node] = []
end
}
next if node == 'title'
define_method(node, ->(content=UNASSIGNED, deprecated_id=nil, id:nil,
#{refiners_arguments_string},
#{meta_attr_arguments_string}) {
if unassigned?(content)
get_first_node(node)
else
if deprecated_id
warn "second argument is deprecated. use id: keyword argument"
id = deprecated_id
end
send(node + "_clear")
add_metadata(node, content, id: id, #{refiners_arguments_set_string}, #{meta_attr_arguments_set_string})
end
})
define_method(node+'=') {
|content|
send(node + "_clear")
return if content.nil?
if node == 'date'
add_date(content)
else
add_metadata(node, content)
end
}
next if ["identifier", "date", "creator", "contributor"].include?(node)
define_method('add_' + node) {
|content, id|
add_metadata(node, content, id: id)
}
}
def add_title(content, deprecated_id = nil, deprecated_title_type = nil, id: nil,
#{refiners_arguments_string},
#{meta_attr_arguments_string})
if deprecated_id
warn 'second argument for add_title is deprecated. use id: instead'
id = deprecated_id
end
if deprecated_title_type
warn 'third argument for add_title is deprecated. use title_type: instead'
title_type = deprecated_title_type
end
meta = add_metadata('title', content, id: id,
#{refiners_arguments_set_string},
#{meta_attr_arguments_set_string})
yield meta if block_given?
meta
end
def add_person(name, content, deprecated_id = nil, deprecated_role = nil, id: nil,
#{refiners_arguments_string},
#{meta_attr_arguments_string})
if deprecated_id
warn 'second argument for add_person is deprecated. use id: instead'
id = deprecated_id
end
if deprecated_role
warn 'third argument for add_person is deprecated. use role: instead'
role = deprecated_role
end
meta = add_metadata(name, content, id: id,
#{refiners_arguments_set_string},
#{meta_attr_arguments_set_string})
yield meta if block_given?
meta
end
def add_creator(content, deprecated_id = nil, deprecated_role = nil, id: nil,
#{refiners_arguments_string},
#{meta_attr_arguments_string})
if deprecated_id
warn 'second argument for add_creator is deprecated. use id: instead'
id = deprecated_id
end
if deprecated_role
warn 'third argument for add_creator is deprecated. use role: instead'
role = deprecated_role
end
role = 'aut' if role.nil?
meta = add_person('creator', content, id: id,
#{refiners_arguments_set_string},
#{meta_attr_arguments_set_string})
yield meta if block_given?
meta
end
def add_contributor(content, deprecated_id = nil, deprecated_role = nil, id: nil,
#{refiners_arguments_string},
#{meta_attr_arguments_string})
if deprecated_id
warn 'second argument for add_contributor is deprecated. use id: instead'
id = deprecated_id
end
if deprecated_role
warn 'third argument for add_contributor is deprecated. use role: instead'
role = deprecated_role
end
meta = add_person('contributor', content, id: id,
#{refiners_arguments_set_string},
#{meta_attr_arguments_set_string})
yield meta if block_given?
meta
end
def add_metadata(name, content, id: nil, itemclass: Meta,
#{refiners_arguments_string},
#{meta_attr_arguments_string}
)
meta = add_metadata_internal(name, content, id: id, itemclass: itemclass)
#{refiners_string}.each do |refiner|
if refiner[:value]
meta.refine(refiner[:name], refiner[:value])
end
end
if lang
meta.lang = lang
end
if alternates
meta.add_alternates alternates
end
yield meta if block_given?
meta
end
end
end
EOF