@@ -4,47 +4,38 @@ defmodule ChromicPDF.PDFOptions do
4
4
@ moduledoc false
5
5
6
6
require EEx
7
+ import ChromicPDF.Utils , only: [ rendered_to_binary: 1 ]
7
8
8
9
def prepare_input_options ( source , opts ) do
9
10
opts
10
- |> set_cookie_for_assigns_plug ( source )
11
11
|> put_source ( source )
12
12
|> replace_wait_for_with_evaluate ( )
13
13
|> stringify_map_keys ( )
14
- |> iolists_to_binary ( )
14
+ |> sanitize_binaries ( )
15
15
end
16
16
17
- defp set_cookie_for_assigns_plug ( opts , source ) do
18
- cond do
19
- ! Keyword . has_key? ( opts , :assigns ) ->
20
- opts
21
-
22
- ! match? ( { :url , _ } , source ) ->
23
- raise ( ":assigns option invalid with :url source" )
24
-
25
- Keyword . has_key? ( opts , :set_cookie ) ->
26
- raise ( ":assigns option conflicts with :set_cookie" )
17
+ defp put_source ( opts , { :file , source } ) , do: put_source ( opts , { :url , source } )
18
+ defp put_source ( opts , { :path , source } ) , do: put_source ( opts , { :url , source } )
19
+ defp put_source ( opts , { :html , source } ) , do: put_source ( opts , :html , source )
27
20
28
- true ->
29
- do_set_cookie_for_assigns_plug ( opts , source )
21
+ defp put_source ( opts , { :plug , plug_opts } ) do
22
+ if Keyword . has_key? ( opts , :set_cookie ) do
23
+ raise "plug source conflicts with set_cookie"
30
24
end
31
- end
32
25
33
- defp do_set_cookie_for_assigns_plug ( opts , { :url , url } ) do
34
- { assigns , rest } = Keyword . pop ( opts , :assigns )
26
+ { url , plug_opts } = Keyword . pop! ( plug_opts , :url )
35
27
36
28
set_cookie_opts =
37
- assigns
38
- |> ChromicPDF.AssignsPlug . start_agent_and_get_cookie ( )
29
+ plug_opts
30
+ |> ChromicPDF.Plug . start_agent_and_get_cookie ( )
39
31
|> Map . put ( :url , url )
32
+ |> Map . put ( :secure , String . starts_with? ( url , "https" ) )
40
33
41
- Keyword . put ( rest , :set_cookie , set_cookie_opts )
34
+ opts
35
+ |> Keyword . put ( :set_cookie , set_cookie_opts )
36
+ |> put_source ( :url , url )
42
37
end
43
38
44
- defp put_source ( opts , { :file , source } ) , do: put_source ( opts , { :url , source } )
45
- defp put_source ( opts , { :path , source } ) , do: put_source ( opts , { :url , source } )
46
- defp put_source ( opts , { :html , source } ) , do: put_source ( opts , :html , source )
47
-
48
39
defp put_source ( opts , { :url , source } ) do
49
40
url =
50
41
if File . exists? ( source ) do
@@ -110,19 +101,17 @@ defmodule ChromicPDF.PDFOptions do
110
101
Enum . into ( map , % { } , fn { k , v } -> { to_string ( k ) , v } end )
111
102
end
112
103
113
- @ iolist_options [
104
+ @ binary_options [
114
105
[ :html ] ,
115
106
[ :print_to_pdf , "headerTemplate" ] ,
116
107
[ :print_to_pdf , "footerTemplate" ]
117
108
]
118
109
119
- defp iolists_to_binary ( opts ) do
120
- Enum . reduce ( @ iolist_options , opts , fn path , acc ->
110
+ defp sanitize_binaries ( opts ) do
111
+ Enum . reduce ( @ binary_options , opts , fn path , acc ->
121
112
update_in ( acc , path , fn
122
113
nil -> ""
123
- { :safe , value } -> :erlang . iolist_to_binary ( value )
124
- value when is_list ( value ) -> :erlang . iolist_to_binary ( value )
125
- value -> value
114
+ other -> rendered_to_binary ( other )
126
115
end )
127
116
end )
128
117
end
0 commit comments