Browse Source

Added support for "extends" in layouts

dev
Andy Meneely 12 years ago
parent
commit
373d841823
  1. 3
      README.md
  2. 12
      lib/squib/deck.rb
  3. 1
      lib/squib/input_helpers.rb
  4. 33
      samples/custom-layout.yml
  5. 16
      samples/use_layout.rb

3
README.md

@ -6,7 +6,8 @@ Squib is a Ruby [DSL](http://en.wikipedia.org/wiki/Domain-specific_language) for
* Reading `.xlsx` files * Reading `.xlsx` files
* Basic shape drawing * Basic shape drawing
* Rendering to PNGs and PDFs * Rendering to PNGs and PDFs
* Unit conversion (inches) * Unit conversion
* Specfiying your layouts in a YML file
* Plus the full power of Ruby! * Plus the full power of Ruby!
Check this out. Check this out.

12
lib/squib/deck.rb

@ -1,4 +1,5 @@
require 'yaml' require 'yaml'
require 'pp'
require 'squib/card' require 'squib/card'
require 'squib/input_helpers' require 'squib/input_helpers'
require 'squib/constants' require 'squib/constants'
@ -86,7 +87,16 @@ module Squib
# @api private # @api private
def load_layout(file) def load_layout(file)
return if file.nil? 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 end
################## ##################

1
lib/squib/input_helpers.rb

@ -25,6 +25,7 @@ module Squib
end end
module_function :needs module_function :needs
# @api private
def layoutify(opts) def layoutify(opts)
unless opts[:layout].nil? unless opts[:layout].nil?
entry = @layout[opts[:layout].to_s] entry = @layout[opts[:layout].to_s]

33
samples/custom-layout.yml

@ -3,25 +3,32 @@ frame:
y: 38 y: 38
width: 750 width: 750
height: 1050 height: 1050
radius: 25
title: title:
x: 125 x: 125
y: 50 y: 50
width: 625 width: 625
height: 150 height: 100
align: !ruby/symbol center align: !ruby/symbol center #see http://www.yaml.org/YAML_for_ruby.html#symbols
hint: !ruby/symbol cyan valign: !ruby/symbol middle
subtitle: subtitle:
x: 125 x: 150
y: 150 y: 150
width: 625 width: 575
height: 150 height: 60
icon_left: align: !ruby/symbol center
x: 200 valign: !ruby/symbol middle
y: 250 icon:
width: 125 width: 125
height: 125 height: 125
icon_right:
x: 400
y: 250 y: 250
width: 125 icon_left:
height: 125 extends: icon
x: 150
icon_middle:
extends: icon
x: 350
y: 400 #overrides the y inherited from icon
icon_right:
extends: icon
x: 550

16
samples/use_layout.rb

@ -2,20 +2,28 @@ require 'squib'
Squib::Deck.new(layout: 'custom-layout.yml') do Squib::Deck.new(layout: 'custom-layout.yml') do
background color: :white 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 rect layout: :frame
# You can also override a given layout entry in the command # 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 # Any command with x-y-width-height options, we can use a custom layout
text str: 'The Title', layout: :title 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 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' text str: 'subtitle', layout: 'subtitle'
# For debugging purposes, you can always print out the loaded layout
# require 'pp'
# pp @layout
save_png prefix: 'layout_' save_png prefix: 'layout_'
end end
Loading…
Cancel
Save