From 373d841823c840d6d7f8fda4c80ae8fa9acf031e Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Tue, 29 Jul 2014 16:18:01 -0400 Subject: [PATCH] Added support for "extends" in layouts --- README.md | 3 ++- lib/squib/deck.rb | 12 +++++++++++- lib/squib/input_helpers.rb | 1 + samples/custom-layout.yml | 33 ++++++++++++++++++++------------- samples/use_layout.rb | 16 ++++++++++++---- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1d9437b..b11fe6d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ Squib is a Ruby [DSL](http://en.wikipedia.org/wiki/Domain-specific_language) for * Reading `.xlsx` files * Basic shape drawing * Rendering to PNGs and PDFs -* Unit conversion (inches) +* Unit conversion +* Specfiying your layouts in a YML file * Plus the full power of Ruby! Check this out. diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index 9b7d826..d40a6c6 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -1,4 +1,5 @@ require 'yaml' +require 'pp' require 'squib/card' require 'squib/input_helpers' require 'squib/constants' @@ -86,7 +87,16 @@ module Squib # @api private def load_layout(file) return if file.nil? - @layout = YAML.load_file(file) + prelayout = YAML.load_file(file) + @layout = {} + prelayout.each do |key, value| + if value.key? "extends" + @layout[key] = prelayout[value["extends"]].merge prelayout[key] + else + @layout[key] = value + end + end + Squib::logger.warn "Multi-level extends not supported. If you want them, contact the developer." if @layout.to_s.include? '"extends"=>' end ################## diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index 3234407..8538c83 100644 --- a/lib/squib/input_helpers.rb +++ b/lib/squib/input_helpers.rb @@ -25,6 +25,7 @@ module Squib end module_function :needs + # @api private def layoutify(opts) unless opts[:layout].nil? entry = @layout[opts[:layout].to_s] diff --git a/samples/custom-layout.yml b/samples/custom-layout.yml index 7ab2ae4..409afd6 100644 --- a/samples/custom-layout.yml +++ b/samples/custom-layout.yml @@ -3,25 +3,32 @@ frame: y: 38 width: 750 height: 1050 + radius: 25 title: x: 125 y: 50 width: 625 - height: 150 - align: !ruby/symbol center - hint: !ruby/symbol cyan + height: 100 + align: !ruby/symbol center #see http://www.yaml.org/YAML_for_ruby.html#symbols + valign: !ruby/symbol middle subtitle: - x: 125 + x: 150 y: 150 - width: 625 - height: 150 -icon_left: - x: 200 - y: 250 + width: 575 + height: 60 + align: !ruby/symbol center + valign: !ruby/symbol middle +icon: width: 125 height: 125 -icon_right: - x: 400 y: 250 - width: 125 - height: 125 +icon_left: + extends: icon + x: 150 +icon_middle: + extends: icon + x: 350 + y: 400 #overrides the y inherited from icon +icon_right: + extends: icon + x: 550 diff --git a/samples/use_layout.rb b/samples/use_layout.rb index 745d930..5d13b13 100644 --- a/samples/use_layout.rb +++ b/samples/use_layout.rb @@ -2,20 +2,28 @@ require 'squib' Squib::Deck.new(layout: 'custom-layout.yml') do background color: :white + hint text: :cyan - # Layouts are YAML files that specify x, y, width, and height coordinates + # Layouts are YAML files that specify any option as a default rect layout: :frame # You can also override a given layout entry in the command - rect layout: :frame, width: 50, height: 50 + circle layout: :frame, x: 50, y: 50, radius: 25 # Any command with x-y-width-height options, we can use a custom layout text str: 'The Title', layout: :title - png file: 'shiny-purse.png', layout: :icon_left + + # Layouts also support an "extends" attribute to reuse settings + svg file: 'spanner.svg', layout: :icon_left + png file: 'shiny-purse.png', layout: :icon_middle svg file: 'spanner.svg', layout: :icon_right - # Strings can also be used in layouts + # Strings can also be used to specify a layout (e.g. from a data file) text str: 'subtitle', layout: 'subtitle' + # For debugging purposes, you can always print out the loaded layout + # require 'pp' + # pp @layout + save_png prefix: 'layout_' end \ No newline at end of file