asciidoctor-diagram-layout 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f22dac919c2d693bff3278c5bf346b95f1c0e019e8778ab067d9cb24bbedc339
4
+ data.tar.gz: 153c7b5ee567acde3e3fec89caf1d99a7d7d721ebd2e93c5cc38e31dee31fc31
5
+ SHA512:
6
+ metadata.gz: 3e240b340cfb7a98bb4a92e8488f3cc29f8b1f0346bdd6254bc9bd425ca86640b81d23260bd42cafb1adcb71345ed6e5004848421161998716e363891d3f9a03
7
+ data.tar.gz: ef99d52540a84f27354a62aa1170c69537e641921fe1fabf67f69d1f037a80fa72b124702cdc48bf0c6027c12989a306b1f650302e5191569cb5b25bee0d4c21
data/LICENSE ADDED
@@ -0,0 +1,192 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and Derivative Works thereof.
46
+
47
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
48
+ in the Work by the copyright owner or by an individual or Legal Entity
49
+ authorized to submit on behalf of the copyright owner. For the purposes
50
+ of this definition, "submitted" means any form of electronic, verbal,
51
+ or written communication sent to the Licensor or its representatives,
52
+ including but not limited to communication on electronic mailing lists,
53
+ source code control systems, and issue tracking systems that are managed
54
+ by, or on behalf of, the Licensor for the purpose of developing and
55
+ coordinating the Work, but excluding communication that is conspicuously
56
+ marked or designated in writing by the copyright owner as "Not a
57
+ Contribution."
58
+
59
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
60
+ whom a Contribution has been received by the Licensor and included
61
+ within the Work.
62
+
63
+ 2. Grant of Copyright License. Subject to the terms and conditions of
64
+ this License, each Contributor hereby grants to You a perpetual,
65
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
66
+ copyright license to reproduce, prepare Derivative Works of,
67
+ publicly display, publicly perform, sublicense, and distribute the
68
+ Work and such Derivative Works in Source or Object form.
69
+
70
+ 3. Grant of Patent License. Subject to the terms and conditions of
71
+ this License, each Contributor hereby grants to You a perpetual,
72
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
73
+ (except as stated in this section) patent license to make, have made,
74
+ use, offer to sell, sell, import, and otherwise transfer the Work,
75
+ where such license applies only to those patent claims licensable
76
+ by such Contributor that are necessarily infringed by their
77
+ Contribution(s) alone or by the combination of their Contributions
78
+ with the Work to which such Contributions were submitted. If You
79
+ institute patent litigation against any entity (including a
80
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
81
+ or a Contribution incorporated within the Work constitutes direct
82
+ or contributory patent infringement, then any patent licenses
83
+ granted to You under this License for that Work shall terminate
84
+ as of the date such litigation is filed.
85
+
86
+ 4. Redistribution. You may reproduce and distribute copies of the
87
+ Work or Derivative Works thereof in any medium, with or without
88
+ modifications, and in Source or Object form, provided that You
89
+ meet the following conditions:
90
+
91
+ (a) You must give any other recipients of the Work or Derivative
92
+ Works a copy of this License; and
93
+
94
+ (b) You must cause any modified files to carry prominent notices
95
+ stating that You changed the files; and
96
+
97
+ (c) You must retain, in the Source form of any Derivative Works
98
+ that You distribute, all copyright, patent, trademark, and
99
+ attribution notices from the Source form of the Work,
100
+ excluding those notices that do not pertain to any part of
101
+ the Derivative Works; and
102
+
103
+ (d) If the Work includes a "NOTICE" text file as part of its
104
+ distribution, You must include a readable copy of the
105
+ attribution notices contained within such NOTICE file, in
106
+ at least one of the following places: within a NOTICE text
107
+ file distributed as part of the Derivative Works; within
108
+ the Source form or documentation, if provided along with the
109
+ Derivative Works; or, within a display generated by the
110
+ Derivative Works, if and wherever such third-party notices
111
+ normally appear. The contents of the NOTICE file are for
112
+ informational purposes only and do not modify the License.
113
+ You may add Your own attribution notices within Derivative
114
+ Works that alongside or as an addendum to the NOTICE text
115
+ from the Work, provided that such additional attribution
116
+ notices cannot be construed as modifying the License.
117
+
118
+ You may add Your own license statement for Your modifications and
119
+ may provide additional grant of rights to use, copy, modify, merge,
120
+ publish, distribute, sublicense, and/or sell copies of the
121
+ Derivative Works, subject to the terms and conditions of this License.
122
+
123
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
124
+ any Contribution intentionally submitted for inclusion in the Work
125
+ by You to the Licensor shall be under the terms and conditions of
126
+ this License, without any additional terms or conditions.
127
+ Notwithstanding the above, nothing herein shall supersede or modify
128
+ the terms of any separate license agreement you may have executed
129
+ with Licensor regarding such Contributions.
130
+
131
+ 6. Trademarks. This License does not grant permission to use the trade
132
+ names, trademarks, service marks, or product names of the Licensor,
133
+ except as required for reasonable and customary use in describing the
134
+ origin of the Work and reproducing the content of the NOTICE file.
135
+
136
+ 7. Disclaimer of Warranty. Unless required by applicable law or
137
+ agreed to in writing, Licensor provides the Work (and each
138
+ Contributor provides its Contributions) on an "AS IS" BASIS,
139
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
140
+ implied, including, without limitation, any warranties or conditions
141
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
142
+ PARTICULAR PURPOSE. You are solely responsible for determining the
143
+ appropriateness of using or reproducing the Work and assume any
144
+ risks associated with Your exercise of permissions under this License.
145
+
146
+ 8. Limitation of Liability. In no event and under no legal theory,
147
+ whether in tort (including negligence), contract, or otherwise,
148
+ unless required by applicable law (such as deliberate and grossly
149
+ negligent acts) or agreed to in writing, shall any Contributor be
150
+ liable to You for damages, including any direct, indirect, special,
151
+ incidental, or exemplary damages of any character arising as a
152
+ result of this License or out of the use or inability to use the
153
+ Work (even if such Contributor has been advised of the possibility
154
+ of such damages).
155
+
156
+ 9. Accepting Warranty or Liability. While redistributing the Work or
157
+ Derivative Works thereof, You may choose to offer, and charge a fee
158
+ for, acceptance of support, warranty, indemnity, or other liability
159
+ obligations and/or rights consistent with this License. However, in
160
+ accepting such obligations, You may offer only conditions that do not
161
+ conflict with the terms and conditions of this License, and for which
162
+ You may act only on Your own behalf and on Your sole responsibility,
163
+ not on behalf of any other Contributor. On such condition, you must
164
+ fully indemnify and hold harmless each Contributor for any liability
165
+ incurred by such Contributor as a result of warranties, liability
166
+ obligations assumed by You.
167
+
168
+ END OF TERMS AND CONDITIONS
169
+
170
+ APPENDIX: How to apply the Apache License to your work.
171
+
172
+ To apply the Apache License to your work, attach the following
173
+ boilerplate notice, with the fields enclosed by brackets "[]"
174
+ replaced with your own identifying information. (Don't include
175
+ the brackets!) The text should be enclosed in the appropriate
176
+ comment syntax for the syntax of your file format. Please also
177
+ include a brief description of how this work is licensed under
178
+ the Apache License, Version 2.0.
179
+
180
+ Copyright [2024] Qulix Systems
181
+
182
+ Licensed under the Apache License, Version 2.0 (the "License");
183
+ you may not use this file except in compliance with the License.
184
+ You may obtain a copy of the License at
185
+
186
+ http://www.apache.org/licenses/LICENSE-2.0
187
+
188
+ Unless required by applicable law or agreed to in writing, software
189
+ distributed under the License is distributed on an "AS IS" BASIS,
190
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
191
+ See the License for the specific language governing permissions and
192
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # asciidoctor-diagram-layout
2
+
3
+ An Asciidoctor extension that adds a `[layout-rowcol]` block for
4
+ rendering flex-based layout diagrams as HTML or SVG.
5
+
6
+ ## Installation
7
+
8
+ Add to your `Gemfile`:
9
+
10
+ ```ruby
11
+ gem "asciidoctor-diagram-layout"
12
+ ```
13
+
14
+ Or install directly:
15
+
16
+ ```
17
+ gem install asciidoctor-diagram-layout
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Register the extension and use the `[layout-rowcol]` block in your
23
+ AsciiDoc documents:
24
+
25
+ ```ruby
26
+ require "asciidoctor"
27
+ require "asciidoctor-diagram-layout"
28
+
29
+ Asciidoctor::Extensions.register do
30
+ block AsciidoctorDiagramLayout::Asciidoc::LayoutBlockProcessor
31
+ end
32
+ ```
33
+
34
+ ### Basic example
35
+
36
+ ```asciidoc
37
+ [layout-rowcol]
38
+ ----
39
+ cols:
40
+ cell(30): Sidebar
41
+ rows:
42
+ cell: Header
43
+ cell: Content
44
+ ----
45
+ ```
46
+
47
+ ### DSL syntax
48
+
49
+ Each line declares one node. Indentation (4 spaces or one tab) defines
50
+ nesting.
51
+
52
+ * `cols:` - container that arranges children horizontally
53
+ * `rows:` - container that arranges children vertically
54
+ * `cell: Name` - leaf cell with a visible label
55
+ * `cell(30): Name` - leaf cell with a fixed size of 30% of its parent
56
+ * Containers without an explicit size take equal shares of the remaining
57
+ space
58
+ * Lines starting with `#` are comments
59
+
60
+ ### Block attributes
61
+
62
+ * `direction` - implicit direction for top-level cells when no container
63
+ is declared: `rows` (default) or `cols`
64
+ * `renderer` - force output format: `html` or `svg`; defaults to `html`
65
+ for HTML backends and `svg` for all others including PDF
66
+ * `palette` - color scheme: `rainbow` (default), `pastel`, `warm`,
67
+ `cool`, `mono`
68
+ * `width` - diagram width, e.g. `100%` (default) or `800px`
69
+ * `height` - diagram height, e.g. `400px`; not set by default
70
+ * `target` - SVG file name (without extension) when rendering to PDF
71
+
72
+ ### PDF output
73
+
74
+ When converting to PDF, the extension writes the diagram as an SVG file
75
+ and embeds it as an image block.
76
+ Use the `target` attribute to control the output file name:
77
+
78
+ ```asciidoc
79
+ [layout-rowcol, target="my-diagram"]
80
+ ----
81
+ cols:
82
+ cell: Left
83
+ cell: Right
84
+ ----
85
+ ```
86
+
87
+ ## License
88
+
89
+ Apache License 2.0.
90
+ See [LICENSE](LICENSE).
@@ -0,0 +1,11 @@
1
+ require_relative "asciidoctor_diagram_layout/version"
2
+ require_relative "asciidoctor_diagram_layout/parser"
3
+ require_relative "asciidoctor_diagram_layout/renderer/color_palette"
4
+ require_relative "asciidoctor_diagram_layout/renderer/scheme/golden_ratio_scheme"
5
+ require_relative "asciidoctor_diagram_layout/renderer/scheme/analogous_scheme"
6
+ require_relative "asciidoctor_diagram_layout/renderer/scheme/monochromatic_scheme"
7
+ require_relative "asciidoctor_diagram_layout/renderer/scheme/cell_color_scheme_factory"
8
+ require_relative "asciidoctor_diagram_layout/renderer/render_options"
9
+ require_relative "asciidoctor_diagram_layout/renderer/html_renderer"
10
+ require_relative "asciidoctor_diagram_layout/renderer/svg_renderer"
11
+ require_relative "asciidoctor_diagram_layout/asciidoc/layout_block_processor"
@@ -0,0 +1,83 @@
1
+ require "asciidoctor"
2
+ require "asciidoctor/extensions"
3
+
4
+ module AsciidoctorDiagramLayout
5
+ module Asciidoc
6
+ class LayoutBlockProcessor < Asciidoctor::Extensions::BlockProcessor
7
+ use_dsl
8
+ named :"layout-rowcol"
9
+ on_contexts :listing, :literal
10
+
11
+ def process(parent, reader, attrs)
12
+ dsl = reader.read
13
+ implicit_dir = attrs["direction"] == "cols" ? :cols : :rows
14
+ root = Parser.new.parse(dsl, implicit_dir)
15
+ backend = parent.document.attr("backend").to_s
16
+ renderer_attr = attrs["renderer"]
17
+ strategy = resolve_strategy(renderer_attr, backend)
18
+ palette = attrs.fetch("palette", "rainbow")
19
+ pdf = backend == "pdf"
20
+ options = Renderer::RenderOptions.new(
21
+ width: attrs.fetch("width", "100%"),
22
+ height: attrs["height"],
23
+ palette: palette,
24
+ pdf: pdf,
25
+ name_converter: method(:identity)
26
+ )
27
+ case strategy
28
+ when :html then render_html(parent, root, options)
29
+ when :svg then render_svg(parent, root, backend, options, attrs)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def resolve_strategy(renderer_attr, backend)
36
+ case renderer_attr.to_s.downcase
37
+ when "html" then :html
38
+ when "svg" then :svg
39
+ else
40
+ case backend.downcase
41
+ when "html", "html5" then :html
42
+ else :svg
43
+ end
44
+ end
45
+ end
46
+
47
+ def render_html(parent, root, options)
48
+ html = Renderer::HtmlRenderer.new.render(root, options)
49
+ create_pass_block(parent, html, {}, subs: nil)
50
+ end
51
+
52
+ def render_svg(parent, root, backend, options, attrs)
53
+ svg = Renderer::SvgRenderer.new.render(root, options)
54
+ if backend == "pdf"
55
+ target = attrs["target"]
56
+ outdir = parent.document.attr("outdir").to_s
57
+ path = write_svg(svg, target, outdir)
58
+ create_image_block(parent, { "target" => path, "format" => "svg" })
59
+ else
60
+ create_pass_block(parent, svg, {}, subs: nil)
61
+ end
62
+ end
63
+
64
+ def write_svg(svg, name, outdir)
65
+ has_outdir = outdir && outdir != "null" && !outdir.empty?
66
+ has_name = name && !name.empty?
67
+ if has_outdir && has_name
68
+ path = File.join(outdir, "#{name}.svg")
69
+ elsif has_name
70
+ path = File.join(Dir.tmpdir, "#{name}-#{SecureRandom.hex(4)}.svg")
71
+ else
72
+ path = File.join(Dir.tmpdir, "layout-#{SecureRandom.hex(4)}.svg")
73
+ end
74
+ File.write(path, svg, encoding: "UTF-8")
75
+ path
76
+ end
77
+
78
+ def identity(text)
79
+ text
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,124 @@
1
+ module AsciidoctorDiagramLayout
2
+ class ParseError < StandardError; end
3
+
4
+ # Parses flex layout DSL text into a node tree.
5
+ #
6
+ # Format: one directive per line. Containers (cols, rows) declare
7
+ # a nested block via indentation. Leaf zones use cell.
8
+ # An optional size in parentheses sets a fixed percentage:
9
+ # cell(30): Name or cols(40):
10
+ class Parser
11
+ LINE_PATTERN = /\A(?<keyword>cols|rows|cell)(?:\((?<size>\d+)\))?:(?<rest>.*)\z/i
12
+ TAB_WIDTH = 4
13
+
14
+ def parse(dsl, implicit_direction = :rows)
15
+ lines = dsl.split("\n", -1)
16
+ state = ParseState.new(lines)
17
+ roots = parse_children(state, 0)
18
+ raise ParseError, "Empty input: expected at least one cell, rows, or cols" if roots.empty?
19
+ if roots.size == 1 && roots.first.is_a?(ContainerNode)
20
+ roots.first
21
+ else
22
+ ContainerNode.new(implicit_direction, :auto, roots)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def parse_children(state, indent)
29
+ nodes = []
30
+ while state.more?
31
+ line = state.peek
32
+ trimmed = strip_comment(line).rstrip
33
+ if trimmed.strip.empty?
34
+ state.advance
35
+ next
36
+ end
37
+ line_indent = count_indent(line)
38
+ break if line_indent < indent
39
+ if line_indent > indent
40
+ raise ParseError, "Unexpected indentation at line #{state.index + 1}: #{line.inspect}"
41
+ end
42
+ state.advance
43
+ content = trimmed.strip
44
+ m = LINE_PATTERN.match(content)
45
+ raise ParseError, "Invalid syntax at line #{state.index}: #{line.inspect}" unless m
46
+ keyword = m[:keyword].downcase
47
+ size = m[:size] ? m[:size].to_i : :auto
48
+ rest = m[:rest].strip
49
+ nodes << build_node(keyword, size, rest, state, indent, state.index, line)
50
+ end
51
+ nodes
52
+ end
53
+
54
+ def build_node(keyword, size, rest, state, indent, line_number, original_line)
55
+ if keyword == "cell"
56
+ raise ParseError, "cell requires a name at line #{line_number}: #{original_line.inspect}" if rest.empty?
57
+ return CellNode.new(size, rest)
58
+ end
59
+ unless rest.empty?
60
+ raise ParseError, "#{keyword} is a container and must not have a value at line #{line_number}: #{original_line.inspect}"
61
+ end
62
+ direction = keyword == "cols" ? :cols : :rows
63
+ nested_indent = detect_nested_indent(state, indent, line_number)
64
+ children = parse_children(state, nested_indent)
65
+ ContainerNode.new(direction, size, children)
66
+ end
67
+
68
+ def detect_nested_indent(state, current_indent, line_number)
69
+ while state.more?
70
+ line = state.peek
71
+ trimmed = strip_comment(line).rstrip
72
+ unless trimmed.strip.empty?
73
+ indent = count_indent(line)
74
+ if indent <= current_indent
75
+ raise ParseError, "Expected indented block after line #{line_number} but found: #{line.inspect}"
76
+ end
77
+ return indent
78
+ end
79
+ state.advance
80
+ end
81
+ raise ParseError, "Expected indented block after line #{line_number} but reached end of input"
82
+ end
83
+
84
+ def count_indent(line)
85
+ count = 0
86
+ line.each_char do |c|
87
+ if c == ' '
88
+ count += 1
89
+ elsif c == "\t"
90
+ count += TAB_WIDTH
91
+ else
92
+ break
93
+ end
94
+ end
95
+ count
96
+ end
97
+
98
+ def strip_comment(line)
99
+ i = line.index('#')
100
+ i ? line[0, i] : line
101
+ end
102
+ end
103
+
104
+ ParseState = Struct.new(:lines, :index) do
105
+ def initialize(lines)
106
+ super(lines, 0)
107
+ end
108
+
109
+ def more?
110
+ index < lines.size
111
+ end
112
+
113
+ def peek
114
+ lines[index]
115
+ end
116
+
117
+ def advance
118
+ self.index += 1
119
+ end
120
+ end
121
+
122
+ ContainerNode = Struct.new(:direction, :size, :children)
123
+ CellNode = Struct.new(:size, :name)
124
+ end
@@ -0,0 +1,47 @@
1
+ module AsciidoctorDiagramLayout
2
+ module Renderer
3
+ module ColorPalette
4
+ STROKE_COLOR = "#d0d0d0"
5
+ GOLDEN_RATIO = 0.618033988749895
6
+ HUE_RANGE = 360
7
+
8
+ # Converts HSL to a hex color string, identical to Java ColorPalette.hslToHex.
9
+ def self.hsl_to_hex(hue, saturation, lightness)
10
+ s = saturation / 100.0
11
+ l = lightness / 100.0
12
+ c = (1 - (2 * l - 1).abs) * s
13
+ x = c * (1 - ((hue / 60.0) % 2 - 1).abs)
14
+ m = l - c / 2.0
15
+ if hue < 60
16
+ r, g, b = c, x, 0
17
+ elsif hue < 120
18
+ r, g, b = x, c, 0
19
+ elsif hue < 180
20
+ r, g, b = 0, c, x
21
+ elsif hue < 240
22
+ r, g, b = 0, x, c
23
+ elsif hue < 300
24
+ r, g, b = x, 0, c
25
+ else
26
+ r, g, b = c, 0, x
27
+ end
28
+ ri = (r + m) * 255
29
+ gi = (g + m) * 255
30
+ bi = (b + m) * 255
31
+ format("#%02x%02x%02x", ri.round, gi.round, bi.round)
32
+ end
33
+
34
+ # Java String.hashCode: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
35
+ # using 32-bit signed integer overflow semantics.
36
+ def self.java_hash(str)
37
+ return 0 if str.nil? || str.empty?
38
+ h = 0
39
+ str.each_char do |c|
40
+ h = (31 * h + c.ord) & 0xffffffff
41
+ end
42
+ # convert to signed 32-bit
43
+ h >= 0x80000000 ? h - 0x100000000 : h
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,60 @@
1
+ module AsciidoctorDiagramLayout
2
+ module Renderer
3
+ class HtmlRenderer
4
+ CELL_BASE = "display:flex; align-items:center; justify-content:center;" \
5
+ " padding:8px; font-weight:bold; font-family:sans-serif;" \
6
+ " min-height:60px; box-sizing:border-box; color:#333;"
7
+
8
+ def render(root, options = RenderOptions.new)
9
+ sb = +""
10
+ height_style = options.height ? " min-height:#{options.height};" : ""
11
+ wrapper_style = "display:flex; width:#{options.width};#{height_style}" \
12
+ " box-sizing:border-box; font-family:sans-serif; overflow:hidden;"
13
+ sb << "<div style=\"#{wrapper_style}\">\n"
14
+ render_node(root, sb, 1, options)
15
+ sb << "</div>\n"
16
+ sb
17
+ end
18
+
19
+ private
20
+
21
+ def render_node(node, sb, depth, options)
22
+ case node
23
+ when ContainerNode then render_container(node, sb, depth, options)
24
+ when CellNode then render_cell(node, sb, depth, options)
25
+ end
26
+ end
27
+
28
+ def render_container(container, sb, depth, options)
29
+ flex_dir = container.direction == :cols ? "row" : "column"
30
+ style = "display:flex; flex-direction:#{flex_dir}; #{size_style(container.size)}" \
31
+ " align-items:stretch; box-sizing:border-box;"
32
+ indent(sb, depth)
33
+ sb << "<div style=\"#{style}\">\n"
34
+ container.children.each { |child| render_node(child, sb, depth + 1, options) }
35
+ indent(sb, depth)
36
+ sb << "</div>\n"
37
+ end
38
+
39
+ def render_cell(cell, sb, depth, options)
40
+ scheme = options.color_scheme
41
+ color = scheme.fill_color(cell.name)
42
+ grad_end = scheme.gradient_end(cell.name)
43
+ stroke = scheme.stroke_color(cell.name)
44
+ gradient = "linear-gradient(135deg,#{color},#{grad_end})"
45
+ style = "#{CELL_BASE} #{size_style(cell.size)}" \
46
+ " background:#{gradient}; border:1px solid #{stroke};"
47
+ indent(sb, depth)
48
+ sb << "<div style=\"#{style}\">#{options.name_converter.call(cell.name)}</div>\n"
49
+ end
50
+
51
+ def size_style(size)
52
+ size == :auto ? "flex:1;" : "flex:0 0 #{size}%; box-sizing:border-box;"
53
+ end
54
+
55
+ def indent(sb, depth)
56
+ sb << " " * depth
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,22 @@
1
+ module AsciidoctorDiagramLayout
2
+ module Renderer
3
+ class RenderOptions
4
+ attr_reader :width, :height, :color_scheme, :name_converter
5
+
6
+ def initialize(width: "100%", height: nil, palette: "rainbow", pdf: false,
7
+ color_scheme: nil, name_converter: nil)
8
+ @width = width
9
+ @height = height
10
+ @color_scheme = color_scheme || Scheme::CellColorSchemeFactory.resolve(palette, pdf: pdf)
11
+ @name_converter = name_converter || method(:escape)
12
+ end
13
+
14
+ private
15
+
16
+ def escape(text)
17
+ return "" if text.nil?
18
+ text.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ module AsciidoctorDiagramLayout
2
+ module Renderer
3
+ module Scheme
4
+ class AnalogousScheme
5
+ GRADIENT_HUE_SHIFT = 15
6
+ STROKE_LIGHTNESS = 65
7
+ STROKE_SATURATION = 25
8
+
9
+ def initialize(base_hue, hue_range, saturation, lightness)
10
+ @base_hue = base_hue
11
+ @hue_range = hue_range
12
+ @saturation = saturation
13
+ @lightness = lightness
14
+ end
15
+
16
+ def fill_color(name)
17
+ ColorPalette.hsl_to_hex(hue(name), @saturation, @lightness)
18
+ end
19
+
20
+ def gradient_end(name)
21
+ ColorPalette.hsl_to_hex((hue(name) + GRADIENT_HUE_SHIFT) % ColorPalette::HUE_RANGE,
22
+ @saturation, @lightness)
23
+ end
24
+
25
+ def stroke_color(name)
26
+ ColorPalette.hsl_to_hex(hue(name), STROKE_SATURATION, STROKE_LIGHTNESS)
27
+ end
28
+
29
+ private
30
+
31
+ def hue(name)
32
+ hash = ColorPalette.java_hash(name).abs
33
+ offset = (hash * ColorPalette::GOLDEN_RATIO % 1.0) * @hue_range
34
+ (@base_hue + offset.to_i) % ColorPalette::HUE_RANGE
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,38 @@
1
+ module AsciidoctorDiagramLayout
2
+ module Renderer
3
+ module Scheme
4
+ module CellColorSchemeFactory
5
+ WARM_BASE_HUE = 30
6
+ COOL_BASE_HUE = 210
7
+ ANALOGOUS_HUE_RANGE = 60
8
+ ANALOGOUS_SATURATION = 25
9
+ ANALOGOUS_LIGHTNESS = 86
10
+ MONO_HUE = 210
11
+ MONO_SATURATION = 20
12
+ DEFAULT_SATURATION = 22
13
+ DEFAULT_LIGHTNESS = 88
14
+ PDF_SATURATION = 15
15
+ PDF_LIGHTNESS = 90
16
+ PASTEL_SATURATION = 18
17
+ PASTEL_LIGHTNESS = 93
18
+
19
+ def self.resolve(name, pdf: false)
20
+ key = name.to_s.downcase
21
+ case key
22
+ when "pastel"
23
+ GoldenRatioScheme.new(PASTEL_SATURATION, PASTEL_LIGHTNESS)
24
+ when "warm"
25
+ AnalogousScheme.new(WARM_BASE_HUE, ANALOGOUS_HUE_RANGE, ANALOGOUS_SATURATION, ANALOGOUS_LIGHTNESS)
26
+ when "cool"
27
+ AnalogousScheme.new(COOL_BASE_HUE, ANALOGOUS_HUE_RANGE, ANALOGOUS_SATURATION, ANALOGOUS_LIGHTNESS)
28
+ when "mono"
29
+ MonochromaticScheme.new(MONO_HUE, MONO_SATURATION)
30
+ else
31
+ pdf ? GoldenRatioScheme.new(PDF_SATURATION, PDF_LIGHTNESS)
32
+ : GoldenRatioScheme.new(DEFAULT_SATURATION, DEFAULT_LIGHTNESS)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,36 @@
1
+ module AsciidoctorDiagramLayout
2
+ module Renderer
3
+ module Scheme
4
+ class GoldenRatioScheme
5
+ GRADIENT_HUE_SHIFT = 20
6
+ STROKE_LIGHTNESS = 65
7
+ STROKE_SATURATION = 30
8
+
9
+ def initialize(saturation, lightness)
10
+ @saturation = saturation
11
+ @lightness = lightness
12
+ end
13
+
14
+ def fill_color(name)
15
+ ColorPalette.hsl_to_hex(hue(name), @saturation, @lightness)
16
+ end
17
+
18
+ def gradient_end(name)
19
+ ColorPalette.hsl_to_hex((hue(name) + GRADIENT_HUE_SHIFT) % ColorPalette::HUE_RANGE,
20
+ @saturation, @lightness)
21
+ end
22
+
23
+ def stroke_color(name)
24
+ ColorPalette.hsl_to_hex(hue(name), STROKE_SATURATION, STROKE_LIGHTNESS)
25
+ end
26
+
27
+ private
28
+
29
+ def hue(name)
30
+ hash = ColorPalette.java_hash(name).abs
31
+ ((hash * ColorPalette::GOLDEN_RATIO % 1.0) * ColorPalette::HUE_RANGE).to_i
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ module AsciidoctorDiagramLayout
2
+ module Renderer
3
+ module Scheme
4
+ class MonochromaticScheme
5
+ LIGHTNESS_MIN = 75
6
+ LIGHTNESS_MAX = 95
7
+ GRADIENT_SHIFT = 5
8
+ STROKE_LIGHTNESS = 60
9
+ STROKE_SATURATION = 20
10
+
11
+ def initialize(hue, saturation)
12
+ @hue = hue
13
+ @saturation = saturation
14
+ end
15
+
16
+ def fill_color(name)
17
+ ColorPalette.hsl_to_hex(@hue, @saturation, lightness(name))
18
+ end
19
+
20
+ def gradient_end(name)
21
+ l = [lightness(name) + GRADIENT_SHIFT, 98].min
22
+ ColorPalette.hsl_to_hex(@hue, @saturation, l)
23
+ end
24
+
25
+ def stroke_color(name)
26
+ ColorPalette.hsl_to_hex(@hue, STROKE_SATURATION, STROKE_LIGHTNESS)
27
+ end
28
+
29
+ private
30
+
31
+ def lightness(name)
32
+ hash = ColorPalette.java_hash(name).abs
33
+ range = LIGHTNESS_MAX - LIGHTNESS_MIN
34
+ LIGHTNESS_MIN + (hash % (range + 1))
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,137 @@
1
+ module AsciidoctorDiagramLayout
2
+ module Renderer
3
+ class SvgRenderer
4
+ DEFAULT_WIDTH = 600
5
+ DEFAULT_HEIGHT = 300
6
+ FONT_SIZE = 14
7
+
8
+ def render(root, options = RenderOptions.new)
9
+ w = parse_px(options.width, DEFAULT_WIDTH)
10
+ h = parse_px(options.height, DEFAULT_HEIGHT)
11
+ defs = +""
12
+ body = +""
13
+ render_node(root, defs, body, options.color_scheme, 0, 0, w, h)
14
+ sb = +""
15
+ sb << "<svg xmlns=\"http://www.w3.org/2000/svg\""
16
+ sb << " width=\"#{w}\""
17
+ sb << " height=\"#{h}\""
18
+ sb << " style=\"font-family:sans-serif;\""
19
+ sb << ">\n"
20
+ if defs.length > 0
21
+ sb << " <defs>\n" << defs << " </defs>\n"
22
+ end
23
+ sb << body
24
+ sb << "</svg>\n"
25
+ sb
26
+ end
27
+
28
+ private
29
+
30
+ def parse_px(value, default_value)
31
+ return default_value if value.nil?
32
+ trimmed = value.end_with?("px") ? value[0..-3] : value
33
+ Integer(trimmed.strip)
34
+ rescue ArgumentError
35
+ default_value
36
+ end
37
+
38
+ def render_node(node, defs, body, scheme, x, y, w, h)
39
+ case node
40
+ when ContainerNode then render_container(node, defs, body, scheme, x, y, w, h)
41
+ when CellNode then render_cell(node, defs, body, scheme, x, y, w, h)
42
+ end
43
+ end
44
+
45
+ def render_container(container, defs, body, scheme, x, y, w, h)
46
+ children = container.children
47
+ total = container.direction == :cols ? w : h
48
+ sizes = distribute_pixels(children, total)
49
+ offset = 0
50
+ children.each_with_index do |child, i|
51
+ cx = container.direction == :cols ? x + offset : x
52
+ cy = container.direction == :cols ? y : y + offset
53
+ cw = container.direction == :cols ? sizes[i] : w
54
+ ch = container.direction == :cols ? h : sizes[i]
55
+ render_node(child, defs, body, scheme, cx, cy, cw, ch)
56
+ offset += sizes[i]
57
+ end
58
+ end
59
+
60
+ def render_cell(cell, defs, body, scheme, x, y, w, h)
61
+ label = extract_label(cell.name)
62
+ color = scheme.fill_color(cell.name)
63
+ light = scheme.gradient_end(cell.name)
64
+ stroke = scheme.stroke_color(cell.name)
65
+ grad_id = "g#{x}-#{y}"
66
+ defs << " <linearGradient id=\"#{grad_id}\""
67
+ defs << " gradientUnits=\"userSpaceOnUse\""
68
+ defs << " x1=\"#{x}\" y1=\"#{y}\""
69
+ defs << " x2=\"#{x + w}\" y2=\"#{y + h}\">\n"
70
+ defs << " <stop offset=\"0%\" stop-color=\"#{color}\"/>\n"
71
+ defs << " <stop offset=\"100%\" stop-color=\"#{light}\"/>\n"
72
+ defs << " </linearGradient>\n"
73
+ body << " <rect"
74
+ body << " x=\"#{x}\""
75
+ body << " y=\"#{y}\""
76
+ body << " width=\"#{w}\""
77
+ body << " height=\"#{h}\""
78
+ body << " fill=\"url(##{grad_id})\""
79
+ body << " stroke=\"#{stroke}\""
80
+ body << " stroke-width=\"1\""
81
+ body << "/>\n"
82
+ tx = x + w / 2
83
+ ty = y + h / 2
84
+ body << " <text"
85
+ body << " x=\"#{tx}\""
86
+ body << " y=\"#{ty}\""
87
+ body << " text-anchor=\"middle\""
88
+ body << " dominant-baseline=\"middle\""
89
+ body << " font-size=\"#{FONT_SIZE}\""
90
+ body << " font-weight=\"bold\""
91
+ body << " fill=\"#333\""
92
+ body << ">#{escape_xml(label)}</text>\n"
93
+ end
94
+
95
+ def distribute_pixels(children, total_pixels)
96
+ fixed_sum = 0
97
+ auto_count = 0
98
+ children.each do |child|
99
+ s = child.size
100
+ s == :auto ? auto_count += 1 : fixed_sum += s
101
+ end
102
+ remaining_percent = [0, 100 - fixed_sum].max
103
+ auto_percent = auto_count > 0 ? remaining_percent / auto_count : 0
104
+ pixels = children.map do |child|
105
+ pct = child.size == :auto ? auto_percent : child.size
106
+ total_pixels * pct / 100
107
+ end
108
+ used = pixels.sum
109
+ remainder = total_pixels - used
110
+ if remainder != 0
111
+ (children.size - 1).downto(0) do |i|
112
+ if children[i].size == :auto
113
+ pixels[i] += remainder
114
+ break
115
+ end
116
+ end
117
+ end
118
+ pixels
119
+ end
120
+
121
+ def extract_label(name)
122
+ return "" if name.nil?
123
+ name.gsub(/xref:[^\[]*\[([^\]]*)\]/, '\1')
124
+ .gsub(/\{[^}]*\}/, "")
125
+ .strip
126
+ end
127
+
128
+ def escape_xml(text)
129
+ return "" if text.nil?
130
+ text.gsub("&", "&amp;")
131
+ .gsub("<", "&lt;")
132
+ .gsub(">", "&gt;")
133
+ .gsub('"', "&quot;")
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,3 @@
1
+ module AsciidoctorDiagramLayout
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asciidoctor-diagram-layout
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Pechinsky
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: asciidoctor
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ description: Provides a [layout] block for Asciidoctor that renders layout diagrams
41
+ as HTML or SVG.
42
+ email: anton@pechinsky.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - README.md
49
+ - lib/asciidoctor-diagram-layout.rb
50
+ - lib/asciidoctor_diagram_layout/asciidoc/layout_block_processor.rb
51
+ - lib/asciidoctor_diagram_layout/parser.rb
52
+ - lib/asciidoctor_diagram_layout/renderer/color_palette.rb
53
+ - lib/asciidoctor_diagram_layout/renderer/html_renderer.rb
54
+ - lib/asciidoctor_diagram_layout/renderer/render_options.rb
55
+ - lib/asciidoctor_diagram_layout/renderer/scheme/analogous_scheme.rb
56
+ - lib/asciidoctor_diagram_layout/renderer/scheme/cell_color_scheme_factory.rb
57
+ - lib/asciidoctor_diagram_layout/renderer/scheme/golden_ratio_scheme.rb
58
+ - lib/asciidoctor_diagram_layout/renderer/scheme/monochromatic_scheme.rb
59
+ - lib/asciidoctor_diagram_layout/renderer/svg_renderer.rb
60
+ - lib/asciidoctor_diagram_layout/version.rb
61
+ homepage: https://github.com/apechinsky/asciidoctor-diagram-layout
62
+ licenses:
63
+ - Apache-2.0
64
+ metadata: {}
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.6.9
80
+ specification_version: 4
81
+ summary: Asciidoctor extension for rendering flex layout diagrams
82
+ test_files: []