-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
locals.with_indifferent_access
is called twice
#2
Comments
If I understand correctly, you're using I'm thinking of adding a method to You can still mutate This won't break any existing usage of the gem and I think it will fix your problem. What do you think? @stex |
@tobyhinloopen Thanks for your quick response!
Exactly. I need to not only replace the BBCodes, but also make the original information available in the scope which called the BBCode parsing. I think that would indeed help me. I'd still have to mutate the locals as each tag might behave a bit differently and the result therefore could not be created purely in the An example of what I'm currently doing: Bbcode.register_handler :enhanced_post, Bbcode::PostHandler.new(
html: lambda {|elem, locals|
"#{locals[:placeholder_prefix]}_#{locals[:placeholder_idx] += 1}".tap do |placeholder|
(locals[:elements] ||= {})[placeholder] = {
content: elem.content,
attributes: elem.attributes
}
end
}
)
[1] pry(main)> s = "asdf\n[html id='asdf']asdfasdfasdfasdf[/html]\nasdfasdf"
=> "asdf\n[html id='asdf']asdfasdfasdfasdf[/html]\nasdfasdf"
[2] pry(main)> ll = {placeholder_prefix: 'PLACEHOLDER', placeholder_idx: 0}
=> {:placeholder_prefix=>"PLACEHOLDER", :placeholder_idx=>0}
[3] pry(main)> s.as_bbcode.to :enhanced_post, ll
=> "asdf\nPLACEHOLDER_1\nasdfasdf"
[4] pry(main)> ll
=> {:placeholder_prefix=>"PLACEHOLDER", :placeholder_idx=>1, :elements=>{"PLACEHOLDER_1"=>{:content=>["asdfasdfasdfasdf"], :attributes=>{"id"=>"asdf"}}}} |
See branch |
@tobyhinloopen This works fine for me, I basically just removed my monkey-patched version of I had to change my handler slightly, even though I'm not yet sure why: (locals[:elements] ||= {})[placeholder] = {} lead to the locals having the following key/value pair: The following code however works completely fine, I'll have to look into that sometime, might be related to locals[:elements] ||= {}
locals[:elements][placeholder] = {} |
Hey!
I hope you haven't abandoned the gem yet as it is really useful due to its customisability :)
I'm currently using it for a case which is not exactly a default configuration: I need to extract BBCodes from a string and replace them with a placeholder while capturing the content, e.g.
The main problem here is that the locals are not passed to the elements as is, but rather after being converted to a
HashWithIndifferentAccess
- and that's done twice:lib/bbcode/handler.rb#locals
lib/bbcode/base.rb#to
As I've written my own handler, it's quite easy to override the
locals=
method to use the original hash, but as it's done again in#to
, I need to actually monkey-patch the method in my application.Wouldn't it be sufficient to completely rely on the handler's attribute setter?
The text was updated successfully, but these errors were encountered: