diff --git a/hammer/hammer.rb b/hammer/hammer.rb
index 84d5fd4..e965f2f 100644
--- a/hammer/hammer.rb
+++ b/hammer/hammer.rb
@@ -67,9 +67,44 @@ def get_mime_type
WEBrick::HTTPUtils::mime_type(@filesystem_path.to_s, mime_file)
end
- def self.error(message)
- puts "Hammer Error: #{message.gsub!(/(<[^>]*>)|\n|\t/s) {""}}".colorize(:red)
- return "Hammer Error: #{message}"
+ def self.error(message, options={})
+ options = {
+ comment: false,
+ message: "",
+ warning: false
+ }.merge(options)
+
+ type = options[:warning] ? "Warning" : "Error"
+
+ error_background_color = options[:warning] ? "#9BD3DD" : "red"
+ error_text_color = options[:warning] ? "black" : "white"
+ span_style = "color: #000; border: 1px dashed #{error_background_color}; background-color: #fff; border-radius: 3px; font-family: monospace; padding: 0 3px 0 0;"
+ error_style= "color: #{error_text_color}; background-color: #{error_background_color}; border-radius: 3px 0 0 3px; padding: 0 3px; font-family: monospace;"
+
+ console_error = "Hammer #{type}: #{message.gsub(/(<[^>]*>)|\n|\t/s) {""}}"
+ puts console_error.colorize(:red)
+
+ error = "Hammer #{type}: #{message}"
+ if options[:comment]
+ ""
+ else
+ "#{error} #{options[:message]}"
+ end
+ end
+
+ def self.key_missing(key,options={})
+ options = {
+ parent_key: nil,
+ comment: false,
+ message: ""
+ }.merge(options)
+
+ style = "background-color: #eee; border-radius: 3px; font-family: monospace; padding: 0 3px;"
+ if options[:parent_key]
+ error("Missing key #{key}:
under #{options[:parent_key]}:
in mock_data.yml", options)
+ else
+ error("Missing key #{key}:
in mock_data.yml", options)
+ end
end
end
diff --git a/hammer/services/dentaku_ext.rb b/hammer/services/dentaku_ext.rb
new file mode 100644
index 0000000..0982ba0
--- /dev/null
+++ b/hammer/services/dentaku_ext.rb
@@ -0,0 +1,36 @@
+module Dentaku
+ module CustomFunctions
+ FUNCTIONS = [
+ [
+ :length,
+ :numeric,
+ ->(str) { str.length }
+ ],
+ [
+ :blank,
+ :logical,
+ ->(str) { str.blank? }
+ ],
+ [
+ :contains,
+ :logical,
+ ->(mainStr, subStr) { mainStr.include?(subStr) }
+ ],
+ [
+ :startswith,
+ :logical,
+ ->(mainStr, subStr) { mainStr.starts_with?(subStr) }
+ ],
+ [
+ :endswith,
+ :logical,
+ ->(mainStr, subStr) { mainStr.ends_with?(subStr) }
+ ],
+ [
+ :matches,
+ :logical,
+ ->(str, regexStr) { !Regexp.new(regexStr).match(str).nil? }
+ ]
+ ]
+ end
+end
diff --git a/hammer/services/tag_binding.rb b/hammer/services/tag_binding.rb
index b15ba5e..fc2a75a 100644
--- a/hammer/services/tag_binding.rb
+++ b/hammer/services/tag_binding.rb
@@ -1,9 +1,11 @@
require 'dentaku'
+require '../hammer/services/dentaku_ext.rb'
module Radius
class TagBinding
CALCULATOR = Dentaku::Calculator.new
+ CALCULATOR.add_functions(Dentaku::CustomFunctions::FUNCTIONS)
# Evaluates the current tag and returns the rendered contents.
def expand(newcontext=nil,oldcontext=nil)
@@ -36,7 +38,7 @@ def attributes
begin
memo[k] = parse_value(v)
rescue Exception => e
- memo[k] = Hammer.error "Attribute Error #{name} #{e}"
+ Hammer.error "Attribute Error #{k} #{e}"
#raise AttributeParseError.new(name, k, v, e.backtrace)
end
@@ -112,7 +114,8 @@ def math_dictionary
memo
end
end
-
+ # This method is used to parse tag attributes that may refer to variables or
+ # other tags
def parse_value(v)
str = v.to_s.strip
if str =~ /^{\s*\$(\w+)\s*}$/
@@ -129,5 +132,6 @@ def parse_value(v)
v
end
end
+
end
end
diff --git a/hammer/services/tags/asset.rb b/hammer/services/tags/asset.rb
index ef41278..583e217 100644
--- a/hammer/services/tags/asset.rb
+++ b/hammer/services/tags/asset.rb
@@ -22,32 +22,19 @@ class Asset < TagContainer
%w(id title name alt_text description).each do |mthd|
tag "file:#{mthd}" do |tag|
tag.locals.asset[mthd.to_sym]
- #Hammer.error "file:#{mthd} tag is not implemented yet"
end
end
tag 'file:filename' do |tag|
- # tag.locals.asset.try(:filename)
- asset = tag.locals.asset
- #Hammer.error "file:filename tag is not implemented yet"
- asset[:filename]
+ tag.locals.asset[:filename]
end
tag 'file:download_url' do |tag|
- asset = tag.locals.asset
- # asset.public_download_url
- #Hammer.error "file:download_url tag is not implemented yet"
- asset[:download_url]
+ tag.locals.asset[:download_url]
end
tag 'file:image_url' do |tag|
- asset = tag.locals.asset
- # size = tag.attr['size']
- #
- # if asset.is_a?(ImageAsset)
- # asset.image_url(size)
- # end
- asset[:image_url]
+ tag.locals.asset[:image_url]
end
@@ -59,73 +46,61 @@ class Asset < TagContainer
%w(id class alt title).each do |opt|
options[opt.to_sym] = tag.attr[opt] if tag.attr.has_key?(opt)
end
- #
- # if asset.is_a?(ImageAsset)
+
options[:alt] ||= asset[:alt_text]
- # asset.image_tag(size, options)
- # end
- #Hammer.error "file:image_tag is not implemented yet"
- content_tag :img, { :src=>asset[:image_url], :id => options[:id], :class => options[:class], :alt => options[:alt], :title => options[:title]}
+ content_tag :img, {
+ src: asset[:image_url],
+ id: options[:id],
+ class: options[:class],
+ alt: options[:alt],
+ title: options[:title]
+ }
end
- #
tag 'files' do |tag|
- # tag.locals.assets = find_with_options(tag, tag.globals.site.assets)
- # tag.expand
- if tag.globals.context.data && tag.globals.context.data['files']
+ if tag.globals.context.data['files']
tag.expand
+ else
+ Hammer.key_missing "files"
end
-
- #Hammer.error "files tag is not implemented yet"
end
tag 'files:count' do |tag|
- # count_items tag, tag.locals.assets
- if tag.globals.context.data && tag.globals.context.data['files']
- files = tag.globals.context.data['files']
- loop_over(tag, files, false).count
- end
+ tag.globals.context.data['files'].count
end
tag 'files:each' do |tag|
- # loop_over tag, tag.locals.assets
- if tag.globals.context.data && tag.globals.context.data['files']
- #loop_over tag, tag.globals.context.data['files']
- files = tag.globals.context.data['files']
- loop_over tag, files
- end
-
- #Hammer.error "files:each tag is not implemented yet"
+ loop_over tag, tag.globals.context.data['files']
end
- # def self.decorated_asset(asset)
- # unless asset.is_a? ApplicationDecorator
- # AssetDecorator.decorate(asset)
- # end
- # end
+ def self.decorated_asset(asset)
+ # unless asset.is_a? ApplicationDecorator
+ # AssetDecorator.decorate(asset)
+ # end
+ end
def self.find_with_options(tag, target)
conditions = tag.attr.symbolize_keys
filter = {
- :title => conditions[:title],
- :types => conditions[:types] || [],
- :tags => conditions[:labels] || [],
- :tags_op => conditions[:labels_match] || 'any',
- :order => conditions[:by] || 'name',
- :reverse_order => conditions[:order] == 'desc' ? '1' : '0',
- :random => conditions[:random].to_s.to_b,
- :page => conditions[:offset].present? ? conditions[:offset].to_i : 1,
- :limit => conditions[:limit].present? ? conditions[:limit].to_i : 50
+ title: conditions[:title],
+ types: conditions[:types] || [],
+ tags: conditions[:labels] || [],
+ tags_op: conditions[:labels_match] || 'any',
+ order: conditions[:by] || 'name',
+ reverse_order: conditions[:order] == 'desc' ? '1' : '0',
+ random: conditions[:random].to_s.to_b,
+ page: conditions[:offset].present? ? conditions[:offset].to_i : 1,
+ limit: conditions[:limit].present? ? conditions[:limit].to_i : 50
}
# assets = Filter::Assets.new(target, filter).all
end
def self.count_items(tag, target)
- items = find_with_options(tag, target)
- items.reorder(nil).count # Order is irrelevant for counting
+ # items = find_with_options(tag, target)
+ # items.reorder(nil).count # Order is irrelevant for counting
end
def self.loop_over(tag, target, out=true)
diff --git a/hammer/services/tags/basic.rb b/hammer/services/tags/basic.rb
index a8134cd..ff542c8 100644
--- a/hammer/services/tags/basic.rb
+++ b/hammer/services/tags/basic.rb
@@ -42,7 +42,7 @@ class Basic < TagContainer
if tag.globals.context.data && tag.globals.context.data['site'] && tag.globals.context.data['site'][attr.to_s]
tag.globals.context.data['site'][attr.to_s]
else
- Hammer.error "Missing key #{attr} under site:"
+ Hammer.key_missing attr, {parent_key: "site"}
end
end
end
@@ -56,7 +56,7 @@ class Basic < TagContainer
if tag.globals.context.data && tag.globals.context.data['site'] && tag.globals.context.data['site']['data'] && tag.globals.context.data['site']['data'][attr]
tag.globals.context.data['site']['data'][attr]
else
- Hammer.error "Missing key #{attr} under site:data"
+ Hammer.key_missing attr, {parent_key: "site:data"}
end
end
diff --git a/hammer/services/tags/blog.rb b/hammer/services/tags/blog.rb
index c444375..feaf12e 100644
--- a/hammer/services/tags/blog.rb
+++ b/hammer/services/tags/blog.rb
@@ -34,19 +34,35 @@ def initialize
end
tag 'article:id' do |tag|
- tag.locals.article['id']
+ if tag.locals.article['id']
+ tag.locals.article['id']
+ else
+ Hammer.key_missing "id", {parent_key: "article"}
+ end
end
tag 'article:name' do |tag|
- tag.locals.article['name']
+ if tag.locals.article['name']
+ tag.locals.article['name']
+ else
+ Hammer.key_missing "name", {parent_key: "article"}
+ end
end
tag 'article:title' do |tag|
- tag.locals.article['title']
+ if tag.locals.article['title']
+ tag.locals.article['title']
+ else
+ Hammer.key_missing "title", {parent_key: "article"}
+ end
end
tag 'article:path' do |tag|
- tag.render 'page:url', tag.attr
+ if tag.locals.article['url']
+ tag.locals.article['url']
+ else
+ tag.render 'page:url', tag.attr
+ end
end
tag 'article:content' do |tag|
@@ -54,48 +70,66 @@ def initialize
tag.locals.article[:content]
end
- # TODO: Use a different taggable attribute, such as 'tags', instead of 'labels'.
- # I think labels should be used for admin purposes and 'tags' should be used
- # for the public.
tag 'article:tags' do |tag|
# tag.locals.article.label_list.join(',')
if tag.locals.article['tags'].kind_of?(Array)
tag.locals.article['tags'].join(',')
elsif
- Hammer.error 'Article tags should be an array see Hammer Mock Data Wiki'
+ Hammer.error 'Article tags should be an array'
end
end
tag 'article:published_at' do |tag|
- tag.locals.article[:published_at]
+ if tag.locals.article[:published_at]
+ tag.locals.article[:published_at]
+ else
+ "#{(0..10).to_a.sample} days ago"
+ end
end
tag 'article:author_first_name' do |tag|
- tag.locals.article[:created_by][:first_name]
+ if tag.locals.article[:created_by][:first_name]
+ tag.locals.article[:created_by][:first_name]
+ else
+ Hammer.key_missing "first_name", {parent_key: "article"}
+ end
end
tag 'article:author_last_name' do |tag|
- tag.locals.article[:created_by][:last_name]
+ if tag.locals.article[:created_by][:last_name]
+ tag.locals.article[:created_by][:last_name]
+ else
+ Hammer.key_missing "last_name", {parent_key: "article"}
+ end
end
tag 'article:author_full_name' do |tag|
- tag.locals.article[:created_by][:first_name] + ' ' + tag.locals.article[:created_by][:last_name]
+ if tag.locals.article[:created_by]
+ if tag.locals.article[:created_by][:first_name] || tag.locals.article[:created_by][:last_name]
+ [tag.locals.article[:created_by][:first_name], tag.locals.article[:created_by][:last_name]].join(" ")
+ else
+ content = []
+ content << (Hammer.key_missing "first_name or last_name", {parent_key: "article:created_by", comment: true, warning: true})
+ content << [Faker::Name.first_name, Faker::Name.last_name].join(" ")
+ content.join("")
+ end
+ else
+ content = []
+ content << (Hammer.key_missing "created_by", {parent_key: "article", comment: true})
+ content << [Faker::Name.first_name, Faker::Name.last_name].join(" ")
+ content.join("")
+ end
end
tag 'articles' do |tag|
- # tag.locals.blog ||= load_blog(tag)
- # tag.locals.articles = filter_articles(tag, tag.locals.blog.children.published)
- # tag.expand
-
- page_id = tag.globals.context.data['page']['id']
- articles = tag.globals.context.data['blog'].select{|w,v| w['id'] == page_id }
- if articles.count > 0
- tag.locals.articles = articles.first['articles']
+
+ tag.locals.blog ||= load_blog(tag)
+ if tag.locals.blog['articles']
+ tag.locals.articles = tag.locals.blog['articles']
+ tag.expand
else
- tag.locals.articles = []
+ Hammer.key_missing "articles", {parent_key: "blog"}
end
- tag.expand
-
end
tag 'articles:each' do |tag|
@@ -116,7 +150,7 @@ def initialize
tag 'articles:if_no_articles' do |tag|
# cnt = tag.locals.articles.try(:all).try(:count)
# tag.expand if cnt.nil? or cnt == 0
- if tag.locals.articles.count.nil? or tag.locals.articles.count == 0
+ if tag.locals.articles.count.nil? || tag.locals.articles.count == 0
tag.expand
end
end
@@ -227,51 +261,55 @@ def gather_options(tag, defaults = {})
end
def load_blog(tag)
- # page = if tag.attr['id'].present?
- # tag.globals.site.pages.find(tag.attr['id']) rescue nil
- # else
- # tag.locals.page
- # end
- #
- # if page.type == 'ArticlePage'
- # page = page.parent
- # elsif page.type != 'BlogPage'
- # page = nil
- # end
- #
- # decorated_page(page)
- if tag.globals.context.data && tag.globals.context.data['blog'].kind_of?(Array)
- if tag.attr['id']
- tag.locals.blog = tag.globals.context.data['blog'].select{|w,v| w['id'].to_s ==(tag.attr['id']) }.first
+ tag.locals.errors = []
+ if tag.globals.context.data['page']
+ tag.locals.page = tag.globals.context.data['page']
+ else
+ tag.locals.errors << (Hammer.key_missing "page")
+ end
+
+ blogs = tag.globals.context.data['blogs'] || tag.globals.context.data['blog']
+
+ if tag.globals.context.data['blog']
+ tag.locals.errors << (Hammer.error "Depreciation Notice: blog:
key to be renamed blogs:
in future release", {comment: true, warning: true})
+ end
+ if blogs
+ if blogs.kind_of?(Array)
+ if blogs.select{|w| w['id'].to_s == (tag.locals.page['id'].to_s) }.first
+ tag.locals.blog = blogs.select{|w| w['id'].to_s == (tag.locals.page['id'].to_s) }.first
+ else
+ tag.locals.errors << (Hammer.error "Could not find blog with id: #{tag.locals.page['id']} in mock_data.yml")
+ end
else
- tag.locals.blog = tag.globals.context.data['blog'].first
+ tag.locals.errors << (Hammer.error "Depreciation Notice: in future release blogs:
should be an Array in mock_data.yml", {comment: true, warning: true})
+ tag.locals.blog = tag.globals.context.data['blog']
end
else
- Hammer.error 'No Blog Found in Mock Data File'
+ tag.locals.errors << (Hammer.key_missing "blogs")
end
-
end
def load_article(tag)
- if tag.globals.context.data && tag.globals.context.data['blog'] && tag.globals.context.data['blog'].first['articles']
- tag.globals.context.data['blog'].first['articles'].sample
- else
- content = <<-CONTENT
-
#{Faker::Lorem.paragraph(2)}
- #{Faker::Lorem.paragraph(5)}
- #{Faker::Lorem.paragraph(3)}
- CONTENT
- article = {
- :name => Faker::Lorem.sentence(1),
- :title => Faker::Lorem.sentence(1),
- :created_by => { :first_name => Faker::Name.first_name, :last_name => Faker::Name.last_name },
- :content => content,
- :published_at => Random.rand(11).to_s+ " days ago"
- }
- tag.locals.article = article
- article
- end
+ # if tag.globals.context.data && tag.globals.context.data['blog'] && tag.globals.context.data['blog'].first['articles']
+ # tag.globals.context.data['blog'].first['articles'].sample
+ # else
+ # content = <<-CONTENT
+ # #{Faker::Lorem.paragraph(2)}
+ # #{Faker::Lorem.paragraph(5)}
+ # #{Faker::Lorem.paragraph(3)}
+ # CONTENT
+ # article = {
+ # :name => Faker::Lorem.sentence(1),
+ # :title => Faker::Lorem.sentence(1),
+ # :created_by => { :first_name => Faker::Name.first_name, :last_name => Faker::Name.last_name },
+ # :content => content,
+ # :published_at => Random.rand(11).to_s+ " days ago"
+ # }
+ # tag.locals.article = article
+ # article
+ # end
+ tag.locals.blog['articles'].sample
end
def decorated_page(page)
diff --git a/hammer/services/tags/content.rb b/hammer/services/tags/content.rb
index e923304..4a3b1ab 100644
--- a/hammer/services/tags/content.rb
+++ b/hammer/services/tags/content.rb
@@ -14,7 +14,7 @@ class Content < TagContainer
tag.globals.content_for[name]
else
if tag.globals.yield.empty?
- Hammer.error "No yield data. Are you displaying a layout?"
+ Hammer.error "No yield data. Are you trying to view a layout?"
else
tag.globals.yield
end
@@ -49,24 +49,38 @@ class Content < TagContainer
end
tag 'editable_region' do |tag|
- if tag.globals.context.data
- if tag.globals.context.data['editable_region'] && tag.globals.context.data['editable_region'][tag.attr['name']]
- content = tag.globals.context.data['editable_region'][tag.attr['name']]
+ # if tag.globals.context.data
+ # if tag.globals.context.data['editable_region'] && tag.globals.context.data['editable_region'][tag.attr['name']]
+ # content = tag.globals.context.data['editable_region'][tag.attr['name']]
+ # else
+ # content = Hammer.error "Set data for key: #{tag.attr['name']} under editable_region in the mock_data file"
+ # end
+ # else
+ # content = Faker::Lorem.paragraph(rand(2..10))
+ # end
+ # if tag.globals.context.data['show_editable_regions']
+ # if tag.attr['scope'] == "site"
+ # ""+content+"
"
+ # else
+ # ""+content+"
"
+ # end
+ # else
+ # content
+ # end
+ rname = tag.attr["name"]
+ if tag.globals.context.data['editable_region']
+ if tag.globals.context.data['editable_region'][rname]
+ tag.globals.context.data['editable_region'][rname]
else
- content = Hammer.error "Set data for key: #{tag.attr['name']} under editable_region in the mock_data file"
+ content = []
+ content << (Hammer.key_missing rname, {parent_key: "editable_region", warning: true, message: "auto generated paragraph added below"})
+ content << "#{Faker::Lorem.paragraph(rand(2..10))}
"
+ content.join("")
end
else
- content = Faker::Lorem.paragraph(rand(2..10))
- end
- if tag.globals.context.data['show_editable_regions']
- if tag.attr['scope'] == "site"
- ""+content+"
"
- else
- ""+content+"
"
- end
- else
- content
+ Hammer.key_missing "editable_region"
end
+
end
# The contents of this tag will only be rendered/accessible in the editor. This could be used, for example, to
@@ -75,7 +89,7 @@ class Content < TagContainer
# templates, if desired.
tag 'edit_mode_only' do |tag|
# if tag.globals.mode == Slate::ViewModes::EDIT
- if tag.globals.context.data && tag.globals.context.data['edit_mode'] == true
+ if tag.globals.context.data['edit_mode'] == true
tag.expand
end
end
diff --git a/hammer/services/tags/menus.rb b/hammer/services/tags/menus.rb
index 1f9535c..d64bb0f 100644
--- a/hammer/services/tags/menus.rb
+++ b/hammer/services/tags/menus.rb
@@ -1,4 +1,4 @@
-module Tags
+module Tags
class Menus < TagContainer
tag 'site_menu' do |tag|
@@ -6,6 +6,7 @@ class Menus < TagContainer
tag.globals.context.data['site_menu']
else
<<-MENU
+ #{Hammer.key_missing "site_menu", { message: "auto generated menu inserted below"}}
- Page 1
- Page 2
@@ -21,7 +22,9 @@ class Menus < TagContainer
if tag.globals.context.data && tag.globals.context.data['sub_menu']
tag.globals.context.data['sub_menu']
else
+
<<-MENU
+ #{Hammer.key_missing "sub_menu", { message: "auto generated menu inserted below"}}
- Sub Page 1
- Sub Page 2
@@ -38,6 +41,7 @@ class Menus < TagContainer
tag.globals.context.data['ancestor_menu']
else
<<-MENU
+ #{Hammer.key_missing "ancestor_menu", { message: "auto generated menu inserted below"}}
- Sub Page 1
- Sub Page 2
diff --git a/hammer/services/tags/page.rb b/hammer/services/tags/page.rb
index 415fcdd..2c180fd 100644
--- a/hammer/services/tags/page.rb
+++ b/hammer/services/tags/page.rb
@@ -3,186 +3,115 @@ class Page < TagContainer
# Page tags
tag 'page_name' do |tag|
- # tag.globals.page.name
- if tag.globals.context.data
- if tag.globals.context.data['page'] && tag.globals.context.data['page']['name']
- tag.globals.context.data['page']['name']
- elsif tag.globals.context.data['page_name']
- tag.globals.context.data['page_name']
- else
- Hammer.error "Add key name under page"
- end
+ tag.locals.page ||= tag.globals.context.data['page']
+ if tag.locals.page['name']
+ tag.locals.page['name']
else
- "Page Name"
+ Hammer.key_missing "name", {parent_key: "page"}
end
end
tag 'root' do |tag|
- # tag.locals.page = tag.globals.site.root_page
- # tag.expand
- if tag.globals.context.data['root']
- tag.globals.context.data['root'] ? tag.expand : false
+ if tag.locals.page['root']
+ tag.expand
+ else
+ Hammer.key_missing "root", {parent_key: "page"}
end
end
# Reset the page context to the current (global) page.
tag 'current_page' do |tag|
- # tag.locals.page = tag.globals.page
- # tag.expand
- # Hammer.error "current_page tag is not yet implemented"
tag.expand
end
tag 'if_current_page' do |tag|
- # tag.expand if tag.locals.page.id === tag.globals.page.id
- # Hammer.error "if_current_page tag is not yet implemented"
tag.expand
end
tag 'unless_current_page' do |tag|
- # tag.expand if tag.locals.page.id != tag.globals.page.id
- # Hammer.error "unless_current_page tag is not yet implemented"
tag.expand
end
tag 'parent' do |tag|
- # tag.locals.page = decorated_page tag.locals.page.parent
- # tag.expand
- # Hammer.error "parent tag is not yet implemented"
tag.expand
end
tag 'if_parent' do |tag|
- # parent = tag.locals.page.parent
- # tag.expand if parent && !parent.root?
- # Hammer.error "if_parent tag is not yet implemented"
tag.expand
end
tag 'unless_parent' do |tag|
- # parent = tag.locals.page.parent
- # tag.expand unless parent && !parent.root?
- # Hammer.error "unless_parent tag is not yet implemented"
tag.expand
end
tag 'previous_sibling' do |tag|
- # tag.locals.page = p = decorated_page(tag.locals.page.previous_sibling)
- # tag.expand if p.present?
tag.expand
end
tag 'next_sibling' do |tag|
- # tag.locals.page = p = decorated_page(tag.locals.page.next_sibling)
- # tag.expand if p.present?
tag.expand
end
tag 'if_children' do |tag|
- # tag.expand if tag.locals.page.has_children?
- # Hammer.error "if_children tag is not yet implemented"
tag.expand
end
tag 'if_childless' do |tag|
- # tag.expand if tag.locals.page.is_childless?
- # Hammer.error "if_childless tag is not yet implemented"
tag.expand
end
tag 'if_has_siblings' do |tag|
- # tag.expand if tag.locals.page.has_siblings?
- # Hammer.error "if_has_siblings tag is not yet implemented"
tag.expand
end
tag 'if_only_child' do |tag|
- # tag.expand if tag.locals.page.is_only_child?
- # Hammer.error "if_only_child tag is not yet implemented"
tag.expand
end
tag 'if_ancestor' do |tag|
- # tag.expand if (tag.globals.page.ancestor_ids + [tag.globals.page.id]).include?(tag.locals.page.id)
- # Hammer.error "if_ancestor tag is not yet implemented"
tag.expand
end
tag 'if_page_depth_eq' do |tag|
allowed_options = %w(page_depth)
options = tag.attr.select { |k,v| allowed_options.include?(k) }
- # tag.expand if options['page_depth'].to_i.abs === tag.globals.page.depth
- if tag.globals.context.data && tag.globals.context.data['if_page_depth_eq']
+ if tag.globals.context.data['if_page_depth_eq']
tag.expand if options['page_depth'].to_i.abs === tag.globals.context.data['if_page_depth_eq']
else
+ Hammer.key_missing 'if_page_depth_eq', {message: "tag expanded anyways"}
tag.expand
end
-
- #"fix if_page_depth_eq tag"
end
tag 'if_page_depth_gt' do |tag|
allowed_options = %w(page_depth)
options = tag.attr.select { |k,v| allowed_options.include?(k) }
- # tag.expand if tag.globals.page.depth > options['page_depth'].to_i.abs
- if tag.globals.context.data && tag.globals.context.data['if_page_depth_eq']
- tag.expand if tag.globals.context.data['if_page_depth_gt'] > options['page_depth'].to_i.abs
+
+ if tag.globals.context.data['if_page_depth_gt']
+ tag.expand if options['page_depth'].to_i.abs < tag.globals.context.data['if_page_depth_gt']
else
+ Hammer.key_missing 'if_page_depth_gt', {message: "tag expanded anyways"}
tag.expand
end
end
tag 'unless_ancestor' do |tag|
- # tag.expand unless (tag.globals.page.ancestor_ids + [tag.globals.page.id]).include?(tag.locals.page.id)
- # Hammer.error "unless_ancestor tag is not yet implemented"
tag.expand
end
- # The get_page tag allows you to retrieve a site page other than the current page and then use other
- # page related tags within it to interact with it and pull information from it.
- #
- # Example:
- #
- #
- #
- #
- #
- # The example above will output the name of the page with ID 1234, as long as the page exists and
- # belongs to the current site.
tag 'get_page' do |tag|
- # page = tag.globals.site.pages.find(tag.attr['id']) rescue nil
- #
- # unless page.present?
- # "Could not find the given page."
- # else
- # tag.locals.page = decorated_page(page)
- # tag.expand
- # end
- # Hammer.error "get_page tag is not yet implemented"
- # tag.expand
-
-
- if tag.globals.context.data && tag.globals.context.data['pages']
- data = tag.globals.context.data['pages'].find { |h| h['id'] == tag.attr['id'].to_i }
-
- puts "Processing tag get_page with page id #{data["id"]}".colorize(:light_blue)
- oldcontext = tag.globals.context.data['page']
-
- if data
- newcontext = data
- tag.expand(newcontext,oldcontext)
+ if tag.globals.context.data['pages']
+ if tag.globals.context.data['pages'].find { |h| h['id'] == tag.attr['id'].to_i }
+ tag.locals.page = tag.globals.context.data['pages'].find { |h| h['id'] == tag.attr['id'].to_i }
+ tag.expand
else
- if tag.attr['id'].nil
- Hammer.error "get_page tag attribute of id is nil"
- else
- Hammer.error "key pages:id of: #{tag.attr['id']} was not found in mock_data file"
- end
+ Hammer.error "There is no page with the id: #{tag.attr['id']} in the mock_data.yml "
end
else
- Hammer.error "Set key pages in mock_data file"
+ Hammer.key_missing "pages"
end
end
@@ -193,16 +122,22 @@ class Page < TagContainer
tag.expand
end
- # Expands the content of the tag if the current (local) page is the current (global) site's
- # default page.
tag 'page:if_site_default' do |tag|
- tag.expand if tag.globals.context.data['page']['default_page']
+ if tag.locals.page['default_page']
+ tag.expand
+ else
+ Hammer.key_missing "default_page", {parent_key: "page"}
+ end
end
# Expands the content of the tag if the current (local) page is NOT the current (global) site's
# default page.
tag 'page:unless_site_default' do |tag|
- tag.expand unless tag.globals.context.data['page']['default_page']
+ unless tag.locals.page['default_page']
+ tag.expand
+ else
+ Hammer.key_missing "default_page", {parent_key: "page"}
+ end
end
[:id, :name, :path, :slug, :meta_description, :title, :alternate_name, :depth].each do |attr|
@@ -211,7 +146,7 @@ class Page < TagContainer
if tag.locals.page[attr.to_s]
tag.locals.page[attr.to_s]
else
- Hammer.error "Attribute missing page:#{attr}"
+ Hammer.key_missing attr, {parent_key: "page"}
end
end
end
@@ -220,92 +155,83 @@ class Page < TagContainer
tag "page:#{attr.to_s}" do |tag|
# tag.locals.page.send(attr)
#{"}fix page:#{attr.to_s} tag"
- if tag.globals.context.data && tag.globals.context.data['page'] && tag.globals.context.data['page'][attr.to_s]
- tag.globals.context.data['page'][attr.to_s]
+
+ if tag.locals.page[attr.to_s]
+ tag.locals.page[attr.to_s]
else
- Random.rand(11).to_s+ " days ago"
+ content = []
+ content << (Hammer.key_missing attr.to_s, {parent_key: "page", message: "Auto generated date inserted below", comment: true, warning: true})
+ content << Random.rand(11).to_s+ " days ago"
+ content.join("")
end
end
end
tag 'page:url' do |tag|
- # format = tag.attr['format'].to_s.strip
- # format = nil if format.downcase =~ /\Ahtml?\Z/
- # # No need to append HTML or HTM to URLs since HTML is the default.
- #
- # url = tag.locals.page.url(tag.globals.mode)
- # url << ".#{format}" if format.present?
- # url
- # Hammer.error "page:url tag is not yet implemented"
- if tag.globals.context.data && tag.globals.context.data['page'] && tag.globals.context.data['page']['url']
- tag.globals.context.data['page']['url']
+ if tag.locals.page['url']
+ tag.locals.page['url']
else
+ # TODO: Figure out how to inject error messages as DOM based, if in pure HTML its fine, but in an attribute it breaks
+ # Hammer.key_missing 'url', {parent_key: "page", message: "Auto generated url inserted below"}
tag.context.globals.context.request.path
end
end
# Retrieve an attribute from the current page.
tag 'page:attr' do |tag|
- attr = tag.attr['name']
+ # attr = tag.attr['name']
# page = tag.locals.page
# page.send(attr) if page.radius_attributes.include?(attr)
- if tag.globals.context.data && tag.globals.context.data['page'][attr]
- tag.globals.context.data['page'][attr]
+
+ attr = tag.attr['name']
+ if tag.locals.page[attr.to_s]
+ tag.locals.page[attr.to_s]
else
- Hammer.error "Page Attribute missing Page:#{attr}"
+ Hammer.key_missing attr, {parent_key: "page"}
end
end
# Retrieve an attribute from the custom_data for the page.
tag 'page:data' do |tag|
- attr = tag.attr['name']
+ # attr = tag.attr['name']
#page = tag.locals.page
#(page.custom_data || {})[attr] || "ERROR: Custom data for '#{attr}' does not exist."
- if tag.globals.context.data && tag.globals.context.data['page'] && tag.globals.context.data['page']['data'] && tag.globals.context.data['page']['data'][attr]
- tag.globals.context.data['page']['data'][attr]
+
+ attr = tag.attr['name']
+ if tag.locals.page['data'] && tag.locals.page['data'][attr.to_s]
+ tag.locals.page['data'][attr.to_s]
else
- Hammer.error "Page Data attribute missing Page:data:#{attr}"
+ Hammer.key_missing attr, {parent_key: "page:data"}
end
-
-
end
#Fake a url for editing a page
tag 'page:edit_url' do |tag|
- if tag.globals.context.data && tag.globals.context.data['page'] && tag.globals.context.data['page']['edit_url']
- tag.globals.context.data['page']['edit_url']
+ if tag.locals.page['edit_url']
+ tag.locals.page['edit_url']
else
- # return a hash as a url.
- "#"
+ Hammer.key_missing "edit_url", {parent_key: "page"}
end
end
# Retrieve the value of the first attribute, from the list of comma separated attributes given in the 'names' tag attribute, that has does not have a blank value.
tag 'page:first_non_blank_attr' do |tag|
- if tag.globals.context.data && tag.globals.context.data["page"]
- attrs = (tag.attr['names'] || '').split(',').map{ |a| a.strip.to_sym }
- attr = self.first_present_attribute(attrs.select{ |attr| tag.globals.context.data["page"].include?(attr.to_s) }.uniq)
- tag.globals.context.data["page"][attr]
- else
- Hammer.error "Set key page in mock_data file"
- end
+ attrs = (tag.attr['names'] || '').split(',').map{ |a| a.strip.to_sym }
+ attr = self.first_present_attribute(attrs.select{ |attr| tag.locals.page.include?(attr.to_s) }.uniq)
+ tag.locals.page[attr.to_s]
end
tag 'page:content' do |tag|
- # rname = tag.attr['name'].strip
- # if tag.globals.context.data
- # if tag.globals.context.data["page"] && tag.globals.context.data["page"]["content"] && tag.globals.context.data["page"]["content"][rname]
- # tag.globals.context.data["page"]["content"][rname]
- # else
- # Hammer.error "Set key page:content:#{rname} in mock_data file"
- # end
- # end
rname = tag.attr['name'].strip
- if tag.locals.page[:content][rname]
- tag.locals.page[:content][rname]
+ if tag.locals.page[:content]
+ if tag.locals.page[:content][rname]
+ tag.locals.page[:content][rname]
+ else
+ Hammer.key_missing rname, {parent_key: "page:content"}
+ end
else
- Hammer.error "Set key page:content:#{rname} in mock_data file"
+ Hammer.key_missing "content", {parent_key: "page"}
end
end
@@ -315,87 +241,73 @@ class Page < TagContainer
rname = tag.attr['region'].strip
always = tag.attr['always_show_in_edit_mode'].to_s.to_b && tag.globals.context.data['edit_mode'] == true
content = nil
- if tag.globals.context.data["page"] && tag.globals.context.data["page"]["content"] && tag.globals.context.data["page"]["content"][rname]
- content = tag.globals.context.data["page"]["content"][rname]
+ if tag.locals.page
+ if tag.locals.page['content']
+ if tag.locals.page['content'][rname]
+ content = tag.locals.page['content'][rname]
+ tag.expand if always || !content.nil?
+ else
+ Hammer.key_missing rname, {parent_key: 'page:content'}
+ end
+ else
+ Hammer.key_missing 'content', {parent_key: 'page'}
+ end
+ else
+ Hammer.key_missing "page"
end
- tag.expand if always || !content.nil?
end
tag 'page:unless_has_content_for' do |tag|
rname = tag.attr['region'].strip
always = tag.attr['always_show_in_edit_mode'].to_s.to_b && tag.globals.context.data['edit_mode'] == true
content = nil
- if tag.globals.context.data["page"] && tag.globals.context.data["page"]["content"] && tag.globals.context.data["page"]["content"][rname]
- content = tag.globals.context.data["page"]["content"][rname]
+ if tag.locals.page
+ if tag.locals.page['content']
+ if tag.locals.page['content'][rname]
+ content = tag.locals.page['content'][rname]
+ tag.expand if always || !content.nil?
+ else
+ Hammer.key_missing rname, {parent_key: 'page:content'}
+ end
+ else
+ Hammer.key_missing "content", {parent_key: "page"}
+ end
+ else
+ Hammer.key_missing "page"
end
- tag.expand if always || content.nil?
end
# Page template tags
tag 'page:template' do |tag|
- # tag.locals.page_template = tag.locals.page.template
- # tag.expand
- if tag.globals.context.data && tag.globals.context.data["page"]
- if tag.globals.context.data["page"]["template"]
- tag.locals.page_template = tag.globals.context.data["page"]["template"]
- end
- end
tag.expand
-
end
tag 'page:template:name' do |tag|
- # tag.locals.page_template.name
- if tag.locals.page_template && tag.locals.page_template["name"]
- name = tag.locals.page_template["name"]
- else
- name = File.basename(tag.context.globals.context.request.path, ".*")
- end
- name
+ File.basename(tag.context.globals.context.request.path, ".*")
end
# Page tree navigation tags
[:descendants, :ancestors, :children, :siblings].each do |method|
tag method.to_s do |tag|
- # tag.locals.send("#{method.to_s}=", find_with_options(tag, tag.locals.page.send(method)))
tag.expand
end
tag "#{method.to_s}:count" do |tag|
- #count_items tag, tag.locals.send(method)
- if tag.globals.context.data && tag.globals.context.data["pages"]
- tag.globals.context.data["pages"].count
+ if tag.globals.context.data['pages']
+ tag.globals.context.data["pages"].count > 0
else
- Hammer.error "#{method.to_s}:count needs the 'pages' key in mock_data.yml"
+ Hammer.key_missing "pages"
end
+
end
tag "#{method.to_s}:each" do |tag|
if tag.locals.context.data['pages']
loop_over tag, tag.locals.context.data['pages']
else
- Hammer.error "#{method.to_s}:each tag needs the 'pages' key in mock_data.yml"
+ Hammer.key_missing "pages"
end
-
- #loop_over tag, tag.locals.send(method)
- # if tag.globals.context.data && tag.globals.context.data["pages"]
- # mock_pages = tag.globals.context.data["pages"]
- # mock_pages.unshift(tag.globals.context.data["page"]) #include original page object
- # oldcontext = tag.globals.context.data["page"]
- # output = ""
- # mock_pages.each do |page|
- # tag.globals.context.data["page"] = page #set the current context
- # output = output + tag.expand
- # end
- # tag.globals.context.data["page"] = oldcontext
- # output
- # else
- # Hammer.error "#{method.to_s}:each needs the 'pages' key in mock_data.yml"
- # end
-
-
-
end
end
@@ -445,16 +357,6 @@ def self.count_items(tag, target)
end
def self.loop_over(tag, target)
- # items = find_with_options(tag, target)
- # output = []
- #
- # items.each_with_index do |item, index|
- # tag.locals.child = decorated_page item
- # tag.locals.page = decorated_page item
- # output << tag.expand
- # end
- #
- # output.flatten.join('')``
if tag.attributes
if tag.attributes['limit']
limit = tag.attributes['limit'].to_i - 1
diff --git a/hammer/services/theme_context.rb b/hammer/services/theme_context.rb
index 2c8a31c..2254eeb 100644
--- a/hammer/services/theme_context.rb
+++ b/hammer/services/theme_context.rb
@@ -37,22 +37,26 @@ def self.tag(name, &block)
end
def tag_missing(name, attributes, &block)
- Hammer.error "OH NOES! <r:#{name} /> does not yet exist in hammer. Be a good samaritan and file a Github issue!"
+ style = "background-color: #eee; border-radius: 3px; font-family: monospace; padding: 0 3px;"
+ Hammer.error "OH NOES! Tag or tag method #{name}
does not yet exist in hammer. Be a good samaritan and file a Github issue!"
end
private
def load_tags_from(object)
method_regex = /^tag:/
tag_methods = object.methods.grep(method_regex).sort
+
tag_methods.each do |mname|
tag_method = object.method(mname)
-
define_tag mname[4..-1] do |tag_binding|
if tag_method.arity == 0
tag_method.call
-
else
- tag_method.call tag_binding
+ begin
+ tag_method.call tag_binding
+ rescue TypeError => e
+ Hammer.error "Something is wrong with radius #{mname} Error Message: #{e}"
+ end
end
end
end