middleman-gnuplot 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 071fc96ea7edf03919474b030367b44a9af58abe
4
- data.tar.gz: 48713824fe62f43d7e8936b674ca4be7a5cb5bc7
3
+ metadata.gz: 15fda679ea252f213b2952d616710ff9e8967f93
4
+ data.tar.gz: bb5c0a442a12bda847cd88406b7ff5b65855a487
5
5
  SHA512:
6
- metadata.gz: b67ca783f3617ae2ed436abade4f0d5c4bfac922300a1a6f2ca93f8d8ed92d5f02ab83b4a26bcdd009ccb47d479d248b7f79569378d156104d94b7c2b6dcd2c8
7
- data.tar.gz: 15c1526067544731bcc1726f853efc1d1867892d8151d2167a958ce3e1249c91562ef33473a85471611f7d1091866d9460f255ff43323e7c1698dfeb0413b0b3
6
+ metadata.gz: fd2373b58e1837f009f77c8399eeaec32d85f7d7419f4e37ace228cdeab6c5531c0e082055a74c7993abcbfb0c0fcc6fe8500bf44291e2427bddbdd8f264e69c
7
+ data.tar.gz: f725dd22211886fa0accd53bac00c9d5f8076167697e735e7ce4dd96f02a09a7b3cff10251f3415ba967f7dd9011bc622db2d879e9c305233d8abad20e0345d4
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ doc
2
+ Gemfile.lock
3
+ .yardoc
4
+ .rspec
5
+ *.gem
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ lib/**/*.rb
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in middleman-gnuplot.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rake'
8
+ gem 'rdoc'
9
+ gem 'yard'
10
+ end
11
+
12
+ group :test do
13
+ gem 'rspec'
14
+ gem 'rspec-html-matchers'
15
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Hermann Detz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ <Copyright (C) 2018 Hermann Detz>
2
+
3
+ <This software may be modified and distributed under the terms>
4
+ <of the MIT license. See the LICENSE file for details.>
5
+
6
+ # middleman-gnuplot
7
+
8
+ This extension to the [Middleman](http://middlemanapp.com/) framework
9
+ provides helper functions to generate plots using gnuplot.
10
+
11
+ ## Setup
12
+
13
+ Add the gem to your Gemfile:
14
+
15
+ gem 'middleman-gnuplot', :path => "path-to/middleman-gnuplot"
16
+
17
+ Update your gem list:
18
+
19
+ $ bundle install
20
+
21
+
22
+ Modify the `config.rb` of your project as follows:
23
+
24
+ activate :gnulot do |opts|
25
+ opts.gp_tmpdir = 'tmp' # path, where plots are stored before copied to build dir
26
+ opts.gp_outdir = 'img' # directory holding plot files with build dir
27
+ opts.gp_format = 'png' # determines output format (currently supports png and pdf)
28
+ end
29
+ ## Usage
30
+
31
+ All functions return the output filename of the plot, which can be used
32
+ as argument for the `image_tag` helper function.
33
+
34
+ The middleman-gnuplot extension provides the following helper methods:
35
+
36
+ * `plot_functions ([{:expression, :style, :color}], filename, plot title)`:
37
+ Plot simple expressions, which can be defined as single hash or array thereof.
38
+ The function returns a filename, which can be used as argument for the
39
+ `image_tag` helper. `expression` can hold terms that can be interpreted by
40
+ gnuplot, e.g. sin(x) or similar. `style`: lines, points, etc. `color` takes
41
+ an RGB color definition in hex format. `filename`defines the output filename.
42
+ If nil, a random filename is generated and returned by the function.
43
+
44
+ ```ruby
45
+ image_tag plot_functions([{:expression => "sin(x)",
46
+ :style => "lines",
47
+ :color => "#4575B4"},
48
+ {:expression => "tan(x)",
49
+ :color => "#D73027"}],
50
+ "filename",
51
+ "Plot Title")
52
+ ```
53
+
54
+ * `plot_script(script, filename, title)`: This helper can be used to
55
+ generate a plot with a gnuplot script. `script` should contain the
56
+ path to the gnuplot script. `filename` defines the output filename. If nil,
57
+ a random filename is generated and returned by the function. The `title`
58
+ parameter can be used to define a plot title.
59
+ ```ruby
60
+ image_tag plot_script("path_to_gnuplot_script", "filename", "Plot Title")
61
+ ```
62
+
63
+ ## Note
64
+
65
+ The build summary of middleman lists plot files that were generated in
66
+ previous runs as being _removed_ from the build directory. Nevertheless,
67
+ the plots generated in the current run are moved to the build directory
68
+ _after_ the middleman sitemap is evaluated and therefore do not show up
69
+ in this list.
70
+
71
+ Feel free to contact me, if you have a better solution for this issue!
72
+
73
+ ## Feedback & Contributions
74
+
75
+ 1. Fork it ( http://github.com/hermanndetz/middleman-gnuplot/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
@@ -0,0 +1,192 @@
1
+ require "middleman-gnuplot/version"
2
+ require "middleman-gnuplot/gp_helpers"
3
+ require "middleman-core"
4
+ require "numo/gnuplot"
5
+ require "fileutils"
6
+
7
+ module Middleman
8
+ class GnuplotExtension < Middleman::Extension
9
+
10
+ option :gp_outdir, nil, 'Output path for generated plots'
11
+ option :gp_tmpdir, nil, 'Path where gnuplot files are stored before being merged into build'
12
+ option :gp_format, 'png', 'Output file format'
13
+ option :gp_rndfilelength, 8, 'Length of randomly generated filenames'
14
+
15
+ @@base_dir = ""
16
+ @@plot_names = []
17
+
18
+ # Initializes the middleman-gnuplot extension.
19
+ def initialize(app, options_hash={}, &block)
20
+ super
21
+ app.config[:gp_outdir] = options.gp_outdir
22
+ app.config[:gp_tmpdir] = options.gp_tmpdir
23
+ app.config[:gp_format] = options.gp_format
24
+
25
+ @@base_dir = "./#{app.config[:gp_tmpdir]}/#{app.config[:images_dir]}/"
26
+
27
+ FileUtils.mkdir_p "#{@@base_dir}/#{app.config[:gp_outdir]}/"
28
+
29
+ term = Middleman::Gnuplot::get_gnuplot_term(options.gp_format)
30
+
31
+ @@gp = Numo::Gnuplot.new
32
+ @@gp.debug_on
33
+ @@gp.set term:"#{term}"
34
+
35
+ app.after_build do |builder|
36
+ # Move generated plots to build dir
37
+ FileUtils.cp_r @@base_dir, app.config[:build_dir]
38
+ end
39
+ end
40
+
41
+ # Generates a plot via gnuplot using a given expression.
42
+ # The function must be provided as hash using the following fields:
43
+ # @param [Array<Hash>] functions function definition hash
44
+ # @option functions [String] expression expression hat can be processed by gnuplot (e.g. sin(x))
45
+ # @option functions [String] style _lines_ or _points_
46
+ # @option functions [String] color RGB colro definition in hex format
47
+ # @option functions [String] title name of trace
48
+ # @param [String] filename base filename for output file
49
+ # @param [String] title plot title
50
+ def plot_functions (functions=[], filename=nil, title=nil)
51
+ # stub method to enable documentation in yard
52
+ end
53
+
54
+ # Generates a plot directly from an existing gnuplot script
55
+ # Params:
56
+ # @param [String] script path to the gnuplot script
57
+ # @param [String] filename for output file (can be overridden in script)
58
+ # @param [String] title plot title (can be overridden in script)
59
+ def plot_script (script, filename=nil, title=nil)
60
+ # stub method to enable documentation in yard
61
+ end
62
+
63
+ # Generates a random filename, if the given paramter is nil or empty
64
+ # Returns filename (given or random)
65
+ # Params.
66
+ # +filename+:: input filename
67
+ def random_filename_if_nil (filename=nil)
68
+ # stub method to enable documentation in yard
69
+ end
70
+
71
+ helpers do
72
+ def plot_functions (functions=[], filename=nil, title=nil)
73
+ filename = random_filename_if_nil(filename)
74
+
75
+ if title.nil?
76
+ title = ""
77
+ end
78
+
79
+ outfile = "#{app.config[:gp_outdir]}/#{filename}.#{app.config[:gp_format]}"
80
+
81
+ @@gp.set output:"./#{@@base_dir}/#{outfile}"
82
+ @@gp.set title:"#{title}"
83
+
84
+ gp_command = []
85
+
86
+ functions.each_with_index do |function,i|
87
+ gp_command << function[:expression]
88
+
89
+ if function[:style].nil? == false
90
+ gp_command << {w:function[:style]}
91
+ else
92
+ gp_command << {w:'lines'}
93
+ end
94
+
95
+ if function[:color].nil? == false
96
+ gp_command << {lc:"rgb '#{function[:color]}'"}
97
+ end
98
+
99
+ if function[:title].nil? == false
100
+ gp_command << {t:"#{function[:title]}"}
101
+ else
102
+ gp_command << {t:''}
103
+ end
104
+
105
+ gp_command << ", "
106
+ end
107
+
108
+ gp_command[-1] = ''
109
+
110
+ @@gp.plot gp_command
111
+
112
+ register_filename(filename)
113
+
114
+ return outfile
115
+ end
116
+
117
+ def plot_script (script, filename=nil, title=nil)
118
+ filename = random_filename_if_nil(filename)
119
+
120
+ outfile = "#{app.config[:gp_outdir]}/#{filename}.#{app.config[:gp_format]}"
121
+
122
+ Numo.gnuplot do
123
+ set output: "./#{@@base_dir}/#{outfile}"
124
+
125
+ unless title.nil?
126
+ set title: title
127
+ end
128
+
129
+ load script
130
+ end
131
+
132
+ register_filename(filename)
133
+
134
+ return outfile
135
+ end
136
+
137
+
138
+ private
139
+
140
+ # Generates a random filename, if the given paramter is nil or empty
141
+ # Returns filename (given or random)
142
+ # Params.
143
+ # +filename+:: input filename
144
+ def random_filename_if_nil (filename=nil)
145
+ if filename.nil? or filename == ""
146
+ loop do
147
+ filename = ([*('A'..'Z'),*('0'..'9')]).sample(app.config[:gp_rndfilelength]).join
148
+
149
+ break if @@plot_names.include?(filename) == false
150
+ end
151
+ end
152
+
153
+ return filename
154
+ end
155
+
156
+ # Adds a generated or given filename to the log array to ensure
157
+ # unique plot names, if no filename is given.
158
+ # Note: In case a given filename already exists - e.g. because a
159
+ # user adds the same filename twice - a warning is issued.
160
+ # Params.
161
+ # +filename+:: filename to be added to array
162
+ def register_filename (filename)
163
+ if @@plot_names.include?(filename)
164
+ message "Warning: Filename #{filename} for plot already in use!", :warning
165
+ end
166
+
167
+ @@plot_names.append(filename)
168
+ end
169
+
170
+ # Prints a colored text message to terminal.
171
+ # Params.
172
+ # +text+:: the message body to be printed
173
+ # +type+:: :warning (yellow) or :error (red)
174
+ def message (text, type=:warning)
175
+ colString = ''
176
+ colReset = "\033[0m"
177
+ offset = "\033[14C"
178
+
179
+ case type
180
+ when :warning
181
+ colString = "\033[1;33m"
182
+ when :error
183
+ colString = "\033[1;31m"
184
+ end
185
+
186
+ message = "#{colString}#{offset}middleman-gnuplot: #{text}#{colReset}"
187
+
188
+ puts message
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ module Middleman
7
+ module Gnuplot
8
+
9
+ module_function
10
+ def get_gnuplot_term (extension)
11
+ term = ''
12
+
13
+ case extension
14
+ when 'png'
15
+ term = 'pngcairo'
16
+ when 'pdf'
17
+ term = 'pdfcairo'
18
+ else
19
+ term = 'pngcairo'
20
+ end
21
+
22
+ return term
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ module Middleman
7
+ module Gnuplot
8
+ VERSION = "0.1.1"
9
+ end
10
+ end
11
+
@@ -0,0 +1,10 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ ::Middleman::Extensions.register :gnuplot do
7
+ require_relative 'middleman-gnuplot/extension'
8
+ Middleman::GnuplotExtension
9
+ end
10
+
@@ -0,0 +1,6 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ require 'middleman-gnuplot'
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'middleman-gnuplot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "middleman-gnuplot"
8
+ spec.version = Middleman::Gnuplot::VERSION
9
+ spec.authors = ["Hermann Detz"]
10
+ spec.email = ["hermann.detz@gmail.com"]
11
+ spec.description = <<-EOF
12
+ This middleman extension provides helper functions,
13
+ which allow to create plots using gnuplot and
14
+ integrate them easily with middleman pages.
15
+ EOF
16
+ spec.summary = %q{Middleman extension for gnuplot}
17
+ spec.homepage = "https://github.com/hermanndetz/middleman-gnuplot"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files`.split($/)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_runtime_dependency "numo-gnuplot", ["~> 0.2.4"]
26
+ spec.add_runtime_dependency "middleman-core", ["~> 4.0"]
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-gnuplot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hermann Detz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-03 00:00:00.000000000 Z
11
+ date: 2018-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-gnuplot
@@ -47,7 +47,18 @@ email:
47
47
  executables: []
48
48
  extensions: []
49
49
  extra_rdoc_files: []
50
- files: []
50
+ files:
51
+ - ".gitignore"
52
+ - ".yardopts"
53
+ - Gemfile
54
+ - LICENSE
55
+ - README.md
56
+ - lib/middleman-gnuplot.rb
57
+ - lib/middleman-gnuplot/extension.rb
58
+ - lib/middleman-gnuplot/gp_helpers.rb
59
+ - lib/middleman-gnuplot/version.rb
60
+ - lib/middleman_extension.rb
61
+ - middleman-gnuplot.gemspec
51
62
  homepage: https://github.com/hermanndetz/middleman-gnuplot
52
63
  licenses:
53
64
  - MIT