mirzam 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +8 -0
- data/action.yml +45 -0
- data/assets/fonts/Abel-Regular.ttf +0 -0
- data/assets/fonts/OFL.txt +93 -0
- data/exe/mirzam +6 -0
- data/lib/mirzam/version.rb +5 -0
- data/lib/mirzam.rb +302 -0
- data/sig/mirzam.rbs +9 -0
- metadata +75 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 982e3a0d7e8a664d623a46d033b9efe1aff80bc29544d4cf2d4b2d3534233cff
|
|
4
|
+
data.tar.gz: c278c0925fc080cb1d87e123222df7f86ce5e61162352541b2cb6ed8be0bb244
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 027753a83045073c87d38c4e81aacca24bc8a7d15354b3a040c069d1624b0fde2fab10625edd2fc9f73a29139ad1d085aaff6368d8fa1bb9de05b2bde9831884
|
|
7
|
+
data.tar.gz: '01009d02036ad5bbe29e8d2c87baa0fc68cdb2fe7c7b51c3071eddeb66bdaa701ba7f4336193902d6bf0f187cefa5b657ffb482a1d2dff462c56436b37451301'
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yudai Takada
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all 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,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Mirzam
|
|
2
|
+
|
|
3
|
+
Deterministic 1200×630 OGP image generation from Markdown front matter. It
|
|
4
|
+
provides default, minimal, and feature templates, title fitting, and a SHA256
|
|
5
|
+
sidecar cache for batch builds.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
gem install mirzam
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
mirzam render --input content/posts/hello.md --out public/ogp/hello.png
|
|
17
|
+
mirzam render --input content/posts/hello.json --out public/ogp/hello.png
|
|
18
|
+
mirzam render --title "Hello" --author noxdea --size 1200x630 --out hello.png
|
|
19
|
+
mirzam batch "content/posts/**/*.md" --out-dir public/ogp
|
|
20
|
+
mirzam templates
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Front matter requires `title`; JSON input uses the same metadata keys.
|
|
24
|
+
`author`, `date`, `tags`, `accent`, and `ogp.template` are optional. The Ruby
|
|
25
|
+
API is `Mirzam.render(title: "Hello")`.
|
|
26
|
+
The same batch command is available through [`action.yml`](action.yml).
|
|
27
|
+
|
|
28
|
+
## Development
|
|
29
|
+
|
|
30
|
+
Run `rake spec` and `gem build --strict mirzam.gemspec`.
|
|
31
|
+
|
|
32
|
+
## Contributing
|
|
33
|
+
|
|
34
|
+
Bug reports and pull requests are welcome at https://github.com/noxdea/mirzam.
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
MIT.
|
data/Rakefile
ADDED
data/action.yml
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
name: Mirzam OGP
|
|
2
|
+
description: Generate deterministic OGP images from Markdown front matter
|
|
3
|
+
inputs:
|
|
4
|
+
pattern:
|
|
5
|
+
description: Glob of Markdown files
|
|
6
|
+
required: true
|
|
7
|
+
out-dir:
|
|
8
|
+
description: Output directory
|
|
9
|
+
required: false
|
|
10
|
+
default: public/ogp
|
|
11
|
+
template:
|
|
12
|
+
description: Template name or Ruby file
|
|
13
|
+
required: false
|
|
14
|
+
default: default
|
|
15
|
+
theme:
|
|
16
|
+
description: dark, light, high_contrast, or a token file
|
|
17
|
+
required: false
|
|
18
|
+
default: dark
|
|
19
|
+
force:
|
|
20
|
+
description: Ignore sidecar hashes
|
|
21
|
+
required: false
|
|
22
|
+
default: 'false'
|
|
23
|
+
runs:
|
|
24
|
+
using: composite
|
|
25
|
+
steps:
|
|
26
|
+
- name: Set up Ruby
|
|
27
|
+
uses: ruby/setup-ruby@984c0c890880bbf811283d6f09c4607c62d210a4 # v1.323.0
|
|
28
|
+
with:
|
|
29
|
+
ruby-version: '3.3'
|
|
30
|
+
- name: Install Mirzam
|
|
31
|
+
shell: bash
|
|
32
|
+
run: gem install mirzam --no-document # zizmor: ignore[adhoc-packages]
|
|
33
|
+
- name: Render OGP images
|
|
34
|
+
shell: bash
|
|
35
|
+
env:
|
|
36
|
+
MIRZAM_PATTERN: ${{ inputs.pattern }}
|
|
37
|
+
MIRZAM_OUT_DIR: ${{ inputs.out-dir }}
|
|
38
|
+
MIRZAM_TEMPLATE: ${{ inputs.template }}
|
|
39
|
+
MIRZAM_THEME: ${{ inputs.theme }}
|
|
40
|
+
MIRZAM_FORCE: ${{ inputs.force }}
|
|
41
|
+
run: |
|
|
42
|
+
args=(batch "$MIRZAM_PATTERN" --out-dir "$MIRZAM_OUT_DIR" --template "$MIRZAM_TEMPLATE")
|
|
43
|
+
[[ "$MIRZAM_THEME" != dark ]] && args+=(--theme "$MIRZAM_THEME")
|
|
44
|
+
[[ "$MIRZAM_FORCE" == true ]] && args+=(--force)
|
|
45
|
+
mirzam "${args[@]}"
|
|
Binary file
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Copyright (c) 2011, Matthew Desmond (http://www.madtype.com | mattdesmond@gmail.com),with Reserved Font Name Abel.
|
|
2
|
+
|
|
3
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
4
|
+
This license is copied below, and is also available with a FAQ at:
|
|
5
|
+
http://scripts.sil.org/OFL
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
-----------------------------------------------------------
|
|
9
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
10
|
+
-----------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
PREAMBLE
|
|
13
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
14
|
+
development of collaborative font projects, to support the font creation
|
|
15
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
16
|
+
open framework in which fonts may be shared and improved in partnership
|
|
17
|
+
with others.
|
|
18
|
+
|
|
19
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
20
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
21
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
22
|
+
redistributed and/or sold with any software provided that any reserved
|
|
23
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
24
|
+
however, cannot be released under any other type of license. The
|
|
25
|
+
requirement for fonts to remain under this license does not apply
|
|
26
|
+
to any document created using the fonts or their derivatives.
|
|
27
|
+
|
|
28
|
+
DEFINITIONS
|
|
29
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
30
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
31
|
+
include source files, build scripts and documentation.
|
|
32
|
+
|
|
33
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
34
|
+
copyright statement(s).
|
|
35
|
+
|
|
36
|
+
"Original Version" refers to the collection of Font Software components as
|
|
37
|
+
distributed by the Copyright Holder(s).
|
|
38
|
+
|
|
39
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
40
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
41
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
42
|
+
new environment.
|
|
43
|
+
|
|
44
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
45
|
+
writer or other person who contributed to the Font Software.
|
|
46
|
+
|
|
47
|
+
PERMISSION & CONDITIONS
|
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
49
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
50
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
51
|
+
Software, subject to the following conditions:
|
|
52
|
+
|
|
53
|
+
1) Neither the Font Software nor any of its individual components,
|
|
54
|
+
in Original or Modified Versions, may be sold by itself.
|
|
55
|
+
|
|
56
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
57
|
+
redistributed and/or sold with any software, provided that each copy
|
|
58
|
+
contains the above copyright notice and this license. These can be
|
|
59
|
+
included either as stand-alone text files, human-readable headers or
|
|
60
|
+
in the appropriate machine-readable metadata fields within text or
|
|
61
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
62
|
+
|
|
63
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
64
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
65
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
66
|
+
presented to the users.
|
|
67
|
+
|
|
68
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
69
|
+
Software shall not be used to promote, endorse or advertise any
|
|
70
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
71
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
72
|
+
permission.
|
|
73
|
+
|
|
74
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
75
|
+
must be distributed entirely under this license, and must not be
|
|
76
|
+
distributed under any other license. The requirement for fonts to
|
|
77
|
+
remain under this license does not apply to any document created
|
|
78
|
+
using the Font Software.
|
|
79
|
+
|
|
80
|
+
TERMINATION
|
|
81
|
+
This license becomes null and void if any of the above conditions are
|
|
82
|
+
not met.
|
|
83
|
+
|
|
84
|
+
DISCLAIMER
|
|
85
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
88
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
89
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
90
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
91
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
92
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
93
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
data/exe/mirzam
ADDED
data/lib/mirzam.rb
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
require "digest"
|
|
5
|
+
require "fileutils"
|
|
6
|
+
require "json"
|
|
7
|
+
require "optparse"
|
|
8
|
+
require "pathname"
|
|
9
|
+
require "yaml"
|
|
10
|
+
require "zlib"
|
|
11
|
+
require "zaniah"
|
|
12
|
+
begin
|
|
13
|
+
require "auva"
|
|
14
|
+
rescue LoadError
|
|
15
|
+
end
|
|
16
|
+
require_relative "mirzam/version"
|
|
17
|
+
|
|
18
|
+
module Mirzam
|
|
19
|
+
class Error < StandardError; end
|
|
20
|
+
Source = Data.define(:title, :author, :date, :tags, :description, :accent, :template, :logo, :avatar) do
|
|
21
|
+
def self.from(hash)
|
|
22
|
+
raise Error, "metadata must be a mapping" unless hash.is_a?(Hash)
|
|
23
|
+
values = hash.transform_keys(&:to_sym)
|
|
24
|
+
ogp = values[:ogp].is_a?(Hash) ? values[:ogp].transform_keys(&:to_sym) : {}
|
|
25
|
+
new(title: values.fetch(:title).to_s, author: values[:author]&.to_s,
|
|
26
|
+
date: values[:date]&.to_s, tags: Array(values[:tags]).map(&:to_s),
|
|
27
|
+
description: values[:description]&.to_s, accent: values[:accent] || ogp[:accent],
|
|
28
|
+
template: (values[:template] || ogp[:template])&.to_sym,
|
|
29
|
+
logo: (values[:logo] || ogp[:logo])&.to_s, avatar: (values[:avatar] || ogp[:avatar])&.to_s)
|
|
30
|
+
rescue KeyError
|
|
31
|
+
raise Error, "front matter requires title"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
module FrontMatter
|
|
36
|
+
module_function
|
|
37
|
+
|
|
38
|
+
def parse(text)
|
|
39
|
+
text = text.to_s.delete_prefix("\uFEFF")
|
|
40
|
+
return Source.from(title: text.to_s) unless text.start_with?("---")
|
|
41
|
+
closing = text.match(/^---\s*$\n?/, 3)
|
|
42
|
+
raise Error, "front matter is not closed" unless closing
|
|
43
|
+
header = text[4...closing.begin(0)]
|
|
44
|
+
values = YAML.safe_load(header, permitted_classes: [Date, Time], aliases: false) || {}
|
|
45
|
+
raise Error, "front matter must be a mapping" unless values.is_a?(Hash)
|
|
46
|
+
Source.from(values)
|
|
47
|
+
rescue Psych::Exception => error
|
|
48
|
+
line = error.respond_to?(:line) ? error.line : nil
|
|
49
|
+
raise Error, "invalid front matter#{line ? " at line #{line}" : ""}: #{error.message}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def read(path)
|
|
53
|
+
parse(File.read(path, encoding: "UTF-8"))
|
|
54
|
+
rescue Errno::ENOENT
|
|
55
|
+
raise Error, "input file not found: #{path}"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
module Input
|
|
60
|
+
module_function
|
|
61
|
+
|
|
62
|
+
def read(path)
|
|
63
|
+
source = File.read(path, encoding: "UTF-8").delete_prefix("\uFEFF")
|
|
64
|
+
metadata = File.extname(path).downcase == ".json" ? Source.from(JSON.parse(source)) : FrontMatter.parse(source)
|
|
65
|
+
base = File.dirname(File.expand_path(path))
|
|
66
|
+
metadata.with(logo: resolve_asset(metadata.logo, base), avatar: resolve_asset(metadata.avatar, base))
|
|
67
|
+
rescue Errno::ENOENT
|
|
68
|
+
raise Error, "input file not found: #{path}"
|
|
69
|
+
rescue JSON::ParserError => error
|
|
70
|
+
raise Error, "invalid JSON: #{error.message}"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def resolve_asset(value, base)
|
|
74
|
+
return value unless value && !value.empty? && !Pathname.new(value).absolute?
|
|
75
|
+
File.expand_path(value, base)
|
|
76
|
+
rescue ArgumentError
|
|
77
|
+
value
|
|
78
|
+
end
|
|
79
|
+
private_class_method :resolve_asset
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
module Sizing
|
|
83
|
+
SIZES = [64, 56, 48, 42, 36].freeze
|
|
84
|
+
module_function
|
|
85
|
+
|
|
86
|
+
def fit_size(text, max_width:, max_height:, typesetter: nil)
|
|
87
|
+
return SIZES.last unless typesetter
|
|
88
|
+
SIZES.find do |size|
|
|
89
|
+
paragraph = typesetter.layout_paragraph(text.to_s, width: max_width, size: size,
|
|
90
|
+
wrap: :word, kinsoku: :hanging)
|
|
91
|
+
paragraph.height <= max_height
|
|
92
|
+
end || SIZES.last
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
module Templates
|
|
97
|
+
class Default
|
|
98
|
+
SIZE = [1200, 630].freeze
|
|
99
|
+
PADDING = 72
|
|
100
|
+
|
|
101
|
+
def initialize(theme: Zaniah::Theme.dark, accent: nil)
|
|
102
|
+
@theme = theme
|
|
103
|
+
@accent = accent ? Zaniah::Color.parse(accent) : theme.colors.accent
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def call(source, renderer)
|
|
107
|
+
size = renderer.fit_size(source.title, max_width: SIZE[0] - PADDING * 2, max_height: 300)
|
|
108
|
+
root = Zaniah::Div.new.flex_col.p(PADDING).gap(20).bg(@theme.colors.background)
|
|
109
|
+
.child(brand(source))
|
|
110
|
+
.child(Zaniah::Text.new(source.author ? "#{source.author} · #{source.date}" : "",
|
|
111
|
+
size: 20, color: @accent))
|
|
112
|
+
.child(Zaniah::Text.new(source.title, size: size, color: @theme.colors.text))
|
|
113
|
+
.child(Zaniah::Text.new(source.tags.empty? ? "" : source.tags.map { |tag| "##{tag}" }.join(" "),
|
|
114
|
+
size: 18, color: @theme.colors.text_muted))
|
|
115
|
+
root
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
private
|
|
119
|
+
|
|
120
|
+
def brand(source)
|
|
121
|
+
path = source.avatar || source.logo
|
|
122
|
+
return Zaniah::Div.new.h(1) unless path && File.file?(path)
|
|
123
|
+
Zaniah::Image.new(path).style(width: 64, height: 64)
|
|
124
|
+
rescue StandardError
|
|
125
|
+
Zaniah::Div.new.h(1)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
class Minimal < Default
|
|
130
|
+
def call(source, renderer)
|
|
131
|
+
size = renderer.fit_size(source.title, max_width: 1056, max_height: 420)
|
|
132
|
+
Zaniah::Div.new.flex_col.justify_center.p(72).bg(@theme.colors.background)
|
|
133
|
+
.child(Zaniah::Text.new(source.title, size: size, color: @theme.colors.text))
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
class Feature < Default
|
|
138
|
+
def call(source, renderer)
|
|
139
|
+
Default.new(theme: @theme, accent: source.accent || @accent).call(source, renderer)
|
|
140
|
+
.child(Zaniah::Text.new(source.description.to_s, size: 16, color: @theme.colors.text_muted))
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
module_function
|
|
145
|
+
|
|
146
|
+
def resolve(name, theme: Zaniah::Theme.dark)
|
|
147
|
+
if name.to_s.end_with?(".rb") && File.file?(name.to_s)
|
|
148
|
+
path = File.expand_path(name.to_s)
|
|
149
|
+
value = eval(File.read(path, encoding: "UTF-8"), TOPLEVEL_BINDING, path, 1)
|
|
150
|
+
return value if value.respond_to?(:call)
|
|
151
|
+
raise Error, "template file must return a callable"
|
|
152
|
+
end
|
|
153
|
+
case name.to_s
|
|
154
|
+
when "default" then Default.new(theme: theme)
|
|
155
|
+
when "minimal" then Minimal.new(theme: theme)
|
|
156
|
+
when "feature" then Feature.new(theme: theme)
|
|
157
|
+
else raise Error, "unknown template: #{name}"
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
class Renderer
|
|
163
|
+
SIZE = [1200, 630].freeze
|
|
164
|
+
FONTS = Dir[File.expand_path("../assets/fonts/*.{ttf,otf}", __dir__)].freeze
|
|
165
|
+
|
|
166
|
+
attr_reader :typesetter
|
|
167
|
+
|
|
168
|
+
def initialize(theme: Zaniah::Theme.dark, width: SIZE[0], height: SIZE[1], font_dir: nil)
|
|
169
|
+
@theme, @width, @height, @font_dir = theme, width, height, font_dir
|
|
170
|
+
@font_db = Zaniah::TextSystem::FontDB.new(paths: font_dir ? Array(font_dir) : FONTS)
|
|
171
|
+
@font = @font_db.find(family: theme.typography.font_sans)
|
|
172
|
+
@text_system = Zaniah::TextSystem::Renderer.new(font: @font, font_db: @font_db)
|
|
173
|
+
@typesetter = Zaniah::TextSystem::Typesetter.new(font: @font, font_db: @font_db)
|
|
174
|
+
rescue StandardError
|
|
175
|
+
@font_db = @font = @text_system = @typesetter = nil
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def fit_size(text, max_width:, max_height:)
|
|
179
|
+
return Sizing.fit_size(text, max_width: max_width, max_height: max_height, typesetter: @typesetter) if @typesetter
|
|
180
|
+
Sizing::SIZES.last
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def cache_key
|
|
184
|
+
[@width, @height, @font_dir, @theme.inspect].join("\0")
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def render(source, template: Templates::Default.new(theme: @theme))
|
|
188
|
+
app = Zaniah::App.new
|
|
189
|
+
window = app.open_window(backend: :headless, width: @width, height: @height)
|
|
190
|
+
app.global(:theme, @theme)
|
|
191
|
+
window.text_system = @text_system if @text_system
|
|
192
|
+
window.draw { template.call(source, self) }
|
|
193
|
+
window.tick
|
|
194
|
+
device = window.device
|
|
195
|
+
Zaniah::PNG.encode(device.width.to_i, device.height.to_i, device.pixels)
|
|
196
|
+
ensure
|
|
197
|
+
window&.close
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
class Batch
|
|
202
|
+
def initialize(renderer: Renderer.new, template: :default)
|
|
203
|
+
@renderer, @template = renderer, template
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def render(paths, out_dir:, force: false)
|
|
207
|
+
FileUtils.mkdir_p(out_dir)
|
|
208
|
+
paths.sort.filter_map do |path|
|
|
209
|
+
source = Input.read(path)
|
|
210
|
+
destination = File.join(out_dir, "#{File.basename(path, ".*")}.png")
|
|
211
|
+
template_path = @template.to_s
|
|
212
|
+
template_signature = File.file?(template_path) ? File.binread(template_path) : @template.to_s
|
|
213
|
+
signature = Digest::SHA256.hexdigest(File.binread(path) + template_signature + @renderer.cache_key)
|
|
214
|
+
state_path = "#{destination}.json"
|
|
215
|
+
next if !force && File.file?(destination) && File.file?(state_path) && File.read(state_path).include?(signature)
|
|
216
|
+
png = @renderer.render(source, template: Templates.resolve(source.template || @template))
|
|
217
|
+
File.binwrite(destination, png)
|
|
218
|
+
File.write(state_path, "{\"sha256\":\"#{signature}\"}\n")
|
|
219
|
+
destination
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
module_function
|
|
225
|
+
|
|
226
|
+
def theme(value)
|
|
227
|
+
return value if value.respond_to?(:colors)
|
|
228
|
+
return Auva.load(value) if defined?(Auva) && File.file?(value.to_s)
|
|
229
|
+
return Auva.builtin(value) if defined?(Auva)
|
|
230
|
+
Zaniah::Theme.public_send(value.to_s)
|
|
231
|
+
rescue NoMethodError
|
|
232
|
+
raise Error, "unknown theme: #{value}"
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def render(title:, author: nil, date: nil, tags: [], description: nil, accent: nil, logo: nil, avatar: nil,
|
|
236
|
+
template: :default, theme: :dark, width: 1200, height: 630, font_dir: nil)
|
|
237
|
+
source = Source.new(title: title.to_s, author: author&.to_s, date: date&.to_s,
|
|
238
|
+
tags: Array(tags).map(&:to_s), description: description&.to_s, accent: accent, template: template.to_sym,
|
|
239
|
+
logo: logo&.to_s, avatar: avatar&.to_s)
|
|
240
|
+
selected_theme = theme(theme)
|
|
241
|
+
Renderer.new(theme: selected_theme, width: width, height: height, font_dir: font_dir)
|
|
242
|
+
.render(source, template: Templates.resolve(template, theme: selected_theme))
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
class CLI
|
|
246
|
+
def self.run(argv, out: $stdout, err: $stderr)
|
|
247
|
+
command = %w[render batch templates preview].include?(argv.first) ? argv.shift : "render"
|
|
248
|
+
case command
|
|
249
|
+
when "templates"
|
|
250
|
+
out.puts %w[default minimal feature]
|
|
251
|
+
return 0
|
|
252
|
+
when "render", "preview" then render(argv)
|
|
253
|
+
when "batch" then batch(argv)
|
|
254
|
+
end
|
|
255
|
+
rescue OptionParser::ParseError, KeyError, Error => error
|
|
256
|
+
err.puts "mirzam: #{error.message}"
|
|
257
|
+
1
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def self.render(argv)
|
|
261
|
+
options = {title: nil, author: nil, logo: nil, avatar: nil, out: nil, input: nil, template: :default, theme: :dark, width: 1200, height: 630, font_dir: nil}
|
|
262
|
+
OptionParser.new do |opts|
|
|
263
|
+
opts.on("--input PATH") { |v| options[:input] = v }
|
|
264
|
+
opts.on("--title TITLE") { |v| options[:title] = v }
|
|
265
|
+
opts.on("--author NAME") { |v| options[:author] = v }
|
|
266
|
+
opts.on("--logo PATH") { |v| options[:logo] = v }
|
|
267
|
+
opts.on("--avatar PATH") { |v| options[:avatar] = v }
|
|
268
|
+
opts.on("--out PATH") { |v| options[:out] = v }
|
|
269
|
+
opts.on("--template NAME") { |v| options[:template] = v.end_with?(".rb") ? v : v.to_sym }
|
|
270
|
+
opts.on("--theme NAME") { |v| options[:theme] = v }
|
|
271
|
+
opts.on("--size SIZE") { |v| options[:width], options[:height] = v.split("x", 2).map { |part| Integer(part, 10) } }
|
|
272
|
+
opts.on("--font-dir PATH") { |v| options[:font_dir] = v }
|
|
273
|
+
end.parse!(argv)
|
|
274
|
+
source = options[:input] ? Input.read(options[:input]) : Source.from(title: options[:title] || argv.fetch(0),
|
|
275
|
+
author: options[:author], logo: options[:logo], avatar: options[:avatar])
|
|
276
|
+
selected_theme = Mirzam.theme(options[:theme])
|
|
277
|
+
selected_template = source.template || options[:template]
|
|
278
|
+
png = Renderer.new(theme: selected_theme, width: options[:width], height: options[:height], font_dir: options[:font_dir])
|
|
279
|
+
.render(source, template: Templates.resolve(selected_template, theme: selected_theme))
|
|
280
|
+
File.binwrite(options[:out] || "mirzam.png", png)
|
|
281
|
+
0
|
|
282
|
+
end
|
|
283
|
+
private_class_method :render
|
|
284
|
+
|
|
285
|
+
def self.batch(argv)
|
|
286
|
+
options = {out_dir: "public/ogp", template: :default, force: false, theme: :dark, width: 1200, height: 630, font_dir: nil}
|
|
287
|
+
OptionParser.new do |opts|
|
|
288
|
+
opts.on("--out-dir DIR") { |v| options[:out_dir] = v }
|
|
289
|
+
opts.on("--template NAME") { |v| options[:template] = v.end_with?(".rb") ? v : v.to_sym }
|
|
290
|
+
opts.on("--theme NAME") { |v| options[:theme] = v }
|
|
291
|
+
opts.on("--size SIZE") { |v| options[:width], options[:height] = v.split("x", 2).map { |part| Integer(part, 10) } }
|
|
292
|
+
opts.on("--font-dir PATH") { |v| options[:font_dir] = v }
|
|
293
|
+
opts.on("--force") { options[:force] = true }
|
|
294
|
+
end.parse!(argv)
|
|
295
|
+
paths = argv.flat_map { |pattern| Dir[pattern] }
|
|
296
|
+
renderer = Renderer.new(theme: Mirzam.theme(options[:theme]), width: options[:width], height: options[:height], font_dir: options[:font_dir])
|
|
297
|
+
Batch.new(renderer: renderer, template: options[:template]).render(paths, out_dir: options[:out_dir], force: options[:force])
|
|
298
|
+
0
|
|
299
|
+
end
|
|
300
|
+
private_class_method :batch
|
|
301
|
+
end
|
|
302
|
+
end
|
data/sig/mirzam.rbs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
module Mirzam
|
|
2
|
+
VERSION: String
|
|
3
|
+
def self.render: (title: String, ?author: String?, ?date: String?, ?tags: Array[String], ?description: String?, ?accent: String?, ?template: Symbol, ?theme: untyped, ?width: Integer, ?height: Integer, ?font_dir: String?) -> String
|
|
4
|
+
class Error < StandardError
|
|
5
|
+
end
|
|
6
|
+
class Renderer
|
|
7
|
+
def render: (untyped source, ?template: untyped) -> String
|
|
8
|
+
end
|
|
9
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mirzam
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Yudai Takada
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: zaniah
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.6.0
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0.7'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: 0.6.0
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '0.7'
|
|
32
|
+
description: Generate OGP PNGs from article metadata using Zaniah.
|
|
33
|
+
email:
|
|
34
|
+
- t.yudai92@gmail.com
|
|
35
|
+
executables:
|
|
36
|
+
- mirzam
|
|
37
|
+
extensions: []
|
|
38
|
+
extra_rdoc_files: []
|
|
39
|
+
files:
|
|
40
|
+
- CHANGELOG.md
|
|
41
|
+
- LICENSE.txt
|
|
42
|
+
- README.md
|
|
43
|
+
- Rakefile
|
|
44
|
+
- action.yml
|
|
45
|
+
- assets/fonts/Abel-Regular.ttf
|
|
46
|
+
- assets/fonts/OFL.txt
|
|
47
|
+
- exe/mirzam
|
|
48
|
+
- lib/mirzam.rb
|
|
49
|
+
- lib/mirzam/version.rb
|
|
50
|
+
- sig/mirzam.rbs
|
|
51
|
+
homepage: https://github.com/noxdea/mirzam
|
|
52
|
+
licenses:
|
|
53
|
+
- MIT
|
|
54
|
+
metadata:
|
|
55
|
+
allowed_push_host: https://rubygems.org
|
|
56
|
+
source_code_uri: https://github.com/noxdea/mirzam
|
|
57
|
+
rubygems_mfa_required: 'true'
|
|
58
|
+
rdoc_options: []
|
|
59
|
+
require_paths:
|
|
60
|
+
- lib
|
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: 3.2.0
|
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
71
|
+
requirements: []
|
|
72
|
+
rubygems_version: 4.0.16
|
|
73
|
+
specification_version: 4
|
|
74
|
+
summary: Deterministic OGP image generator for Ruby
|
|
75
|
+
test_files: []
|