Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into multi-thread-gen-trial
Browse files Browse the repository at this point in the history
  • Loading branch information
LightGuard committed Apr 13, 2015
2 parents 30443b3 + 33415cb commit 159fd80
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 80 deletions.
10 changes: 6 additions & 4 deletions awestruct.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ Haml and Markdown filters are touchy things. Redcarpet or Rdiscount work well if
s.add_dependency 'haml', '~> 4.0.5'
s.add_dependency 'asciidoctor' # we're pretty good about backwards compat
s.add_dependency 'tilt', '~> 2.0.1'
s.add_dependency 'compass', '~> 1.0.1'
s.add_dependency 'compass-960-plugin', '~> 0.10.4'
s.add_dependency 'bootstrap-sass', '~> 3.2.0.2'
s.add_dependency 'zurb-foundation', '~> 4.3.2'
s.add_dependency 'mime-types', '~> 2.1'
s.add_dependency 'rest-client', '~> 1.7.2'
s.add_dependency 'ruby-s3cmd', '~> 0.1.5'
Expand All @@ -47,7 +43,13 @@ Haml and Markdown filters are touchy things. Redcarpet or Rdiscount work well if
s.add_dependency 'git', '~> 1.2.6'
s.add_dependency 'guard-livereload', '~> 2.1.2'
s.add_dependency 'colorize', '~> 0.7.1'
s.add_dependency 'oga', '~> 0.3'

s.add_dependency 'parallel', '> 1.1.1'

s.add_development_dependency 'nokogiri', '~> 1.5.10'
s.add_development_dependency 'compass', '~> 1.0.1'
s.add_development_dependency 'compass-960-plugin', '~> 0.10.4'
s.add_development_dependency 'bootstrap-sass', '~> 3.2.0.2'
s.add_development_dependency 'zurb-foundation', '~> 4.3.2'
end
46 changes: 25 additions & 21 deletions lib/awestruct/cli/auto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ def run()
generate_thread = nil
current_path = nil

guard = if ( @config.options.livereload )
guard = Guard::LiveReload.new
guard.start
guard
else
nil
begin
guard = if ( @config.options.livereload )
Guard.init({})
guard = Guard::LiveReload.new
guard.start
guard
else
nil
end
rescue => e
puts e
puts e.backtrace
end

force_polling = ( RUBY_PLATFORM =~ /mingw/ ? true : false )
Expand All @@ -45,26 +51,24 @@ def run()

generate_thread = Thread.new {
begin
if ( File.extname(path) == '.sass' || File.extname(path) == '.scss' )
# TODO use sass here, eventually, as soon as I can figure out how to get it and sprites to work
::Compass::Commands::UpdateProject.new(engine.site.dir, {}).perform
# fixes the nil later on, there's an outstanding bug that css
# pages aren't output in the correct directory
pages = []
else
page = engine.page_by_source_path(path)
pages = []
if ( page )
page = engine.page_by_source_path(path)
pages = []
if ( page )
unless ( guard )
pages = engine.generate_page_and_dependencies( page )
else
if File.exist? path
# chances are this is an extension or yaml file
pages = engine.run_auto_for_non_page(path)
end
pages = engine.page_dependencies( page )
end
else
if File.exist? path
# chances are this is an extension or yaml file
pages = engine.run_auto_for_non_page(path, !@config.options.generate_on_access)
end
end

$LOG.info "Regeneration finished." if $LOG.info?
unless ( guard )
$LOG.info "Regeneration finished." if $LOG.info?
end

if ( guard )
urls = pages.map do |p|
Expand Down
5 changes: 3 additions & 2 deletions lib/awestruct/cli/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ module Awestruct
module CLI
class Generate

def initialize(config, profile=nil, base_url=nil, default_base_url=Options::DEFAULT_BASE_URL, force=false)
def initialize(config, profile=nil, base_url=nil, default_base_url=Options::DEFAULT_BASE_URL, force=false, generate=true)
@profile = profile
@base_url = base_url
@default_base_url = default_base_url
@force = force
@generate = generate
@engine = Awestruct::Engine.new( config )
end

def run()
begin
base_url = @base_url || @default_base_url
$LOG.info "Generating site: #{base_url}" if $LOG.info?
@engine.run( @profile, @base_url, @default_base_url, @force )
@engine.run( @profile, @base_url, @default_base_url, @force, @generate )
rescue =>e
ExceptionHelper.log_building_error e, ''
return false
Expand Down
3 changes: 2 additions & 1 deletion lib/awestruct/cli/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def self.framework_path(path, framework = nil)
copy_file('_ext/pipeline.rb', Init.framework_path('base_pipeline.rb'))
copy_file('.awestruct_ignore', Init.framework_path('base_awestruct_ignore'))
copy_file('Rakefile', Init.framework_path('base_Rakefile'))
copy_file('Gemfile', Init.framework_path('base_Gemfile'))
mkdir('stylesheets')
}

Expand All @@ -31,6 +30,8 @@ def initialize(dir = Dir.pwd, framework = 'compass', scaffold = true)
def run()
manifest = Manifest.new(BASE_MANIFEST)
scaffold_name = @framework
manifest.template_file('Gemfile', Init.framework_path('base_Gemfile'), {:framework => @framework})

lib = nil
case @framework
when 'compass'
Expand Down
4 changes: 2 additions & 2 deletions lib/awestruct/cli/invoker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def invoke_force()

def invoke_generate()
base_url = profile['base_url'] || options.base_url
@success = Awestruct::CLI::Generate.new( config, options.profile, base_url, Options::DEFAULT_BASE_URL, options.force ).run
@success = Awestruct::CLI::Generate.new( config, options.profile, base_url, Options::DEFAULT_BASE_URL, options.force, !options.generate_on_access ).run
end

def invoke_deploy()
Expand All @@ -144,7 +144,7 @@ def invoke_auto()
end

def invoke_server()
run_in_thread( Awestruct::CLI::Server.new( options.output_dir, options.bind_addr, options.port ) )
run_in_thread( Awestruct::CLI::Server.new( options.output_dir, options.bind_addr, options.port, options.generate_on_access ) )
end


Expand Down
41 changes: 41 additions & 0 deletions lib/awestruct/cli/manifest.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'sass/callbacks'
require 'compass'
require 'compass/commands'
require 'erb'
require 'rubygems/specification'
require 'ostruct'

# TODO: We could create our own installer and use that

Expand All @@ -22,6 +25,10 @@ def mkdir(path)
steps << MkDir.new(path)
end

def template_file(path, input_path, state = {})
steps << TemplateFile.new(path, input_path, state.merge(load_gem))
end

def copy_file(path, input_path, opts = {})
steps << CopyFile.new(path, input_path, opts)
end
Expand Down Expand Up @@ -68,6 +75,18 @@ def unperform(dir)
end
end

private

def load_gem
spec = {:dependencies => {}}
gem_spec = Gem::Specification::load(
File.join(File.dirname(__FILE__), "../../../", "awestruct.gemspec"))

gem_spec.dependencies.each { |d| spec[:dependencies][d.name] = d}
spec[:awestruct_version] = gem_spec.version
spec
end

##
##
##
Expand Down Expand Up @@ -202,6 +221,28 @@ def notunperform(dir)

end

class TemplateFile
def initialize(path, input_path, state = {})
@path = path
@input_path = input_path
@state = state
end

def perform(dir)

rendered = ERB.new(File.read(@input_path), nil, '<>').result(
OpenStruct.new(@state).instance_eval { binding })

p = File.join(dir, @path)
$LOG.info "Create file: #{p}" if $LOG.info?
File.open(p, 'w') { |f| f.write(rendered) }
end

def unperform(dir)
# nothing
end
end

class InstallCompass
def initialize(framework='compass')
@framework = framework
Expand Down
11 changes: 10 additions & 1 deletion lib/awestruct/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Options
DEFAULT_BIND_ADDR = '0.0.0.0'
DEFAULT_PORT = 4242
DEFAULT_BASE_URL = %(http://#{LOCAL_HOSTS[DEFAULT_BIND_ADDR] || DEFAULT_BIND_ADDR}:#{DEFAULT_PORT})
DEFAULT_GENERATE_ON_ACCESS = false

attr_accessor :generate
attr_accessor :server
Expand All @@ -36,6 +37,7 @@ class Options
attr_accessor :output_dir
attr_accessor :livereload
attr_accessor :debug
attr_accessor :generate_on_access

def initialize()
@generate = nil
Expand All @@ -56,6 +58,7 @@ def initialize()
@livereload = false
@source_dir = Dir.pwd
@output_dir = File.expand_path '_site'
@generate_on_access = DEFAULT_GENERATE_ON_ACCESS
end

def self.parse!(args)
Expand Down Expand Up @@ -92,19 +95,25 @@ def parse!(args)
opts.on( '-u', '--url URL', 'Set site.base_url' ) do |url|
self.base_url = url
end
opts.on( '-d', '--dev', "Run site in development mode (--auto, --server, --port #{DEFAULT_PORT} and --profile development)" ) do |url|
opts.on( '-d', '--dev', "Run site in development mode (--auto, --server, --port #{DEFAULT_PORT}, --profile development, --livereload and --generate_on_access)" ) do |url|
self.server = true
self.auto = true
self.port = DEFAULT_PORT
self.profile = 'development'
self.livereload = true
self.generate_on_access = true
end
opts.on( '-a', '--auto', 'Auto-generate when changes are noticed' ) do |a|
self.auto = a
self.livereload = true
end
opts.on( '--[no-]livereload', 'Support for browser livereload' ) do |livereload|
self.livereload = livereload
self.generate_on_access = true if self.livereload
end

opts.on( '--[no-]generate-on-access', 'Support for calling generate on HTTP access' ) do |generate_on_access|
self.generate_on_access = generate_on_access
end

opts.on( '-P', '--profile PROFILE', 'Activate a configuration profile' ) do |profile|
Expand Down
25 changes: 19 additions & 6 deletions lib/awestruct/cli/server.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
require 'rack'
require 'rack/builder'
require 'rack/server'
require 'awestruct/rack/app'
require 'awestruct/rack/generate'

module Awestruct
module CLI
class Server
attr_reader :server

def initialize(path, bind_addr=Options::DEFAULT_BIND_ADDR, port=Options::DEFAULT_PORT)
def initialize(path, bind_addr=Options::DEFAULT_BIND_ADDR, port=Options::DEFAULT_PORT, generate_on_access=Options::DEFAULT_GENERATE_ON_ACCESS)
@path = path
@bind_addr = bind_addr
@port = port
@generate_on_access = generate_on_access
end

def run
url = %(http://#{Options::LOCAL_HOSTS[@bind_addr] || @bind_addr}:#{@port})
msg = %(Starting preview server at #{url} (Press Ctrl-C to shutdown))
puts %(#{'*' * msg.length}\n#{msg}\n#{'*' * msg.length}\n)
::Rack::Server::start( :app => Awestruct::Rack::App.new( @path ),
:Port => @port,
:Host => @bind_addr
)

path = @path
generate_on_access = @generate_on_access
app = ::Rack::Builder.new do
use Awestruct::Rack::GenerateOnAccess if generate_on_access
map "/" do
run Awestruct::Rack::App.new( path )
end
end

::Rack::Server::start( :app => app,
:Port => @port,
:Host => @bind_addr
)
end
end
end
end
end
35 changes: 15 additions & 20 deletions lib/awestruct/context_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'rexml/document'
require 'oga'

module Awestruct
module ContextHelper
include REXML

def html_to_text(str)
str.gsub( /<[^>]+>/, '' ).gsub( /&nbsp;/, ' ' )
Expand Down Expand Up @@ -39,27 +38,23 @@ def summarize(text, numwords=20, ellipsis='...')
end

def fully_qualify_urls(base_url, text)
doc = Document.new text
doc.context[:attribute_quote] = :quote # Set double-quote as the attribute value delimiter
doc = Oga.parse_html text

XPath.each(doc, "//a") do |a|
a.attributes['href'] = fix_url( base_url, a.attributes['href'] ) if a.attributes['href']
end

XPath.each(doc, "//link") do |link|
link.attributes['href'] = fix_url( base_url, link.attributes['href'] )
end

XPath.each(doc, "//img") do |img|
img.attributes['src'] = fix_url( base_url, img.attributes['src'] )
doc.each_node do |elem|
if (elem.is_a?(Oga::XML::Element) && elem.html?)
case elem.name
when 'a'
elem.set 'href', fix_url(base_url, elem.get('href')) if elem.get('href')
when 'link'
elem.set 'href', fix_url(base_url, elem.get('href')) if elem.get('href')
when 'img'
elem.set 'src', fix_url(base_url, elem.get('src')) if elem.get('src')
end
end
end

if RUBY_VERSION.start_with? '1.8'
doc.to_s
else
doc.to_s.tap do |d|
d.force_encoding(text.encoding) if d.encoding != text.encoding
end
doc.to_xml.tap do |d|
d.force_encoding(text.encoding) if d.encoding != text.encoding
end
end

Expand Down

0 comments on commit 159fd80

Please sign in to comment.