Module: Trekyll

Defined in:
lib/trekyll.rb,
lib/trekyll/version.rb,
lib/trekyll/trello_get.rb,
lib/trekyll/filemanager.rb,
lib/trekyll/configurator.rb,
lib/trekyll/initializator.rb,
lib/trekyll/navigation_manager.rb

Defined Under Namespace

Classes: NavigationManager, TemplateFilesInit, TrekyllConfigurator, TrekyllFileManager

Constant Summary collapse

VERSION =
"0.2.0".freeze

Class Method Summary collapse

Class Method Details

.command_interpretter(arg) ⇒ Object

CLI argument interpretter



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
# File 'lib/trekyll.rb', line 7

def self.command_interpretter(arg)
  case arg
      when "-v", "--version"
          puts "trekyll " + Trekyll::VERSION
      when "init"
          #initialize coppy _init.yml and _config.yml
          fc = Trekyll::TemplateFilesInit.new
          fc.templates_init()
      when "get"
          # start get trello data
          if Trekyll.start_trekyll()
              # when get is complete run jekyll build
              puts "Now Build your Website using jekyll build!"
              # cmd ="jekyll build"
              # system(cmd)
          end
      when "clear"
          # Clear all data files
      when "-h","--help", nil
          # defoult info
          puts "trekyll " + Trekyll::VERSION + " -- Jekyll plugin to use Trello board as CMS backend"
          puts "Usage:"
          puts "  trekyll <subcommand>"
          puts ""
          puts "Subcommands:"
          puts "  --help"
          puts "  -h       Print this help message"
          puts "  init     Initialize Trekyll configuration (creates _init.yml and default Jekyll _config.yml)"
          puts "  get      Get data from Trello board and modify it for Jekyll usage"
      else
          puts "Trekyll command not recognized"
          puts "use: -h or --help to see available commands"
  end

end

.start_trekyllObject



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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/trekyll/trello_get.rb', line 11

def self.start_trekyll
    # Define start file and folder structure #
    f_manager = TrekyllFileManager.new

    f_manager.delete_file_struct()
    f_manager.create_file_struct()

    # Get configuration from _init.yml
    init_config = TrekyllConfigurator.new

    # Create navigation manager object
    navigation = NavigationManager.new(f_manager.data_dir)

    # Configure trello connection
    Trello.configure do |config|
        config.developer_public_key = init_config.key
        config.member_token = init_config.token
    end


    # Test trello connection
    user = Trello::Member.find("mucicnenad")
    puts "********************"
    # Print user name
    puts "*** #{user.full_name}! ***"
    # Print user bio
    puts "* #{user.bio} *"
    puts "********************"

    board_name = init_config.board

    board = Trello::Board.all.find { |b| b.name == board_name }

    if board.nil?
        abort "Unable to find board named: #{board_name}"
    end

    # Globals
    cards = board.cards
    lists = board.lists
    board_custom_fields = ""

    # Get board plugin fields
    unless board.plugin_data.nil? then
        plugins = board.plugin_data
        plugins.each do |plugin|
            plugin.value.map {|k,fields|
                board_custom_fields = fields
            }
        end
    end

    counter = 1
    wgtcounter = 1
    cards.each do |card|

        # Create Navigation if navigation list name is defined in _nav.yml
        if card.list.name == init_config.navigation then
            page_name = card.name
            navigation.add_page(page_name)

        # Create pages from Pages List
        elsif card.list.name == init_config.pages then

            page_name = card.name

            #create a file name (remove special chars and convert spaces to _ )
            f_name = page_name.sanitize_as_page_name

            # page type indentificator
            page_type = "" # page, subpage ..

            #get widget selector from card content and replace with liquid
            content = card.desc.gsub('[>','{{site.widgets| where: "name","')
            content = content.gsub('<]','"}}')

            # If navigation List is not set in _init.yml
            # create navigation from pages
            if init_config.navigation.nil? then

                navigation.add_page(page_name)
                page_type = navigation.page_type
                page_name = navigation.page_name
                f_name = navigation.page_file_name

            end

            # Get cover image
            cover_id = card.cover_image_id
            cover_image_url = ""
            unless card.attachments.nil? then
                card.attachments.each do |attachment|
                    if attachment.id == cover_id
                        cover_image_url = attachment.url
                    end
                end
                puts "Page cover image:  #{cover_image_url} " if cover_image_url != ""
            end


            # TODO: Create Module for Custom Fields: #
            # get_board_fields(board)                #
            # get_card_fields(board_fields, card)    #
            # get_card_layout(board_fields, card)    #
            #                                        #
            # Get card plugin custom fields values (Hash)
            card_field_values = ""
            unless card.plugin_data.nil? then
                card_plugin = card.plugin_data
                card_plugin.each do |plugin|
                    plugin.value.map {|k,v| card_field_values = v}
                end
            end

            # Custom fields for printing in .md file
            custom_fields = ""
            layout = "page"
            unless card.plugin_data.nil? then
                custom_fields = board_custom_fields.map do |field|
                    # if field type is: text, number, checkbox, date
                    if (0..3) === field["t"]
                      "#{field["n"]}: \"#{card_field_values[field["id"]]}\""
                    # if field type is: dropdown list
                    elsif field["t"] === 4
                        # if field name is layout set layout variable
                        if field["n"] == "layout"
                            drpdwn_id = card_field_values[field["id"]]
                            hash_with_val = field["o"].find {|element| element["id"] === drpdwn_id}
                            if hash_with_val != nil
                                layout = hash_with_val["value"]
                                next
                            end

                        else
                            drpdwn_id = card_field_values[field["id"]]
                            hash_with_val = field["o"].find {|element| element["id"] === drpdwn_id}
                            "#{field["n"]}: \"#{hash_with_val["value"]}\"" if hash_with_val != nil
                        end
                    end
                end
            end

            # Write data to a file

            page_file = "#{f_name}.md"

            puts "Creating file: #{page_file}"

            # Page template
            template = File.open page_file, "w"

            template.puts <<~DOC.gsub(/\t/, '')
                    ---
                    layout: #{layout}
                    type: #{page_type}
                    title: "#{page_name}"
                    date: #{card.created_at}
                    cover: "#{cover_image_url}"
                    weight: #{counter}
                    permalink: /#{f_name}/
                    #{custom_fields.join("\n")}
                    ---
                    DOC
            template.puts "#{content}"
            template.close
            counter = counter.next

        # Create Widgets
        elsif card.list.name == init_config.widgets then

            #get widgets dir name from file manager config
            dir_name = f_manager.widgets_dir
            f_name = card.name.sanitize_as_page_name
            widget_name = "#{f_name}.md"

            # Get cover image
            cover_id = card.cover_image_id
            cover_image_url = ""
            unless card.attachments.nil? then
                card.attachments.each do |attachment|
                    if attachment.id == cover_id
                        cover_image_url = attachment.url
                    end
                end
                puts "Widget cover image:  #{cover_image_url} " if cover_image_url != ""
            end

            template = File.open(File.join(Dir.pwd, dir_name, widget_name),"w")

            #Write data into file
            template.puts <<~DOC.gsub(/\t/, '')
                    ---
                    name: #{card.name}
                    weight: #{wgtcounter}
                    cover: "#{cover_image_url}"
                    ---
                    DOC
                    template.puts "#{card.desc}"
            template.close
            wgtcounter = wgtcounter.next
        # Create Hard coded Blocks (like carousel's)
        elsif card.list.name == init_config.blocks then

            #get blocks dir name from file manager config
            dir_name = f_manager.blocks_dir
            f_name = card.name.sanitize_as_page_name
            block_name = "#{f_name}.md"

            # Get attachments from widget
            tmp = Hash.new
            attachment_url = ""
            # hash_test = [] ("pos" still not implemented in trello API)
            unless card.attachments.nil? then
                card.attachments.each do |attachment|
                    #Get url and pos (position) into temp Hash
                    k = attachment.pos
                    v = attachment.url
                    tmp[k] = v
                end
                # Sort hash and get url's
                sorted_hash = tmp.sort.reverse.to_h
                attachment_url = sorted_hash.map { |k, v| "\"#{[v].flatten.join(',')}\"" }.join(', ')

                puts "Attachment: #{attachment_url}" if attachment_url != ""
            end

            template = File.open(File.join(Dir.pwd, dir_name, block_name),"w")
            #Write data into file
            template.puts <<~DOC.gsub(/\t/, '')
                    ---
                    name: #{card.name}
                    attachments: [ #{attachment_url} ]
                    ---
                    DOC
                    # template.puts "#{card.desc}" Interpret content ?! TO DO
            template.close

        # Create Posts from posts List (multiple list names allowed)
        elsif init_config.posts.include?(card.list.name) then
            #get dir with list name
            dir_name = f_manager.posts_dir
            #create file name based on date and card name
            file_name = "#{card.created_at.strftime("%Y-%m-%d")}-#{card.name.sanitize_as_post_name}.md"

            label_name = ""

            unless card.labels.nil? then
                card.labels.each do |label|
                    label_name = label_name + " " + label.name
                end
            end

            # Get cover image
            cover_id = card.cover_image_id
            cover_image_url = ""
            unless card.attachments.nil? then
                card.attachments.each do |attachment|
                    if attachment.id == cover_id
                        cover_image_url = attachment.url
                    end
                end

                puts "Post cover image:  #{cover_image_url} " if cover_image_url != ""

            end

            # TODO: Create Module for Custom Fields: #
            # get_board_fields(board)                #
            # get_card_fields(board_fields, card)    #
            # get_card_layout(board_fields, card)    #
            #                                        #
            # Get card plugin custom fields values (Hash)
            card_field_values = ""
            unless card.plugin_data.nil? then
                card_plugin = card.plugin_data
                card_plugin.each do |plugin|
                    plugin.value.map {|k,v| card_field_values = v}
                end
            end

            # Custom fields for printing in .md file
            custom_fields = ""
            layout = "post"
            unless card.plugin_data.nil? then
                custom_fields = board_custom_fields.map do |field|
                    # if field type is: text, number, checkbox, date
                    if (0..3) === field["t"]
                      "#{field["n"]}: \"#{card_field_values[field["id"]]}\""
                    # if field type is: dropdown list
                    elsif field["t"] === 4
                        # if field name is layout set layout variable
                        if field["n"] == "layout"
                            drpdwn_id = card_field_values[field["id"]]
                            hash_with_val = field["o"].find {|element| element["id"] === drpdwn_id}
                            if hash_with_val != nil
                                layout = hash_with_val["value"]
                                next
                            end

                        else
                            drpdwn_id = card_field_values[field["id"]]
                            hash_with_val = field["o"].find {|element| element["id"] === drpdwn_id}
                            "#{field["n"]}: \"#{hash_with_val["value"]}\"" if hash_with_val != nil
                        end
                    end
                end
            end

            #get widget selector from card content and replace with liquid
            content = card.desc.gsub('[>','{{site.widgets| where: "name","')
            content = content.gsub('<]','"}}')

            template = File.open(File.join(Dir.pwd, dir_name, file_name),"w")
            #Write data into file
            template.puts <<~DOC.gsub(/\t/, '')
                    ---
                    layout: #{layout}
                    cover: "#{cover_image_url}"
                    title: #{card.name}
                    date: #{card.created_at}
                    categories: #{card.list.name.sanitize_as_post_name}
                    tags: #{label_name}
                    #{custom_fields.join("\n")}
                    ---
                    DOC
                    template.puts "#{content}"
            template.close
        end
    end
    return true
end