{curl 4.0, 5.0, 6.0, 7.0 applet}

|| Maintained by: cbcurl@gmail.com

{applet
    manifest = "../manifest.mcurl",
    copyright = "Copyright 2008 Christopher Barber",
    license = |"
Licensed under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations 
under the License. 
"|,
    {compiler-directives careful? = true}
}

|# USAGE:
    .../example.curl

        Shows an empty example box.

    .../example.curl?title=<title>&url=<path> 

        Shows example box with given title and content loaded from <path>.

    .../example.curl?title=<title>@content=<code>

        Shows example box with given title and content set to <code>.

    .../example.curl?manifest=<path>...

       Uses the manifest at <path> as the default manifest for the example.
       By default the ZUZU.LIB manifest will be used.

    This applet can show an example specified by the query string and can be 
    used to display embeded examples in blog entries and the like.
#|

{import * from CURL.DOC.CONTENT.ACCESSORIES}
{document-style PlainDocument}
{value
    def query = {get-base-url}.query
    let manifest:String = ""
    let title:String = ""
    let contents:StringInterface = ""
    {if {query.prefix? "?"} then
        {for part in {query.split split-chars = "?&"} do
            def i = {part.find '='}
            {if i > 0 then
                def name = {part.substr 0, i}
                def value = {part.substr i + 1, part.size - i - 1}
                {switch name
                 case "title" do
                    set title = {url-decode-string value}
                    {{get-the-applet}.set-title title}
                 case "url" do
                    {try
                        def u = {url {url-decode-filename value}}
                        set contents = {read-from u}
                     catch ex:Exception do
                        set contents =
                            {format
                                {stringify
                                    {text color = "red", Could not read example:
                                        %w
                                    }
                                },
                                ex.message
                            }
                    }
                 case "content" do
                    set contents = {url-decode-string value}
                 case "manifest" do
                    set manifest =
                        {format
                            |"manifest = {import-manifest {url %w}}, "|,
                            {url-decode-filename value}
                        }
                }
            }
        }
    }
    {evaluate
        {format
            {stringify
                {example %stitle = %w,
                %s
                }
            },
            manifest,
            title,
            contents
        }
    }
}


