Class: Scriptorium::Theme

Inherits:
Object
  • Object
show all
Extended by:
Contract, Helpers
Includes:
Contract, Exceptions, Helpers
Defined in:
lib/scriptorium/theme.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

add_post_to_state_file, cf_time, change_config, clean_slugify, copy_gem_asset_to_user, copy_to_clipboard, d4, escape_html, find_asset, format_date, generate_missing_asset_svg, get_asset_path, get_from_clipboard, getvars, list_gem_assets, make_dir, make_tree, need, parse_commented_file, post_compare, post_in_state_file?, read_commented_file, read_file, read_post_state_file, remove_post_from_state_file, see, see_file, slugify, substitute, system!, tty, view_dir, write_file, write_file!, write_post_state_file, ymdhms, ymdhms_filename

Methods included from Exceptions

#make_exception

Methods included from Contract

assume, check_invariants, enabled?, invariant, verify

Constructor Details

#initialize(root, name) ⇒ Theme



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/scriptorium/theme.rb', line 179

def initialize(root, name)
  assume { root.is_a?(String) && !root.empty? }
  assume { name.is_a?(String) && !name.empty? }
  
  @root = root
  @name = name
  
  define_invariants
  verify { @root == root }
  verify { @name == name }
  check_invariants
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/scriptorium/theme.rb', line 9

def name
  @name
end

Class Method Details

.copy_gem_assets_to_library(root) ⇒ Object



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
# File 'lib/scriptorium/theme.rb', line 118

def self.copy_gem_assets_to_library(root)
  # Try to find gem assets and copy application-wide assets to the library
  gem_spec = Gem.loaded_specs['scriptorium']
  if gem_spec
    gem_assets_dir = "#{gem_spec.full_gem_path}/assets"
  else
    # Development environment - use the working path
    gem_assets_dir = File.expand_path("assets")
  end
  
  if Dir.exist?(gem_assets_dir)
    # Copy application-wide assets to library directory
    library_dir = root/"assets"/"library"
    FileUtils.mkdir_p(library_dir) unless Dir.exist?(library_dir)
    
    # Copy sample assets (application-wide)
    samples_gem_dir = "#{gem_assets_dir}/samples"
    if Dir.exist?(samples_gem_dir)
      FileUtils.cp_r("#{samples_gem_dir}/.", library_dir)
    end
    
    # Copy icons directory (needed for UI and social buttons)
    icons_gem_dir = "#{gem_assets_dir}/icons"
    if Dir.exist?(icons_gem_dir)
      icons_dir = root/"assets"/"icons"
      FileUtils.mkdir_p(icons_dir) unless Dir.exist?(icons_dir)
      FileUtils.cp_r("#{icons_gem_dir}/.", icons_dir)
    end
    
    # Copy missing image placeholder
    missing_image_gem = "#{gem_assets_dir}/imagenotfound.jpg"
    if File.exist?(missing_image_gem)
      FileUtils.cp(missing_image_gem, root/"assets"/"imagenotfound.jpg")
    end
  end
rescue => e
  # If gem lookup fails, continue without copying gem assets
  # This is expected in development/testing environments
end

.copy_gem_assets_to_theme(theme_dir) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
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
# File 'lib/scriptorium/theme.rb', line 68

def self.copy_gem_assets_to_theme(theme_dir)
  # Try to find gem assets and copy only theme-specific assets to the theme
  gem_spec = Gem.loaded_specs['scriptorium']
  if gem_spec
    gem_assets_dir = "#{gem_spec.full_gem_path}/assets"
  else
    # Development environment - use the working path
    gem_assets_dir = File.expand_path("assets")
  end
  
  if Dir.exist?(gem_assets_dir)
    # Copy only theme-specific assets to theme assets directory
    theme_assets_dir = theme_dir/"assets"
    FileUtils.mkdir_p(theme_assets_dir) unless Dir.exist?(theme_assets_dir)
    
    # Copy theme-specific assets (themes/ directory)
    theme_gem_dir = "#{gem_assets_dir}/themes"
    if Dir.exist?(theme_gem_dir)
      FileUtils.cp_r("#{theme_gem_dir}/.", theme_assets_dir)
    end
    
    # Copy theme-specific icons (icons/ui/ and icons/social/ - these could be theme-specific)
    icons_gem_dir = "#{gem_assets_dir}/icons"
    if Dir.exist?(icons_gem_dir)
      # Create icons directory in theme assets
      theme_icons_dir = theme_assets_dir/"icons"
      FileUtils.mkdir_p(theme_icons_dir)
      
      # Copy UI icons (could be theme-specific)
      ui_icons_dir = "#{icons_gem_dir}/ui"
      if Dir.exist?(ui_icons_dir)
        theme_ui_dir = theme_icons_dir/"ui"
        FileUtils.mkdir_p(theme_ui_dir)
        FileUtils.cp_r("#{ui_icons_dir}/.", theme_ui_dir)
      end
      
      # Copy social icons (could be theme-specific)
      social_icons_dir = "#{icons_gem_dir}/social"
      if Dir.exist?(social_icons_dir)
        theme_social_dir = theme_icons_dir/"social"
        FileUtils.mkdir_p(theme_social_dir)
        FileUtils.cp_r("#{social_icons_dir}/.", theme_social_dir)
      end
    end
  end
rescue => e
  # If gem lookup fails, continue without copying gem assets
  # This is expected in development/testing environments
end

.create_standard(root) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
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
66
# File 'lib/scriptorium/theme.rb', line 17

def self.create_standard(root)
  assume { root.is_a?(String) && !root.empty? }
  
  make_tree(root/:themes/:standard, "    .\n    \u251C\u2500\u2500 README.txt\n    \u251C\u2500\u2500 assets/\n    \u251C\u2500\u2500 config.txt\n    \u251C\u2500\u2500 header/\n    \u251C\u2500\u2500 initial/\n    \u2502   \u2514\u2500\u2500 post.lt3\n    \u251C\u2500\u2500 layout/\n    \u2502   \u251C\u2500\u2500 config/\n    \u2502   \u2502   \u251C\u2500\u2500 footer.txt\n    \u2502   \u2502   \u251C\u2500\u2500 header.txt\n    \u2502   \u2502   \u251C\u2500\u2500 left.txt\n    \u2502   \u2502   \u251C\u2500\u2500 main.txt\n    \u2502   \u2502   \u2514\u2500\u2500 right.txt\n    \u2502   \u251C\u2500\u2500 gen/\n    \u2502   \u2502   \u2514\u2500\u2500 text.css\n    \u2502   \u2514\u2500\u2500 layout.txt\n    \u2514\u2500\u2500 templates/\n        \u251C\u2500\u2500 index.lt3\n        \u251C\u2500\u2500 post.lt3\n        \u251C\u2500\u2500 index_entry.lt3\n        \u2514\u2500\u2500 widget.lt3\n  EOS\n  write_file(\"/tmp/ttree.txt\", `tree`)\n  predef = Scriptorium::StandardFiles.new\n  std = root/:themes/:standard\n  write_file(std/:initial/\"post.lt3\",          predef.initial_post(:raw))\n  copy_support_file('templates/post.lt3', std/:templates/\"post.lt3\")\n  copy_support_file('templates/index_entry.lt3', std/:templates/\"index_entry.lt3\")\n  layout_text = std/:layout/\"layout.txt\"\n  copy_support_file('templates/layout.txt', layout_text)\n  config, gen = std/:layout/:config, std/:layout/:gen\n  copy_support_file('theme/header.lt3', config/\"header.txt\")\n  copy_support_file('theme/footer.lt3', config/\"footer.txt\")\n  copy_support_file('theme/left.lt3', config/\"left.txt\")\n  copy_support_file('theme/right.lt3', config/\"right.txt\")\n  copy_support_file('theme/main.lt3', config/\"main.txt\")\n  \n  # Copy gem assets to standard theme\n  copy_gem_assets_to_theme(std)\n  \n  # Create system.txt to identify system themes\n  write_file(root/:themes/\"system.txt\", \"standard\\n\")\n  \n  verify { Dir.exist?(root/:themes/\"standard\") }\nend\n")

Instance Method Details

#define_invariantsObject

Invariants



12
13
14
15
# File 'lib/scriptorium/theme.rb', line 12

def define_invariants
  invariant { @root.is_a?(String) && !@root.empty? }
  invariant { @name.is_a?(String) && !@name.empty? }
end

#file(portion) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/scriptorium/theme.rb', line 158

def file(portion)
  check_invariants
  assume { portion.is_a?(String) && !portion.empty? }
  
  paths = Find.find(@root/:themes/name)
  found = paths.find_all {|x| x.include?(portion) }
  case 
  when found.size == 1
    result = found[0]
    verify { result.is_a?(String) && File.exist?(result) }
    check_invariants
    return result
  when found.size > 1
    # puts "Search for #{portion} found"
    # found.each {|x| puts "  #{x}"}
    raise MoreThanOneResult(portion)
  else 
    raise ThemeFileNotFound(portion)
  end
end