Skip to content

Commit

Permalink
changes listed in CHANGELOG.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
yarivvv committed Apr 1, 2007
1 parent 80cf62d commit b592b63
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Expand Up @@ -42,6 +42,10 @@ for instance.

- Ensured that MaxLength is converted to a list in erlyweb_html:input().

- Added content_type/1 and content_type/2 to yaws_headers.erl.

- Fixed the handling of includes in ErlTL.

v0.5

- erlyweb:compile() now obey relative include paths passed as {i, Path} options. It treats the base directory as [AppDir]/src. Absolute paths also work now.
Expand Down
69 changes: 59 additions & 10 deletions src/erltl/erltl.erl
Expand Up @@ -140,7 +140,8 @@

-module(erltl).
-author("Yariv Sadan (yarivsblog@gmail.com, http://yarivsblog.com)").
-export([compile/1, compile/2, forms_for_file/1, forms_for_data/2]).
-export([compile/1, compile/2, forms_for_file/1,
forms_for_file/2, forms_for_data/2, forms_for_data/3]).

-define(L(Msg), io:format("~b ~p~n", [?LINE, Msg])).

Expand All @@ -163,7 +164,13 @@ compile(FileName) ->
%%
%% @spec compile(FileName::string(), Options::[option()]) -> ok | {error, Err}
compile(FileName, Options) ->
case forms_for_file(FileName) of
IncludePaths = lists:foldl(
fun({i, Path}, Acc) ->
[Path | Acc];
(_Other, Acc) ->
Acc
end, [], Options),
case forms_for_file(FileName, IncludePaths) of
{ok, Forms} ->
case compile:forms(Forms,
Options) of
Expand Down Expand Up @@ -195,27 +202,69 @@ compile(FileName, Options) ->
Err -> Err
end.


%% @equiv forms_for_file(Filename, []).
forms_for_file(FileName) ->
forms_for_file(FileName, []).

%% @doc Parse the ErlTL file and return its representation in Erlang
%% abstract forms.
%% @spec forms_for_file(FileName::string()) -> {ok, [form()]} | {error, Err}
forms_for_file(FileName) ->
%% @spec forms_for_file(FileName::string(),
%% IncludePaths:[string()]) -> {ok, [form()]} | {error, Err}
forms_for_file(FileName, IncludePaths) ->
case file:read_file(FileName) of
{ok, Binary} ->
BaseName = filename:rootname(filename:basename(FileName)),
forms_for_data(Binary, list_to_atom(BaseName));
forms_for_data(Binary, list_to_atom(BaseName), IncludePaths);
Err ->
Err
end.

%% @equiv forms_form_data(Data, ModuleName, []).
forms_for_data(Data, ModuleName) ->
forms_for_data(Data, ModuleName, []).

%% @doc Parse the raw text of an ErlTL template and return its
%% representation in abstract forms.
%% @spec forms_for_data(FileName::string(), ModuleName::atom()) ->
%% @spec forms_for_data(Data::binary() | string(), ModuleName::atom(),
%% IncludePaths::[string()]) ->
%% {ok, [form()]} | {error, Err}
forms_for_data(Data, ModuleName) when is_binary(Data) ->
forms_for_data(binary_to_list(Data), ModuleName);
forms_for_data(Data, ModuleName) ->
forms_for_data(Data, ModuleName, IncludePaths) when is_binary(Data) ->
forms_for_data(binary_to_list(Data), ModuleName, IncludePaths);
forms_for_data(Data, ModuleName, IncludePaths) ->
Lines = make_lines(Data),
forms(Lines, ModuleName).
case forms(Lines, ModuleName) of
{ok, Forms} ->
case catch lists:map(
fun({attribute, _, include, Include}) ->
process_include(
Include, [[], ["."] |
IncludePaths]);
(Form) ->
Form
end, Forms)
of
{'EXIT', Err} ->
{error, Err};
Res ->
{ok, lists:flatten(Res)}
end;
Err ->
Err
end.

process_include(Include, []) ->
exit({file_not_found, Include});
process_include(Include, [Path | Rest]) ->
case epp:parse_file(Path ++ "/" ++ Include, [], []) of
{error, enoent} ->
process_include(Include, Rest);
{ok, IncludeForms} ->
lists:sublist(
IncludeForms,
2,
length(IncludeForms) - 2)
end.

make_lines(Str) ->
make_lines(Str, [], []).
Expand Down
8 changes: 6 additions & 2 deletions src/erlydb/erlydb_base.erl
Expand Up @@ -605,7 +605,8 @@ insert1(Recs) ->
Mod = get_module(hd(Recs)),
Fields = Mod:db_fields(),
Fields1 = [erlydb_field:name(Field) ||
Field <- Fields, erlydb_field:extra(Field) =/= identity],
Field <- Fields,
not is_read_only(Field)],
Rows1 = lists:map(
fun(Rec) ->
Rec1 = Mod:before_save(Rec),
Expand Down Expand Up @@ -1505,7 +1506,7 @@ make_save_statement(Rec) ->
Module = get_module(Rec),
Fields = [erlydb_field:name(Field) ||
Field <- Module:db_fields(),
not lists:member(read_only, erlydb_field:attributes(Field))],
not is_read_only(Field)],
case is_new(Rec) of
false ->
Vals = [{Field, Module:Field(Rec)} || Field <- Fields],
Expand Down Expand Up @@ -1744,3 +1745,6 @@ check_limits(Val, Name, Min, Max) ->
exit({invalid_value, Name, Val});
true -> Val
end.

is_read_only(Field) ->
lists:member(read_only, erlydb_field:attributes(Field)).
5 changes: 1 addition & 4 deletions src/erlyweb/erlyweb.erl
Expand Up @@ -315,10 +315,7 @@ get_ewc(A) ->
get_ewc(A, lookup_app_data_module(A)).

get_ewc(A, AppData) ->
Prefix = lists:dropwhile(
fun($?) -> true;
(_) -> false
end, yaws_arg:appmoddata(A)),
Prefix = erlyweb_util:get_url_prefix(A),
case string:tokens(Prefix, "/") of
[] -> {page, "/"};
[ComponentStr]->
Expand Down
3 changes: 2 additions & 1 deletion src/erlyweb/erlyweb_compile.erl
Expand Up @@ -325,7 +325,8 @@ compile_file(FileName, BaseName, Extension, Type,
".et" ->
?Debug("Compiling ErlTL file ~p", [BaseName]),
erltl:compile(FileName,
Options ++ [nowarn_unused_vars]);
Options ++ [nowarn_unused_vars] ++
[{i, P} || P <- IncludePaths]);
".erl" ->
?Debug("Compiling Erlang file ~p", [BaseName]),
compile_file(FileName, BaseName, Type, Options,
Expand Down
12 changes: 11 additions & 1 deletion src/erlyweb/erlyweb_util.erl
Expand Up @@ -11,7 +11,7 @@
-module(erlyweb_util).
-author("Yariv Sadan (yarivsblog@gmail.com, http://yarivsblog.com").
-export([log/5, create_app/2, create_component/2, get_appname/1,
get_app_root/1,
get_app_root/1, get_url_prefix/1,
get_cookie/2, indexify/2]).

-define(Debug(Msg, Params), log(?MODULE, ?LINE, debug, Msg, Params)).
Expand Down Expand Up @@ -180,6 +180,16 @@ get_appname(A) ->
get_app_root(A)->
erlyweb:get_app_root(A).

%% @doc Get the of the arg's appmoddata value up to the
%% first '?' symbol.
%%
%% @spec get_url_prefix(A::arg()) -> string()
get_url_prefix(A) ->
lists:dropwhile(
fun($?) -> true;
(_) -> false
end, yaws_arg:appmoddata(A)).


%% @doc Get the cookie's value from the arg.
%% @equiv yaws_api:find_cookie_val(Name, yaws_headers:cookie(A))
Expand Down
10 changes: 9 additions & 1 deletion src/erlyweb/yaws_headers.erl
Expand Up @@ -22,7 +22,9 @@
if_unmodified_since/1, if_unmodified_since/2,
range/1, range/2, referer/1, referer/2, user_agent/1, user_agent/2,
accept_ranges/1, accept_ranges/2, cookie/1, cookie/2, keep_alive/1,
keep_alive/2, content_length/1, content_length/2, authorization/1,
keep_alive/2, content_length/1, content_length/2,
content_type/1, content_type/2,
authorization/1,
authorization/2, other/1, other/2]).


Expand Down Expand Up @@ -122,6 +124,12 @@ content_length(Arg) ->
content_length(Arg, Val) ->
(Arg#arg.headers)#headers{content_length = Val}.

content_type(Arg) ->
(Arg#arg.headers)#headers.content_type.

content_type(Arg, Val) ->
(Arg#arg.headers)#headers{content_type = Val}.

authorization(Arg) ->
(Arg#arg.headers)#headers.authorization.

Expand Down

0 comments on commit b592b63

Please sign in to comment.