Skip to content

Commit

Permalink
[feature] Resource: added a new function in Resource named fullpage_w…
Browse files Browse the repository at this point in the history
…ith_doctype allowing to use a certain doctype {xhtml1_1} or {html5} for the moment
  • Loading branch information
Frederic Ye committed Aug 31, 2011
1 parent 030de7e commit cc38ad0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
16 changes: 10 additions & 6 deletions stdlib/core/web/resource/resource.opa
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ base_url =
* to simplify page & full_page * to simplify page & full_page
*/ */
@private @private
html_constructor(title,headers,html,status,customizers,rc_lastm) = html_constructor(title, doctype, headers, html, status, customizers, rc_lastm) =
{ rc_content = { rc_content =
{~html ~customizers {~html ~customizers ~doctype
headers = match title headers = match title
| "" -> headers | "" -> headers
| _ -> headers <+> <title>{title}</title>} | _ -> headers <+> <title>{title}</title>}
Expand Down Expand Up @@ -186,7 +186,7 @@ page(title:string, body: xhtml): resource = styled_page(title, [], body)
* @param styles A list of addresses for CSS pages. * @param styles A list of addresses for CSS pages.
* @param body The contents of the page. * @param body The contents of the page.
*/ */
styled_page(title:string, styles:list(string), body: xhtml): resource = html_constructor(title, styled_page(title:string, styles:list(string), body: xhtml): resource = html_constructor(title, {none},
<> <>
{List.map(url -> <link rel="stylesheet" type="text/css" href="{url}" />, styles)} {List.map(url -> <link rel="stylesheet" type="text/css" href="{url}" />, styles)}
</> </>
Expand All @@ -211,9 +211,12 @@ html = page
* to cellphones or broken browsers. The empty list is a safe default until you start testing your application on exotic platforms. * to cellphones or broken browsers. The empty list is a safe default until you start testing your application on exotic platforms.
*/ */
full_page(title: string, body:xhtml, headers:xhtml, status: web_response, customizers: list(platform_customization)):resource = full_page(title: string, body:xhtml, headers:xhtml, status: web_response, customizers: list(platform_customization)):resource =
html_constructor(title,headers,body,status,List.append(Resource_private.default_customizers, customizers), {volatile}) html_constructor(title, {none}, headers, body, status, List.append(Resource_private.default_customizers, customizers), {volatile})
// Do not cache pages, we always generate a fresh number // Do not cache pages, we always generate a fresh number


full_page_with_doctype(title: string, doctype, body:xhtml, headers:xhtml, status: web_response, customizers: list(platform_customization)):resource =
html_constructor(title, {some=doctype}, headers, body, status, List.append(Resource_private.default_customizers, customizers), {volatile})

/** /**
* Create a resource which is recomputed at each time it is served. * Create a resource which is recomputed at each time it is served.
* @param compute The function was recompute the resource with the * @param compute The function was recompute the resource with the
Expand Down Expand Up @@ -540,10 +543,11 @@ later(maker : ((resource -> void) -> void)) =
export_data({~rc_content rc_lastm=_ rc_status=_}: resource)= export_data({~rc_content rc_lastm=_ rc_status=_}: resource)=
rec aux(rc_content : resource_private_content) = rec aux(rc_content : resource_private_content) =
match rc_content with match rc_content with
| {~html ~headers customizers=_} -> | {~html ~doctype ~headers customizers=_} ->
body= <body id="Body">{html}</body> body= <body id="Body">{html}</body>
head = <head>{headers}</head> head = <head>{headers}</head>
page= Xhtml.of_string_unsafe(Resource_private.shared_html_header) <+> doctype = match doctype with {some=d} -> Resource_private.html_doctype_to_string(d) {none} -> Resource_private.shared_xhtml1_1_header
page= Xhtml.of_string_unsafe(doctype) <+>
<html xmlns="http://www.w3.org/1999/xhtml">{head}{body}</html> <html xmlns="http://www.w3.org/1999/xhtml">{head}{body}</html>
data=Xhtml.serialize_as_standalone_html(page) data=Xhtml.serialize_as_standalone_html(page)
some({~data mimetype="text/html"}) some({~data mimetype="text/html"})
Expand Down
33 changes: 22 additions & 11 deletions stdlib/core/web/resource/resource_private.opa
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ import stdlib.core.compare
* {1 Types defined in this module} * {1 Types defined in this module}
*/ */


/**
* The doctype of an html resource
*/
type html_resource_doctype =
{html5} /
{xhtml1_1}

/** /**
* The actual contents of a resource. * The actual contents of a resource.
*/ */
type resource_private_content = type resource_private_content =
//User-definable resources //User-definable resources
{html:xhtml; headers: xhtml; customizers: list(platform_customization)} {html:xhtml; doctype:option(html_resource_doctype); headers: xhtml; customizers: list(platform_customization)}
/* / {soap:xmlns}*/ /* / {soap:xmlns}*/
/ {xml:xmlns} / {xml:xmlns}
/ {png:binary} / {png:binary}
Expand Down Expand Up @@ -121,7 +128,10 @@ Resource_private =
@private @private
xhtml_equality(a,b) = xhtml_compare(a,b) == {eq} xhtml_equality(a,b) = xhtml_compare(a,b) == {eq}



html_doctype_to_string(doctype:html_resource_doctype) =
match doctype with
{xhtml1_1} -> shared_xhtml1_1_header
{html5} -> shared_html5_header


/** /**
* Construct the inclusion of an external resource that can possibly be modified dynamically for debugging purposes * Construct the inclusion of an external resource that can possibly be modified dynamically for debugging purposes
Expand Down Expand Up @@ -498,12 +508,11 @@ Resource_private =
* {2 Delivery mechanism} * {2 Delivery mechanism}
*/ */


/** shared_xhtml1_1_header =
* The html header shared by all web pages "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"
*/
shared_html_header = shared_html5_header =
"<?xml version=\"1.0\" encoding=\"utf-8\"?> "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE html>"
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"


shared_xml_header = shared_xml_header =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
Expand Down Expand Up @@ -638,7 +647,7 @@ default_customizers = [customizer_for_icon,customizer_for_google_frame,required_
} }
] ]
}) })

/** /**
* A cache for generation of xhtml resources * A cache for generation of xhtml resources
*/ */
Expand Down Expand Up @@ -827,7 +836,7 @@ export_resource(external_css_files: list(string),
| {~later} -> | {~later} ->
(r -> later(( (resource : resource) -> response(force_mimetype)(winfo, resource)(r)))) (r -> later(( (resource : resource) -> response(force_mimetype)(winfo, resource)(r))))


| { html=body ~headers ~customizers } -> | { html=body ~doctype ~headers ~customizers } ->
( (
(req:WebInfo.private.native_request) -> (req:WebInfo.private.native_request) ->
//Prepare customizations //Prepare customizations
Expand Down Expand Up @@ -862,7 +871,9 @@ export_resource(external_css_files: list(string),
<></>, base_url) <></>, base_url)
ready_head = <head>{base}{head_without_id}{global_variable}</head> ready_head = <head>{base}{head_without_id}{global_variable}</head>


page= Xhtml.of_string_unsafe(shared_html_header) <+> doctype = match doctype with {some=d} -> html_doctype_to_string(d) {none} -> shared_xhtml1_1_header

page= Xhtml.of_string_unsafe(doctype) <+>
<html xmlns="http://www.w3.org/1999/xhtml">{ready_head}{ready_body}</html> <html xmlns="http://www.w3.org/1999/xhtml">{ready_head}{ready_body}</html>


//Serialize and send //Serialize and send
Expand Down

0 comments on commit cc38ad0

Please sign in to comment.