Skip to content

Commit

Permalink
Merge pull request #83 from SwedbankPay/feature/dx-914_scale
Browse files Browse the repository at this point in the history
DX-914: Add `:scale` option
  • Loading branch information
asbjornu authored Nov 18, 2021
2 parents 458b401 + 0cfd817 commit 5b73a67
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 43 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,26 @@ kramdown:

### Dimensions and Styling

It's possible to customize the dimensions of the diagram by providing the
`width` and `height` configuration keys. It's also possible to add arbitrary
styling with the `style` key.
It's possible to customize the dimensions and scale of the diagram by providing
the `width`, `height` and `scale` configuration keys. It's also possible to add
arbitrary styling with the `style` key.

`scale` is applied before the diagram is generated, while `width` and `height`
are applied after, meaning they can be combined (to most likely detrimental
results, but YOLO).

```yaml
kramdown:
plantuml:
width: 200px
height: 100px
scale: 2
style: "border: 1px solid black"
```

To remove the `width`, `height` and `style` attributes from the `<svg />`
element, set the key's value to `none`.
element, set the key's value to `none`. `scale` does not support a value of
`none` as it does not need to be removed.

```yaml
kramdown:
Expand Down
11 changes: 6 additions & 5 deletions lib/kramdown-plantuml/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Kramdown
module PlantUml
# Options for PlantUML processing
class Options
attr_reader :theme_name, :theme_directory, :width, :height, :style
attr_reader :theme_name, :theme_directory, :width, :height, :style, :scale

def initialize(options_hash = {})
@logger = LogWrapper.init
Expand Down Expand Up @@ -72,14 +72,15 @@ def extract_style_options(options)
set_instance_property(:width, options)
set_instance_property(:height, options)
set_instance_property(:style, options)
set_instance_property(:scale, options)
end

def set_instance_property(name, options)
return unless options.key? name
def set_instance_property(key, options)
return unless options.key? key

value = options[name]
value = options[key]
value = :none if value.none_s?
prop_name = "@#{name}".to_sym
prop_name = "@#{key}".to_sym
instance_variable_set(prop_name, value)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/kramdown-plantuml/plantuml_diagram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def svg
return @svg_diagram unless @svg_diagram.nil?

@plantuml = @theme.apply(@plantuml)
log(plantuml)
log(@plantuml)
@result = @executor.execute(self)
@svg_diagram = SvgDiagram.new(@result)
rescue StandardError => e
Expand Down
31 changes: 19 additions & 12 deletions lib/kramdown-plantuml/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Kramdown
module PlantUml
# Provides theming support for PlantUML
class Theme
attr_reader :name, :directory
attr_reader :name, :directory, :scale

def initialize(options)
raise ArgumentError, 'options cannot be nil' if options.nil?
Expand All @@ -17,6 +17,7 @@ def initialize(options)
@raise_errors = options.raise_errors?
@logger = LogWrapper.init
@name = options.theme_name
@scale = options.scale
@directory = resolve options.theme_directory
end

Expand All @@ -26,11 +27,6 @@ def apply(plantuml)
return plantuml
end

if @name.nil? || @name.empty?
@logger.debug 'No theme to apply.'
return plantuml.strip
end

theme(plantuml)
end

Expand Down Expand Up @@ -59,13 +55,12 @@ def log_or_raise(message)
end

def theme(plantuml)
startuml = '@startuml'
startuml_index = plantuml.index(startuml) + startuml.length
theme_string = build_theme_string

return plantuml if startuml_index.nil?

theme_string = "\n!theme #{@name}"
theme_string << " from #{@directory}" unless @directory.nil?
if theme_string.empty?
@logger.debug 'No theme to apply.'
return plantuml
end

@logger.debug "Applying #{theme_string.strip}"

Expand All @@ -75,6 +70,18 @@ def theme(plantuml)

plantuml.strip
end

def build_theme_string
theme_string = ''

unless @name.nil? || @name.empty?
theme_string << "\n!theme #{@name}"
theme_string << " from #{@directory}" unless @directory.nil?
end

theme_string << "\nscale #{@scale}" unless @scale.nil?
theme_string
end
end
end
end
30 changes: 18 additions & 12 deletions spec/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
its(:theme_name) { is_expected.to be_nil }
its(:theme_directory) { is_expected.to be_nil }
its(:raise_errors?) { is_expected.to be true }
its(:scale) { is_expected.to be_nil }
its(:to_h) { is_expected.to eq({ }) }
end

context 'empty hash' do
its(:theme_name) { is_expected.to be_nil }
its(:theme_directory) { is_expected.to be_nil }
its(:raise_errors?) { is_expected.to be true }
its(:scale) { is_expected.to be_nil }
its(:to_h) { is_expected.to eq({ }) }
end

Expand All @@ -29,6 +31,7 @@
its(:theme_name) { is_expected.to be_nil }
its(:theme_directory) { is_expected.to be_nil }
its(:raise_errors?) { is_expected.to be true }
its(:scale) { is_expected.to be_nil }
its(:to_h) { is_expected.to eq({ }) }
end

Expand Down Expand Up @@ -73,26 +76,29 @@
end

context 'with symbolic option keys' do
let(:hash) { { plantuml: { theme: { name: 'custom', directory: 'path/to/themes' }, raise_errors: false } } }
its(:theme_name) { is_expected.to eq('custom') }
its(:theme_directory) { is_expected.to eq('path/to/themes') }
let(:hash) { { plantuml: { theme: { name: 'custom', directory: 'path/to/themes' }, raise_errors: false, scale: 0.8 } } }
its(:theme_name) { is_expected.to eq 'custom' }
its(:theme_directory) { is_expected.to eq 'path/to/themes' }
its(:raise_errors?) { is_expected.to be false }
its(:to_h) { is_expected.to eq({ theme: { name: 'custom', directory: 'path/to/themes'}, raise_errors: false }) }
its(:scale) { is_expected.to eq 0.8 }
its(:to_h) { is_expected.to eq({ theme: { name: 'custom', directory: 'path/to/themes'}, raise_errors: false, scale: 0.8 }) }
end

context 'with mixed option keys' do
let(:hash) { { plantuml: { theme: { 'name' => 'custom', 'directory' => 'path/to/themes' }, 'raise_errors' => false } } }
its(:theme_name) { is_expected.to eq('custom') }
its(:theme_directory) { is_expected.to eq('path/to/themes') }
let(:hash) { { plantuml: { theme: { 'name' => 'custom', 'directory' => 'path/to/themes' }, 'raise_errors' => false, scale: '0.8' } } }
its(:theme_name) { is_expected.to eq 'custom' }
its(:theme_directory) { is_expected.to eq 'path/to/themes' }
its(:raise_errors?) { is_expected.to be false }
its(:to_h) { is_expected.to eq({ theme: { name: 'custom', directory: 'path/to/themes'}, raise_errors: false }) }
its(:scale) { is_expected.to eq '0.8' }
its(:to_h) { is_expected.to eq({ theme: { name: 'custom', directory: 'path/to/themes'}, raise_errors: false, scale: '0.8' }) }
end

context 'with string option keys' do
let(:hash) { { 'plantuml' => { 'theme' => { 'name' => 'custom', 'directory' => 'path/to/themes' }, 'raise_errors' => false } } }
its(:theme_name) { is_expected.to eq('custom') }
its(:theme_directory) { is_expected.to eq('path/to/themes') }
let(:hash) { { 'plantuml' => { 'theme' => { 'name' => 'custom', 'directory' => 'path/to/themes' }, 'raise_errors' => false, 'scale' => '0.8' } } }
its(:theme_name) { is_expected.to eq 'custom' }
its(:theme_directory) { is_expected.to eq 'path/to/themes' }
its(:raise_errors?) { is_expected.to be false }
its(:to_h) { is_expected.to eq({ theme: { name: 'custom', directory: 'path/to/themes' }, raise_errors: false })}
its(:scale) { is_expected.to eq '0.8' }
its(:to_h) { is_expected.to eq({ theme: { name: 'custom', directory: 'path/to/themes' }, raise_errors: false, scale: '0.8' })}
end
end
27 changes: 18 additions & 9 deletions spec/theme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
subject { Theme.new(options) }

context 'with symbolic option keys' do
let(:options) { Options.new({ plantuml: { theme: { name: 'custom', directory: 'path/to/themes' } } }) }
let(:options) { Options.new({ plantuml: { theme: { name: 'custom', directory: 'path/to/themes' }, scale: 0.8 } }) }
its(:name) { is_expected.to eq('custom') }
its(:directory) { is_expected.to eq('path/to/themes') }
its(:scale) { is_expected.to eq(0.8) }
end

context 'with mixed option keys' do
let(:options) { Options.new({ plantuml: { theme: { 'name' => 'custom', 'directory' => 'path/to/themes' } } }) }
let(:options) { Options.new({ plantuml: { theme: { 'name' => 'custom', 'directory' => 'path/to/themes' }, scale: '0.8' } }) }
its(:name) { is_expected.to eq('custom') }
its(:directory) { is_expected.to eq('path/to/themes') }
its(:scale) { is_expected.to eq('0.8') }
end

context 'with string option keys' do
let(:options) { Options.new({ 'plantuml' => { 'theme' => { 'name' => 'custom', 'directory' => 'path/to/themes' } } }) }
let(:options) { Options.new({ 'plantuml' => { 'theme' => { 'name' => 'custom', 'directory' => 'path/to/themes' }, 'scale' => '0.8' } }) }
its(:name) { is_expected.to eq('custom') }
its(:directory) { is_expected.to eq('path/to/themes') }
its(:scale) { is_expected.to eq('0.8') }
end
end

Expand All @@ -47,20 +50,26 @@
end

context 'with simple plantuml' do
let(:plantuml) { "@startuml\nactor A\nend" }
it { is_expected.to eq("@startuml\nactor A\nend") }
let(:plantuml) { "@startuml\nactor A\n@enduml" }
it { is_expected.to eq("@startuml\nactor A\n@enduml") }
end

context 'with built-in theme' do
let(:options) { Options.new({ plantuml: { theme: { name: 'spacelab' } } }) }
let(:plantuml) { "@startuml\nactor A\nend" }
it { is_expected.to eq("@startuml\n!theme spacelab\nactor A\nend") }
let(:plantuml) { "@startuml\nactor A\n@enduml" }
it { is_expected.to eq("@startuml\n!theme spacelab\nactor A\n@enduml") }
end

context 'with custom theme' do
let(:options) { Options.new({ plantuml: { theme: { name: 'custom', directory: 'path/to/themes' } } }) }
let(:plantuml) { "@startuml\nactor A\nend" }
it { is_expected.to eq("@startuml\n!theme custom from path/to/themes\nactor A\nend") }
let(:plantuml) { "@startuml\nactor A\n@enduml" }
it { is_expected.to eq("@startuml\n!theme custom from path/to/themes\nactor A\n@enduml") }
end

context 'with scale' do
let(:options) { Options.new({ plantuml: { scale: 0.8 } }) }
let(:plantuml) { "@startuml\nactor A\n@enduml" }
it { is_expected.to eq("@startuml\nscale 0.8\nactor A\n@enduml") }
end
end
end

0 comments on commit 5b73a67

Please sign in to comment.