module Trekyll

@board_custom_fields are JSON “customFields” | trello-api board.custom_fields

@card_field_values are JSON cards: “customFieldItems” | trello-api card.custom_field_items

@lyt is layout expectation eg. page, post

Constants

VERSION

Public Class Methods

command_interpretter(arg) click to toggle source

CLI argument interpretter

# 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_trekyll() click to toggle source
# File lib/trekyll/trello_get.rb, line 12
    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

        #Custom Fields Plugin Manager
        cfm = CustomFieldsManager.new

        # 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 custom fields
        unless board.custom_fields.nil? then
            board_custom_fields = board.custom_fields
        end

        counter = 1
        wgtcounter = 1
        postcounter = 1
        cards.each do |card|
            puts "Currently on card: #{card.name}"

            # 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

                #                                        #
                # cfm.get_fields_and_layout(board_fields, card_fields, layout)   #
                #                                        #

                # Custom fields for printing in .md file
                custom_fields = ""
                layout = "page"
                unless card.custom_field_items.nil? then
                    custom_fields, layout = cfm.get_fields_and_layout(board_custom_fields, card.custom_field_items, layout)
                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(', ')
                    sorted_hash = tmp.sort_by { |k,v| k}.reverse
                    attachment_url = sorted_hash.map { |k, v| v}

                    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
                    arr = []
                    card.labels.each do |label|
                        # label_name = label_name + " " + label.name
                        arr.push(label.name)
                    end
                    label_name = arr.to_s
                end

                # Get cover image and all other attachments
                cover_id = card.cover_image_id
                cover_image_url = ""
                attachments = ""
                unless card.attachments.nil? then
                    arr = Hash.new
                    card.attachments.each do |attachment|

                        k = attachment.pos
                        v = attachment.url
                        arr[k] = v

                        if attachment.id == cover_id
                            cover_image_url = attachment.url
                        end
                    end

                    tmp = arr.sort_by { |k,v| k}.reverse
                    attachments = tmp.map { |k,v| v}

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

                end


                # Custom fields for printing in .md file
                custom_fields = ""
                layout = "post"
                unless card.custom_field_items.nil? then
                    custom_fields, layout = cfm.get_fields_and_layout(board_custom_fields, card.custom_field_items, layout)
                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}
                        weight: #{postcounter}
                        tags: #{label_name}
                        attachments: #{attachments}
                        #{custom_fields.join("\n")}
                        ---
                        DOC
                        template.puts "#{content}"
                template.close
                postcounter = postcounter.next
            end
        end
        return true
    end