[url_normalize_9]

Description

Link: [url_normalize_9]
Author: Jonathan Guthrie
Category: String
Version: 9.x
License: Public Domain
Posted: Sep. 29, 2010
Updated: Sep. 29, 2010
More by this author...

This tag can be used to convert a relative URL into an absolute one. Accepts two parameters: a base URL to compare against (representing the path to the file that calls the relative URL), and a relative URL to convert.

Based on: http://tagswap.net/url_normalize

Parameters

none


Sample Usage

Sample Usage

url_normalize(
   '../../../foo.jpg',
   '/path/to/thing/that/calls/foo/'
)


url_normalize(
    -url='../../../foo.jpg',
    -base='/path/to/thing/that/calls/foo/'
)

 
-> '/path/to/thing/foo.jpg'
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// ==================================
	/*
		[url_normalize]
		http://tagswap.net/url_normalize
		=> Given a base URL, converts the given relative URL to absolute
		
		This tag can be used to convert a relative URL into an absolute one. 
		Accepts two parameters: 
			a base URL to compare against (representing the path to the file that calls the relative URL), 
			and a relative URL to convert.
		
		Parameters
			-url	string, required	The relative URL you wish to convert to absolute.
			-base	string, required	Path to location from which the original relative URL is called.
		
		Sample Usage
			url_normalize( 
			    -url='../../../foo.jpg', 
			    -base='/path/to/thing/that/calls/foo/' 
			); 
			 
		-> '/path/to/thing/foo.jpg' 

	*/
	// ==================================

define url_normalize(url::string,base::string) => {

    // URLs beginning with / or http do not need to be converted 
    #url->beginswith('http://') || #url->beginswith('/') ? return #url
     
	!#base->endswith('/') ? #base->append('/')
     
    if(#url->beginswith('../')) => {
        // count the number of paths to remove 
        local(dirsToRemove = #url->split('../')->size - 1)
         
        #base->removetrailing('/'); 
         
        loop(#dirsToRemove) => {
            #base->removetrailing('/' + #base->split('/')->last)
            #url->removeleading('../')
        }
     
        #base->append('/')
     
        return(#base + #url)
         
    else(#url == '.')
        return(#base)
     
    else(#url->beginswith('./'))
        return(#base + #url->removeleading('./')&)
     
    else
        return (#base + #url)
	} 
}
define url_normalize(-url::string,-base::string) => {
	return url_normalize(#url,#base)
}

 

Comments

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net