Running with Ruby

Tag: gemset (page 1 of 2)

Invalid byte sequence in US-ASCII (ArgumentError) with Ruby on Rails

Lately when starting Unicorn instances, I’ve encountered following issue:

$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D
/gems_path/lib/bundler.rb:294:in `block in load_gemspec_uncached': 
  invalid byte sequence in US-ASCII (ArgumentError)
	from /gems_path/lib/bundler.rb:291:in `chdir'
	from /gems_path/lib/bundler.rb:291:in `load_gemspec_uncached'
	from /gems_path/lib/bundler.rb:282:in `load_gemspec'
	from /gems_path/lib/bundler/source.rb:411:in `block in load_spec_files'
	from /gems_path/lib/bundler/source.rb:410:in `each'
	from /gems_path/lib/bundler/source.rb:410:in `load_spec_files'
	from /gems_path/lib/bundler/source.rb:799:in `load_spec_files'
	from /gems_path/lib/bundler/source.rb:381:in `local_specs'
	from /gems_path/lib/bundler/source.rb:774:in `specs'
	from /gems_path/lib/bundler/lazy_specification.rb:53:in `__materialize__'
	from /gems_path/lib/bundler/spec_set.rb:86:in `block in materialize'
	from /gems_path/lib/bundler/spec_set.rb:83:in `map!'
	from /gems_path/lib/bundler/spec_set.rb:83:in `materialize'
	from /gems_path/lib/bundler/definition.rb:113:in `specs'
	from /gems_path/lib/bundler/definition.rb:158:in `specs_for'
	from /gems_path/lib/bundler/definition.rb:147:in `requested_specs'
	from /gems_path/lib/bundler/environment.rb:23:in `requested_specs'
	from /gems_path/lib/bundler/runtime.rb:11:in `setup'
	from /gems_path/lib/bundler.rb:116:in `setup'
	from /gems_path/lib/bundler/setup.rb:7:in `<top (required)>'
	from /gems_path/rubygems/custom_require.rb:36:in `require'
	from /gems_path/rubygems/custom_require.rb:36:in `require'

There are two ways to fix this. You can export language settings to your shell:

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

or you can put this into your Gemfile:

if RUBY_VERSION =~ /1.9/
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
end

Both methods should work.

Easy way to extend and overwrite bb-ruby bbcode gem translations in Ruby on Rails

When you go to bb-ruby gem readme, there is an instruction how to extend it with your own translations:

my_block = {
  'Quote' => [
    /\[quote(:.*)?=(.*?)\](.*?)\[\/quote\1?\]/mi,
    '<div class="quote"><p>\2</p><block>\3</block></div>',
    'Quote with citation',
    '[quote=mike]please quote me[/quote]',
    :quote
  ],      
}

text.bbcode_to_html(my_blockquote)

This method works quite good but well… it’s way to inconvenient. Where should we put our translation? In our model? Should we create a different model which would store our costume translations? Nah… There is really easy and clean way to add new (and overwrite already existing) translations into a bbcode parser.

Create in your app initializers directory (/config/initializers/) a file called bb-ruby.rb. Below you can see an example with [spoiler][/spoiler] tag:

module BBRuby
  if @@tags
    @@tags['Spoiler'] = [
      /\[spoiler\](.*?)\[\/spoiler\1?\]/mi,
      '<span class="spoiler">\1</span>',
      'spoiler',
      '[spoiler]spoiler[/spoiler]',
      :spoiler]
  end
end

And how to overwrite existing translation? Just replace it with new one. Below example which replaces standard “Link” rule, with same rule but with rel=”nofollow”:

@@tags['Link'] = [
  /\[url=(.*?)\](.*?)\[\/url\]/mi,
  '<a href="\1" rel="nofollow">\2</a>',
  'Hyperlink to somewhere else',
  'Maybe try looking on [url=http://google.com]Google[/url]?',
  :link]
Olderposts

Copyright © 2018 Running with Ruby

Theme by Anders NorenUp ↑