PublicanCreatorsChange.fix_authorgroup()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
1
# Changer Module for PublicanCreators
2
# PublicanCreatorsChange
3
# @author Sascha Manns
4
# @abstract Class for all file changes
5
#
6
# Copyright (C) 2015-2017  Sascha Manns <[email protected]>
7
# License: MIT
8
9
# Dependencies
10
11
require 'nokogiri'
12
require 'publican_creators/create'
13
require 'publican_creators/checker'
14
require 'manns_shared'
15
16
# Module what contains all methods who are doing changes in files
17
module PublicanCreatorsChange
18
  # Method for replacing content in agroup
19
  # @param [String] nice_description is the default text in the target file
20
  # @param [String] value_name the replace text
21
  # @param [String] file Target file like Author_Group.xml
22
  # @return [String] true or false
23
  def self.add_result(nice_description, value_name, file)
24
    text = File.read(file)
25
    new_value = text.gsub(nice_description, value_name)
26
    puts new_value
27
    File.open(file, 'w') do |file1|
28
      file1.puts new_value
29
    end
30
  end
31
32
  # This method checks the environment and runs the method for
33
  # @param [String] environment shows if you actually want to create a private
34
  #                             or Business Publication. If Work is given it
35
  #                             reads your global entity file and appends it on
36
  #                             the ent file.
37
  # @param [String] title comes from the get method. This param represents the
38
  #                 name or title of your work. It is used in all important
39
  #                 code places.
40
  # @param [String] type represents the Document-Type like Article or Book.
41
  # @param [String] language is just the ISO Code of your target language like:
42
  #                 de-DE, en-GB or such things.
43
  # @param [String] brand can be a special customized brand for your company to
44
  #                 fit the styleguide.
45
  # @param [String] db5 just sets your preferences. If you like to have DocBook
46
  #                 5.x as default you can set it there.
47
  # @param [String] brand_homework can be a special customized brand for
48
  #                 distance learning schools.
49
  # @param [String] brand_private is used in all methods with a "private" in the
50
  #                 name. If this brand is set it will be used instead of the
51
  #                 original publican brand.
52
  # @param [String] homework if homework is set
53
  # @return [String] true or false
54
  def self.check_environment(environment, title, type, language, brand, db5,
55
      homework, brand_homework, brand_private)
56
    if environment == 'Work'
57
      PublicanCreatorsCreate.init_docu_work(title, type, language, brand, db5)
58
    else
59
      PublicanCreatorsCreate.init_docu_private(title, type, homework, language,
60
                                               brand_homework, brand_private,
61
                                               db5)
62
    end
63
  end
64
65
  # By working for my employer i'm creating publications which refers to a
66
  # global entity file.
67
  # This method adds the entities from that file into the local one. It returns
68
  # a success or fail.
69
  # @param [String] environment shows if you actually want to create a private
70
  #                 or Business Publication. If Work is given it reads your
71
  #                 global entity file and appends it on the ent file.
72
  # @param [String] global_entities is just the path to the global entity file.
73
  # @param [String] ent Path to the entity file
74
  # @return [String] true or false
75
  # This method smells of :reek:UncommunicativeVariableName
76
  def self.add_entity(environment, global_entities, ent)
77
    if environment == 'Work'
78
      if global_entities.empty?
79
        puts 'Nothing to do'
80
      else
81
        puts 'Adding global entities...'
82
        # @note Adding global entities
83
        open(ent, 'a') do |f|
84
          f << "\n"
85
          f << "<!-- COMMON ENTITIES -->\n"
86
        end
87
        input = File.open(global_entities)
0 ignored issues
show
Coding Style introduced by
Consider using the block version of File.open. It does automatic clean-up once the block is finished.
Loading history...
88
        data_to_copy = input.read
89
        output = File.open(ent, 'a')
0 ignored issues
show
Coding Style introduced by
Consider using the block version of File.open. It does automatic clean-up once the block is finished.
Loading history...
90
        output.write(data_to_copy)
91
        input.close
92
        output.close
93
      end
94
    else
95
      puts 'Nothing to do'
96
    end
97
  end
98
99
  # In this method the standard-holder from the local entity-file will be
100
  # replaced with the company_name or if it is a private work the name of the
101
  # present user. It returns a sucess or fail.
102
  # @param [String] title comes from the get method. This @param represents the
103
  #                 name or title of your work. It is used in all important code
104
  #                 places.
105
  # @param [String] environment shows if you actually want to create a private
106
  #                 or Business Publication. If Work is given it reads your
107
  #                 global entity file and appends it on the ent file.
108
  # @param [String] name is your name.
109
  # @param [String] company_name is the name of your company
110
  # @param [String] ent Path to the entity file
111
  # @return [String] true or false
112
  # @note If the environment "Work" is given the entity file will be set as
113
  # HOLDER otherwise it sets your name.
114
  def self.change_holder(title, environment, name, company_name, ent)
115
    # @note Replace the Holder with the real one
116
    puts 'Replace holder field with the present user'
117
    if environment == 'Work'
118
      namefill = "#{company_name}"
119
    else
120
      namefill = "#{name}"
121
    end
122
    change_holder_do(namefill, title, ent)
123
  end
124
125
  # This method does the changes
126
  # @param [String] namefill can be the name or the company_name depends on
127
  #                 environment
128
  # @param [String] title comes from the get method. This @param represents the
129
  #                 name or title of your work. It is used in all important code
130
  #                 places.
131
  # @param [String] ent Path to the entity file
132
  # @return [String] true or false
133
  def self.change_holder_do(namefill, title, ent)
134
    text = File.read(ent)
135
    new_contents = text.gsub("| You need to change the HOLDER entity in the \
136
de-DE/#{title}.ent file |", "#{namefill}")
137
    puts new_contents
138
    File.open(ent, 'w') { |file| file.puts new_contents }
139
  end
140
141
  # This method removes the <orgname> node from the XML file. Remove titlepage
142
  # logo because of doing this with the publican branding files. This method
143
  # will applied if environment is Work, "type" is Article and title_logo is
144
  # "false".
145
  # It returns a sucess or fail.
146
  # @param [String] info can be bookinfo or artinfo
147
  # @param [String] title_logo means that you can set if you want to use
148
  #                 Publican's Title Logo or use your own Title Logo with your
149
  #                 Stylesheets.
150
  # @return [String] true or false
151
  def self.remove_orgname(info, title_logo)
152
    if title_logo == 'false'
153
      puts 'Remove title logo from Article_Info or Books_Info'
154
      puts info
155
      doc = Nokogiri::XML(IO.read(info))
156
      doc.search('orgname').each do |node|
157
        node.remove
158
        node.content = 'Children removed'
159
      end
160
      IO.write(info, doc.to_xml)
161
    end
162
  end
163
164
  # Checks if bookinfo or artinfo is needed, then it starts remove_orgname
165
  # @param [String] bookinfo Book_Info. Which is used there depends on the
166
  #                 param "type".
167
  # @param [String] artinfo Article_Info. Which is used there depends on the
168
  #                 param "type".
169
  # @param [String] title_logo means that you can set if you want to use
170
  #                 Publican's Title Logo or use your own Title Logo with your
171
  #                 Stylesheets.
172
  # @param [String] type represents the Document-Type like Article or Book.
173
  def self.remove_orgname_prepare(bookinfo, artinfo, title_logo, type)
174
    info = artinfo if type == 'Article'
175
    info = bookinfo if type == 'Book'
176
    remove_orgname(info, title_logo)
177
  end
178
  # This method replaces the old productversion to the new revision
179
  # @param [String] language The default language from the config file
180
  # @param [String] revision The new revision number
181
  # @param [String] edition The new edition number
182
  # @return [String] true or false
183
  def self.replace_productnumber(revision, edition, language)
184
    puts 'Replacing the productnumber'
185
    if File.exist?("#{language}/Article_Info.xml")
186
      info = "#{language}/Article_Info.xml"
187
    else
188
      info = "#{language}/Book_Info.xml"
189
    end
190
    doc = Nokogiri::XML(IO.read(info))
191
    doc.search('productnumber').each do |node|
192
      node.content = "#{revision}"
193
    end
194
    doc.search('edition').each do |node|
195
      node.content = "#{edition}"
196
    end
197
    IO.write(info, doc.to_xml)
198
  end
199
200
  # This method removes the XI-Includes for the legal notice
201
  # It returns a sucess or fail.
202
  # @param [String] environment shows if you actually want to create a private
203
  #                 or Business Publication. If Work is given it reads your
204
  #                 global entity file and appends it on the ent file.
205
  # @param [String] type represents the Document-Type like Article or Book.
206
  # @param [String] legal means if you don't like to have a Legal Notice on
207
  #                 Publican's default place you can define it there. Actually
208
  #                 it just works with Articles. In my case i'm using the
209
  #                 Legal Notice inside the Article's Structure.
210
  # @param [String] artinfo Article_Info. Which is used there depends on the
211
  #                 param "type".
212
  # @return [String] true or false
213
  def self.remove_legal(environment, type, legal, artinfo)
214
    if environment == 'Work'
215
      if type == 'Article'
216
        if legal == 'true'
217
          # @note Remove the Legal Notice XI-Include in case it is an article.
218
          # XCOM articles using another way to add them.
219
          puts 'Remove XI-Includes for Legal Notice...'
220
          text = File.read(artinfo)
221
          # rubocop:disable Metrics/LineLength
222
          new_contents = text.gsub('<xi:include href="Common_Content/Legal_Notice.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />', '')
223
          puts new_contents
224
          File.open(artinfo, 'w') { |file| file.puts new_contents }
225
        end
226
      else
227
        puts 'Nothing to do'
228
      end
229
    else
230
      puts 'Nothing to do'
231
    end
232
  end
233
234
  # This method splits the name variable into firstname and surname. These
235
  # variables are setted into the Revision_History. If the environment is "Work"
236
  # your email_business will be used, otherwise your private email_address.
237
  # It returns a sucess or fail.
238
  # @param [String] revhist Path to the Revision_History
239
  # @param [String] environment shows if you actually want to create a private
240
  #                 or Business Publication. If Work is given it reads your
241
  #                 global entity file and appends it on the ent file.
242
  # @param [String] name is your name.
243
  # @param [String] email_business is your business email address.
244
  # @param [String] email is your private email address.
245
  # @return [String] true or false
246
  def self.fix_revhist(environment, name, email_business, email, revhist)
247
    firstname, surname = get_name(name)
248
    # @note Revision_History: Change default stuff to the present user
249
    puts 'Replace the default content with the new content from the user
250
(Revision History)'
251
    add_result('Enter your first name here.', "#{firstname}", revhist)
252
    add_result('Enter your surname here.', "#{surname}", revhist)
253
    add_result('Initial creation by publican', 'Initial creation', revhist)
254
255
    if environment == 'Work'
256
      add_result('Enter your email address here.', "#{email_business}", revhist)
257
    else
258
      add_result('Enter your email address here.', "#{email}", revhist)
259
    end
260
  end
261
262
  # This method replaces the standard values from Author_Group to the present
263
  # user issues. It will be launched for the Work environment. It returns a
264
  # sucess or fail.
265
  # @param [String] name is your name.
266
  # @param [String] email_business is your business email address.
267
  # @param [String] company_name is just your companies name.
268
  # @param [String] company_division is your companies part/division.
269
  # @param [String] email is your private email address.
270
  # @param [String] environment shows if you actually want to create a private
271
  #                 or Business Publication. If Work is given it reads your
272
  #                 global entity file and appends it on the ent file.
273
  # @param [String] agroup Path to Author_Group.xml
274
  # @return [String] true or false
275
  def self.fix_authorgroup(name, email_business, company_name, company_division,
276
      email, environment, agroup)
277
    firstname, surname = get_name(name)
278
    # @note Author Group: Change the default stuff to the present user
279
    puts 'Replace the default content with the new content from the user
280
(Authors_Group)'
281
    add_result('Enter your first name here.', "#{firstname}", agroup)
282
    add_result('Enter your surname here.', "#{surname}", agroup)
283
    add_result('Initial creation by publican', 'Initial creation', agroup)
284
285
    if environment == 'Work'
286
      add_result('Enter your email address here.', "#{email_business}", agroup)
287
      add_result('Enter your organisation\'s name here.', "#{company_name}",
288
                 agroup)
289
      add_result('Enter your organisational division here.',
290
                 "#{company_division}", agroup)
291
    else
292
      add_result('Enter your email address here.', "#{email}", agroup)
293
      add_result('Enter your organisation\'s name here.', '', agroup)
294
      add_result('Enter your organisational division here.', '', agroup)
295
    end
296
  end
297
298
  # Method for splitting the name variable into firstname and surname
299
  # @param [String] name The name from config file
300
  # @return [String] true or false
301
  def self.get_name(name)
302
    namechomp = name.chomp
303
    # @note Split the variable to the array title[*]
304
    name = namechomp.split(' ')
305
    firstname = name[0]
306
    surname = name[1]
307
    [firstname, surname]
308
  end
309
end
310