static 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/collection.rb ADDED
@@ -0,0 +1,112 @@
1
+ class Collection
2
+ attr_accessor :name, :attrs, :base, :listing, :entries, :contents
3
+ include FileBase
4
+ def initialize base, where, name
5
+ set_home where
6
+ @base = base
7
+ @name = name
8
+ @attrs = []
9
+ @listing = []
10
+ @entries = []
11
+ setup_location 'collections'
12
+ set_home path+name
13
+ end
14
+
15
+ def file_name
16
+ name.as_file
17
+ end
18
+
19
+ def self.start base, where, name, attrs
20
+ return nil if self.collection_exists?(base, where, name)
21
+ c = self.new base, where, name
22
+ c.attrs << attrs
23
+ c.attrs.flatten!
24
+ c.write_attribute_file
25
+ c.load_attributes
26
+ c.write_display_file
27
+ copy base+'.templates/disco/link_list', c.home
28
+ c.write_publish_file
29
+ c.write '', c.home+'listing'
30
+ c.mkdir c.home+'entries'
31
+ c
32
+ end
33
+
34
+ def self.collection_exists?(base, where, name)
35
+ exists?(:folder, base) && exists?(:folder, where+'collections/'+name) && exists?(:file, where+"collections/#{name}/attributes")
36
+ end
37
+ def self.load base, where, name
38
+ return nil unless self.collection_exists?(base, where, name)
39
+ c = self.new base, where, name
40
+ c.load_attributes
41
+ c.load_listing
42
+ c.sanitize_listing
43
+ c.load_entries
44
+ c
45
+ end
46
+
47
+ def save_listing
48
+ File.open(home+'listing', 'w') { |yf| YAML.dump(@listing, yf) }
49
+ end
50
+
51
+ def sanitize_listing
52
+ tmp = []
53
+ @listing.each { |item|
54
+ entry = load_entry(item)
55
+ add = false
56
+ entry.each { |key, value|
57
+ add = true if value and key != 'date' and key != :id
58
+ }
59
+ tmp << item if add
60
+ }
61
+ @listing = tmp
62
+ end
63
+
64
+ def write_attribute_file
65
+ write_template base+'.templates/disco/attributes', attributes
66
+ end
67
+ def write_display_file
68
+ write_template base+'.templates/disco/display', blueprint
69
+ end
70
+ def write_publish_file
71
+ write_template base+'.templates/disco/publish', publish
72
+ end
73
+ def write_link_list_file
74
+ write_template base+'.templates/disco/link_list', publish
75
+ end
76
+ def publish
77
+ home+'publish'
78
+ end
79
+ def blueprint
80
+ home+'display'
81
+ end
82
+ def attributes
83
+ home+'attributes'
84
+ end
85
+ def load_attributes
86
+ File.open( home+'attributes' ) { |yf| @attrs = YAML::load( yf )[name] }
87
+ end
88
+ def load_listing
89
+ File.open( home+'listing' ) { |yf| @listing = YAML::load( yf ) }
90
+ @listing ||= []
91
+ @listing = @listing.sort.reverse
92
+ end
93
+ def load_entries
94
+ listing.each {|entry| entries << load_entry(entry) }
95
+ entries
96
+ end
97
+ def load_entry(file)
98
+ tmp = nil
99
+ File.open( home+'entries/'+file ) {|yf| tmp = YAML::load( yf )[name].merge({:id => file})}
100
+ tmp
101
+ end
102
+ def add_entry title=nil
103
+ timestamp = Time.now.to_i.to_s
104
+ title ||= timestamp
105
+ load_listing
106
+ listing.unshift timestamp
107
+ write_template home+'attributes', home+'entries/'+timestamp
108
+ return home+'entries/'+timestamp
109
+ end
110
+ end
111
+
112
+
@@ -0,0 +1,43 @@
1
+
2
+ class Class # :nodoc:
3
+ def cattr_reader(*syms)
4
+ syms.flatten.each do |sym|
5
+ class_eval(<<-EOS, __FILE__, __LINE__)
6
+ unless defined? @@#{sym}
7
+ @@#{sym} = nil
8
+ end
9
+
10
+ def self.#{sym}
11
+ @@#{sym}
12
+ end
13
+
14
+ def #{sym}
15
+ @@#{sym}
16
+ end
17
+ EOS
18
+ end
19
+ end
20
+
21
+ def cattr_writer(*syms)
22
+ syms.flatten.each do |sym|
23
+ class_eval(<<-EOS, __FILE__, __LINE__)
24
+ unless defined? @@#{sym}
25
+ @@#{sym} = nil
26
+ end
27
+
28
+ def self.#{sym}=(obj)
29
+ @@#{sym} = obj
30
+ end
31
+
32
+ def #{sym}=(obj)
33
+ @@#{sym} = obj
34
+ end
35
+ EOS
36
+ end
37
+ end
38
+
39
+ def cattr_accessor(*syms)
40
+ cattr_reader(*syms)
41
+ cattr_writer(*syms)
42
+ end
43
+ end
@@ -0,0 +1,10 @@
1
+
2
+
3
+ class String
4
+ def as_file; self.gsub(/([!?,]|[^a-zA-Z0-9\.]$)/, '').gsub(/[^a-zA-Z0-9\.\/]/, '_').downcase end
5
+ def as_folder; self.as_file+((self.as_file=~/\/$/||self=='') ? '' : '/') end
6
+ def as_ext; self[0,0]='.' unless self[0,1]=='.'|| self==''; self end
7
+ def as_file_title; self.as_file.gsub /_/, ' ' end
8
+ def filename_as_symbol; self.as_file.to_sym end
9
+ end
10
+
data/lib/filebase.rb ADDED
@@ -0,0 +1,84 @@
1
+
2
+ require 'erb'
3
+ require 'fileutils'
4
+
5
+ module FileBase
6
+ include FileUtils
7
+
8
+ Home = ''
9
+ def home; (@home||Home).as_folder end
10
+ def set_home(here)
11
+ mkdir here unless exists? :folder, here
12
+ @home=here
13
+ end
14
+
15
+ def location; (@location||'').as_folder end
16
+ def set_location where; @location=where end
17
+ def setup_location where; @location=where; mkdir path unless exists? :folder, path end
18
+ def reset_location; @location='' end
19
+ def has_location?; location!='' end
20
+
21
+ def path; home+location end
22
+ def file_path; home+location+filename end
23
+
24
+ def exists? type, item
25
+ return false unless File.exist? item
26
+ truth = case type
27
+ when :folder
28
+ return(File.directory?(item))
29
+ when :file
30
+ return(!File.directory?(item))
31
+ else
32
+ return(true)
33
+ end
34
+ end
35
+
36
+ def items_under(pth, match=nil)
37
+ match||=''
38
+ folders = []
39
+ files = []
40
+ Dir.entries(pth).each { |item| (exists?(:folder, pth.as_folder+item) ? folders << item : files << item) unless item[0,1]=='.' or not item=~/#{match}/}
41
+ {:folders => folders, :files => files}
42
+ end
43
+
44
+
45
+ def move(from, to=home)
46
+ FileUtils.mv from, to
47
+ end
48
+
49
+ def copy(from, to=home)
50
+ FileUtils.cp_r from, to
51
+ end
52
+
53
+ def mkdir where, force=false
54
+ begin
55
+ Dir.mkdir where
56
+ rescue
57
+ raise $! unless force
58
+ destroy where
59
+ mkdir where
60
+ end
61
+ where
62
+ end
63
+
64
+ def make_dir path, force=false
65
+ mkdir path, force
66
+ puts 'creating '+path
67
+ end
68
+
69
+ def destroy what
70
+ FileUtils.rm_rf what
71
+ puts 'destroying '+what
72
+ end
73
+
74
+ def write(contents, where)
75
+ File.open(where, 'w'){ |f| f << contents }
76
+ end
77
+ def write_template what, where
78
+ write ERB.new(File.read(what)).result(binding), where
79
+ end
80
+
81
+ alias loc location
82
+ alias loc= set_location
83
+ end
84
+
data/lib/generator.rb ADDED
@@ -0,0 +1,20 @@
1
+ class Generator
2
+ include Singleton
3
+ cattr_accessor :act_on, :actors
4
+
5
+ def self.load(what)
6
+ self.actors ||= []
7
+ self.actors << self.act_on if self.act_on
8
+ self.act_on = what
9
+ end
10
+
11
+ def self.unload
12
+ self.act_on = self.actors.shift
13
+ end
14
+
15
+ def self.method_missing(sym, *args)
16
+ act_on.send(sym, *args)
17
+ end
18
+ end
19
+
20
+
data/lib/page.rb ADDED
@@ -0,0 +1,124 @@
1
+ class Page
2
+ include FileBase
3
+
4
+ cattr_accessor :pages
5
+ @@pages=[]
6
+ attr_accessor :name, :subs, :page_location, :contents, :stylesheets, :index, :site_location, :site_base, :breadcrumbs
7
+ @@templates = {}
8
+
9
+ def initialize(name, home, location, generate_to, subs=nil, index=nil)
10
+ @home = home
11
+ @page_location = location
12
+ @name = name
13
+ @subs = subs
14
+ @site_base = generate_to.as_folder
15
+ @index = index
16
+ @breadcrumbs = []
17
+ @stylesheets = []
18
+ pages << self
19
+ end
20
+
21
+ def file_name
22
+ name.as_file
23
+ end
24
+ def to_s
25
+ name.downcase
26
+ end
27
+ def self.find(name)
28
+ item = nil
29
+ name = name.to_s
30
+ self.pages.each{ |p| item = p if p.file_name==name.as_file}
31
+ item
32
+ end
33
+
34
+ def index?; @index end
35
+
36
+ def generate collection_name
37
+ stylesheets << contents['stylesheets'] if contents['stylesheets']
38
+ mkdir in_site_location unless exists?(:folder, in_site_location) or index?
39
+ collection = load_collection(collection_name) if collection_name
40
+ if collection
41
+ Generator.class_eval { cattr_accessor :include_text }
42
+ Generator.include_text = File.read home+"blueprint/#{page_location}_#{collection_name}_for_#{file_name}/#{collection.first}"
43
+ write ::Rbml::Processor.new.instance_eval(File.read(home+'layout/structure/default.rbml')), in_site_location+'index.html'
44
+ puts 'writing the index'
45
+ collection.each { |col|
46
+ Generator.include_text = File.read home+"blueprint/#{page_location}_#{collection_name}_for_#{file_name}/#{col}"
47
+ write ::Rbml::Processor.new.instance_eval(File.read(home+'layout/structure/default.rbml')), in_site_location+col+'.html'
48
+ puts "writing the entry for #{col}"
49
+ }
50
+ Generator.class_eval { @include_text = nil }
51
+ else
52
+ write ::Rbml::Processor.new.instance_eval(File.read(home+'layout/structure/default.rbml')), in_site_location+'index.html'
53
+ end
54
+ puts "writing #{in_site_location}index.html"
55
+ end
56
+
57
+ def load_collection name
58
+ stuff = nil
59
+ File.open(home+ 'blueprint/'+page_location+"_#{name}_for_#{file_name}/page.list") { |yf| stuff = YAML::load( yf ) }
60
+ stuff
61
+ end
62
+
63
+ def in_site_location
64
+ (index? ? site_base : ( has_subs? && subs.kind_of?(Array) ? site_base+page_location : site_base+page_location+name.as_file )).as_folder
65
+ end
66
+ def in_site_link
67
+ in_site_location.sub(site_base, '')
68
+ end
69
+ def level
70
+ tmp = (index? || has_subs? && subs.kind_of?(Array)) ? '' : '../'
71
+ tmp+page_location.gsub(/\w+\//, '../')
72
+ end
73
+
74
+ def menu_title
75
+ case subs
76
+ when Array : name
77
+ when String : subs
78
+ else
79
+ nil
80
+ end
81
+ end
82
+
83
+ def load_contents
84
+ File.open(home+ 'contents/'+page_location+name.as_file+'.copy') { |yf| @contents = YAML::load( yf ) }
85
+ end
86
+
87
+ def extentions
88
+ {
89
+ :contents => '.copy',
90
+ :blueprint => '.rbml',
91
+ :page => '.html'
92
+ }
93
+ end
94
+
95
+ def make(type)
96
+ page_path = home+type.to_s+'/'+page_location
97
+ mkdir page_path unless exists? :folder, page_path
98
+ template_file = case type
99
+ when :contents : {:main => home+'.templates/page.copy', :sub => home+'.templates/submenu.copy'}
100
+ when :blueprint : {:main => home+'layout/contents.erbml', :sub => home+'layout/submenu.erbml'}
101
+ end
102
+
103
+ if subs && subs.kind_of?(Array)
104
+ unless exists?(:file, page_path+'submenu'+extentions[type])
105
+ write_template template_file[:sub], page_path+'submenu'+extentions[type]
106
+ puts "writing #{page_path}submenu#{extentions[type]}"
107
+ else
108
+ puts "#{page_path}submenu#{extentions[type]} already exists"
109
+ end
110
+ end
111
+ unless exists?(:file, page_path+name.as_file+extentions[type])
112
+ write_template template_file[:main], page_path+name.as_file+extentions[type]
113
+ puts "writing #{page_path}#{name.as_file}#{extentions[type]}"
114
+ else
115
+ puts "#{page_path}#{name.as_file}#{extentions[type]} already exists"
116
+ end
117
+ end
118
+
119
+ def has_subs?
120
+ subs && !subs.empty?
121
+ end
122
+ end
123
+
124
+
data/lib/site.rb ADDED
@@ -0,0 +1,144 @@
1
+ class Site
2
+ include FileBase
3
+
4
+ attr_accessor :name, :map, :info, :pages, :main_links, :site_loc
5
+
6
+ def initialize(where, name, generate_to, templates=nil)
7
+ set_home where+name
8
+ @name = name
9
+ @pages = []
10
+ @main_links = []
11
+ mkdir generate_to unless exists? :folder, generate_to
12
+ @site_loc = generate_to + name
13
+ end
14
+
15
+ def self.load(where, name, generate_to, templates)
16
+ return nil unless self.site_exists?(where, name)
17
+ s = self.new(where, name, generate_to, templates)
18
+ s.load_info
19
+ s.load_pages
20
+ s
21
+ end
22
+
23
+ def self.site_exists?(where, name)
24
+ (exists?(:folder, where+name) && exists?(:folder, where+name+'/project_info'))
25
+ end
26
+ def self.start where, name, generate_to, templates
27
+ return nil if self.site_exists?(where, name)
28
+ site = Site.new(where, name, generate_to, templates)
29
+ site.mkdir(site.home+'project_info')
30
+ site.setup_location 'project_info'
31
+ site.write_template templates+'site.map', site.path+'site.map'
32
+ site.write_template templates+'site.info', site.path+'site.info'
33
+ site.setup_location '.templates'
34
+ site.copy templates+'page.copy', site.path+'page.copy'
35
+ site.copy templates+'submenu.copy', site.path+'submenu.copy'
36
+ site.load_info
37
+ site
38
+ end
39
+
40
+ def load_info(file=nil)
41
+ File.open(home+ 'project_info/site.map') { |yf| @map = YAML::load( yf ) } unless file=='site.info'
42
+ File.open(home+'project_info/site.info') { |yf| @info = YAML::load( yf ) } unless file=='site.map'
43
+ end
44
+
45
+ def save_info(file=nil)
46
+ File.open(home+'project_info/site.map', 'w') { |yf| YAML.dump(@map, yf) } unless file=='site.info'
47
+ File.open(home+'project_info/site.info', 'w') { |yf| YAML.dump(@info, yf) } unless file=='site.map'
48
+ end
49
+
50
+ def set_layout(name, address)
51
+ copy address+name, home+'layout/' unless exists? :folder, home+'layout' rescue puts $!
52
+ info['layout'] = {'name' => name, 'address' => address}
53
+ end
54
+
55
+ def load_pages
56
+ gather_pages
57
+ make_breadcrumbs
58
+ end
59
+ def gather_pages(page=nil, first_time=true, parent=nil)
60
+ index = nil
61
+ if first_time
62
+ reset_location
63
+ @pages = []
64
+ page ||= map
65
+ else
66
+ index = true if @pages.empty?
67
+ end
68
+ case page
69
+ when String : pages << Page.new(page, home, location, site_loc, parent, index)
70
+ when Array then
71
+ tmp = page.dup
72
+ gather_pages(tmp.shift, false, parent) while(tmp.size > 0)
73
+ when Hash then
74
+ page.each { |page_name, sub_pages|
75
+ unless first_time
76
+ set_location(pages.empty? ? location : location+page_name )
77
+ pages << Page.new(page_name, home, location, site_loc, sub_pages, index)
78
+ else
79
+ page_name = nil
80
+ end
81
+ gather_pages(sub_pages, false, page_name)
82
+ set_location location.sub(page_name.as_file+'/', '') if page_name
83
+ }
84
+ else
85
+ return nil
86
+ end
87
+ end
88
+
89
+ def make_breadcrumbs
90
+ pages.each do |page|
91
+ page.breadcrumbs << page.name.as_file
92
+ page.subs.each { |p|
93
+ if p.kind_of?(Hash)
94
+ p.each{ |k, v|
95
+ tmp = Page.find(k)
96
+ tmp.breadcrumbs << page.breadcrumbs }
97
+ else
98
+ tmp = Page.find(p).breadcrumbs << page.breadcrumbs
99
+ end
100
+ } if page.has_subs? && page.subs.kind_of?(Array)
101
+ page.breadcrumbs.flatten!
102
+ end
103
+ end
104
+
105
+ def load_main_links
106
+ map['site_map'].each { |page|
107
+ case page
108
+ when String
109
+ main_links << page
110
+ when Hash
111
+ page.each { |name, subs| main_links << name }
112
+ end
113
+ }
114
+ end
115
+
116
+ def build_header
117
+ puts info.inspect
118
+ write_template home+"layout/page_head.erbml", home+'layout/structure/page_head.rbml'
119
+ write_template home+"layout/main_navigation.erbml", home+'layout/structure/main_navigation.rbml'
120
+ end
121
+
122
+ def build(type)
123
+ setup_location(type.to_s)
124
+ if type == :blueprint
125
+ @main_links = []
126
+ load_main_links
127
+ build_header
128
+ end
129
+ pages.each{|page| page.make(type)}
130
+ end
131
+
132
+ def generate
133
+ mkdir site_loc unless exists? :folder, site_loc
134
+ copy home+'layout/stylesheets', site_loc rescue puts $!
135
+ pages.each do |page|
136
+ page.stylesheets = []
137
+ Generator.load(page)
138
+ page.load_contents
139
+ page.stylesheets << info['stylesheets']
140
+ page.generate info['paginate'] ? info['paginate'][page.file_name] : nil
141
+ end
142
+ end
143
+ end
144
+
data/lib/static.rb ADDED
@@ -0,0 +1,96 @@
1
+
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
4
+
5
+ require 'yaml'
6
+ require 'erb'
7
+ require 'singleton'
8
+
9
+ require 'extentions/string'
10
+ require 'extentions/class'
11
+
12
+ require 'filebase'
13
+
14
+ require 'site'
15
+ require 'page'
16
+
17
+ require 'collection'
18
+ require 'generator'
19
+
20
+
21
+ require 'rubygems'
22
+ require 'rbml'
23
+
24
+ require 'xhtml'
25
+
26
+ $ENV = {}
27
+ $ENV['EDITOR'] = 'vi'
28
+
29
+ module Static
30
+ VERSION = '0.0.1'
31
+ include FileBase
32
+
33
+ def start_site name
34
+ site = Site.start($ENV['SITE_BANK'], name, $ENV['STATIC_VIEWING'], $ENV['TEMPLATE_HOME'])
35
+ site
36
+ end
37
+
38
+ def load_site name
39
+ Site.load($ENV['SITE_BANK'], name, $ENV['STATIC_VIEWING'], $ENV['TEMPLATE_HOME']) if name
40
+ end
41
+
42
+ def set_layout which, force
43
+ return nil unless which
44
+ destroy $active_site.home+'layout/' if force
45
+ $active_site.set_layout(which, $ENV['LAYOUT_HOME']) unless exists? :folder, $active_site.home+'layout/'
46
+ $active_site.save_info('site.info')
47
+ end
48
+
49
+ def start_collection name, attrs=[]
50
+ Collection.start $ENV['STATIC_HOME'], $active_site.home, name, attrs
51
+ end
52
+ def load_collection name
53
+ Collection.load $ENV['STATIC_HOME'], $active_site.home, name.to_s
54
+ end
55
+
56
+ def assign_pagination collection, page
57
+ collection = collection.to_s
58
+ page = page.to_s
59
+ $active_site.info['paginate'] ||= {}
60
+ $active_site.info['paginate'].merge!({page => collection}) unless $active_site.info['paginate'].include?({page => collection})
61
+ $active_site.save_info
62
+ end
63
+
64
+ def paginate page, publishing_to=nil
65
+ @page = Page.find(page)
66
+ Generator.load(load_collection($active_site.info['paginate'][@page.file_name]))
67
+ Generator.class_eval {
68
+ def self.entry_bank
69
+ if @tmp_entries and @tmp_entries.empty?
70
+ @tmp_entries = nil
71
+ return @tmp_entries
72
+ end
73
+ @tmp_entries ||= self.act_on.entries.dup
74
+ @tmp_entries
75
+ end
76
+ }
77
+ publishing_to ||= "#{@page.home}blueprint/#{@page.page_location}_#{Generator.act_on.name}_for_#{@page.file_name}/"
78
+ mkdir publishing_to, :force
79
+ @page_list = []
80
+ while(Generator.entry_bank and Generator.entry_bank)
81
+ rbml = ::Rbml::Processor.new
82
+ #Generator.contents = {:id => entry_bank.first[:id]}
83
+ publish_file = File.read(Generator.act_on.home+'publish')
84
+ id = Generator.entry_bank.first[:id]
85
+ puts "writing partial for #{id}"
86
+ write rbml.render('xhtml', rbml.load_language('xhtml'), :partial => true) { eval(publish_file) }, publishing_to+id
87
+ @page_list << id
88
+ end
89
+ File.open(publishing_to+'page.list', 'w') { |yf| YAML.dump(@page_list, yf) }
90
+ write_template Generator.act_on.home+'link_list', "#{@page.home}blueprint/#{@page.page_location}/#{@page.file_name}_#{Generator.act_on.name}_links.rbml"
91
+ puts "writing link list #{@page.file_name}_#{Generator.act_on.name}_links"
92
+ @page_list = nil
93
+ Generator.unload
94
+ end
95
+ end
96
+