Skip to content

Image URLs #85

Closed
AndrewVos opened this Issue Jun 15, 2011 · 11 comments

2 participants

@AndrewVos

If I add an image in a title with a relative url for example:

<img src="/bla.jpg"> </img>

then it will display fine but in feedburner it obviously points to feeds.feedburner.com/bla.jpg so I end up putting the full url in there which means I can't preview the article before pushing it to my host!

Is there any way I can add something like env.url to the article?

@ixti
ixti commented Aug 8, 2011

You can output config.url without any problem:

<img src="<%= @config[:url] %>/bla.jpg" />

Alternatively you can extend Toto::Site::Context in your config.ru:

module Toto
  class Site
    class Context
      def assets url
        "http://#{(@config[:url].sub("http://", '') + url).squeeze('/')}"
      end
    end
  end
end

so then you'll be able to:

<img src="<%= assets '/bla.jpg' %>" />
@AndrewVos

I'm talking about adding images in an actual article, which does not get rendered with Erubis so those <%= code blocks won't work.

@ixti
ixti commented Aug 8, 2011

Ahh. Sorry didn't got it. Now I see. In this case you can override Articles body method in this case I guess:

module Toto
  class Article
    alias :body_old :body
    def body
      self[:body].gsub! '!baseurl!', "http://#{(@config[:url].sub("http://", '') + '/').squeeze('/')}"
      body_old
    end
  end
end

and then

<img src="!baseurl!/bla.jpg" />
@ixti
ixti commented Aug 8, 2011

Also, I'm not familiar with feedburner's internals (although I also use it and so it's really interesting for me as well) but isn't it's possible to provide it "baseurl" somehow? :)) Just an idea. probably stupid and crazy :))

@AndrewVos

You may just be right about the baseurl!

@ixti
ixti commented Aug 8, 2011

Yeah! Looks like we can use "xml:base" http://www.w3.org/TR/xmlbase/ attribute in feed generator.

@AndrewVos
@ixti
ixti commented Aug 8, 2011

Wish to get problem solved is not an idiocy! The more idiotic way was chosen by me - yesterday I spent about one hour in the night implementing absolutely ridiculous thing #95 that could be done by simply placing files under separate directory. :))

@AndrewVos AndrewVos closed this Aug 11, 2011
@AndrewVos AndrewVos reopened this Aug 11, 2011
@AndrewVos

Ok I just read through that spec and it doesn't apply to rss feeds it's just an xml thing.

@ixti
ixti commented Aug 11, 2011

That's sad. Well in this case I can see another possible solution: we can use nokogiri to find and replace relative urls in the feed :)) Something like this:

Nokogiri::XML(article.summary).xpath("//img").each do |img|
  src = img.attr 'src'
  img.set_attribute('src', "#{@config[:url]}/#{src}".squeeze('/')) unless src.start_with? 'http://'
end.to_s
@ixti
ixti commented Aug 16, 2011

I have implemented a small fix the way I described before for my own blog (https://github.com/ixti/blog/blob/e99dc7762eb3d71753f1b8081701c5c229fa9287/templates/index.builder), although it might be not perfect - it works for me :))

Alternative way is to patch Toto::Article to replace "magic" word, e.g.

body.gsub('##baseurl##', "#{@config[:url]}/#{@config[:prefix]}")
@AndrewVos AndrewVos closed this Jun 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.