cloudhead / toto

the 10 second blog-engine for hackers
MIT License
1.49k stars 244 forks source link

Image URLs #85

Closed AndrewVos closed 9 years ago

AndrewVos commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago

You may just be right about the baseurl!

ixti commented 13 years ago

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

AndrewVos commented 13 years ago

Ok I feel like a bit if an idiot now. I'll close this request when I get behind a computer.

On 8 Aug 2011, at 18:33, ixti reply@reply.github.com wrote:

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

Reply to this email directly or view it on GitHub: https://github.com/cloudhead/toto/issues/85#issuecomment-1756187

ixti commented 13 years ago

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 commented 13 years ago

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

ixti commented 13 years ago

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 commented 13 years ago

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]}")