Class: Adocsite::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/adocsite/templates.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplates

Returns a new instance of Templates.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/adocsite/templates.rb', line 4

def initialize
  @options = {:remove_whitespace => true}
  # files to copy
  @styles = Array.new
  @scripts = Array.new
  @images = Array.new
  @files = Array.new
  @resources = Array.new

  # theme template files
  # hashmap keys are template names without .haml extension and values are full paths
  # literal names are full file names (without path portion) and values are full paths
  @layouts = Hash.new
  @partials = Hash.new
  @includes = Hash.new
  @literals = Hash.new

  @theme_location = File.join(Adocsite::config[:TEMPLATES_FOLDER], Adocsite::config[:THEME])
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def files
  @files
end

#imagesObject (readonly)

Returns the value of attribute images.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def images
  @images
end

#includesObject (readonly)

Returns the value of attribute includes.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def includes
  @includes
end

#layoutsObject (readonly)

Returns the value of attribute layouts.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def layouts
  @layouts
end

#literalsObject (readonly)

Returns the value of attribute literals.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def literals
  @literals
end

#partialsObject (readonly)

Returns the value of attribute partials.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def partials
  @partials
end

#resourcesObject (readonly)

Returns the value of attribute resources.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def resources
  @resources
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def scripts
  @scripts
end

#stylesObject (readonly)

Returns the value of attribute styles.



3
4
5
# File 'lib/adocsite/templates.rb', line 3

def styles
  @styles
end

Instance Method Details

#get_include(include_name) ⇒ Object



73
74
75
76
77
# File 'lib/adocsite/templates.rb', line 73

def get_include(include_name)
  include_tpl = @includes[include_name]
  tpl = Tilt.new(include_tpl, @options)
  return tpl
end

#get_layout(layout_name = "default") ⇒ Object



86
87
88
89
90
# File 'lib/adocsite/templates.rb', line 86

def get_layout(layout_name = "default")
  layout_tpl = @layouts[layout_name]
  tpl = Tilt.new(layout_tpl, @options)
  return tpl
end

#get_literal(literal_name) ⇒ Object



79
80
81
82
83
84
# File 'lib/adocsite/templates.rb', line 79

def get_literal(literal_name)
  literal_tpl = @literals[literal_name]
  # literals are not processed, they are literals
  tpl = File.read(literal_tpl)
  return tpl
end

#get_partial(partial_name) ⇒ Object



67
68
69
70
71
# File 'lib/adocsite/templates.rb', line 67

def get_partial(partial_name)
  partial_tpl = @partials[partial_name]
  tpl = Tilt.new(partial_tpl, @options)
  return tpl
end

#load_assetsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/adocsite/templates.rb', line 28

def load_assets
  all_files = find_all_files

  templates = all_files.select {|path| path.end_with?(*Adocsite::config[:TEMPLATES])}
  # theme resources have to be copied with folder structure preserved so we treat them separately in Site class
  @resources = Dir[File.join(@theme_location, "resources", "**", "*")].reject {|x| File.directory?(x)}

  @styles = all_files.select {|path| path.end_with?(*Adocsite::config[:STYLES])} - @resources
  @scripts = all_files.select {|path| path.end_with?(*Adocsite::config[:SCRIPTS])} - @resources
  @images = all_files.select {|path| path.end_with?(*Adocsite::config[:IMAGES])} - @resources
  @files = all_files - @images - @scripts - @styles - @resources - templates

  # loads layouts, partials, includes and literals into hashmaps.
  # hashmap keys are template names without .haml extension and values are full paths
  # literal names are full file names (without path portion) and values are full paths
  layouts = templates.select {|path| path.start_with?(File.join(@theme_location, "layouts")) }
  layouts.each {|layout|
    layout_name = File.basename(layout, '.*')
    @layouts[layout_name] = layout
  }
  partials = templates.select {|path| path.start_with?(File.join(@theme_location, "partials")) }
  partials.each {|partial|
    partial_name = File.basename(partial, '.*')
    @partials[partial_name] = partial
  }
  includes = templates.select {|path| path.start_with?(File.join(@theme_location, "includes")) }
  includes.each {|include|
    include_name = File.basename(include, '.*')
    @includes[include_name] = include
  }
  literals = @files.select {|path| path.start_with?(File.join(@theme_location, "literals")) }
  literals.each {|literal|
    literal_name = File.basename(literal)
    @literals[literal_name] = literal
  }
  @files = @files - literals
  all_files
end