Hi Michael,
On 2010-12-15 23:26 +0100 Michael Franzl wrote:> I thought it would be nice if the relocatable tag could also generate
> absolute URLs. Example:
>
> {relocatable: default.css}
>
> generates
>
> http://www.mysite.com/folder/default.css
>
> The application would be when you post your generated HTML code into
> a blogging tool and you want that the images are displayed also
> there, without additional work.
I have written an answer to this question on the mailing list on
2008-09-10 in the Thread "page with absolute links", here is the
relevant part:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is currently not possible but would be easy to add since all
internally generated links are generated by Node#link_to. Adding an
option to generate an absolute link and then doing it there would be
sufficient.
You could try the following for the time being: Put the following code
snippet into ext/init.rb:
class Webgen::Node
alias_method :old_route_to, :route_to
def route_to(other)
path = old_route_to(other)
if self[''site_url'']
path = File.join(self[''site_url''],
(self.class.url(@path) + path).path) end
path
end
end
Then you can add a meta information called `site_url` in each page file
that should contain absolute links when rendered. Be aware that ALL
generated routes are absolute, for example, also the output of the
relocatable tag! This means, for example, that the CSS is used from the
site_url and not the local URL (ie. if site_url is like your example,
the css file from http://www.example.com/ will be used instead of the
one from http://sub.example.com/).
If you just want to make links absolute, you can put the following into
your ext/init.rb file:
class Webgen::Node
def link_to(node, attr = {})
attr = node[''link_attrs''].merge(attr) if
node[''link_attrs''].kind_of?(Hash)
rnode = node.routing_node(attr[:lang] || @lang)
link_text = attr[:link_text] || (rnode != node &&
rnode[''routed_title'']) || node[''title'']
attr.delete_if {|k,v| k.kind_of?(Symbol)}
use_link = (rnode != self ||
website.config[''website.link_to_current_page''])
if use_link && self[''site_url'']
attr[''href''] =
File.join(self[''site_url''], (self.class.url(@path) +
self.route_to(node)).path)
elsif use_link
attr[''href''] = self.route_to(rnode)
end
attrs = attr.collect {|name,value|
"#{name.to_s}=\"#{value}\""
}.sort.unshift('''').join('' '')
(use_link ? "<a#{attrs}>#{link_text}</a>" :
"<span#{attrs}>#{link_text}</span>")
end
end
This will make only generated links absolute (used, for example, by the
menu and breadcrumb_trail tags).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I haven''t looked through the above if this still works unchanged but it
should provide a starting point!
Best regards,
Thomas