md2key 0.8.4 → 0.9.0

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
- SHA1:
3
- metadata.gz: cbd92251f22aada15ef3f7c4f0a857162d49381a
4
- data.tar.gz: 8233265abc5fe0a51252b1c0373334d96b99232e
2
+ SHA256:
3
+ metadata.gz: a24069768cd5c06091add93733afb5dd4ce5442fcc94795e5c47f4d263cfe6ec
4
+ data.tar.gz: 4b9c7b2f8aeaba697e42df6fbab3416c82631410d71e8e5a4df949cdb8e4f8af
5
5
  SHA512:
6
- metadata.gz: 6df6f091d045ed0b9b0c71a7496c96f8f8056afdc317cbd87cb91e6811f998aaf0768466b1ccf2fa58eb652c7a46241962136f0892bb824fef3b66c9b133e863
7
- data.tar.gz: 006b538a2457e1d4ade2b84e6c129c74129dae4ba2beaa943e0d7ead775f683c78e1d447f0a9e979f66a0c2af9547a7bc480de5d8ea307badad223c105cd5918
6
+ metadata.gz: 1ae1c513793ada5d034f2118029f1faf51a2e7b00805b1476a6ed37d2bb62a8335cb3bcdefc33b208cee5af49ec914c9fd170f578076aa62f5b2cdefc3545c29
7
+ data.tar.gz: 1c847482c40c60cf0e57e086240c1dbe5e4c13281be5917aab9b00d42b5ca9b25d6d51ee1596dada1dea5874c992808a45c88c4f1c088bea1e010908eb81e3e2
@@ -1,3 +1,8 @@
1
+ # v0.9.0
2
+
3
+ - Use master slides named "cover", "h1", "h2", ...
4
+ - See https://github.com/k0kubun/md2key/pull/46 for details
5
+
1
6
  # v0.8.4
2
7
 
3
8
  - Add `md2key listen` subcommand to update Keynote by watching file
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in md2key.gemspec
4
4
  gemspec
5
+
6
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.2.5")
7
+ gem 'listen', '~> 3.0'
8
+ end
@@ -1,6 +1,7 @@
1
1
  require 'md2key/config_builder'
2
- require 'md2key/converter'
2
+ require 'md2key/config_loader'
3
3
  require 'md2key/parser'
4
+ require 'md2key/renderer'
4
5
  require 'thor'
5
6
  require 'yaml'
6
7
 
@@ -11,8 +12,9 @@ module Md2key
11
12
  abort "md2key: `#{path}` does not exist" unless File.exist?(path)
12
13
 
13
14
  markdown = File.read(path)
15
+ config = ConfigLoader.load('~/.md2key', './.md2key')
14
16
  ast = Parser.new.parse(markdown)
15
- Converter.new.convert!(ast)
17
+ Renderer.new(config).render!(ast)
16
18
  end
17
19
 
18
20
  desc 'init', 'Put .md2key template to current directory'
@@ -0,0 +1,41 @@
1
+ require 'md2key/configuration'
2
+
3
+ module Md2key
4
+ class ConfigLoader
5
+ class << self
6
+ # @param [Array<String>] paths - paths of YAML configs. Latter config overwrites former ones.
7
+ def load(*paths)
8
+ hash = {}
9
+ paths.each do |path|
10
+ hash.merge!(load_if_available(File.expand_path(path)))
11
+ end
12
+ Configuration.new(symbolize_keys(hash))
13
+ end
14
+
15
+ private
16
+
17
+ def load_if_available(path)
18
+ if File.exist?(path)
19
+ YAML.load(File.read(path))
20
+ else
21
+ {}
22
+ end
23
+ end
24
+
25
+ def symbolize_keys(object)
26
+ case object
27
+ when Hash
28
+ Hash.new.tap do |result|
29
+ object.each do |key, value|
30
+ result[key.to_sym] = symbolize_keys(value)
31
+ end
32
+ end
33
+ when Array
34
+ object.map(&method(:symbolize_keys))
35
+ else
36
+ object
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -2,67 +2,50 @@ require 'yaml'
2
2
 
3
3
  module Md2key
4
4
  class Configuration
5
- class << self
6
- def load
7
- config = {}
8
- config.merge!(load_if_available(File.expand_path('~/.md2key')))
9
- config.merge!(load_if_available(File.expand_path('./.md2key')))
10
- new(symbolize_keys(config))
5
+ class Master
6
+ # @param [String] name
7
+ # @param [TrueClass,FalseClass,nil] cover
8
+ # @param [Integer,nil] template
9
+ def initialize(name:, cover: nil, template: nil)
10
+ @name = name
11
+ @cover = cover
12
+ @template = template
11
13
  end
12
14
 
13
- private
14
-
15
- def load_if_available(path)
16
- if File.exist?(path)
17
- YAML.load(File.read(path))
18
- else
19
- {}
20
- end
21
- end
22
-
23
- def symbolize_keys(object)
24
- case object
25
- when Hash
26
- Hash.new.tap do |result|
27
- object.each do |key, value|
28
- result[key.to_sym] = symbolize_keys(value)
29
- end
30
- end
31
- when Array
32
- object.map(&method(:symbolize_keys))
33
- else
34
- object
35
- end
36
- end
15
+ attr_reader :name, :cover, :template
37
16
  end
38
17
 
18
+ # @param [Array<Hash{ Symbol => String,Integer,TrueClass,FalseClass }>] masters
39
19
  def initialize(masters: [])
40
- @masters = masters
20
+ @masters = masters.map { |m| Master.new(m) }
41
21
  validate!
42
22
  end
43
23
 
24
+ # @return [String,nil]
44
25
  def cover_master
45
26
  master = @masters.find do |master|
46
- master[:cover]
27
+ master.cover
47
28
  end
48
- master && master[:name]
29
+ master && master.name
49
30
  end
50
31
 
32
+ # @param [Integer] level
33
+ # @return [String,nil]
51
34
  def slide_master(level)
52
35
  master = @masters.find do |master|
53
- master[:template] == level
36
+ master.template == level
54
37
  end
55
- master && master[:name]
38
+ master && master.name
56
39
  end
57
40
 
58
41
  private
59
42
 
60
43
  def validate!
61
- if @masters.select { |m| m[:cover] }.length > 1
44
+ if @masters.select(&:cover).length > 1
62
45
  abort "Config error!\n`cover: true` cannot be specified multiple times"
63
46
  end
64
47
  (1..5).each do |level|
65
- if @masters.select { |m| m[:template] == level }.length > 1
48
+ if @masters.select { |m| m.template == level }.length > 1
66
49
  abort "`Config error!\ntemplate: #{level}` cannot be specified multiple times"
67
50
  end
68
51
  end
@@ -91,10 +91,13 @@ module Md2key
91
91
  execute_applescript('insert_note', slides_count, note)
92
92
  end
93
93
 
94
+ # @param [Integer] slide_index
95
+ # @return [String]
94
96
  def fetch_master_slide_name(slide_index)
95
97
  execute_applescript('fetch_master_slide_name', slide_index).rstrip
96
98
  end
97
99
 
100
+ # @return [Array<String>]
98
101
  def fetch_master_slide_names
99
102
  execute_applescript('fetch_master_slide_names').strip.split("\n")
100
103
  end
@@ -0,0 +1,71 @@
1
+ require 'md2key/configuration'
2
+ require 'md2key/keynote'
3
+
4
+ module Md2key
5
+ class Renderer
6
+ # Magic number index for master slide named "cover"
7
+ COVER_LEVEL = 0
8
+
9
+ # @param [Md2key::Configuration] config
10
+ def initialize(config)
11
+ @config = config
12
+ end
13
+
14
+ # @param [Md2key::Nodes::Presentation] ast
15
+ def render!(ast)
16
+ prepare_document_base
17
+ generate_contents(ast)
18
+ ensure
19
+ Keynote.delete_template_slide
20
+ end
21
+
22
+ private
23
+
24
+ def prepare_document_base
25
+ Keynote.ensure_template_slide_availability
26
+ Keynote.delete_extra_slides
27
+ end
28
+
29
+ # @param [Md2key::Nodes::Presentation] ast
30
+ def generate_contents(ast)
31
+ first_master = Keynote.fetch_master_slide_name(1)
32
+ second_master = Keynote.fetch_master_slide_name(2)
33
+ master_by_level = fetch_master_by_level
34
+
35
+ cover_master = @config.cover_master || master_by_level.fetch(COVER_LEVEL, first_master)
36
+ Keynote.update_cover(ast.cover, cover_master)
37
+
38
+ ast.slides.each do |slide|
39
+ slide_master = @config.slide_master(slide.level) || master_by_level.fetch(slide.level, second_master)
40
+
41
+ if slide.table
42
+ Keynote.create_slide_with_table(slide, slide.table.rows, slide.table.columns, slide_master)
43
+ Keynote.insert_table(slide.table.data)
44
+ else
45
+ Keynote.create_slide(slide, slide_master)
46
+ Keynote.insert_image(slide.image) if slide.image
47
+ Keynote.insert_code(slide.code) if slide.code
48
+ end
49
+
50
+ Keynote.insert_note(slide.note) if slide.note
51
+ end
52
+ end
53
+
54
+ # Find master names like "h1", "h2", ... and return { 1 => "h1", 2 => "h2" } only for available ones.
55
+ # @return [Hash{ Integer => String }]
56
+ def fetch_master_by_level
57
+ masters = Keynote.fetch_master_slide_names
58
+
59
+ {}.tap do |result|
60
+ masters.each do |master|
61
+ if master.match(/\Ah(?<level>[1-5])\z/)
62
+ level = Integer(Regexp.last_match[:level])
63
+ result[level] = master
64
+ elsif master == 'cover'
65
+ result[COVER_LEVEL] = 'cover'
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,3 +1,3 @@
1
1
  module Md2key
2
- VERSION = '0.8.4'
2
+ VERSION = '0.9.0'
3
3
  end
@@ -25,7 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency 'redcarpet', '~> 3.3'
26
26
  spec.add_dependency 'oga', '~> 1.2'
27
27
  spec.add_development_dependency 'bundler', '~> 1.12'
28
- spec.add_development_dependency 'listen', '~> 3.0'
29
28
  spec.add_development_dependency 'rake', '~> 10.0'
30
29
  spec.add_development_dependency 'pry'
31
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md2key
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-21 00:00:00.000000000 Z
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.12'
69
- - !ruby/object:Gem::Dependency
70
- name: listen
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '3.0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '3.0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: rake
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -132,8 +118,8 @@ files:
132
118
  - lib/md2key.rb
133
119
  - lib/md2key/cli.rb
134
120
  - lib/md2key/config_builder.rb
121
+ - lib/md2key/config_loader.rb
135
122
  - lib/md2key/configuration.rb
136
- - lib/md2key/converter.rb
137
123
  - lib/md2key/diagram.rb
138
124
  - lib/md2key/highlight.rb
139
125
  - lib/md2key/keynote.rb
@@ -144,6 +130,7 @@ files:
144
130
  - lib/md2key/nodes/slide.rb
145
131
  - lib/md2key/nodes/table.rb
146
132
  - lib/md2key/parser.rb
133
+ - lib/md2key/renderer.rb
147
134
  - lib/md2key/version.rb
148
135
  - md2key.gemspec
149
136
  - scripts/activate_slide.scpt.erb
@@ -183,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
170
  version: '0'
184
171
  requirements: []
185
172
  rubyforge_project:
186
- rubygems_version: 2.5.2
173
+ rubygems_version: 2.6.14
187
174
  signing_key:
188
175
  specification_version: 4
189
176
  summary: Convert markdown to keynote
@@ -1,46 +0,0 @@
1
- require 'md2key/configuration'
2
- require 'md2key/keynote'
3
-
4
- module Md2key
5
- class Converter
6
- attr_reader :config
7
-
8
- def initialize
9
- @config = Configuration.load
10
- end
11
-
12
- # @param [Md2key::Nodes::Presentation] ast
13
- def convert!(ast)
14
- prepare_document_base
15
- generate_contents(ast)
16
- ensure
17
- Keynote.delete_template_slide
18
- end
19
-
20
- private
21
-
22
- def prepare_document_base
23
- Keynote.ensure_template_slide_availability
24
- Keynote.delete_extra_slides
25
- end
26
-
27
- def generate_contents(ast)
28
- cover_master = Keynote.fetch_master_slide_name(1)
29
- main_master = Keynote.fetch_master_slide_name(2)
30
-
31
- Keynote.update_cover(ast.cover, config.cover_master || cover_master)
32
- ast.slides.each do |slide|
33
- master = config.slide_master(slide.level) || main_master
34
- if slide.table
35
- Keynote.create_slide_with_table(slide, slide.table.rows, slide.table.columns, master)
36
- Keynote.insert_table(slide.table.data)
37
- else
38
- Keynote.create_slide(slide, master)
39
- Keynote.insert_image(slide.image) if slide.image
40
- Keynote.insert_code(slide.code) if slide.code
41
- end
42
- Keynote.insert_note(slide.note) if slide.note
43
- end
44
- end
45
- end
46
- end