Skip to content

Commit

Permalink
functions to operate url's (make absolute url etc)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivkosh committed Jan 24, 2011
1 parent d0cb9e2 commit 9e9ed1d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/t.erl
Expand Up @@ -174,5 +174,25 @@ go1(What, Where) ->
Out = find_el(What, HtmlTree, []),
{Out, length(Out)}.

%% abs url inside the same server ej: /img/image.png
full_url({Root, _Context}, ComponentUrl=[$/|_]) -> Root ++ ComponentUrl;

%% full url ej: http://other.com/img.png
full_url({_Root, _Context}, ComponentUrl="http://"++_) -> ComponentUrl;
full_url({_Root, _Context}, ComponentUrl="https://"++_) -> ComponentUrl;
full_url({_Root, _Context}, ComponentUrl="ftp://"++_) -> ComponentUrl;

% everything else is considerer a relative path.. obviously its wrong (../img)
full_url({Root, Context}, ComponentUrl) ->
Root ++ Context ++ "/" ++ ComponentUrl.

% returns the domain, and current context path.
% url_context("http://www.some.domain.com/content/index.html)
% -> {"http://www.some.domain.com", "/content"}
url_context(URL) ->
{Proto, _, Root, _Port, Path, _Query} = http_uri:parse(URL),
Ctx = string:sub_string(Path, 1, string:rstr(Path,"/")),
{atom_to_list(Proto)++"://" ++ Root, Ctx}.

%% mochiweb_html:tokens (???)
%% mochiweb_html:to_html

0 comments on commit 9e9ed1d

Please sign in to comment.