Repository: getnikola/nikola

Issue:
URLs for sites not in the webserver root are broken [was: The image url is not correct.]
My site is in a subfolder, such as http://www.abc.com/blog/. The url of image in the gallery is not correct.
If I put a jpg file in galleries/software/screen.jpg, after building, the url will be http://www.abc.com/galleries/software/screen.jpg. In face it should be http://www.abc.com/blog/galleries/software/screen.jpg.

The images in the posts are good.

Nikola version 6.2.1



Discussion:
I _believe_ there was an issue of this sort already.  Nikola seems not to like sites deployed to a subdirectory.  But this needs more investigation.

Looks like it’s a part of #912.  Copy-pasting so it’s in a sane location, also re-titled the issue properly:

> Unfortunately, I do not know the gallery code well enough to easily hack on it (without any doubts I will break something, and I know it’s easy to break).  @ralsina, mind taking a look?
> 
> It looks like we have:
> 
> ```
> render_gallery_index.img_list → 'galleries/Finestrat/2013-09-06 18.40.30.jpg'
> render_gallery_index.thumb    → 'output/galleries/Finestrat/2013-09-06 18.40.30.thumbnail.jpg'
> ```
> 
> And that probably causes our problem.  `post.permalink` also should be checked, as it also creates incorrect URLs.  I think `link` is our cause because it tries to do `/[anything]`, and then passes that (a) as the return value or (b) to `urljoin`, which happily fucks it up, ignoring the `/zeug/` part.

Is `BASE_URL` not working in this case? or not set correctly to `http://www.abc.com/blog/`?

I have identified the issue before (when the issue came up, might not be up-to-date) to be `urljoin` doing this: `http://abc.com/blog/` + `/galleries/` = `http://abc.com/galleries/`

``` pycon
>>> urljoin('http://a.com/foo/', 'bar/')
'http://a.com/foo/bar/'

>>> urljoin('http://a.com/foo/', '/bar/')
'http://a.com/bar/'
```

@Kwpolska, that is because beginning with / refers to root. Should be easy enough to strip out the leading slash.

We are actually doing this in some places! `.lstrip('/')` — will fix now.

not fixed yet, sorry.

I had to take out a semi-fix because it crashed the tests.  It won’t be included in the new release then.

i'd like to try and  reproduce this, anyone with some public  m.w.e.?    is it  known to affect 9f2d6919555c83a9949935c546134706dba1396f  ? (current master as of the time im writting)

@mike4999 yes: it’s all broken everywhere.  urljoin is the main culprit, there is also some crazy code in galleries.

@Kwpolska   so to  reproduce all that needs to be done is create galleries in a project with  
`BASE_URL = http://www.abc.com/blog/`   right?

yes.  More things are broken, including links in RSS, sitemaps; see 87bb8b722cad3f805ebfcd3ee0c130b4522297bc for more (possibly not everything)

One way to fix it is write a replacement  urljoin that strips the initial "/" in the paths.

or better, if the input starts with a / it could do it mindful of the base url.

Working on it.

Reproduced.

RSS feeds are also broken.

@max-arnold which rss feeds, the gallery ones?


Resolving patch:
diff --git a/nikola/plugins/task/galleries.py b/nikola/plugins/task/galleries.py
--- a/nikola/plugins/task/galleries.py
+++ b/nikola/plugins/task/galleries.py
@@ -176,6 +176,7 @@ def gen_tasks(self):
 
                 thumbs = ['.thumbnail'.join(os.path.splitext(p)) for p in image_list]
                 thumbs = [os.path.join(self.kw['output_folder'], t) for t in thumbs]
+                dest_img_list = [os.path.join(self.kw['output_folder'], t) for t in image_list]
 
                 folders = []
 
@@ -219,7 +220,7 @@ def gen_tasks(self):
                             template_name,
                             dst,
                             context,
-                            image_list,
+                            dest_img_list,
                             thumbs,
                             file_dep))],
                     'clean': True,


Test patch: