rodolfo 2.0.0.pre.pre → 2.1.0.pre.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2b0721e70e5346041cdfbf0c46ee84b67220cc5
4
- data.tar.gz: 7a1f3b769ef3aed6dd24bffa95f18d03d86becb6
3
+ metadata.gz: 0aceaa5a574544dc1100b4e1dead4a6244eef30d
4
+ data.tar.gz: 56e2220d79671b41a60a94c86b6d69cc83e268e8
5
5
  SHA512:
6
- metadata.gz: acf315467d8d0de4ba117b64f05d6cec9f03169dbee28dd3b8f84cd6907cbe6abc5850750eb69c6bc517af8268378e71634467e964de1099eae2daf6a2db195f
7
- data.tar.gz: 22f66c10d3c83ab6ce896bbedac939717c36a031e9522d46ddbcf3f1e7b047f6f5ea18f43534176c3477c7b18cc85805d12f2f21683e0ca8c8653c978f98e9c8
6
+ metadata.gz: 9499635ff37b753221d4c75d0f9c3ae3efad4da4b60a3ce3e836b744511002f71beb683cf7bf3af6e342066a46666a5f48c35031bf1a3e4f2354ceeb1f9d50e5
7
+ data.tar.gz: 5615bcf44bcdd191c9beab884724d291cc8fcbf39d7adec7414a45963a563d3c1615a70499b614d9ce8d2c675019eaf475db5b435b91cbb9cfe836d0da647d9c
data/lib/rodolfo/cli.rb CHANGED
@@ -1,9 +1,45 @@
1
1
  require 'thor'
2
2
  require_relative 'exceptions'
3
3
  require_relative 'meta'
4
+ require_relative 'reader'
4
5
  require_relative 'renderer'
5
6
 
6
7
  module Rodolfo
8
+ # Rodolfo recipe generator
9
+ class Generator < Thor::Group
10
+ argument :folder, type: :string, desc: 'Recipe folder name'
11
+ argument :description, type: :string, desc: 'Recipe description'
12
+
13
+ include Thor::Actions
14
+
15
+ def self.source_root
16
+ File.join __dir__, 'templates'
17
+ end
18
+
19
+ def one
20
+ puts Generator.source_root
21
+ empty_directory(folder)
22
+ end
23
+
24
+ def two
25
+ template('schema.tt', "#{folder}/schema.json")
26
+ end
27
+
28
+ def three
29
+ copy_file('template.tt', "#{folder}/template.rb")
30
+ end
31
+
32
+ def four
33
+ copy_file('data.tt', "#{folder}/data.json")
34
+ end
35
+
36
+ def five
37
+ puts
38
+ puts 'Recipe ready! Try it with:'
39
+ puts "cat #{folder}/data.json | rodolfo render #{folder} --save-to #{folder}.pdf"
40
+ end
41
+ end
42
+
7
43
  # Rodolfo CLI
8
44
  class CLI < Thor
9
45
  def self.exit_on_failure?
@@ -17,16 +53,26 @@ module Rodolfo
17
53
  puts VERSION
18
54
  end
19
55
 
20
- desc 'schema PACKAGE PATH', 'print the package json schema'
21
- def schema(package_path)
22
- puts Rodolfo::Renderer.new(package_path, {}).json_schema
56
+ desc 'schema RECIPE PATH', 'print the recipe json schema'
57
+ def schema(recipe_path)
58
+ puts Rodolfo::Renderer.new(recipe_path, {}).json_schema
23
59
  end
24
60
 
25
- desc 'render PACKAGE PATH', 'render a rodolfo package path'
26
- def render(package_path)
61
+ desc 'read PDF', 'prints pdf metadata and rodolfo info'
62
+ def read(pdf)
63
+ puts Rodolfo::Reader.new(pdf).to_json
64
+ end
65
+
66
+ desc 'render RECIPE PATH [--save-to file.pdf]', 'render a rodolfo recipe'
67
+ method_option 'save-to', type: :string, aliases: '-s'
68
+ def render(recipe_path)
27
69
  data = $stdin.tty? ? {} : JSON.parse($stdin.read, symbolize_names: true)
28
- package = Rodolfo::Renderer.new package_path, data
29
- STDOUT.write package.render
70
+ recipe = Rodolfo::Renderer.new recipe_path, data
71
+
72
+ content = recipe.render
73
+
74
+ file_name = options['save-to']
75
+ file_name ? File.write(file_name, content) : STDOUT.write(content)
30
76
  exit 0
31
77
  rescue RenderError, SchemaValidationError => error
32
78
  STDOUT.write error.errors
@@ -37,5 +83,8 @@ module Rodolfo
37
83
  puts "Rodolfo v#{Rodolfo::VERSION}"
38
84
  super(*args, &block)
39
85
  end
86
+
87
+ register(Generator, 'generator', 'generator FOLDER DESCRIPTION',
88
+ 'Scaffold new recipe')
40
89
  end
41
90
  end
@@ -1,5 +1,5 @@
1
1
  module Rodolfo
2
- # Package rendering failed
2
+ # Template rendering failed
3
3
  class RenderError < RuntimeError
4
4
  attr_reader :errors
5
5
 
@@ -4,7 +4,7 @@ require_relative 'exceptions'
4
4
 
5
5
  # Create PDFs from the CLI using Prawn
6
6
  module Rodolfo
7
- # Rodolfo Package JSON Schema
7
+ # Rodolfo Recipe JSON Schema
8
8
  class JSONSchema
9
9
  def initialize(path)
10
10
  @path = path
@@ -17,6 +17,11 @@ module Rodolfo
17
17
  strict: true }
18
18
  errors = JSON::Validator.fully_validate json, data, opts
19
19
  raise SchemaValidationError, errors unless errors.empty?
20
+ data
21
+ end
22
+
23
+ def to_h
24
+ JSON.parse json
20
25
  end
21
26
 
22
27
  def json
data/lib/rodolfo/meta.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # Create PDFs from the CLI using Prawn
2
2
  module Rodolfo
3
- VERSION = '2.0.0-pre'.freeze
3
+ VERSION = '2.1.0-pre'.freeze
4
4
  end
@@ -0,0 +1,25 @@
1
+ require 'json'
2
+ require 'pdf/reader'
3
+
4
+ # Create PDFs from the CLI using Prawn
5
+ module Rodolfo
6
+ # Reads a PDF info and metadata
7
+ class Reader
8
+ def initialize(pdf)
9
+ @reader = PDF::Reader.new pdf
10
+ end
11
+
12
+ def to_h
13
+ {
14
+ PdfVersion: @reader.pdf_version,
15
+ Info: @reader.info,
16
+ Meta: @reader.metadata,
17
+ Pages: @reader.page_count
18
+ }
19
+ end
20
+
21
+ def to_json
22
+ JSON.pretty_generate to_h
23
+ end
24
+ end
25
+ end
@@ -26,18 +26,33 @@ module Rodolfo
26
26
  @schema = JSONSchema.new File.join(@path, 'schema.json')
27
27
  end
28
28
 
29
- # Get the package json schema
29
+ # Get the recipe json schema
30
30
  def json_schema
31
31
  @schema.to_s
32
32
  end
33
33
 
34
+ # Validates the data against the json schema
35
+ def validated_data
36
+ @schema.validate(@data)
37
+ end
38
+
39
+ def pdf_meta
40
+ schema_meta = @schema.to_h
41
+
42
+ { CreationDate: Time.now.iso8601,
43
+ Renderer: "Rodolfo v#{VERSION}",
44
+ JsonSchema: {
45
+ description: schema_meta['description'],
46
+ id: schema_meta['id'],
47
+ schema: schema_meta['$schema']
48
+ } }
49
+ end
50
+
34
51
  # Render the template
35
- # May raises a ValidationError
36
52
  def render
37
- @schema.validate(@data)
38
53
  require File.join @path, 'template'
39
- Rodolfo::Template.new(@data).render
40
-
54
+ t = Rodolfo::Template.new(validated_data)
55
+ Prawn::Document.new(info: pdf_meta, **t.config, &t).render
41
56
  rescue NoMethodError
42
57
  msg = 'Missing or incorrect data, template can\'t be rendered'
43
58
  raise RenderError, [msg]
@@ -0,0 +1,3 @@
1
+ {
2
+ "msg": "Hello World"
3
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "id": "http://www.example.com/json-schema/v2/tpl/<%= folder %>",
3
+ "$schema": "http://json-schema.org/draft-04/schema#",
4
+ "description": "<%= description %>",
5
+ "properties": {
6
+ "msg": {
7
+ "type": "string"
8
+ }
9
+ },
10
+ "required": ["msg"]
11
+ }
@@ -0,0 +1,21 @@
1
+ module Rodolfo
2
+ # A Rodolfo Prawn pdf generator
3
+ class Template
4
+ def initialize(data)
5
+ @data = data
6
+ end
7
+
8
+ # Prawn config
9
+ def config
10
+ { page_layout: :portrait }
11
+ end
12
+
13
+ def to_proc
14
+ data = @data
15
+
16
+ proc do
17
+ text data[:msg]
18
+ end
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodolfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre.pre
4
+ version: 2.1.0.pre.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Initios
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-18 00:00:00.000000000 Z
11
+ date: 2016-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pdf-reader
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: prawn
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -112,26 +126,6 @@ dependencies:
112
126
  - - ">="
113
127
  - !ruby/object:Gem::Version
114
128
  version: 2.4.0
115
- - !ruby/object:Gem::Dependency
116
- name: pdf-inspector
117
- requirement: !ruby/object:Gem::Requirement
118
- requirements:
119
- - - "~>"
120
- - !ruby/object:Gem::Version
121
- version: 1.2.1
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: 1.2.1
125
- type: :development
126
- prerelease: false
127
- version_requirements: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: 1.2.1
132
- - - ">="
133
- - !ruby/object:Gem::Version
134
- version: 1.2.1
135
129
  - !ruby/object:Gem::Dependency
136
130
  name: rake
137
131
  requirement: !ruby/object:Gem::Requirement
@@ -244,7 +238,11 @@ files:
244
238
  - lib/rodolfo/exceptions.rb
245
239
  - lib/rodolfo/json_schema.rb
246
240
  - lib/rodolfo/meta.rb
241
+ - lib/rodolfo/reader.rb
247
242
  - lib/rodolfo/renderer.rb
243
+ - lib/rodolfo/templates/data.tt
244
+ - lib/rodolfo/templates/schema.tt
245
+ - lib/rodolfo/templates/template.tt
248
246
  homepage: https://github.com/initios/rodolfo
249
247
  licenses:
250
248
  - MIT