locomotivecms_wagon 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +27 -0
- data/.rspec +2 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +57 -0
- data/TODO +57 -0
- data/bin/wagon +9 -0
- data/generators/blank/Gemfile.tt +20 -0
- data/generators/blank/app/content_types/.empty_directory +1 -0
- data/generators/blank/app/views/pages/404.liquid +11 -0
- data/generators/blank/app/views/pages/index.liquid +19 -0
- data/generators/blank/app/views/snippets/.empty_directory +1 -0
- data/generators/blank/config/deploy.yml +12 -0
- data/generators/blank/config/site.yml.tt +16 -0
- data/generators/blank/config/translations.yml +8 -0
- data/generators/blank/config.ru +3 -0
- data/generators/blank/data/.empty_directory +1 -0
- data/generators/blank/public/fonts/.empty_directory +1 -0
- data/generators/blank/public/images/.empty_directory +1 -0
- data/generators/blank/public/javascripts/.empty_directory +1 -0
- data/generators/blank/public/samples/.empty_directory +1 -0
- data/generators/blank/public/stylesheets/.empty_directory +1 -0
- data/generators/bootstrap/Gemfile.tt +20 -0
- data/generators/bootstrap/app/content_types/.empty_directory +1 -0
- data/generators/bootstrap/app/views/pages/404.liquid +13 -0
- data/generators/bootstrap/app/views/pages/404.liquid.haml +10 -0
- data/generators/bootstrap/app/views/pages/index.liquid +89 -0
- data/generators/bootstrap/app/views/pages/index.liquid.haml +72 -0
- data/generators/bootstrap/app/views/snippets/footer.liquid +3 -0
- data/generators/bootstrap/app/views/snippets/footer.liquid.haml +2 -0
- data/generators/bootstrap/config/deploy.yml +12 -0
- data/generators/bootstrap/config/site.yml.tt +16 -0
- data/generators/bootstrap/config/translations.yml +8 -0
- data/generators/bootstrap/config.ru +3 -0
- data/generators/bootstrap/data/.empty_directory +1 -0
- data/generators/bootstrap/public/fonts/FontAwesome.otf +0 -0
- data/generators/bootstrap/public/fonts/font-awesome-ie7.min.css +23 -0
- data/generators/bootstrap/public/fonts/font-awesome.css +469 -0
- data/generators/bootstrap/public/fonts/font-awesome.min.css +34 -0
- data/generators/bootstrap/public/fonts/fontawesome-webfont.eot +0 -0
- data/generators/bootstrap/public/fonts/fontawesome-webfont.ttf +0 -0
- data/generators/bootstrap/public/fonts/fontawesome-webfont.woff +0 -0
- data/generators/bootstrap/public/javascripts/bootstrap.js +2159 -0
- data/generators/bootstrap/public/javascripts/bootstrap.min.js +6 -0
- data/generators/bootstrap/public/samples/.empty_directory +1 -0
- data/generators/bootstrap/public/stylesheets/application.css.scss +39 -0
- data/generators/bootstrap/public/stylesheets/bootstrap-responsive.css +1092 -0
- data/generators/bootstrap/public/stylesheets/bootstrap-responsive.min.css +9 -0
- data/generators/bootstrap/public/stylesheets/bootstrap.css +5652 -0
- data/generators/bootstrap/public/stylesheets/bootstrap.min.css +726 -0
- data/generators/content_type/app/content_types/%name%.yml.tt +58 -0
- data/generators/content_type/data/%name%.yml.tt +24 -0
- data/generators/foundation/Gemfile.tt +20 -0
- data/generators/foundation/app/content_types/.empty_directory +1 -0
- data/generators/foundation/app/views/pages/404.liquid +13 -0
- data/generators/foundation/app/views/pages/404.liquid.haml +10 -0
- data/generators/foundation/app/views/pages/index.liquid +152 -0
- data/generators/foundation/app/views/pages/index.liquid.haml +130 -0
- data/generators/foundation/app/views/snippets/footer.liquid +16 -0
- data/generators/foundation/app/views/snippets/footer.liquid.haml +23 -0
- data/generators/foundation/config/deploy.yml +12 -0
- data/generators/foundation/config/site.yml.tt +16 -0
- data/generators/foundation/config/translations.yml +8 -0
- data/generators/foundation/config.ru +3 -0
- data/generators/foundation/data/.empty_directory +1 -0
- data/generators/foundation/public/fonts/.empty_directory +1 -0
- data/generators/foundation/public/javascripts/foundation/foundation.alerts.js +50 -0
- data/generators/foundation/public/javascripts/foundation/foundation.clearing.js +480 -0
- data/generators/foundation/public/javascripts/foundation/foundation.cookie.js +74 -0
- data/generators/foundation/public/javascripts/foundation/foundation.dropdown.js +130 -0
- data/generators/foundation/public/javascripts/foundation/foundation.forms.js +395 -0
- data/generators/foundation/public/javascripts/foundation/foundation.joyride.js +612 -0
- data/generators/foundation/public/javascripts/foundation/foundation.js +372 -0
- data/generators/foundation/public/javascripts/foundation/foundation.magellan.js +130 -0
- data/generators/foundation/public/javascripts/foundation/foundation.orbit.js +365 -0
- data/generators/foundation/public/javascripts/foundation/foundation.placeholder.js +159 -0
- data/generators/foundation/public/javascripts/foundation/foundation.reveal.js +270 -0
- data/generators/foundation/public/javascripts/foundation/foundation.section.js +272 -0
- data/generators/foundation/public/javascripts/foundation/foundation.tooltips.js +195 -0
- data/generators/foundation/public/javascripts/foundation/foundation.topbar.js +225 -0
- data/generators/foundation/public/javascripts/foundation.min.js +14 -0
- data/generators/foundation/public/javascripts/vendor/custom.modernizr.js +4 -0
- data/generators/foundation/public/javascripts/vendor/jquery.js +9597 -0
- data/generators/foundation/public/javascripts/vendor/zepto.js +1884 -0
- data/generators/foundation/public/samples/.empty_directory +1 -0
- data/generators/foundation/public/stylesheets/foundation.css +3818 -0
- data/generators/foundation/public/stylesheets/foundation.min.css +1 -0
- data/generators/foundation/public/stylesheets/normalize.css +396 -0
- data/generators/page/template.liquid.haml.tt +35 -0
- data/generators/page/template.liquid.tt +1 -0
- data/generators/snippet/template.liquid.haml.tt +4 -0
- data/generators/snippet/template.liquid.tt +4 -0
- data/lib/locomotive/wagon/cli.rb +238 -0
- data/lib/locomotive/wagon/exceptions.rb +35 -0
- data/lib/locomotive/wagon/generators/content_type.rb +47 -0
- data/lib/locomotive/wagon/generators/page.rb +63 -0
- data/lib/locomotive/wagon/generators/site/base.rb +30 -0
- data/lib/locomotive/wagon/generators/site/blank.rb +23 -0
- data/lib/locomotive/wagon/generators/site/bootstrap.rb +35 -0
- data/lib/locomotive/wagon/generators/site/foundation.rb +35 -0
- data/lib/locomotive/wagon/generators/site/unzip.rb +81 -0
- data/lib/locomotive/wagon/generators/site.rb +99 -0
- data/lib/locomotive/wagon/generators/snippet.rb +60 -0
- data/lib/locomotive/wagon/liquid/drops/base.rb +44 -0
- data/lib/locomotive/wagon/liquid/drops/content_entry.rb +48 -0
- data/lib/locomotive/wagon/liquid/drops/content_types.rb +121 -0
- data/lib/locomotive/wagon/liquid/drops/page.rb +36 -0
- data/lib/locomotive/wagon/liquid/drops/site.rb +21 -0
- data/lib/locomotive/wagon/liquid/errors.rb +7 -0
- data/lib/locomotive/wagon/liquid/filters/date.rb +98 -0
- data/lib/locomotive/wagon/liquid/filters/html.rb +154 -0
- data/lib/locomotive/wagon/liquid/filters/misc.rb +28 -0
- data/lib/locomotive/wagon/liquid/filters/resize.rb +18 -0
- data/lib/locomotive/wagon/liquid/filters/text.rb +50 -0
- data/lib/locomotive/wagon/liquid/filters/translate.rb +24 -0
- data/lib/locomotive/wagon/liquid/patches.rb +47 -0
- data/lib/locomotive/wagon/liquid/tags/consume.rb +58 -0
- data/lib/locomotive/wagon/liquid/tags/csrf.rb +34 -0
- data/lib/locomotive/wagon/liquid/tags/editable/base.rb +46 -0
- data/lib/locomotive/wagon/liquid/tags/editable/control.rb +19 -0
- data/lib/locomotive/wagon/liquid/tags/editable/file.rb +15 -0
- data/lib/locomotive/wagon/liquid/tags/editable/long_text.rb +15 -0
- data/lib/locomotive/wagon/liquid/tags/editable/short_text.rb +15 -0
- data/lib/locomotive/wagon/liquid/tags/editable.rb +5 -0
- data/lib/locomotive/wagon/liquid/tags/extends.rb +25 -0
- data/lib/locomotive/wagon/liquid/tags/google_analytics.rb +28 -0
- data/lib/locomotive/wagon/liquid/tags/inline_editor.rb +16 -0
- data/lib/locomotive/wagon/liquid/tags/locale_switcher.rb +106 -0
- data/lib/locomotive/wagon/liquid/tags/nav.rb +167 -0
- data/lib/locomotive/wagon/liquid/tags/paginate.rb +105 -0
- data/lib/locomotive/wagon/liquid/tags/seo.rb +74 -0
- data/lib/locomotive/wagon/liquid/tags/snippet.rb +44 -0
- data/lib/locomotive/wagon/liquid/tags/with_scope.rb +43 -0
- data/lib/locomotive/wagon/liquid.rb +19 -0
- data/lib/locomotive/wagon/listen.rb +57 -0
- data/lib/locomotive/wagon/logger.rb +54 -0
- data/lib/locomotive/wagon/misc/core_ext.rb +29 -0
- data/lib/locomotive/wagon/misc/dragonfly.rb +79 -0
- data/lib/locomotive/wagon/misc/httparty.rb +46 -0
- data/lib/locomotive/wagon/misc/i18n.rb +2 -0
- data/lib/locomotive/wagon/misc/will_paginate.rb +16 -0
- data/lib/locomotive/wagon/misc.rb +5 -0
- data/lib/locomotive/wagon/server/dynamic_assets.rb +31 -0
- data/lib/locomotive/wagon/server/entry_submission.rb +116 -0
- data/lib/locomotive/wagon/server/favicon.rb +17 -0
- data/lib/locomotive/wagon/server/locale.rb +42 -0
- data/lib/locomotive/wagon/server/logging.rb +32 -0
- data/lib/locomotive/wagon/server/middleware.rb +59 -0
- data/lib/locomotive/wagon/server/not_found.rb +19 -0
- data/lib/locomotive/wagon/server/page.rb +61 -0
- data/lib/locomotive/wagon/server/path.rb +34 -0
- data/lib/locomotive/wagon/server/renderer.rb +105 -0
- data/lib/locomotive/wagon/server/templatized_page.rb +32 -0
- data/lib/locomotive/wagon/server.rb +81 -0
- data/lib/locomotive/wagon/standalone_server.rb +28 -0
- data/lib/locomotive/wagon/version.rb +5 -0
- data/lib/locomotive/wagon.rb +163 -0
- data/locales/de.yml +156 -0
- data/locales/en.yml +177 -0
- data/locales/es.yml +132 -0
- data/locales/et.yml +153 -0
- data/locales/fr.yml +146 -0
- data/locales/it.yml +154 -0
- data/locales/nb.yml +190 -0
- data/locales/nl.yml +159 -0
- data/locales/pl.yml +202 -0
- data/locales/pt-BR.yml +138 -0
- data/locales/ru.yml +223 -0
- data/locomotivecms_wagon.gemspec +44 -0
- data/spec/integration/cassettes/pull.yml +590 -0
- data/spec/integration/cassettes/push.yml +810 -0
- data/spec/integration/integration_helper.rb +14 -0
- data/spec/integration/server_spec.rb +38 -0
- data/spec/integration/sites_spec.rb +29 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/helpers.rb +13 -0
- metadata +544 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Filters
|
5
|
+
module Translate
|
6
|
+
|
7
|
+
def translate(key, locale = nil)
|
8
|
+
translation = @context.registers[:mounting_point].translations[key.to_s]
|
9
|
+
|
10
|
+
if translation
|
11
|
+
translation.get(locale) || translation.get(Locomotive::Mounter.locale)
|
12
|
+
else
|
13
|
+
"[unknown translation key: #{key}]"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
::Liquid::Template.register_filter(Translate)
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Liquid
|
2
|
+
|
3
|
+
class Drop
|
4
|
+
|
5
|
+
def mounting_point
|
6
|
+
@context.registers[:mounting_point]
|
7
|
+
end
|
8
|
+
|
9
|
+
def site
|
10
|
+
@context.registers[:site]
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class Template
|
16
|
+
|
17
|
+
# creates a new <tt>Template</tt> object from liquid source code
|
18
|
+
def parse_with_utf8(source, context = {})
|
19
|
+
if RUBY_VERSION =~ /1\.9/
|
20
|
+
source = source.force_encoding('UTF-8') if source.present?
|
21
|
+
end
|
22
|
+
self.parse_without_utf8(source, context)
|
23
|
+
end
|
24
|
+
|
25
|
+
alias_method_chain :parse, :utf8
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
module StandardFilters
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def to_number(obj)
|
34
|
+
case obj
|
35
|
+
when Numeric
|
36
|
+
obj
|
37
|
+
when String
|
38
|
+
(obj.strip =~ /^\d+\.\d+$/) ? obj.to_f : obj.to_i
|
39
|
+
when DateTime, Date, Time
|
40
|
+
obj.to_time.to_i
|
41
|
+
else
|
42
|
+
0
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
|
6
|
+
# Consume web services as easy as pie directly in liquid !
|
7
|
+
#
|
8
|
+
# Usage:
|
9
|
+
#
|
10
|
+
# {% consume blog from 'http://nocoffee.tumblr.com/api/read.json?num=3' username: 'john', password: 'easy', format: 'json', expires_in: 3000 %}
|
11
|
+
# {% for post in blog.posts %}
|
12
|
+
# {{ post.title }}
|
13
|
+
# {% endfor %}
|
14
|
+
# {% endconsume %}
|
15
|
+
#
|
16
|
+
class Consume < ::Liquid::Block
|
17
|
+
|
18
|
+
Syntax = /(#{::Liquid::VariableSignature}+)\s*from\s*(#{::Liquid::QuotedString}+)/
|
19
|
+
|
20
|
+
def initialize(tag_name, markup, tokens, context)
|
21
|
+
if markup =~ Syntax
|
22
|
+
@target = $1
|
23
|
+
@url = $2.gsub(/['"]/, '')
|
24
|
+
@options = {}
|
25
|
+
markup.scan(::Liquid::TagAttributes) do |key, value|
|
26
|
+
@options[key] = value if key != 'http'
|
27
|
+
end
|
28
|
+
@options.delete('expires_in')
|
29
|
+
else
|
30
|
+
raise ::Liquid::SyntaxError.new("Syntax Error in 'consume' - Valid syntax: consume <var> from \"<url>\" [username: value, password: value]")
|
31
|
+
end
|
32
|
+
|
33
|
+
super
|
34
|
+
end
|
35
|
+
|
36
|
+
def render(context)
|
37
|
+
context.stack do
|
38
|
+
_response = nil
|
39
|
+
|
40
|
+
begin
|
41
|
+
_response = Locomotive::Wagon::Httparty::Webservice.consume(@url, @options.symbolize_keys)
|
42
|
+
rescue Exception => e
|
43
|
+
_response = { 'error' => e.message.to_s }.to_liquid
|
44
|
+
end
|
45
|
+
|
46
|
+
context.scopes.last[@target.to_s] = _response
|
47
|
+
|
48
|
+
render_all(@nodelist, context)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
::Liquid::Template.register_tag('consume', Consume)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
module Csrf
|
6
|
+
|
7
|
+
class Param < ::Liquid::Tag
|
8
|
+
|
9
|
+
def render(context)
|
10
|
+
%{<input type="hidden" name="authenticity_token" value="helloworld" />}
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class Meta < ::Liquid::Tag
|
16
|
+
|
17
|
+
def render(context)
|
18
|
+
%{
|
19
|
+
<meta name="csrf-param" content="authenticity_token" />
|
20
|
+
<meta name="csrf-token" content="helloworld" />
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
::Liquid::Template.register_tag('csrf_param', Csrf::Param)
|
29
|
+
::Liquid::Template.register_tag('csrf_meta', Csrf::Meta)
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
module Editable
|
6
|
+
class Base < ::Liquid::Block
|
7
|
+
|
8
|
+
Syntax = /(#{::Liquid::QuotedFragment})(\s*,\s*#{::Liquid::Expression}+)?/
|
9
|
+
|
10
|
+
def initialize(tag_name, markup, tokens, context)
|
11
|
+
if markup =~ Syntax
|
12
|
+
@slug = $1.gsub(/[\"\']/, '')
|
13
|
+
@options = {}
|
14
|
+
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') }
|
15
|
+
else
|
16
|
+
raise ::Liquid::SyntaxError.new("Syntax Error in 'editable_xxx' - Valid syntax: editable_xxx <slug>(, <options>)")
|
17
|
+
end
|
18
|
+
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
def render(context)
|
23
|
+
current_page = context.registers[:page]
|
24
|
+
|
25
|
+
element = current_page.find_editable_element(context['block'].try(:name), @slug)
|
26
|
+
|
27
|
+
if element.present?
|
28
|
+
render_element(context, element)
|
29
|
+
else
|
30
|
+
super
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
def render_element(context, element)
|
37
|
+
element.content
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
module Editable
|
6
|
+
class Control < Base
|
7
|
+
|
8
|
+
def render(context)
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
::Liquid::Template.register_tag('editable_control', Control)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,5 @@
|
|
1
|
+
require 'locomotive/wagon/liquid/tags/editable/base'
|
2
|
+
require 'locomotive/wagon/liquid/tags/editable/short_text'
|
3
|
+
require 'locomotive/wagon/liquid/tags/editable/long_text'
|
4
|
+
require 'locomotive/wagon/liquid/tags/editable/file'
|
5
|
+
require 'locomotive/wagon/liquid/tags/editable/control'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
class Extends < ::Liquid::Extends
|
6
|
+
|
7
|
+
def parse_parent_template
|
8
|
+
mounting_point = @context[:mounting_point]
|
9
|
+
|
10
|
+
page = if @template_name == 'parent'
|
11
|
+
@context[:page].parent
|
12
|
+
else
|
13
|
+
mounting_point.pages[@template_name]
|
14
|
+
end
|
15
|
+
|
16
|
+
::Liquid::Template.parse(page.source, { mounting_point: mounting_point, page: page })
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
::Liquid::Template.register_tag('extends', Extends)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
class GoogleAnalytics < ::Liquid::Tag
|
6
|
+
|
7
|
+
Syntax = /(#{::Liquid::Expression}+)?/
|
8
|
+
|
9
|
+
def initialize(tag_name, markup, tokens, context)
|
10
|
+
if markup =~ Syntax
|
11
|
+
@account_id = $1.gsub('\'', '')
|
12
|
+
else
|
13
|
+
raise ::Liquid::SyntaxError.new("Syntax Error in 'google_analytics' - Valid syntax: google_analytics <account_id>")
|
14
|
+
end
|
15
|
+
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def render(context)
|
20
|
+
"<!-- google analytics for #{@account_id} -->"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
::Liquid::Template.register_tag('google_analytics', GoogleAnalytics)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
# Display the links to change the locale of the current page
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# {% locale_switcher %} => <div id="locale-switcher"><a href="/features" class="current en">Features</a><a href="/fr/fonctionnalites" class="fr">Fonctionnalités</a></div>
|
10
|
+
#
|
11
|
+
# {% locale_switcher label: locale, sep: ' - ' }
|
12
|
+
#
|
13
|
+
# options:
|
14
|
+
# - label: iso (de, fr, en, ...etc), locale (Deutsch, Français, English, ...etc), title (page title)
|
15
|
+
# - sep: piece of html code separating 2 locales
|
16
|
+
#
|
17
|
+
# notes:
|
18
|
+
# - "iso" is the default choice for label
|
19
|
+
# - " | " is the default separating code
|
20
|
+
#
|
21
|
+
class LocaleSwitcher < ::Liquid::Tag
|
22
|
+
|
23
|
+
Syntax = /(#{::Liquid::Expression}+)?/
|
24
|
+
|
25
|
+
def initialize(tag_name, markup, tokens, context)
|
26
|
+
@options = { label: 'iso', sep: ' | ' }
|
27
|
+
|
28
|
+
if markup =~ Syntax
|
29
|
+
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
|
30
|
+
|
31
|
+
@options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
|
32
|
+
else
|
33
|
+
raise ::Liquid::SyntaxError.new("Syntax Error in 'locale_switcher' - Valid syntax: locale_switcher <options>")
|
34
|
+
end
|
35
|
+
|
36
|
+
super
|
37
|
+
end
|
38
|
+
|
39
|
+
def render(context)
|
40
|
+
@site, @page = context.registers[:site], context.registers[:page]
|
41
|
+
@default_locale = context.registers[:mounting_point].default_locale
|
42
|
+
|
43
|
+
output = %(<div id="locale-switcher">)
|
44
|
+
|
45
|
+
output += @site.locales.collect do |locale|
|
46
|
+
Locomotive::Mounter.with_locale(locale) do
|
47
|
+
fullpath = localized_fullpath(locale)
|
48
|
+
|
49
|
+
if @page.templatized?
|
50
|
+
permalink = context['entry']._permalink
|
51
|
+
|
52
|
+
if permalink
|
53
|
+
fullpath.gsub!('*', permalink)
|
54
|
+
else
|
55
|
+
fullpath = '404'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
css = link_class(locale, context['locale'])
|
60
|
+
|
61
|
+
%(<a href="/#{fullpath}" class="#{css}">#{link_label(locale)}</a>)
|
62
|
+
end
|
63
|
+
end.join(@options[:sep])
|
64
|
+
|
65
|
+
output += %(</div>)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def link_class(locale, current_locale)
|
71
|
+
css = [locale]
|
72
|
+
css << 'current' if locale.to_s == current_locale.to_s
|
73
|
+
css.join(' ')
|
74
|
+
end
|
75
|
+
|
76
|
+
def link_label(locale)
|
77
|
+
case @options[:label]
|
78
|
+
when 'iso' then locale
|
79
|
+
when 'locale' then I18n.t("locales.#{locale}")
|
80
|
+
when 'title' then @page.title # FIXME: this returns nil if the page has not been translated in the locale
|
81
|
+
else
|
82
|
+
locale
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def localized_fullpath(locale)
|
87
|
+
return nil if @page.fullpath_translations.blank?
|
88
|
+
|
89
|
+
fullpath = @page.safe_fullpath || @page.fullpath_or_default
|
90
|
+
|
91
|
+
if locale.to_s == @default_locale.to_s # no need to specify the locale
|
92
|
+
@page.index? ? '' : fullpath
|
93
|
+
elsif @page.index? # avoid /en/index or /fr/index, prefer /en or /fr instead
|
94
|
+
locale
|
95
|
+
else
|
96
|
+
File.join(locale, fullpath)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
::Liquid::Template.register_tag('locale_switcher', LocaleSwitcher)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Wagon
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
# Display the children pages of the site, current page or the parent page. If not precised, nav is applied on the current page.
|
6
|
+
# The html output is based on the ul/li tags.
|
7
|
+
#
|
8
|
+
# Usage:
|
9
|
+
#
|
10
|
+
# {% nav site %} => <ul class="nav"><li class="on"><a href="/features">Features</a></li></ul>
|
11
|
+
#
|
12
|
+
# {% nav site, no_wrapper: true, exclude: 'contact|about', id: 'main-nav', class: 'nav', active_class: 'on' }
|
13
|
+
#
|
14
|
+
class Nav < ::Liquid::Tag
|
15
|
+
|
16
|
+
Syntax = /(#{::Liquid::Expression}+)?/
|
17
|
+
|
18
|
+
attr_accessor :current_page, :mounting_point
|
19
|
+
|
20
|
+
def initialize(tag_name, markup, tokens, context)
|
21
|
+
if markup =~ Syntax
|
22
|
+
@source = ($1 || 'page').gsub(/"|'/, '')
|
23
|
+
@options = { :id => 'nav', :class => '', :active_class => 'on', :bootstrap => false }
|
24
|
+
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
|
25
|
+
|
26
|
+
@options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
|
27
|
+
|
28
|
+
if @options[:snippet]
|
29
|
+
if template = self.parse_snippet_template(context, @options[:snippet])
|
30
|
+
@options[:liquid_render] = template
|
31
|
+
end
|
32
|
+
end
|
33
|
+
else
|
34
|
+
raise ::Liquid::SyntaxError.new("Syntax Error in 'nav' - Valid syntax: nav <site|parent|page|<path to a page>> <options>")
|
35
|
+
end
|
36
|
+
|
37
|
+
super
|
38
|
+
end
|
39
|
+
|
40
|
+
def render(context)
|
41
|
+
self.set_accessors_from_context(context)
|
42
|
+
|
43
|
+
children_output = []
|
44
|
+
|
45
|
+
entries = self.fetch_entries
|
46
|
+
|
47
|
+
entries.each_with_index do |p, index|
|
48
|
+
css = []
|
49
|
+
css << 'first' if index == 0
|
50
|
+
css << 'last' if index == entries.size - 1
|
51
|
+
|
52
|
+
children_output << render_entry_link(p, css.join(' '), 1)
|
53
|
+
end
|
54
|
+
|
55
|
+
output = children_output.join("\n")
|
56
|
+
|
57
|
+
if @options[:no_wrapper] != 'true'
|
58
|
+
output = %{<ul id="#{@options[:id]}" class="#{@options[:class]}">\n#{output}</ul>}
|
59
|
+
end
|
60
|
+
|
61
|
+
output
|
62
|
+
end
|
63
|
+
|
64
|
+
protected
|
65
|
+
|
66
|
+
def set_accessors_from_context(context)
|
67
|
+
self.current_page = context.registers[:page]
|
68
|
+
self.mounting_point = context.registers[:mounting_point]
|
69
|
+
end
|
70
|
+
|
71
|
+
def parse_snippet_template(context, template_name)
|
72
|
+
source = if template_name.include?('{')
|
73
|
+
template_name
|
74
|
+
else
|
75
|
+
context[:mounting_point].snippets[template_name].try(:source)
|
76
|
+
end
|
77
|
+
|
78
|
+
source ? ::Liquid::Template.parse(source) : nil
|
79
|
+
end
|
80
|
+
|
81
|
+
def fetch_entries
|
82
|
+
children = (case @source
|
83
|
+
when 'site' then self.mounting_point.pages['index']
|
84
|
+
when 'parent' then self.current_page.parent || self.current_page
|
85
|
+
when 'page' then self.current_page
|
86
|
+
else
|
87
|
+
self.mounting_point.pages[@source]
|
88
|
+
end).children.clone
|
89
|
+
|
90
|
+
children.delete_if { |p| !include_page?(p) }
|
91
|
+
end
|
92
|
+
|
93
|
+
# Determines whether or not a page should be a part of the menu
|
94
|
+
def include_page?(page)
|
95
|
+
if !page.listed? || page.templatized? || !page.published?
|
96
|
+
false
|
97
|
+
elsif @options[:exclude]
|
98
|
+
(page.fullpath =~ @options[:exclude]).nil?
|
99
|
+
else
|
100
|
+
true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns a list element, a link to the page and its children
|
105
|
+
def render_entry_link(page, css, depth)
|
106
|
+
selected = self.current_page.fullpath =~ /^#{page.fullpath}/ ? " #{@options[:active_class]}" : ''
|
107
|
+
|
108
|
+
icon = @options[:icon] ? '<span></span>' : ''
|
109
|
+
|
110
|
+
title = @options[:liquid_render] ? @options[:liquid_render].render('page' => page) : page.title
|
111
|
+
|
112
|
+
label = %{#{icon if @options[:icon] != 'after' }#{title}#{icon if @options[:icon] == 'after' }}
|
113
|
+
|
114
|
+
dropdow = ""
|
115
|
+
link_options = ""
|
116
|
+
href = ::I18n.locale.to_s == self.mounting_point.default_locale.to_s ? "/#{page.fullpath}" : "/#{::I18n.locale}/#{page.fullpath}"
|
117
|
+
caret = ""
|
118
|
+
|
119
|
+
if render_children_for_page?(page, depth) && bootstrap?
|
120
|
+
dropdow = "dropdown"
|
121
|
+
link_options = %{class="dropdown-toggle" data-toggle="dropdown"}
|
122
|
+
href = "#"
|
123
|
+
caret = %{<b class="caret"></b>}
|
124
|
+
end
|
125
|
+
|
126
|
+
output = %{<li id="#{page.slug.to_s.dasherize}-link" class="link#{selected} #{css} #{dropdow}">}
|
127
|
+
output << %{<a href="#{href}" #{link_options}>#{label} #{caret}</a>}
|
128
|
+
output << render_entry_children(page, depth.succ) if (depth.succ <= @options[:depth].to_i)
|
129
|
+
output << %{</li>}
|
130
|
+
|
131
|
+
output.strip
|
132
|
+
end
|
133
|
+
|
134
|
+
def render_children_for_page?(page, depth)
|
135
|
+
depth.succ <= @options[:depth].to_i && page.children.reject { |c| !include_page?(c) }.any?
|
136
|
+
end
|
137
|
+
|
138
|
+
# Recursively creates a nested unordered list for the depth specified
|
139
|
+
def render_entry_children(page, depth)
|
140
|
+
output = %{}
|
141
|
+
|
142
|
+
children = page.children.reject { |c| !include_page?(c) }
|
143
|
+
if children.present?
|
144
|
+
output = %{<ul id="#{@options[:id]}-#{page.slug.to_s.dasherize}" class="#{bootstrap? ? "dropdown-menu" : ""}">}
|
145
|
+
children.each do |c, page|
|
146
|
+
css = []
|
147
|
+
css << 'first' if children.first == c
|
148
|
+
css << 'last' if children.last == c
|
149
|
+
|
150
|
+
output << render_entry_link(c, css.join(' '),depth)
|
151
|
+
end
|
152
|
+
output << %{</ul>}
|
153
|
+
end
|
154
|
+
|
155
|
+
output
|
156
|
+
end
|
157
|
+
|
158
|
+
def bootstrap?
|
159
|
+
@options[:bootstrap] == 'true' || @options[:bootstrap] == true
|
160
|
+
end
|
161
|
+
|
162
|
+
::Liquid::Template.register_tag('nav', Nav)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|