Skip to content

Commit

Permalink
add multiline raw parsing and fix components config
Browse files Browse the repository at this point in the history
  • Loading branch information
SampsonCrowley committed Apr 30, 2019
1 parent a60ce31 commit f80b85c
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 27 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ Here are the names of the defaults:
}
```

## Raw HTML

If you need to include raw html, you can wrap raw content in `<raw>` tags

```html
<raw>
<button></button>
<asdf></asdf>
</raw>
```

This is a feature intended for advanced users. You will be responsible for ensuring all markup between `<raw>` tags is valid. All content after an opening `<raw>` tag will be inserted "As Is" until the next closing `</raw>` tag.

This means that `<raw>` tags CANNOT be nested

## Programmatic Use

The Inky parser can be accessed directly for programmatic use.
Expand Down
4 changes: 2 additions & 2 deletions lib/inky.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(options = {})
spacer: 'spacer',
wrapper: 'wrapper',
menu_item: 'item'
}.merge(options[:components] || {})
}.merge(::Inky.configuration.components).merge(options[:components] || {})

self.component_lookup = components.invert

Expand Down Expand Up @@ -58,7 +58,7 @@ def transform_doc(elem)
def self.extract_raws(string)
raws = []
i = 0
regex = %r(< *raw *>(.*?)</ *raw *>)
regex = %r(<\s*raw\s*>((?!</\s*raw\s*>).*?)</\s*raw\s*>)misux
str = string
while raw = str.match(regex)
raws[i] = raw[1]
Expand Down
17 changes: 15 additions & 2 deletions lib/inky/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.configuration
# Set Inky's configuration
# @param config [Inky::Configuration]
def self.configuration=(config)
@configuration = config
@configuration = config if config.is_a?(Configuration)
end

# Modify Inky's current configuration
Expand All @@ -23,11 +23,24 @@ def self.configure
end

class Configuration
attr_accessor :template_engine, :column_count
attr_reader :template_engine, :column_count, :components

def initialize
@template_engine = :erb
@column_count = 12
@components = {}
end

def template_engine=(value)
@template_engine = value.to_sym
end

def components=(value)
@components = value if value.is_a?(Hash)
end

def column_count=(value)
@column_count = value if value.is_a?(Integer)
end
end
end
2 changes: 1 addition & 1 deletion lib/inky/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Inky
module Rails
VERSION = '1.3.7.5'.freeze
VERSION = '1.3.7.6'.freeze
end
NODE_VERSION, GEM_VERSION = Rails::VERSION.rpartition('.').map(&:freeze)
end
39 changes: 39 additions & 0 deletions spec/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,43 @@
output = inky.release_the_kraken(input)
expect(output).to eql(expected)
end

it 'works on multiple lines' do
input = <<-HTML
<body>
<raw>
<<LCG ProgramTG LCG Coupon Code Default='246996'>>\
<button>
</raw>
</body>
HTML

# Can't do vanilla compare because the second will fail to parse
inky = Inky::Core.new
output = inky.release_the_kraken(input)
expect(output).to include("<<LCG ProgramTG LCG Coupon Code Default='246996'>>")
expect(output).to include("<button>")
expect(output).to_not include('raw')
expect(output).to_not include('<table class="button">')
end

it 'stops at the first </raw> tag' do
input = <<-HTML
<body>
<raw>
<<LCG ProgramTG LCG Coupon Code Default='246996'>>
</raw>
<button href="#">Test</button>
</raw>
</body>
HTML
expected = "<<LCG ProgramTG LCG Coupon Code Default='246996'>>"

# Can't do vanilla compare because the second will fail to parse
inky = Inky::Core.new
output = inky.release_the_kraken(input)
expect(output).to include(expected)
expect(output).to include('<table class="button">')
expect(output).to_not include('<button>')
end
end
108 changes: 86 additions & 22 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,108 @@
require 'spec_helper'

RSpec.describe "Configuration" do
around do |spec|
Inky.configure do |config|
old = config.template_engine
spec.run
config.template_engine = old
RSpec.describe "Inky.configuration" do
it "returns an Inky::Configuration object" do
current_config = Inky.configuration
expect(current_config).to be_an_instance_of Inky::Configuration
end

describe "=" do
it "accepts an Inky::Configuration" do
current_config = Inky.configuration
config = Inky::Configuration.new
Inky.configuration = config
expect(Inky.configuration).to_not eq(current_config)
expect(Inky.configuration).to eq(config)

Inky.configuration = {}
expect(Inky.configuration).to eq(config)

Inky.configuration = current_config
expect(Inky.configuration).to eq(current_config)
end
end

it "default value is :erb" do
Inky::Configuration.new.template_engine = :erb
describe "&block" do
it "returns the yields the current configuration" do
current_config = Inky.configuration
new_config = Inky::Configuration.new
Inky.configuration = new_config

Inky.configuration do |config|
expect(config).to be_an_instance_of Inky::Configuration
expect(config).to_not eq(current_config)
expect(config).to eq(new_config)

config.column_count = 24
expect(new_config.column_count).to eq(24)
end

Inky.configuration = current_config
end
end
end

RSpec.describe "Configuration" do
describe "#template_engine" do
it "default value is :erb" do
expect(Inky::Configuration.new.template_engine).to eq(:erb)
end
end

describe "#configuration=" do
it "can set template_engine" do
describe "#template_engine=" do
it "sets/updates the template_engine" do
config = Inky::Configuration.new
config.template_engine = :haml
expect(config.template_engine).to eq(:haml)
end
end

describe "#column_count" do
it "default value is :erb" do
expect(Inky::Configuration.new.column_count).to eq(12)
end
end

describe "#column_count=" do
it "sets/updates the column_count" do
config = Inky::Configuration.new
config.column_count = 24
expect(config.column_count).to eq(24)
end

it "can set column_count" do
it "accepts integers" do
config = Inky::Configuration.new
config.column_count = 4
expect(config.column_count).to eq(4)
config.column_count = :haml
expect(config.column_count).to eq(12)
end
end

describe "#configuration=" do
before do
Inky.configure do |config|
config.template_engine = :haml
end
describe "#components" do
it "defaults to an empty hash" do
config = Inky::Configuration.new
expect(config.components).to eq({})
end
end

it "returns :haml as configured template_engine" do
template_engine = Inky.configuration.template_engine
describe "#components=" do
it "can set overriden component tags" do
config = Inky::Configuration.new
config.components = { button: 'inky-button' }
expect(config.components).to eq(button: 'inky-button')
end

expect(template_engine).to be_a(Symbol)
expect(template_engine).to eq(:haml)
it "will not set an invalid components override" do
config = Inky::Configuration.new
[
nil,
1,
"{}",
false,
true
].each do |v|
config.components = v
expect(config.components).to eq({})
end
end
end
end

0 comments on commit f80b85c

Please sign in to comment.