labels 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.2-p290@labels --create
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ source "http://rubygems.org"
8
+
9
+ # Specify your gem's dependencies in labels.gemspec
10
+ gemspec
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ Labels
2
+ ======
3
+
4
+ A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
5
+
6
+ Features:
7
+
8
+ * Design mailing, shipping, address, barcode and other labels!
9
+ * Definition of label layout and design using XML
10
+ * Support for different layer elements (shape, text, image, barcode, date)
11
+ and layer ordering
12
+ * Dynamic replacement of layer content by providing layer
13
+ data
14
+ * Rendering (and printing) of label documents as PDF
15
+
16
+ Installation
17
+ ------------
18
+
19
+ Labels is a RubyGem and can be installed using:
20
+
21
+ $ gem install labels
22
+
23
+ Layout / Design
24
+ ---------------
25
+
26
+ Labels uses a special XML document type to define label layouts and data.
27
+ It parses XML documents of this type and creates PDF renderings of the document.
28
+
29
+ See *example/label.xml* for an example label document and *dtd/label.dtd* for
30
+ the XML document type definition.
31
+
32
+ Usage
33
+ -----
34
+
35
+ The simplest way to use the Labels library is to open an existing label XML file
36
+ and render the file to PDF:
37
+
38
+ label = Labels.open('/path/to/file.xml')
39
+ label.to_pdf('/path/to/output.pdf')
40
+
41
+ To save the document back to XML just do:
42
+
43
+ label.to_file('/path/to/file.xml')
44
+
45
+ You can also parse a raw labels XML string:
46
+
47
+ xml = '<?xml...>'
48
+
49
+ label = Labels.parse(xml)
50
+ label.to_pdf('/path/to/output.pdf')
51
+
52
+ Label documents can also be created using the Labels library:
53
+
54
+ document = Labels::Document.new
55
+
56
+ paper = Labels::Paper.new(Labels::Paper::LETTER)
57
+ paper.width = 200
58
+ paper.height = 1000
59
+
60
+ template = Labels::Template.new
61
+ template.title = 'My cool template'
62
+ template.columns = 3
63
+ template.rows = 4
64
+ template.width = 100
65
+ template.height = 100
66
+
67
+ shape = Labels::Shape.new
68
+ shape.width = 40
69
+ shape.height = 50
70
+ shape.fill = true
71
+ shape.fill_color = '777777'
72
+
73
+ document.paper = paper
74
+ document.template = template
75
+ document.layers = [shape]
76
+
77
+ document.to_file('/path/to/label.xml')
78
+ document.to_pdf('/path/to/label.pdf')
79
+
80
+ See the API documentation for more information...
81
+
82
+ Command Line
83
+ ------------
84
+
85
+ Labels also provides a command line tool for rendering label XML files:
86
+
87
+ $ labels -h
88
+ Usage: labels [OPTIONS] source target
89
+
90
+ -v, --version
91
+ -h, --help
92
+
93
+ Example:
94
+
95
+ $ labels ./path/to/label.xml ./path/to/output.pdf
96
+
97
+ License
98
+ -------
99
+
100
+ MIT License. See LICENSE file for details.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ require 'bundler/gem_tasks'
8
+
9
+ require 'rspec/core/rake_task'
10
+ RSpec::Core::RakeTask.new('spec')
11
+
12
+ namespace :labels do
13
+ desc "Start the console"
14
+ task :console do
15
+ sh("irb -rubygems -I lib -r labels.rb")
16
+ end
17
+ end
data/bin/labels ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'labels/exec'
5
+
6
+ Labels::Exec.new(ARGV).run
data/dtd/label.dtd ADDED
@@ -0,0 +1,74 @@
1
+ <!ELEMENT label (paper,template,layers,data)>
2
+
3
+ <!ATTLIST paper title CDATA #IMPLIED>
4
+ <!ELEMENT paper (width,height)>
5
+
6
+ <!ATTLIST template title CDATA #IMPLIED>
7
+ <!ATTLIST template description CDATA #IMPLIED>
8
+ <!ATTLIST template manufacturer CDATA #IMPLIED>
9
+ <!ATTLIST template category CDATA #IMPLIED>
10
+ <!ATTLIST template visible (false|true) #REQUIRED>
11
+ <!ELEMENT template (width,height,columns,rows,left-margin,right-margin,top-margin,bottom-margin,horizontal-gutter,vertical-gutter,elliptical,corner-radius)>
12
+
13
+ <!ELEMENT layers (layer+)>
14
+
15
+ <!ATTLIST layer title CDATA #IMPLIED>
16
+ <!ATTLIST layer type CDATA #REQUIRED>
17
+ <!ATTLIST layer column CDATA #IMPLIED>
18
+ <!ATTLIST layer locked (false|true) #REQUIRED>
19
+ <!ATTLIST layer visible (false|true) #REQUIRED>
20
+ <!ATTLIST layer resizable CDATA #REQUIRED>
21
+ <!ELEMENT layer (x,y,width,height,alpha,rotation,content,corner-radius?,elliptical?,fill?,stroke?,fill-color?,stroke-color?,stroke-weight?,font-family?,font-size?,font-color?,bold?,italic?,justification?,fit?,stretch?,symbology?,bar-width?,bar-height?,color?,caption-height?,caption-size?,captioned?,format?)>
22
+
23
+ <!ELEMENT data (item+)>
24
+
25
+ <!ELEMENT item (key+)>
26
+
27
+ <!ATTLIST key name CDATA #REQUIRED>
28
+ <!ATTLIST key type (string|integer|float|boolean|date) #REQUIRED>
29
+ <!ELEMENT key (#PCDATA)>
30
+
31
+ <!ELEMENT width (#PCDATA)>
32
+ <!ELEMENT height (#PCDATA)>
33
+ <!ELEMENT columns (#PCDATA)>
34
+ <!ELEMENT rows (#PCDATA)>
35
+ <!ELEMENT left-margin (#PCDATA)>
36
+ <!ELEMENT right-margin (#PCDATA)>
37
+ <!ELEMENT top-margin (#PCDATA)>
38
+ <!ELEMENT bottom-margin (#PCDATA)>
39
+ <!ELEMENT horizontal-gutter (#PCDATA)>
40
+ <!ELEMENT vertical-gutter (#PCDATA)>
41
+ <!ELEMENT elliptical (#PCDATA)>
42
+ <!ELEMENT corner-radius (#PCDATA)>
43
+
44
+ <!ELEMENT x (#PCDATA)>
45
+ <!ELEMENT y (#PCDATA)>
46
+ <!ELEMENT alpha (#PCDATA)>
47
+ <!ELEMENT rotation (#PCDATA)>
48
+ <!ELEMENT content (#PCDATA)>
49
+
50
+ <!ELEMENT fill (#PCDATA)>
51
+ <!ELEMENT stroke (#PCDATA)>
52
+ <!ELEMENT fill-color (#PCDATA)>
53
+ <!ELEMENT stroke-color (#PCDATA)>
54
+ <!ELEMENT stroke-weight (#PCDATA)>
55
+
56
+ <!ELEMENT font-family (#PCDATA)>
57
+ <!ELEMENT font-size (#PCDATA)>
58
+ <!ELEMENT font-color (#PCDATA)>
59
+ <!ELEMENT bold (#PCDATA)>
60
+ <!ELEMENT italic (#PCDATA)>
61
+ <!ELEMENT justification (#PCDATA)>
62
+
63
+ <!ELEMENT fit (#PCDATA)>
64
+ <!ELEMENT stretch (#PCDATA)>
65
+
66
+ <!ELEMENT symbology (#PCDATA)>
67
+ <!ELEMENT bar-width (#PCDATA)>
68
+ <!ELEMENT bar-height (#PCDATA)>
69
+ <!ELEMENT color (#PCDATA)>
70
+ <!ELEMENT caption-height (#PCDATA)>
71
+ <!ELEMENT caption-size (#PCDATA)>
72
+ <!ELEMENT captioned (#PCDATA)>
73
+
74
+ <!ELEMENT format (#PCDATA)>
data/example/label.xml ADDED
@@ -0,0 +1,168 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <!DOCTYPE label SYSTEM "label.dtd">
3
+ <label>
4
+
5
+ <!-- Paper -->
6
+ <paper title="Letter">
7
+ <width>612</width>
8
+ <height>792</height>
9
+ </paper>
10
+
11
+ <!-- Template -->
12
+ <template title='Avery 2" x 4" Shipping Labels' description="5163/5263..." manufacturer="Avery" category="Shipping Labels" visible="true">
13
+ <width>288</width>
14
+ <height>144</height>
15
+ <columns>2</columns>
16
+ <rows>5</rows>
17
+ <left-margin>13</left-margin>
18
+ <right-margin>13</right-margin>
19
+ <top-margin>36</top-margin>
20
+ <bottom-margin>36</bottom-margin>
21
+ <horizontal-gutter>0</horizontal-gutter>
22
+ <vertical-gutter>10</vertical-gutter>
23
+ <elliptical>false</elliptical>
24
+ <corner-radius>18</corner-radius>
25
+ </template>
26
+
27
+ <!-- Layers -->
28
+ <layers>
29
+
30
+ <!-- Layer -->
31
+ <layer title="Shape" type="shape" column="string" locked="false" visible="true" resizable="true">
32
+ <x>10</x>
33
+ <y>20</y>
34
+ <z>0</z>
35
+ <width>100</width>
36
+ <height>100</height>
37
+ <alpha>1.0</alpha>
38
+ <rotation>35.0</rotation>
39
+ <content></content>
40
+
41
+ <!-- Layer Type - Shape-->
42
+ <corner-radius>10</corner-radius>
43
+ <elliptical>false</elliptical>
44
+ <fill>true</fill>
45
+ <stroke>true</stroke>
46
+ <fill-color>666666</fill-color>
47
+ <stroke-color>000000</stroke-color>
48
+ <stroke-weight>5</stroke-weight>
49
+ </layer>
50
+
51
+ <layer title="Shape" type="shape" column="string" locked="false" visible="true" resizable="true">
52
+ <x>15</x>
53
+ <y>30</y>
54
+ <z>0</z>
55
+ <width>120</width>
56
+ <height>100</height>
57
+ <alpha>1.0</alpha>
58
+ <rotation>45</rotation>
59
+ <content></content>
60
+
61
+ <!-- Layer Type - Shape-->
62
+ <corner-radius>10</corner-radius>
63
+ <elliptical>false</elliptical>
64
+ <fill>true</fill>
65
+ <stroke>true</stroke>
66
+ <fill-color>3B67A4</fill-color>
67
+ <stroke-color>7B34A4</stroke-color>
68
+ <stroke-weight>5</stroke-weight>
69
+ </layer>
70
+
71
+ <!-- Layer -->
72
+ <layer title="Text" type="text" column="string" locked="false" visible="true" resizable="true">
73
+ <x>20.0</x>
74
+ <y>20.0</y>
75
+ <z>10</z>
76
+ <width>300.0</width>
77
+ <height>120.0</height>
78
+ <alpha>1.0</alpha>
79
+ <rotation>0.0</rotation>
80
+ <content>Donec ullamcorper</content>
81
+
82
+ <!-- Layer Type - Text-->
83
+ <font-family>Helvetica</font-family>
84
+ <font-size>10.0</font-size>
85
+ <font-color>4F6E7A</font-color>
86
+ <bold>false</bold>
87
+ <italic>false</italic>
88
+ <justification>right</justification> <!-- left|right|center|justify -->
89
+ </layer>
90
+
91
+ <!-- Layer -->
92
+ <layer title="Image" type="image" column="data" locked="false" visible="true" resizable="true">
93
+ <x>1.0</x>
94
+ <y>2.0</y>
95
+ <z>0</z>
96
+ <width>20.0</width>
97
+ <height>10.0</height>
98
+ <alpha>0.5</alpha>
99
+ <rotation>10.0</rotation>
100
+ <content>iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==</content>
101
+
102
+ <!-- Layer Type - Image-->
103
+ <fit>false</fit>
104
+ <stretch>true</stretch>
105
+ </layer>
106
+
107
+ <!-- Layer -->
108
+ <layer title="Barcode" type="barcode" column="string" locked="false" visible="true" resizable="true">
109
+ <x>100.0</x>
110
+ <y>100.0</y>
111
+ <z>0</z>
112
+ <width>200.0</width>
113
+ <height>40.0</height>
114
+ <alpha>1.0</alpha>
115
+ <rotation>0.0</rotation>
116
+ <content>123456</content>
117
+
118
+ <!-- Layer Type - Barcode-->
119
+ <symbology>Codabar</symbology>
120
+ <bar-width>20</bar-width>
121
+ <bar-height>500</bar-height>
122
+ <color>000000</color>
123
+ <caption-height>180</caption-height>
124
+ <caption-size>167</caption-size>
125
+ <captioned>true</captioned>
126
+ </layer>
127
+
128
+ <!-- Layer -->
129
+ <layer title="Date" type="date" column="date" locked="0" visible="1" resizable="1">
130
+ <x>1.0</x>
131
+ <y>2.0</y>
132
+ <z>0</z>
133
+ <width>100.0</width>
134
+ <height>10.0</height>
135
+ <alpha>1.0</alpha>
136
+ <rotation>0.0</rotation>
137
+ <content>2007-03-01T13:00:00Z</content>
138
+
139
+ <!-- Layer Type - Date-->
140
+ <font-family>Helvetica</font-family>
141
+ <font-size>10.0</font-size>
142
+ <font-color>333333</font-color>
143
+ <bold>false</bold>
144
+ <italic>false</italic>
145
+ <underline>false</underline>
146
+ <justification>left</justification> <!-- left|right|center|justified -->
147
+ <format>%Y-%m-%d %H:%M:%S</format>
148
+ </layer>
149
+
150
+ <!-- More layers... -->
151
+ </layers>
152
+
153
+ <!-- Data -->
154
+ <data>
155
+
156
+ <!-- Item -->
157
+ <item>
158
+ <key name="string">000222111</key>
159
+ <key name="data">iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==</key>
160
+ <key name="date">2007-06-01T13:00:00Z</key>
161
+ <key name="integer">123</key>
162
+ <key name="float">1.23</key>
163
+ <key name="boolean">false</key>
164
+ </item>
165
+
166
+ <!-- More items... -->
167
+ </data>
168
+ </label>
data/labels.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ # -*- encoding: utf-8 -*-
8
+ $:.push File.expand_path("../lib", __FILE__)
9
+ require "labels/version"
10
+
11
+ Gem::Specification.new do |s|
12
+ s.name = "labels"
13
+ s.version = Labels::VERSION
14
+ s.authors = ["Aaron Wright"]
15
+ s.email = ["acwrightdesign@gmail.com"]
16
+ s.homepage = ""
17
+ s.summary = %q{PDF label creation using Ruby}
18
+ s.description = %q{Labels can help you build and render labels as PDF}
19
+
20
+ s.rubyforge_project = "labels"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+
27
+ # specify any dependencies here; for example:
28
+ # s.add_development_dependency "rspec"
29
+ # s.add_runtime_dependency "rest-client"
30
+ s.add_development_dependency 'rake'
31
+ s.add_development_dependency 'rspec'
32
+ s.add_runtime_dependency 'prawn'
33
+ s.add_runtime_dependency 'nokogiri'
34
+ s.add_runtime_dependency 'uuid'
35
+ s.add_runtime_dependency 'barcodes'
36
+ end
@@ -0,0 +1,15 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ #
8
+ class String
9
+ # Convert string to bool
10
+ def to_bool
11
+ return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
12
+ return false if self == false || self.empty? || self =~ (/(false|f|no|n|0)$/i)
13
+ return false
14
+ end
15
+ end
@@ -0,0 +1,39 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ require 'labels/layer'
8
+
9
+ require 'barcodes'
10
+
11
+ module Labels
12
+
13
+ # This class represents barcode layers.
14
+ # For available symbologies and option descriptions
15
+ # see the Barcodes RubyGem library.
16
+ class Barcode < Layer
17
+
18
+ # Symbology
19
+ attr_accessor :symbology
20
+
21
+ # Bar width in mils
22
+ attr_accessor :bar_width
23
+
24
+ # Bar height in mils
25
+ attr_accessor :bar_height
26
+
27
+ # Color in hex
28
+ attr_accessor :color
29
+
30
+ # Caption height in mils
31
+ attr_accessor :caption_height
32
+
33
+ # Caption font size in mils
34
+ attr_accessor :caption_size
35
+
36
+ # Whether or not to print caption
37
+ attr_accessor :captioned
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ require 'time'
8
+
9
+ require 'labels/text'
10
+
11
+ module Labels
12
+
13
+ # This class represents date layers.
14
+ class Date < Text
15
+
16
+ # The date formatting
17
+ attr_accessor :format
18
+ end
19
+ end
@@ -0,0 +1,64 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ require 'fileutils'
8
+
9
+ require 'labels/paper'
10
+ require 'labels/template'
11
+ require 'labels/xml/builder'
12
+ require 'labels/pdf/builder'
13
+
14
+ module Labels
15
+
16
+ # This class represents a Labels document.
17
+ class Document
18
+
19
+ # The document Labels::Paper instance
20
+ attr_accessor :paper
21
+
22
+ # The document Labels::Template instance
23
+ attr_accessor :template
24
+
25
+ # An array of document Labels::Layer instances
26
+ attr_accessor :layers
27
+
28
+ # An array of document data
29
+ attr_accessor :data
30
+
31
+ # Creates a new Labels::Document instance with optional content
32
+ def initialize(paper=nil, template=nil, layers=[], data=[])
33
+ unless paper.nil?
34
+ @paper = paper
35
+ else
36
+ @paper = Labels::Paper.new(Labels::Paper::LETTER)
37
+ end
38
+
39
+ unless template.nil?
40
+ @template = template
41
+ else
42
+ @template = Labels::Template.new(Labels::Template::DEFAULT)
43
+ end
44
+
45
+ @layers = layers
46
+ @data = data
47
+ end
48
+
49
+ # Writes document to filename as XML
50
+ def write(filename)
51
+ FileUtils.mkdir_p(File.dirname(filename))
52
+ File.open(filename, 'w') do |file|
53
+ file.write Labels::XML::Builder.new(self).xml
54
+ end
55
+ end
56
+ alias_method :to_file, :write
57
+
58
+ # Render document to filename or string
59
+ def render(filename=nil)
60
+ Labels::PDF::Builder.new(self).render(filename)
61
+ end
62
+ alias_method :to_pdf, :render
63
+ end
64
+ end
@@ -0,0 +1,29 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ module Labels
8
+
9
+ # This class is a base class for all document content types
10
+ class Element
11
+
12
+ # Creates a new Labels::Element instance with given arguments
13
+ def initialize(args=nil)
14
+ unless args.nil?
15
+ args.each do |k,v|
16
+ instance_variable_set("@#{k}", v) unless v.nil?
17
+ end
18
+ end
19
+ end
20
+
21
+ # Converts instance variables to hash
22
+ def to_h
23
+ hash = {}
24
+ self.instance_variables.each {|var| hash[var.to_s.delete("@").to_sym] = self.instance_variable_get(var) }
25
+ hash
26
+ end
27
+ alias_method :to_hash, :to_h
28
+ end
29
+ end
@@ -0,0 +1,92 @@
1
+ # A RubyGem for creating mailing, shipping, address, barcode and other labels and rendering as PDF's.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
7
+ require 'optparse'
8
+
9
+ require 'labels'
10
+ require 'labels/version'
11
+
12
+ module Labels
13
+
14
+ # This class is the main handler for the command line tool interface.
15
+ # It takes command line arguments and options and renders a barcode
16
+ # using those options.
17
+ class Exec
18
+
19
+ # Array of command line arguments
20
+ attr_reader :argv
21
+
22
+ # The parser instance
23
+ attr_reader :parser
24
+
25
+ # Hash of parsed options
26
+ attr_reader :options
27
+
28
+ # The input source
29
+ attr_reader :source
30
+
31
+ # The output target
32
+ attr_reader :target
33
+
34
+ # Creates a new instance with given command line arguments and options
35
+ def initialize(argv)
36
+ @argv = argv
37
+ @options = {}
38
+ self._init_parser
39
+ self._parse!
40
+ end
41
+
42
+ # Runs the command and renders label
43
+ def run
44
+ begin
45
+ document = Labels.open(self.source)
46
+
47
+ unless document.nil?
48
+ unless self.target.nil?
49
+ document.render(self.target)
50
+ else
51
+ puts document.render
52
+ end
53
+ end
54
+ rescue Exception => e
55
+ puts e.message
56
+ end
57
+ end
58
+
59
+ protected
60
+
61
+ # Initializes the option parser instance
62
+ def _init_parser
63
+ @parser ||= OptionParser.new do |opts|
64
+ opts.banner = "Usage: labels [OPTIONS] source target"
65
+ opts.separator ""
66
+ opts.on('-v', '--version') { puts self._version ; exit }
67
+ opts.on('-h', '--help') { puts opts ; exit }
68
+ opts.separator ""
69
+ end
70
+
71
+ end
72
+
73
+ # Parses the command line arguments
74
+ def _parse!
75
+
76
+ begin
77
+ self.parser.parse!(@argv)
78
+ rescue
79
+ puts self.parser.help
80
+ exit 1
81
+ end
82
+
83
+ @source = self.argv.shift
84
+ @target = self.argv.shift
85
+ end
86
+
87
+ # Returns the current version
88
+ def _version
89
+ "labels #{Labels::VERSION}"
90
+ end
91
+ end
92
+ end