Git Product home page Git Product logo

Comments (1)

johncrenshaw avatar johncrenshaw commented on August 13, 2024

I found a way to do this in a derived class with an ugly hack. Here's my currently functional code.

  // extension for import to use theme URLs
  function import(&$url, &$media)
  {
    $ret = parent::import($url, $media);
    if ($ret)
    {
      $url = $this->processURL($url);
    }
    return $ret;
  }

  function lib_url($arg)
  {
    return 'url('.$this->processURL($this->compileValue($arg), true).')';
  }
  
  function processURL($url, $relative = false)
  {
    // nasty hack, because processURL entirely replaces the functionality
    // that $this->importDir was previously used for. We look to see if there
    // was a value for $this->importDir, and if so, we move it to a new variable
    // $this->baseUrlDir. Then we clear $this->importDir so that it doesn't get
    // in the way later.
    if ($this->importDir)
    {
      $this->baseUrlDir = $this->importDir;
      $this->importDir = null;
    }

    // remove quotes, if present
    if ($this->quoted($url)) $url = substr($url, 1, -1);
    
    // find the protocol, if any
    $protocol = '';
    if (preg_match('~^([a-zA-Z]+)\:[\\/]?[\\/]?(.*)~', $url, $matches))
    {
      // this URL has a protocol
      $protocol = isset($matches[1]) ? $matches[1] : '';
      $path = isset($matches[2]) ? $matches[2] : '';
      
      // TODO: do we need to do anything special with any protocols?
      switch ($protocol)
      {
      // the following cases should always return the input directly. We don't need to do anything special.
      case 'data':
      case 'http':
      case 'https':
      default:
        return $url; // return the original URL
      }
    }
    else
    {
      // No protocol. This is a relative URL of some sort.
      
      // normalize slashes
      $url = str_replace('\\', '/', $url);
      $docroot = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
      $base = str_replace('\\', '/', $this->baseUrlDir);
      
      // if the url starts with a slash, it is an absolute path from the document root
      if ($url{0} == '/')
      {
        return $url;
      }
      else // just a normal relative path
      {
        // convert to an absolute path
        $url = rtrim($base, '/').'/'.ltrim($url, '/');
        
        if ($relative)
        {
          // a relative path is wanted, so reduce it to a path from the document root
          $url = str_replace($docroot, '', $url);
          if ($url{0} != '/')
          {
            $url = '/'.$url;
          }
        }
        
        return $url;
      }
    }
    
    return $url;
  }

With some small tweaks to the above I can add support for hierarchical themed URLs. This also shows how the processing can address some issues with images, so that the final file always contains the proper full path to the image (fixes some issues with importing files from another directory for example).

from lessphp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.