Class: JekyllAsciidoctorPdf::SidebarYAML

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_asciidoctor_pdf/sidebar.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product_name, name, file, permalinks, parameters, output_path, base_path, cover_page, theme_pdf) ⇒ SidebarYAML

Returns a new instance of SidebarYAML.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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
    @base_path    = base_path
    @cover_page   = cover_page
    @theme_pdf    = theme_pdf

    @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)
    loadAndProcess('Root', toc['entries'], true)

end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def base_path
  @base_path
end

#cover_pageObject (readonly)

Returns the value of attribute cover_page.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def cover_page
  @cover_page
end

#fileObject (readonly)

Returns the value of attribute file.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def parameters
  @parameters
end

Returns the value of attribute permalinks.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def permalinks
  @permalinks
end

#product_nameObject (readonly)

Returns the value of attribute product_name.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def product_name
  @product_name
end

#section_output_pathObject (readonly)

Returns the value of attribute section_output_path.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def section_output_path
  @section_output_path
end

Returns the value of attribute sidebar_output_path.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def sidebar_output_path
  @sidebar_output_path
end

#theme_pdfObject (readonly)

Returns the value of attribute theme_pdf.



51
52
53
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 51

def theme_pdf
  @theme_pdf
end

Class Method Details

.printTree(tree, level) ⇒ Object

Print a tree structure



163
164
165
166
167
168
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 163

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

Instance Method Details

#loadAndProcess(name, children, root) ⇒ Object

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



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

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 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) ⇒ Object



170
171
172
# File 'lib/jekyll_asciidoctor_pdf/sidebar.rb', line 170

def output(string)
    puts ' * ' + string
end