Class: JekyllAsciidoctorPdf::BuildTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/jekyll_asciidoctor_pdf/commands.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :build_pdf) {|_self| ... } ⇒ BuildTask

Returns a new instance of BuildTask.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(name = :build_pdf)
    @name = name
    @description = 'Generate PDF files for each *.adoc in the source_list  '

    @absolute_working_path = Dir.pwd

    @permalinks = Hash.new

    @quiet = { verbose: true } 

    yield self if block_given?

    @git_info    = GitInfo.new(git_token, repo_name)
    @absolute_config_file  = File.join(@absolute_working_path, jekyll_config_file);

    do_jekyll_config_ok? do |t|
        @absolute_source_path   = File.join(@absolute_working_path, @parameters['source_path']);
        @absolute_output_path   = File.join(@absolute_working_path, @parameters['output_path']);
        @absolute_sidebar_path  = File.join(@absolute_working_path, @parameters['sidebar_path']);
        @absolute_cover_page    = File.join(@absolute_output_path, 'assets','cover_page_background.jpg');
        @absolute_theme_pdf     = File.join(@absolute_output_path, 'assets','pdf-theme.yml');

        define_task!
    end
end

Instance Attribute Details

#absolute_config_fileObject (readonly)

Returns the value of attribute absolute_config_file.



63
64
65
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 63

def absolute_config_file
  @absolute_config_file
end

#absolute_cover_pageObject (readonly)

Returns the value of attribute absolute_cover_page.



67
68
69
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 67

def absolute_cover_page
  @absolute_cover_page
end

#absolute_output_pathObject (readonly)

Returns the value of attribute absolute_output_path.



65
66
67
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 65

def absolute_output_path
  @absolute_output_path
end

#absolute_sidebar_pathObject (readonly)

Returns the value of attribute absolute_sidebar_path.



64
65
66
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 64

def absolute_sidebar_path
  @absolute_sidebar_path
end

#absolute_source_pathObject (readonly)

Returns the value of attribute absolute_source_path.



62
63
64
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 62

def absolute_source_path
  @absolute_source_path
end

#absolute_theme_pdfObject (readonly)

Returns the value of attribute absolute_theme_pdf.



68
69
70
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 68

def absolute_theme_pdf
  @absolute_theme_pdf
end

#absolute_working_pathObject (readonly)

Returns the value of attribute absolute_working_path.



61
62
63
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 61

def absolute_working_path
  @absolute_working_path
end

#description#to_s



56
57
58
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 56

def description
  @description
end

#git_infoObject (readonly)

Returns the value of attribute git_info.



69
70
71
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 69

def git_info
  @git_info
end

#name#to_sym



53
54
55
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 53

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



58
59
60
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 58

def parameters
  @parameters
end

Returns the value of attribute permalinks.



59
60
61
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 59

def permalinks
  @permalinks
end

#quietObject (readonly)

Returns the value of attribute quiet.



71
72
73
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 71

def quiet
  @quiet
end

Class Method Details

.callable_attr(attr_name, default_value = nil) { ... } ⇒ Object

Defines attribute accessor with optional default value. When attribute’s value is a Proc with arity 0, then the attribute reader calls it and returns the result.

Yields:

  • When the block is given, then it’s used as a default value. It takes precedence over the default_value. It’s evaluated in an instance context.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 32

def self.callable_attr(attr_name, default_value = nil, &default_block)
  var_name = "@#{attr_name}".sub('?', '').to_sym

  define_method attr_name do
    value = instance_variable_get(var_name)

    if value.nil? && default_block
      do_in_working_dir { instance_eval &default_block }
    elsif value.nil?
      default_value
    elsif value.is_a?(Proc) && value.arity.zero?
      do_in_working_dir &value
    else
      value
    end
  end

  attr_writer attr_name.to_s.sub('?', '')
end

Instance Method Details

#assert(condition, message) ⇒ Object

Assert function



311
312
313
314
315
316
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 311

def assert(condition, message)
    if not condition
        output(message)
        exit 1
    end
end

#assert_key(array, key, message) ⇒ Object



157
158
159
160
161
162
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 157

def assert_key(array, key, message)
    if not array.has_key? key 
        output(message)
        exit 1
    end
end

#cleanObject



179
180
181
182
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 179

def clean
    output("Removing files & output directories ...")
    rm_rf(absolute_output_path, quiet)
end

#define_task!Object



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

def define_task!

    desc "Print information variables"
    task :info do
        print_variables
    end

    desc "Cleaning files & directories"
    task :clean do
        clean
    end

    desc "Initialize files & directories"
    task :init => [ :clean ] do
        init 
    end


    desc description
    task name.to_sym => [:clean, :init] do
        generate_individual_pdf
        generate_fullsite_adoc
        undo_init
    end




end

#do_jekyll_config_ok?Boolean

Check if Jekyll configuration is Okay



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 143

def do_jekyll_config_ok?
    config = YAML.load_file(absolute_config_file)
   
    assert_key(config, 'jap_config', "Expected key 'jap_config' to exist, but does not") 

    @parameters   = config['jap_config']

    assert_key(@parameters, 'source_path' , "Expected key 'jap_config.source_path' to exist, but does not") 
    assert_key(@parameters, 'output_path' , "Expected key 'jap_config.output_path' to exist, but does not") 
    assert_key(@parameters, 'sidebar_path', "Expected key 'jap_config.sidebar_path' to exist, but does not") 

    yield
end

#generate_fullsite_adocObject

Loop over each YAML file in the sidebar_path

Steps:

- Execute generateContent to create all sections and one file with the whole content of the sitebar

Output directories:

<output_path>
     |__ <sidebar>/<section>.pdf           -> Section output
     |__ fullsite/<sidebar>.pdf   -> whole sidebar content (a.k.a. fullsite PDF)


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

def generate_fullsite_adoc


    FileList[absolute_sidebar_path + "*.yml"].each do |file|
        output("Generating content for " + File.basename(file))

        name = File.basename(file).ext('')

        JekyllAsciidoctorPdf::SidebarYAML.new(
            product_name, 
            name, 
            file, 
            permalinks, 
            parameters, 
            absolute_output_path, 
            absolute_source_path,
            absolute_cover_page,
            absolute_theme_pdf
        )

        #cd absolute_working_path

        #sidebar.generatePdf()

    end
end

#generate_individual_pdfObject

Loop over each AsciiDoctor file recursively

Steps:

  1. Read the file

  2. Remove jekyll fronmatter

  3. Generate a PDF with the content and put it in <output>/pages//<filename>.pdf

  4. Get the ‘permalink’ and add the content to a hash to be used by sidebar process

Warning: We should keep the original directory structure to avoid name collisions



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

def generate_individual_pdf


    output("Generating content for each individual pages")

    pdf_pages  = File.join(absolute_output_path, '/pages') 

    cd absolute_source_path
    FileList["**/*.adoc"].each do |file|

        last_modified_at = last_modified_at_unix(file)

        output_path = File.join(pdf_pages,File.dirname(file))
        mkdir_p_if_not_exist(output_path)

        authors = git_info.getAuthorsList(file)

        JekyllAsciidoctorPdf::ADoc.generatePdf(product_name, file, output_path, parameters, absolute_cover_page, absolute_theme_pdf, last_modified_at, authors ) do |front_matter, stream_adoc|
            if front_matter.key?('permalink')  
                name = File.join('/', front_matter['permalink'])
                data = PermalinkData.new(front_matter['sidebar'], stream_adoc, file, last_modified_at)
                permalinks[name] = data
            end 
        end


    end 

end

#getPdfThemeObject

Load the asciidoctor-pdf theme from the jekyll _config.yml



330
331
332
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 330

def getPdfTheme()
    return parameters['pdf_theme'].to_yaml
end

#initObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 187

def init
    output("Initialize files & output directories ...")
    output_fullsite = File.join(absolute_output_path, '/fullsite')
    output_pages    = File.join(absolute_output_path, '/pages')
    output_assets   = File.join(absolute_output_path, '/assets')

    mkdir_p(absolute_output_path, quiet)
    mkdir_p(output_pages,quiet)
    mkdir_p(output_assets,quiet)
    mkdir_p(output_fullsite,quiet)

    fullsite_config         = parameters['fullsite']
    background_image        = File.join(absolute_working_path, fullsite_config['background_image'])
    cp_r(background_image, absolute_cover_page, quiet)

    File.open(absolute_theme_pdf, "w") { |file| file.write(getPdfTheme() ) }

end

#last_modified_at_unix(file) ⇒ Object

Taken from jekyll-last-modified-at



219
220
221
222
223
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 219

def last_modified_at_unix(file)
    last_commit_date = value = %x[ git log -1 --pretty="format:%ct" #{file} ]
     # last_commit_date can be nil iff the file was not committed.
     last_commit_date.nil? || last_commit_date.empty? ? File.mtime(file) : Time.at(last_commit_date.to_i)
end

#mkdir_p_if_not_exist(dir) ⇒ Object

Create a directory only if its necessary



321
322
323
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 321

def mkdir_p_if_not_exist(dir)
    mkdir_p(dir, quiet) unless File.exists?(dir)
end

#output(string) ⇒ Object



334
335
336
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 334

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

Only to debug



168
169
170
171
172
173
174
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 168

def print_variables 
    output("Current directory: "   + absolute_working_path)
    output("Jekyll Config File: "  + absolute_config_file)
    output("Source directory: "    + absolute_source_path)
    output("Sidebar directory: "   + absolute_sidebar_path)
    output("Building directory: "  + absolute_output_path)
end

#undo_initObject



209
210
211
212
213
# File 'lib/jekyll_asciidoctor_pdf/commands.rb', line 209

def undo_init
    output("Removing auxiliaries files & directories ...")
    output_assets = File.join(absolute_output_path, '/assets')
    rm_rf(output_assets, quiet)
end