eudicots / Cactus

Static site generator for designers. Uses Python and Django templates.
BSD 3-Clause "New" or "Revised" License
3.47k stars 314 forks source link

export adds a / to path of static resources #109

Open jirivBE opened 9 years ago

jirivBE commented 9 years ago

The export feature works nearly perfectly, but all my static resources got an extra / in front of them, making them break. building from the blog or portfolio template, if I export

src="{% static 'images/heroImage.jpg' %}"

It generates out too

src="/static/images/logo.jpg"

When it should be

src="static/images/logo.jpg"

maybe a feature request for a modal asking the domain you will be hosting the site on would make sense (and prefix it to the path)? That way all resource links are absolute, a best practice.

krallin commented 9 years ago

Hi there,

Just to make sure — is your use case to serve your website from a non-root path?

Cheers,

jirivBE commented 9 years ago

This was when testing using the mac app, then exporting to desktop and opening in browser from there. The idea was to export it to a github enabled folder, then sync (with github for mac) it to host it on github pages. However just enabling git on the static directory would allow this setup without the need to export I assume...

On Sun, Jan 18, 2015 at 5:35 PM, Thomas Orozco notifications@github.com wrote:

Hi there,

Just to make sure — is your use case to serve your website from a non-root path?

Cheers,

— Reply to this email directly or view it on GitHub https://github.com/koenbok/Cactus/issues/109#issuecomment-70414872.

kartoch commented 8 years ago

That let to another problem for page in sub-directory, instead of:

src="static/images/logo.jpg"

This approach requires:

src="../static/images/logo.jpg"
cjrider commented 8 years ago

hey all. I fixed this for myself with an ugly hack.

This is a plugin, which overrides the default static and url tags and computes relative URLS using the builtin python relpath() Happy if someone else wanted to clean this up and add a config flag. For now it's clean enough for my purposes.

#coding:utf-8
from six.moves import urllib
from django import template
from os.path import relpath
from os.path import dirname

def static(context, link_url):
    """
    Get the path for a static file in the Cactus build.
    We'll need this because paths can be rewritten with fingerprinting.
    """
    #TODO: Support URLS that don't start with `/static/`
    site = context['__CACTUS_SITE__']
    page = context['__CACTUS_CURRENT_PAGE__']

    url = site.get_url_for_static(link_url)

    if url is None:

        # For the static method we check if we need to add a prefix
        helper_keys = [
            "/static/" + link_url,
            "/static"  + link_url,
            "static/"  + link_url
        ]

        for helper_key in helper_keys:

            url_helper_key = site.get_url_for_static(helper_key)

            if url_helper_key is not None:
                return relpath(urllib.parse.urljoin( site.static_path, helper_key), dirname(page.absolute_final_url))

        url = link_url

    return relpath(urllib.parse.urljoin( site.static_path, link_url), dirname(page.absolute_final_url))

def url(context, link_url):
    """
    Get the path for a page in the Cactus build.
    We'll need this because paths can be rewritten with prettifying.
    """
    site = context['__CACTUS_SITE__']
    page = context['__CACTUS_CURRENT_PAGE__']

    url = site.get_url_for_page(link_url)

    if url is None:

        # See if we're trying to link to an /subdir/index.html with /subdir
        link_url_index = os.path.join(link_url, "index.html")
        url_link_url_index = site.get_url_for_page(link_url_index)

        if url_link_url_index is None:
            logger.warn('%s: page resource does not exist: %s', page.link_url, link_url)

        url = link_url

    if site.prettify_urls:
        return relpath(urllib.parse.urljoin(site.url,  url.rsplit('index.html', 1)[0]), dirname(page.absolute_final_url))

    return relpath(urllib.parse.urljoin(site.url, url), dirname(page.absolute_final_url))

def preBuild(site):
    register = template.Library()
    register.simple_tag(takes_context=True)(url)
    register.simple_tag(takes_context=True)(static)
    template.base.builtins.append(register)
psql commented 8 years ago

@cjrider is there anything special I need to do, to get this plugin to work? Do I just toss it into a .py file and dump it into the plugin directory? Do I need to include it?

Sorry, I'm a total noob with the plugin paradigm in cactus.