wani 0.1.17 → 0.1.19

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
  SHA256:
3
- metadata.gz: 5479c59b715d3dd55a496b727c6f6a8e7e40b4f0481408d965c56541b97d90ca
4
- data.tar.gz: 2855dc7e7dff2e0466d8f78a9d28d20e7b334232ba44e6926cc26f0705667c4b
3
+ metadata.gz: 666d21b324b5457905dcdc891c7265426e50828f28763ec7f219ffd9d64ada79
4
+ data.tar.gz: c4ddb01d9df3d6c8c2b48c2bcc47b0f8e84dad874d5c5b0c55797012e5c402a0
5
5
  SHA512:
6
- metadata.gz: 95b4eefc19b1db40db28549a5769f48e477f7be42fb4275eaa0b43071b89b1839c75535b1f62f0f3356bfaaf7cbc411e7e6f630d2f94fa58c0afd28451c28cbf
7
- data.tar.gz: 53463ed286abbe5cdaf7168cbcc36497ef4479aab26a9e2232f813b4241bd3ae9043ecac8442d4db1263a0b08306d5508fcf7b7cd21bfc768b4b46297cc3b956
6
+ metadata.gz: b7a33af1ca64b366e00e591bf9116a99db49787329bc505f222c19e7048a0d47b47eebfcf4c93cd98cbc80ce7db11ee70c2bc1ffe4e2573f3d2715583baae054
7
+ data.tar.gz: 6cc590a84d9618a4fa8f60853f9b5ab4cba52023878d69f42d4481e98d64fdd0985479e87e72551f306a7959437266e558bbc6bf9a9ec90ebf0cf84ad9050d07
data/README.md CHANGED
@@ -78,7 +78,7 @@ notes:
78
78
 
79
79
  ## フォント文字サイズを自動調整
80
80
 
81
- - タイトルは1行に収める
81
+ - タイトルは1行に収める。表紙では `<br>` で改行できる
82
82
  - 本文は全角20文字当の幅を超える場合は自然に折り返し
83
83
  - 明示的な改行は維持
84
84
  - 文字サイズは本文全体で共通
@@ -174,7 +174,7 @@ Front Matter の `time_limit` に分数を指定して変更できます(小
174
174
 
175
175
  - 1920×1080、16:9
176
176
  - 白背景
177
- - 画像は比率を維持して縦に配置し、画像だけの場合は本文領域を広く使います。
177
+ - 画像は比率を維持して縦に配置し、画像だけの場合は本文領域を広く使います。`![bg left](画像パス)` または `![bg right](画像パス)` で画像と文章を左右に配置できます([記法ガイド](docs/markdown.md#画像))。
178
178
 
179
179
  ## 警告
180
180
 
data/assets/build.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
- "version": "0.1.17",
3
- "source_sha256": "84cea7b9da9df559fa9c204becc4a7a400af64907ff232f95e1744bf500ae669",
2
+ "version": "0.1.19",
3
+ "source_sha256": "14dc67db4de65efbcb96d0a65c2ba826bcd553006ea403428eba2665fb133ab8",
4
4
  "loader_commit": "dc6b0ffd5800ed9bfc49268ed8021d456f10ddf8",
5
- "wasm_sha256": "d4200a9923f294b33f985bb84faa88c8f0e95093614507710506be36b2ac51f7"
5
+ "wasm_sha256": "2c9c66985f3c8ddb4b949abc046b843cd4b4a7c530f8dfb8730d34215c062cb5"
6
6
  }
data/assets/style.css CHANGED
@@ -54,7 +54,9 @@ button:focus-visible, a:focus-visible { outline: 2px solid #557e64; outline-offs
54
54
  .titleless .body > :last-child { margin-bottom: 0; }
55
55
  .titleless .body img { margin-inline: auto; }
56
56
  .cover .title { margin-top: 396px; text-align: center; }
57
+ .cover.multiline-title .title { margin-top: 316px; flex-basis: 320px; display: flex; align-items: center; justify-content: center; white-space: pre; }
57
58
  .cover .body { display: flex; flex-direction: column; align-items: flex-end; justify-content: flex-end; text-align: right; }
59
+ .cover.multiline-title .body { margin-top: -160px; }
58
60
  .cover .body > * { flex-shrink: 0; max-width: 100%; }
59
61
  .cover .body > :last-child { margin-bottom: 0; }
60
62
  .body ul, .body ol { margin: .2em 0 .4em; padding-left: 1.35em; }
@@ -65,6 +67,9 @@ button:focus-visible, a:focus-visible { outline: 2px solid #557e64; outline-offs
65
67
  .body table { border-collapse: collapse; width: 100%; }
66
68
  .body th, .body td { border: 1px solid #bac8bd; padding: .15em .3em; }
67
69
  .body img { display: block; object-fit: contain; max-width: 100%; }
70
+ .body.image-columns { display: flex; flex-direction: row; align-items: center; justify-content: space-between; gap: 6%; text-align: left; }
71
+ .image-columns > .image-column-image, .image-columns > .image-column-text { flex: 0 0 47%; min-width: 0; margin: 0; }
72
+ .image-columns > .image-column-image img { margin-inline: auto; }
68
73
  .body a { color: #32634b; }
69
74
  .body p:has(.youtube-video) { margin-bottom: 0; }
70
75
  .body p.youtube-only { width: 100%; text-align: center; }
data/assets/wani.wasm CHANGED
Binary file
@@ -9,6 +9,7 @@ module Wani
9
9
  TITLE_MIN = 24
10
10
  TITLE_MAX = 220
11
11
  NO_WRAP_EMS = 20
12
+ TITLE_GAP_MAX = 64
12
13
 
13
14
  def initialize(document, &warn)
14
15
  @document, @warn = document, warn
@@ -54,17 +55,20 @@ module Wani
54
55
  def fit_slide(section, number)
55
56
  section[:style][:display] = "flex"
56
57
  title = section.querySelector(".title")
58
+ title[:style][:transform] = ""
57
59
  unless title[:style][:display].to_s == "none"
58
60
  fit_font(title, TITLE_MIN, TITLE_MAX)
59
61
  @warn.call("スライド #{number}: タイトルが収まりません: #{title[:textContent]}") unless fits?(title)
60
62
  end
61
63
  body = section.querySelector(".body")
64
+ body[:style][:marginTop] = ""
62
65
  prepare_wrapping(body)
63
66
  fit_images(body)
64
67
  if section[:classList].contains("cover") == JS::True
65
68
  body[:style][:fontSize] = "#{COVER_BODY_SIZE}px"
66
69
  else
67
70
  fit_font(body, BODY_MIN, BODY_MAX)
71
+ add_title_gap(section, title, body)
68
72
  end
69
73
  body_fits = if section[:classList].contains("cover") == JS::True
70
74
  cover_body_fits?(body)
@@ -72,6 +76,15 @@ module Wani
72
76
  fits?(body)
73
77
  end
74
78
  @warn.call("スライド #{number}: 本文が収まりません: #{body[:textContent].to_s[0, 120]}") unless body_fits
79
+ if section[:classList].contains("multiline-title") == JS::True
80
+ title_rect = title.getBoundingClientRect
81
+ body_top = body[:children].to_a.map { |child| child.getBoundingClientRect[:top].to_f }.min
82
+ if body_top
83
+ scale = section.getBoundingClientRect[:width].to_f / WIDTH
84
+ shift = (title_rect[:bottom].to_f + 36 * scale - body_top) / scale
85
+ title[:style][:transform] = "translateY(-#{shift}px)" if shift.positive?
86
+ end
87
+ end
75
88
  if section[:classList].contains("cover") == JS::True && title[:style][:display].to_s != "none"
76
89
  title_rect = title.getBoundingClientRect
77
90
  overlap = body[:children].to_a.any? do |child|
@@ -99,6 +112,17 @@ module Wani
99
112
 
100
113
  private
101
114
 
115
+ def add_title_gap(section, title, body)
116
+ return if title[:style][:display].to_s == "none" || body[:children].to_a.empty? || !fits?(body)
117
+ scale = section.getBoundingClientRect[:width].to_f / WIDTH
118
+ return if scale <= 0
119
+ bottom = body[:children].to_a.map { |child| child.getBoundingClientRect[:bottom].to_f }.max
120
+ spare = (body.getBoundingClientRect[:bottom].to_f - bottom) / scale
121
+ gap = [[spare * 0.2, TITLE_GAP_MAX].min, 0].max
122
+ body[:style][:marginTop] = "#{gap}px"
123
+ body[:style][:marginTop] = "" unless fits?(body)
124
+ end
125
+
102
126
  def prepare_wrapping(body)
103
127
  # Rebuild the wrappers when a slide is fitted again (for example after
104
128
  # editing). Ranges preserve links, emphasis and explicit line breaks.
@@ -169,12 +193,14 @@ module Wani
169
193
  mixed = !content[:textContent].to_s.strip.empty?
170
194
  videos_only = images.all? { |image| image.closest(".youtube-video") != JS::Null }
171
195
  fraction = mixed ? 0.45 : (videos_only ? 0.98 : 0.85)
172
- budget = body[:clientHeight].to_f * fraction / images.length
196
+ columns = body[:classList].contains("image-columns") == JS::True
197
+ budget = body[:clientHeight].to_f * (columns ? 0.95 : fraction) / images.length
173
198
  images.each do |image|
174
199
  next if image[:naturalWidth].to_i.zero? || image[:naturalHeight].to_i.zero?
175
200
  width = image[:naturalWidth].to_f
176
201
  height = image.closest(".youtube-video") == JS::Null ? image[:naturalHeight].to_f : width * 9 / 16
177
- ratio = [body[:clientWidth].to_f / width, budget / height].min
202
+ available_width = body[:clientWidth].to_f * (columns ? 0.47 : 1.0)
203
+ ratio = [available_width / width, budget / height].min
178
204
  image[:style][:width] = "#{width * ratio}px"
179
205
  image[:style][:height] = "#{height * ratio}px"
180
206
  end
@@ -18,9 +18,11 @@ module Wani
18
18
  section[:className] = "slide titleless"
19
19
  end
20
20
  section[:classList].add("cover") if slide.number == 1 && !notes
21
+ section[:classList].add("multiline-title") if slide.number == 1 && !notes && slide.title.include?("\n")
21
22
  body = child(section, "div", "body")
22
23
  body[:innerHTML] = notes ? slide.notes_html : slide.html
23
24
  resolve_links(body)
25
+ prepare_image_columns(body) unless notes
24
26
  body.querySelectorAll("p").to_a.each do |paragraph|
25
27
  children = paragraph[:children].to_a
26
28
  next unless children.length == 1 && children.first[:classList].contains("youtube-video") == JS::True
@@ -108,6 +110,22 @@ module Wani
108
110
 
109
111
  private
110
112
 
113
+ def prepare_image_columns(body)
114
+ image = body[:children].to_a.find do |element|
115
+ element[:tagName].to_s == "P" && element[:children][:length].to_i == 1 &&
116
+ element.querySelector("img.wani-image-left, img.wani-image-right") != JS::Null && element[:textContent].to_s.strip.empty?
117
+ end
118
+ return unless image
119
+ side = image.querySelector("img")[:className].to_s.end_with?("left") ? "left" : "right"
120
+ text = @document.createElement("div")
121
+ text[:className] = "image-column-text"
122
+ body[:childNodes].to_a.each { |node| text.appendChild(node) unless node == image }
123
+ image[:classList].add("image-column-image")
124
+ body[:classList].add("image-columns", "image-#{side}")
125
+ body.appendChild(side == "left" ? image : text)
126
+ body.appendChild(side == "left" ? text : image)
127
+ end
128
+
111
129
  def child(parent, tag, class_name)
112
130
  element = @document.createElement(tag)
113
131
  element[:className] = class_name
@@ -34,7 +34,7 @@ module Wani
34
34
  remove(document.root, title) if title
35
35
  renderer = SafeHTML.new(&warn)
36
36
  note_roots = notes.map { |text| markdown(text).root }
37
- Slide.new(number: number, title: title ? plain(title) : "", html: renderer.render(document.root),
37
+ Slide.new(number: number, title: title ? plain(title, cover: number == 1) : "", html: renderer.render(document.root),
38
38
  page_footnote: page_footnotes.empty? ? "" : page_footnotes.last.strip,
39
39
  notes_html: note_roots.map { |root| renderer.render(root) }.join,
40
40
  images: ([document.root] + note_roots).flat_map { |root| walk(root).select { |node| node.type == :img && SafeHTML.safe_url?(node.attr['src'], image: true) }.map { |node| node.attr['src'] } }.uniq)
@@ -114,12 +114,13 @@ module Wani
114
114
  node.children.each { |child| remove(child, target) }
115
115
  end
116
116
 
117
- def plain(node)
117
+ def plain(node, cover: false)
118
118
  case node.type
119
119
  when :text, :codespan then node.value
120
120
  when :entity then node.value.char
121
121
  when :smart_quote then SafeHTML::QUOTES.fetch(node.value, "")
122
- else node.children.map { |child| plain(child) }.join
122
+ when :html_element then cover && node.value == "br" && node.attr.empty? ? "\n" : node.children.map { |child| plain(child, cover: cover) }.join
123
+ else node.children.map { |child| plain(child, cover: cover) }.join
123
124
  end
124
125
  end
125
126
  end
@@ -60,7 +60,11 @@ module Wani
60
60
  alt = self.class.escape(node.attr["alt"])
61
61
  "<span class=\"youtube-video\" data-youtube-src=\"#{self.class.escape(url)}\"><img src=\"https://i.ytimg.com/vi/#{id}/hqdefault.jpg\" alt=\"#{alt}\"><button type=\"button\" class=\"youtube-play\" aria-label=\"#{alt.empty? ? 'YouTube動画を再生' : alt + 'を再生'}\">▶</button></span>"
62
62
  elsif self.class.safe_url?(node.attr["src"], image: true)
63
- "<img src=\"#{self.class.escape(node.attr['src'])}\" alt=\"#{self.class.escape(node.attr['alt'])}\">"
63
+ alt = node.attr["alt"].to_s
64
+ split = alt.match(/\Abg\s+(left|right)(?:\s+(.*))?\z/m)
65
+ position = split ? " class=\"wani-image-#{split[1]}\"" : ""
66
+ alt = split[2].to_s if split
67
+ "<img#{position} src=\"#{self.class.escape(node.attr['src'])}\" alt=\"#{self.class.escape(alt)}\">"
64
68
  else
65
69
  @diagnostic.call("Unsafe image URL removed")
66
70
  self.class.escape(node.attr["alt"])
data/lib/wani/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Wani
2
- VERSION = "0.1.17"
2
+ VERSION = "0.1.19"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wani
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - ledsun