zenml-slide 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '09ffc8ce977c18086776bae46f104cf501ecc199b07da17130a2a4449d89bfad'
4
+ data.tar.gz: 58a16ab6b6810aea461156e02ddfcb005bc3bdd208583568a58b2d7a8649021d
5
+ SHA512:
6
+ metadata.gz: 2bdf8c46b02a64a23fa2b01c946e7c5a1e9a94eea60098f642a33e57de3267eabe3933530371609f6199461bc62f78f57edb712cb8ec0cdbeda83d95292719f9
7
+ data.tar.gz: 6008043f2aad0e21ce747c4ac9b5914b8f968e5c5aed39202ca8a487249ec3e65b8707d363b0b0d33043e7a70f1a89c59c740763fdc9a1c305c994df86232e1c
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+
3
+
4
+ module Zenithal::Slide
5
+
6
+ VERSION = "1.0.0"
7
+ VERSION_ARRAY = VERSION.split(/\./).map(&:to_i)
8
+
9
+ end
10
+
11
+
12
+ require 'fileutils'
13
+ require 'open3'
14
+ require 'rexml/document'
15
+ require 'selenium-webdriver'
16
+
17
+ require_relative 'slide/converter'
@@ -0,0 +1,189 @@
1
+ # coding: utf-8
2
+
3
+
4
+ class Zenithal::Slide::WholeSlideConverter
5
+
6
+ def initialize(args)
7
+ @mode, @open = nil, false
8
+ @dirs = {:output => "out", :document => "document", :template => "template"}
9
+ @image_size = [1920, 1080]
10
+ options, rest_args = args.partition{|s| s =~ /^\-\w$/}
11
+ if options.include?("-i")
12
+ @mode = :image
13
+ else
14
+ if options.include?("-o")
15
+ @open = true
16
+ end
17
+ @mode = :normal
18
+ end
19
+ @rest_args = rest_args
20
+ @paths = create_paths
21
+ @parser = create_parser
22
+ @converter = create_converter
23
+ @driver = create_driver
24
+ end
25
+
26
+ def execute
27
+ case @mode
28
+ when :image
29
+ execute_image
30
+ when :normal
31
+ execute_normal
32
+ end
33
+ end
34
+
35
+ def execute_normal
36
+ @paths.each_with_index do |path, index|
37
+ convert_normal(path)
38
+ convert_open(path) if @open
39
+ end
40
+ end
41
+
42
+ def execute_image
43
+ @paths.each_with_index do |path, index|
44
+ convert_image(path)
45
+ end
46
+ @driver.quit
47
+ end
48
+
49
+ def convert_normal(path)
50
+ extension = File.extname(path).gsub(/^\./, "")
51
+ output_path = path.gsub(@dirs[:document], @dirs[:output]).then(&method(:modify_extension))
52
+ count_path = path.gsub(@dirs[:document], @dirs[:output]).gsub("slide", "image").gsub(".zml", ".txt")
53
+ output_dir = File.dirname(output_path)
54
+ count_dir = File.dirname(count_path)
55
+ FileUtils.mkdir_p(output_dir)
56
+ FileUtils.mkdir_p(count_dir)
57
+ case extension
58
+ when "zml"
59
+ @parser.update(File.read(path))
60
+ document = @parser.run
61
+ @converter.update(document)
62
+ output = @converter.convert
63
+ count = @converter.variables[:slide_count].to_i.to_s
64
+ File.write(output_path, output)
65
+ File.write(count_path, count)
66
+ when "scss"
67
+ option = {}
68
+ option[:style] = :expanded
69
+ option[:filename] = path
70
+ output = SassC::Engine.new(File.read(path), option).render
71
+ File.write(output_path, output)
72
+ when "html", "js", "svg"
73
+ output = File.read(path)
74
+ File.write(output_path, output)
75
+ end
76
+ end
77
+
78
+ def convert_image(path)
79
+ page_path = path.gsub(@dirs[:document], @dirs[:output]).then(&method(:modify_extension))
80
+ output_path = path.gsub(@dirs[:document], @dirs[:output]).gsub("slide", "image").gsub(".zml", "")
81
+ count_path = path.gsub(@dirs[:document], @dirs[:output]).gsub("slide", "image").gsub(".zml", ".txt")
82
+ output_dir = File.dirname(output_path)
83
+ count = File.read(count_path).to_i
84
+ FileUtils.mkdir_p(output_dir)
85
+ @driver.navigate.to("file:///#{page_path}")
86
+ @driver.manage.window.resize_to(*@image_size)
87
+ @driver.execute_script("document.body.classList.add('simple');")
88
+ count.times do |index|
89
+ @driver.execute_script("document.querySelectorAll('*[class$=\\'slide\\']')[#{index}].scrollIntoView();")
90
+ @driver.save_screenshot("#{output_path}-#{index}.png")
91
+ end
92
+ end
93
+
94
+ def convert_open(path)
95
+ output_path = path.gsub(@dirs[:document], @dirs[:output]).then(&method(:modify_extension))
96
+ Kernel.spawn("start #{output_path}")
97
+ end
98
+
99
+ def create_paths
100
+ paths = []
101
+ if @rest_args.empty?
102
+ dirs = []
103
+ dirs << File.join(@dirs[:document], "slide")
104
+ if @mode == :normal
105
+ dirs << File.join(@dirs[:document], "asset")
106
+ paths << File.join(@dirs[:document], "style", "style.scss")
107
+ paths << File.join(@dirs[:document], "script", "script.js")
108
+ end
109
+ dirs.each do |dir|
110
+ Dir.each_child(dir) do |entry|
111
+ if entry =~ /\.\w+$/
112
+ paths << File.join(dir, entry)
113
+ end
114
+ end
115
+ end
116
+ else
117
+ path = @rest_args.map{|s| s.gsub("\\", "/").gsub("c:/", "C:/")}[0].encode("utf-8")
118
+ paths << path
119
+ end
120
+ return paths
121
+ end
122
+
123
+ def create_parser(main = true)
124
+ parser = ZenithalParser.new("")
125
+ parser.brace_name = "x"
126
+ parser.bracket_name = "xn"
127
+ parser.slash_name = "i"
128
+ if main
129
+ parser.register_macro("import") do |attributes, _|
130
+ import_path = attributes["src"]
131
+ import_parser = create_parser(false)
132
+ import_parser.update(File.read(File.join(@dirs[:document], import_path)))
133
+ document = import_parser.run
134
+ import_nodes = (attributes["expand"]) ? document.root.children : [document.root]
135
+ next import_nodes
136
+ end
137
+ end
138
+ return parser
139
+ end
140
+
141
+ def create_converter
142
+ converter = ZenithalConverter.new(nil, :text)
143
+ Dir.each_child(@dirs[:template]) do |entry|
144
+ if entry.end_with?(".rb")
145
+ binding = TOPLEVEL_BINDING
146
+ binding.local_variable_set(:converter, converter)
147
+ Kernel.eval(File.read(File.join(@dirs[:template], entry)), binding, entry)
148
+ end
149
+ end
150
+ return converter
151
+ end
152
+
153
+ def create_driver
154
+ if @mode == :image
155
+ options = Selenium::WebDriver::Chrome::Options.new
156
+ options.add_argument("--headless")
157
+ options.add_option("excludeSwitches", ["enable-logging"])
158
+ driver = Selenium::WebDriver.for(:chrome, options: options)
159
+ else
160
+ driver = nil
161
+ end
162
+ return driver
163
+ end
164
+
165
+ def modify_extension(path)
166
+ result = path.clone
167
+ result.gsub!(/\.zml$/, ".html")
168
+ result.gsub!(/\.scss$/, ".css")
169
+ result.gsub!(/\.ts$/, ".js")
170
+ return result
171
+ end
172
+
173
+ def output_dir=(dir)
174
+ @dirs[:output] = dir
175
+ end
176
+
177
+ def document_dir=(dir)
178
+ @dirs[:document] = dir
179
+ end
180
+
181
+ def template_dir=(dir)
182
+ @dirs[:template] = dir
183
+ end
184
+
185
+ def image_size=(image_size)
186
+ @image_size = image_size
187
+ end
188
+
189
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zenml-slide
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ziphil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sassc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: selenium-webdriver
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: 'To be written.
42
+
43
+ '
44
+ email:
45
+ - ziphil.shaleiras@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - source/zenml/slide.rb
51
+ - source/zenml/slide/converter.rb
52
+ homepage: https://github.com/Ziphil/ZenithalSlide
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - source
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '2.5'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.0.3
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Summary
75
+ test_files: []