refactored the Listen section + templatized page + fixed a lot of tiny bugs all over the code

did committed Jan 02, 2013
commit 7839f0fceb61249a1d1019eaa25f84810331a1ee
Showing 13 changed files with 237 additions and 60 deletions
TODO +7 -2
@@ @@ -1,16 +1,21 @@
*** REQUIREMENTS ***
- - templatized page
+ x templatized page
+ x localized page
- nice error page (replace the default exception middleware) to display:
- liquid errors
- other exceptions
- nice logs
- - params to setup the server
+ - params to launch the thin server (port, address ?)
- sessions
- post content entry
- listen:
- make it work for external libs
- better installation for all the platforms
+ - content types and content entries / site ?
+ - translations
+
+ - I18n keys
- tests
locomotive/builder/cli.rb b/lib/locomotive/builder/cli.rb +0 -3
@@ @@ -25,9 +25,6 @@ module Locomotive
server = Thin::Server.new('0.0.0.0', port, Locomotive::Builder::Server.new(reader))
# server.threaded = true
server.start
-
- # TODO: To be removed
- # Thin::Server.start('0.0.0.0', port, Locomotive::Builder::Server.new(reader), threaded: true)
end
end
end
locomotive/builder/liquid/drops/content_entry.rb b/lib/locomotive/builder/liquid/drops/content_entry.rb +20 -10
@@ @@ -4,22 +4,32 @@ module Locomotive
module Drops
class ContentEntry < Base
- delegate :_permalink, :seo_title, :meta_keywords, :meta_description, :to => '_source'
+ delegate :seo_title, :meta_keywords, :meta_description, :to => '_source'
- def before_method(meth)
- return '' if @_source.nil?
-
- if not @@forbidden_attributes.include?(meth.to_s)
- value = @_source.send(meth)
- end
+ def _label
+ @_label ||= self._source._label
end
def _permalink
- @_source._permalink.parameterize
+ @_source._permalink.try(:parameterize)
+ end
+
+ def next
+ self
end
- def highlighted_field_value
- @_source.highlighted_field_value
+ def previous
+ self
+ end
+
+ def before_method(meth)
+ return '' if self._source.nil?
+
+ if not @@forbidden_attributes.include?(meth.to_s)
+ self._source.send(meth)
+ else
+ nil
+ end
end
end
locomotive/builder/liquid/tags/editable/base.rb b/lib/locomotive/builder/liquid/tags/editable/base.rb +16 -2
@@ @@ -9,7 +9,7 @@ module Locomotive
def initialize(tag_name, markup, tokens, context)
if markup =~ Syntax
- @slug = $1.gsub('\'', '')
+ @slug = $1.gsub(/[\"\']/, '')
@options = {}
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') }
else
@@ @@ -20,7 +20,21 @@ module Locomotive
end
def render(context)
- super
+ current_page = context.registers[:page]
+
+ element = current_page.find_editable_element(context['block'].try(:name), @slug)
+
+ if element.present?
+ render_element(context, element)
+ else
+ super
+ end
+ end
+
+ protected
+
+ def render_element(context, element)
+ element.content
end
end
locomotive/builder/liquid/tags/editable/control.rb b/lib/locomotive/builder/liquid/tags/editable/control.rb +4 -0
@@ @@ -5,6 +5,10 @@ module Locomotive
module Editable
class Control < Base
+ def render(context)
+ super
+ end
+
end
::Liquid::Template.register_tag('editable_control', Control)
locomotive/builder/liquid/tags/locale_switcher.rb b/lib/locomotive/builder/liquid/tags/locale_switcher.rb +117 -9
@@ @@ -2,7 +2,6 @@ module Locomotive
module Builder
module Liquid
module Tags
-
# Display the links to change the locale of the current page
#
# Usage:
@@ @@ -24,7 +23,7 @@ module Locomotive
Syntax = /(#{::Liquid::Expression}+)?/
def initialize(tag_name, markup, tokens, context)
- @options = { :label => 'iso', :sep => ' | ' }
+ @options = { label: 'iso', sep: ' | ' }
if markup =~ Syntax
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
@@ @@ -38,14 +37,29 @@ module Locomotive
end
def render(context)
- @site, @page = context.registers[:site], context.registers[:page]
+ @site, @page = context.registers[:site], context.registers[:page]
+ @default_locale = context.registers[:mounting_point].default_locale
output = %(<div id="locale-switcher">)
output += @site.locales.collect do |locale|
- fullpath = locale == context['current_locale'] ? '/' : locale
+ Locomotive::Mounter.with_locale(locale) do
+ fullpath = localized_fullpath(locale)
+
+ if @page.templatized?
+ permalink = context['entry']._permalink
+
+ if permalink
+ fullpath.gsub!('*', permalink)
+ else
+ fullpath = '404'
+ end
+ end
+
+ css = link_class(locale, context['locale'])
- %(<a href="/#{fullpath}" class="#{locale} #{'current' if locale == context['current_locale']}">#{link_label(locale)}</a>)
+ %(<a href="/#{fullpath}" class="#{css}">#{link_label(locale)}</a>)
+ end
end.join(@options[:sep])
output += %(</div>)
@@ @@ -53,20 +67,114 @@ module Locomotive
private
+ def link_class(locale, current_locale)
+ css = [locale]
+ css << 'current' if locale.to_s == current_locale.to_s
+ css.join(' ')
+ end
+
def link_label(locale)
case @options[:label]
- when :iso then locale
- when :locale then I18n.t("locomotive.locales.#{locale}", :locale => locale)
- when :title then @page.title # FIXME: this returns nil if the page has not been translated in the locale
+ when 'iso' then locale
+ when 'locale' then I18n.t("locomotive.locales.#{locale}", locale: locale)
+ when 'title' then @page.title # FIXME: this returns nil if the page has not been translated in the locale
else
locale
end
end
+ def localized_fullpath(locale)
+ # @site.localized_page_fullpath(@page, locale)
+
+ return nil if @page.fullpath_translations.blank?
+
+ fullpath = @page.safe_fullpath || @page.fullpath_or_default
+
+ if locale.to_s == @default_locale.to_s # no need to specify the locale
+ @page.index? ? '' : fullpath
+ elsif @page.index? # avoid /en/index or /fr/index, prefer /en or /fr instead
+ locale
+ else
+ File.join(locale, fullpath)
+ end
+ end
+
end
::Liquid::Template.register_tag('locale_switcher', LocaleSwitcher)
end
end
end
- end
\ No newline at end of file
+ end
+ # module Locomotive
+ # module Builder
+ # module Liquid
+ # module Tags
+
+ # # Display the links to change the locale of the current page
+ # #
+ # # Usage:
+ # #
+ # # {% locale_switcher %} => <div id="locale-switcher"><a href="/features" class="current en">Features</a><a href="/fr/fonctionnalites" class="fr">Fonctionnalités</a></div>
+ # #
+ # # {% locale_switcher label: locale, sep: ' - ' }
+ # #
+ # # options:
+ # # - label: iso (de, fr, en, ...etc), locale (Deutsch, Français, English, ...etc), title (page title)
+ # # - sep: piece of html code separating 2 locales
+ # #
+ # # notes:
+ # # - "iso" is the default choice for label
+ # # - " | " is the default separating code
+ # #
+ # class LocaleSwitcher < ::Liquid::Tag
+
+ # Syntax = /(#{::Liquid::Expression}+)?/
+
+ # def initialize(tag_name, markup, tokens, context)
+ # @options = { label: 'iso', sep: ' | ' }
+
+ # if markup =~ Syntax
+ # markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
+
+ # @options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
+ # else
+ # raise ::Liquid::SyntaxError.new("Syntax Error in 'locale_switcher' - Valid syntax: locale_switcher <options>")
+ # end
+
+ # super
+ # end
+
+ # def render(context)
+ # @site, @page = context.registers[:site], context.registers[:page]
+
+ # output = %(<div id="locale-switcher">)
+
+ # output += @site.locales.collect do |locale|
+ # fullpath = locale.to_s == context['default_locale'].to_s ? '/' : locale
+
+ # %(<a href="/#{fullpath}" class="#{locale} #{'current' if locale.to_s == context['default_locale'].to_s}">#{link_label(locale)}</a>)
+ # end.join(@options[:sep])
+
+ # output += %(</div>)
+ # end
+
+ # private
+
+ # def link_label(locale)
+ # case @options[:label]
+ # when :iso then locale
+ # when :locale then I18n.t("locomotive.locales.#{locale}", locale: locale)
+ # when :title then @page.title # FIXME: this returns nil if the page has not been translated in the locale
+ # else
+ # locale
+ # end
+ # end
+
+ # end
+
+ # ::Liquid::Template.register_tag('locale_switcher', LocaleSwitcher)
+ # end
+ # end
+ # end
+ # end
\ No newline at end of file
locomotive/builder/liquid/tags/snippet.rb b/lib/locomotive/builder/liquid/tags/snippet.rb +3 -1
@@ @@ -6,7 +6,9 @@ module Locomotive
class Snippet < ::Liquid::Include
def render(context)
- source = context.registers[:mounting_point].snippets[@template_name].try(:source)
+ name = @template_name.gsub(/[\"\']/, '')
+
+ source = context.registers[:mounting_point].snippets[name].try(:source)
partial = ::Liquid::Template.parse(source)
locomotive/builder/listen.rb b/lib/locomotive/builder/listen.rb +41 -0
@@ @@ -0,0 +1,41 @@
+ require 'listen'
+
+ module Locomotive::Builder
+ class Listen
+
+ attr_accessor :reader
+
+ def self.instance
+ @@instance = new
+ end
+
+ def start(reader)
+ self.reader = reader
+
+ self.build_liquid_listener
+
+ self.build_content_types_listener
+ end
+
+ protected
+
+ def build_liquid_listener
+ reloader = Proc.new do |modified, added, removed|
+ reader.reload(:pages, :snippets)
+ end
+
+ path = File.join(self.reader.mounting_point.path, 'app/views')
+
+ listener = ::Listen.to(path).filter(/\.liquid/).change(&reloader)
+
+ # non blocking listener
+ listener.start(false)
+ end
+
+ def build_content_types_listener
+ # TODO
+ end
+
+ end
+
+ end
\ No newline at end of file
locomotive/builder/server.rb b/lib/locomotive/builder/server.rb +15 -20
@@ @@ -1,6 +1,6 @@
require 'rack/showexceptions'
- require 'listen'
+ require 'locomotive/builder/listen'
require 'locomotive/builder/server/middleware'
require 'locomotive/builder/server/favicon'
require 'locomotive/builder/server/dynamic_assets'
@@ @@ -18,10 +18,21 @@ module Locomotive::Builder
class Server
def initialize(reader)
- # puts Dir[File.join(reader.mounting_point.path, 'app/**/*.liquid*')].inspect
-
@reader = reader
- @app = Rack::Builder.new do
+ @app = self.create_rack_app(@reader)
+
+ Locomotive::Builder::Listen.instance.start(@reader)
+ end
+
+ def call(env)
+ env['builder.mounting_point'] = @reader.mounting_point
+ @app.call(env)
+ end
+
+ protected
+
+ def create_rack_app(reader)
+ Rack::Builder.new do
use Rack::ShowExceptions
use Rack::Lint
@@ @@ -40,22 +51,6 @@ module Locomotive::Builder
run Renderer.new
end
-
- # TODO: refactor it by moving it into another place
- pages_reloader = Proc.new do |modified, added, removed|
- reader.reload(:pages)
- end
-
- listener = Listen.to(File.join(reader.mounting_point.path, 'app/views/pages'))
- .filter(/\.liquid/)
- .change(&pages_reloader)
-
- listener.start(false)
- end
-
- def call(env)
- env['builder.mounting_point'] = @reader.mounting_point
- @app.call(env)
end
end
locomotive/builder/server/locale.rb b/lib/locomotive/builder/server/locale.rb +9 -10
@@ @@ -20,22 +20,21 @@ module Locomotive::Builder
protected
def set_locale!(env)
- path = env['builder.path']
locale = self.mounting_point.default_locale
- if path =~ /^(#{self.mounting_point.locales.join('|')})+(\/|$)/
- locale = $1
- path = path.gsub($1 + $2, '')
-
- # TODO: I18n.locale ???
-
- Locomotive::Mounter.locale = locale
+ if self.path =~ /^(#{self.mounting_point.locales.join('|')})+(\/|$)/
+ locale = $1
+ self.path = self.path.gsub($1 + $2, '')
+ self.path = 'index' if self.path.blank?
end
- puts "[Builder|Locale] path = #{path.inspect}, locale = #{locale.inspect}"
+ Locomotive::Mounter.locale = locale
+ ::I18n.locale = locale
+
+ puts "[Builder|Locale] path = #{self.path.inspect}, locale = #{locale.inspect}"
env['builder.locale'] = locale
- env['builder.path'] = path
+ env['builder.path'] = self.path
end
end
locomotive/builder/server/middleware.rb b/lib/locomotive/builder/server/middleware.rb +2 -1
@@ @@ -3,7 +3,7 @@ module Locomotive::Builder
class Middleware
- attr_accessor :app, :request
+ attr_accessor :app, :request, :path
attr_accessor :mounting_point, :page, :content_entry
@@ @@ -18,6 +18,7 @@ module Locomotive::Builder
protected
def set_accessors(env)
+ self.path = env['builder.path']
self.request = Rack::Request.new(env)
self.mounting_point = env['builder.mounting_point']
self.page = env['builder.page']
locomotive/builder/server/page.rb b/lib/locomotive/builder/server/page.rb +1 -1
@@ @@ -31,7 +31,7 @@ module Locomotive::Builder
self.mounting_point.pages.values.detect do |_page|
matchers.include?(_page.safe_fullpath) ||
- matchers.include?(_page.safe_fullpath.underscore)
+ matchers.include?(_page.safe_fullpath.try(:underscore))
end
end
locomotivecms_builder.gemspec +2 -1
@@ @@ -25,8 +25,9 @@ Gem::Specification.new do |gem|
gem.add_dependency 'dragonfly', '~> 0.9.12'
gem.add_dependency 'rack-cache', '~> 1.1'
gem.add_dependency 'rack-rescue', '~> 0.1.2'
+
gem.add_dependency 'listen', '~> 0.7.0'
- # gerb-fsevent
+
gem.add_dependency 'rmagick', '2.12.2'
gem.add_dependency 'httmultiparty', '~> 0.3.8'
gem.add_dependency 'will_paginate', '~> 3.0.3'