Class: Jekyll::FileBase64

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/ext.rb

Constant Summary collapse

Syntax =
/\s*file\s*=\s*(\S+)/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ FileBase64

Returns a new instance of FileBase64.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/ext.rb', line 342

def initialize(tag_name, text, tokens)

  @file = ""
  @dynamicFile = ""

  if text =~ Syntax
    dynfile = Regexp.last_match(1) 
    @dynamicFile = dynfile.gsub(/^['"]|['"]$/, '')
  else
    @filename = text.gsub(/^['"]|['"]$/, '')
  end
  
 
  
end

Instance Method Details

#getPath(text, context) ⇒ Object



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/ext.rb', line 308

def getPath(text,context)
  rootPath = $g_config['code_root_path'] || 'static'
  if text.start_with?("/")
    filePath = "#{text}"[1..-1].strip()
    filePath = File.expand_path(filePath)
  elsif  text.start_with?("@") 
    # _include/
    filePath = "#{text}"[1..-1].strip()


    site = context.registers[:site]
    user_include_path = File.join(site.source, "_includes", filePath)

    site = Jekyll.sites.first # 或你自己已有的 site
    theme = site.theme

    themeroot = theme.root # 就是当前主题的根目录
    plugin_include_file = File.join(themeroot, "_includes/" + filePath)

    if File.exist?(user_include_path)
      filePath = user_include_path
    elsif File.exist?(plugin_include_file)
      filePath = plugin_include_file
    else
      filePath = user_include_path
    end


  else
    filePath = "#{rootPath}/#{text}".strip()
    filePath = File.expand_path(filePath)
  end
  return filePath
end

#render(context) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/ext.rb', line 358

def render(context)
  filePath = @filename
  if @dynamicFile.length > 1
    filePath = context[@dynamicFile]
  end
  filePath = getPath(filePath,context)

  begin
    file = File.open(filePath)
    return Base64.strict_encode64 file.read()
  rescue => exception
    puts exception
    return "Base64 file:#{filePath} failed   #{@dynamicFile}"
    
  end
end