class JekyllAsciidoctorPdf::SidebarYAML

Data Structure that hold the information of an specific sidebar and generate content with that

TODO:

In order to generalize the directory structure we need to 

- add a base_path with the relative path root of all *.adoc files
- add a prefix_permanlink to get that front_matter.permalink = prefix_permalink + sidebar.url

Attributes

base_path[R]
cover_page[R]
file[R]
name[R]
parameters[R]
prefix[R]
product_name[R]
section_output_path[R]
sidebar_output_path[R]
theme_pdf[R]

Public Class Methods

new(product_name, name, file, permalinks, parameters, output_path, base_path, cover_page, theme_pdf ) click to toggle source
# File lib/jekyll_asciidoctor_pdf/sidebar.rb, line 54
def initialize(product_name, name, file, permalinks, parameters, output_path, base_path, cover_page, theme_pdf )
    @product_name = product_name
    @name         = name
    @file         = file
    @permalinks   = permalinks
    @parameters   = parameters
    @cover_page   = cover_page
    @theme_pdf    = theme_pdf
    @base_path    = base_path
    @prefix       = nil

    @section_output_path = File.join(output_path,name)
    @sidebar_output_path = File.join(output_path,'/fullsite-' + name)

    FileUtils::mkdir_p(section_output_path)
    FileUtils::mkdir_p(sidebar_output_path)

    toc = YAML.load_file(file)


    ##
    #   In the more general structure this prefix is important
    # We use in two ways
    #
    # - Move the base path to execute the asciidoctor process
    # - Use prefix + url to find in the permalinks hash
    #
    if toc and toc.key?('prefix') 
        @prefix       = toc['prefix']
        @base_path    = File.join(base_path, @prefix)
    end

    ##
    #   A few repositories may have sidebar.yml empty.
    # we should ignore it
    #
    if toc and toc.key?('entries')
        loadAndProcess('Root', toc['entries'], true)
    end

end
printTree(tree, level) click to toggle source

Print a tree structure

# File lib/jekyll_asciidoctor_pdf/sidebar.rb, line 188
def self.printTree(tree, level) 
    puts str = ("-" * level) + tree.title
    if tree.children.length > 0 
        tree.children.map { |child| printTree(child, level + 1) }
    end
end

Public Instance Methods

loadAndProcess(name, children, root) click to toggle source

We always call this function with a section or the whole sidebar (in a branch of the Tree Structure)

# File lib/jekyll_asciidoctor_pdf/sidebar.rb, line 102
def loadAndProcess(name, children, root)
    output("- Process " + name)
    node = JekyllAsciidoctorPdf::SidebarEntry.new(name, '','',root)

    #  In the section level, we will display the title
    # and increase the section level for the content
    if not root
        node.add_content("= " + node.title + "\n")
        node.add_content(":leveloffset: +1 \n")
    end

    ##
    # Loop over the children
    #
    # Case 1:
    #    - We are in a section (handled by recursion over the tree)
    #    - Add the content in the 'node' (father) and continue
    # Case 2:
    #    - We are in a leaf
    #    - Avoid absolute URL or not found permalink
    #    - Add the content in the 'node' (father) and continue
    #
    children.each do |child|
        page    = child['url']
        title   = child['title']
        entries = child['entries']

        if child.key?('entries') 
            ##
            # We are in a section
            #
            ret = loadAndProcess(title, entries,false)
            node.add_child(ret)
            node.add_content(ret.content) 
        else
            ##
            # We are in a leaf of the tree
            #
            if page.downcase.start_with?('http:') or page.downcase.start_with?('https:')
                # We ignore absolute links
                output("Absolute links are not supported!")
            else 
                if not prefix.nil?
                    page = '/' + prefix + child['url']
                end

                if permalinks.key?(page) 
                    # We have the content
                    node.add_child(JekyllAsciidoctorPdf::SidebarEntry.new(title,page, permalinks[page].content, false))
                    node.add_content(permalinks[page].content) 
                else
                    # We don't have the content
                    node.add_child(JekyllAsciidoctorPdf::SidebarEntry.new(title,page, "= Page not found "+ page + "\n", false))
                end
            end
        end
    end

    ##
    # We need to generate the content for the whole section or the whole sidebar
    if not root 
        # This is a section
        node.add_content(":leveloffset: -1 \n")
        filename = node.title.gsub(/[^0-9A-Z]/i, '_') + '.pdf'

        JekyllAsciidoctorPdf::ADoc.generateBookTypePdf(filename, section_output_path, product_name, node.title, base_path, node.content, parameters, cover_page, theme_pdf,nil, nil)
    else
        # This is the root of the sidebar
        # WARNING: We are assuming too much!. We are getting the title from the first child
        first_child = children[0];
        title = first_child['title']

        #filename = File.basename(file).ext('.pdf')
        filename = title.gsub(/[^0-9A-Z]/i, '_') + '.pdf'
        filename_path = sidebar_output_path

        JekyllAsciidoctorPdf::ADoc.generateBookTypePdf(filename, filename_path, product_name, title, base_path, node.content, parameters, cover_page, theme_pdf,nil, nil)
    end

    return node
    
end
output(string) click to toggle source
# File lib/jekyll_asciidoctor_pdf/sidebar.rb, line 195
def output(string)
    puts ' * ' + string
end