Permalink
Browse files

Code for the Hello wiki tutorial in the Opa manual.

  • Loading branch information...
0 parents commit da4886b817b31bdff032c47f8d6dafd892085d56 @akoprow akoprow committed Dec 2, 2011
Showing with 297 additions and 0 deletions.
  1. +142 −0 hello_wiki.opa
  2. +142 −0 hello_wiki_simple.opa
  3. +13 −0 resources/css.css
  4. BIN resources/opa-logo.png
142 hello_wiki.opa
@@ -0,0 +1,142 @@
+/**
+ * {1 Import standard classes of bootstrap css}
+ *
+ * see http://twitter.github.com/bootstrap/
+ */
+import stdlib.themes.bootstrap
+
+/**
+ * {1 Import markdown syntax}
+ */
+import stdlib.tools.markdown
+
+/**
+ * {1 Database and database interaction}
+ */
+
+/**
+ * Contents of the wiki.
+ *
+ * Pages which do not exist have content "This page is empty".
+ */
+database stringmap(string) /wiki
+database /wiki[_] = "This page is empty. Double-click to edit."
+
+/**
+ * Read the content associated to a topic from the database and return the
+ * corresponding Markdown source.
+ *
+ * @param topic A topic (arbitrary string).
+ * @return If a page has been saved in for [topic], the source for this
+ * page. Otherwise, the source for the default page.
+ */
+exposed function load_source(topic) {
+ /wiki[topic]
+}
+
+/**
+ * Read the content associated to a topic from the database and return the
+ * corresponding xhtml, ready to insert.
+ *
+ * @param topic A topic (arbitrary string).
+ * @return If a page has been saved in for [topic], the xhtml for this
+ * page. Otherwise, the xhtml for the default page.
+ *
+ * Note: This function does not perform any caching.
+ */
+exposed function load_rendered(topic) {
+ source = load_source(topic)
+ Markdown.xhtml_of_string(Markdown.default_options, source)
+}
+
+/**
+ * Accept source and save the corresponding document in the database.
+ *
+ * @param topic A topic (arbitrary string).
+ * @param source Markdown source to store at this topic.
+ * @return The xhtml for the page that has just been saved.
+ */
+exposed function save_source(topic, source) {
+ /wiki[topic] <- source
+ load_rendered(topic)
+}
+
+/**
+ * {1 User interface}
+ */
+
+/**
+ * Set the user interface in edition mode.
+ *
+ * Load the Markdown source for a topic, display an editable zone
+ * for this markdown.
+ *
+ * @param topic The topic to edit.
+ */
+exposed function edit(topic) {
+ Dom.set_value(#edit_content, load_source(topic))
+ Dom.hide(#show_content)
+ Dom.show(#edit_content)
+ Dom.give_focus(#edit_content)
+}
+
+/**
+ * Set the user interface in reading mode.
+ *
+ * Save the Markdown source for a topic (extracted from [#edit_content]),
+ * display the rendered version.
+ *
+ * @param topic The topic to save.
+ */
+function save(topic) {
+ content = save_source(topic, Dom.get_value(#edit_content))
+ #show_content = content
+ Dom.hide(#edit_content)
+ Dom.show(#show_content)
+}
+
+/**
+ * Main user interface
+ *
+ * @param topic The topic being consulted
+ * @return A resource, ready to be passed to a dispatcher.
+ */
+function display(topic) {
+ Resource.styled_page("About {topic}", ["/resources/css.css"],
+ <div class="topbar"><div class="fill"><div class="container"><div id=#logo></div></div></div></div>
+ <div class="content container">
+ <div class="page-header"><h1>About {topic}</></>
+ <div class="well" id=#show_content ondblclick={function(_) { edit(topic) }}>{load_rendered(topic)}</>
+ <textarea rows="30" id=#edit_content onblur={function(_) { save(topic) }}></>
+ </div>
+ )
+}
+
+/**
+ * {1 Main application}
+ */
+
+/**
+ * Dispatch requests to the user interface
+ *
+ * Note: The empty request is dispatched as if it were "Hello".
+ */
+function start(url) {
+ match (url) {
+ case {path:[] ... } :
+ { display("Hello") }
+ case {~path ...} :
+ { display(String.capitalize(String.to_lower(String.concat("::", path)))) }
+ }
+}
+
+/**
+ * Start the wiki server
+ */
+Server.start(Server.http,
+ /** Statically embed a bundle of resources */
+ [ {bundle: @static_include_directory("resources")}
+ /** Launch the [start] dispatcher */
+ , {filter: Server.Filter.anywhere, dispatch: start}
+ ]
+)
142 hello_wiki_simple.opa
@@ -0,0 +1,142 @@
+/**
+ * {1 Import standard classes of bootstrap css}
+ *
+ * see http://twitter.github.com/bootstrap/
+ */
+import stdlib.themes.bootstrap
+
+/**
+ * {1 Import markdown syntax}
+ */
+import stdlib.tools.markdown
+
+/**
+ * {1 Database and database interaction}
+ */
+
+/**
+ * Contents of the wiki.
+ *
+ * Pages which do not exist have content "This page is empty".
+ */
+database stringmap(string) /wiki
+database /wiki[_] = "This page is empty. Double-click to edit."
+
+/**
+ * Read the content associated to a topic from the database and return the
+ * corresponding Markdown source.
+ *
+ * @param topic A topic (arbitrary string).
+ * @return If a page has been saved in for [topic], the source for this
+ * page. Otherwise, the source for the default page.
+ */
+function load_source(topic) {
+ /wiki[topic]
+}
+
+/**
+ * Read the content associated to a topic from the database and return the
+ * corresponding xhtml, ready to insert.
+ *
+ * @param topic A topic (arbitrary string).
+ * @return If a page has been saved in for [topic], the xhtml for this
+ * page. Otherwise, the xhtml for the default page.
+ *
+ * Note: This function does not perform any caching.
+ */
+function load_rendered(topic) {
+ source = load_source(topic)
+ Markdown.xhtml_of_string(Markdown.default_options, source)
+}
+
+/**
+ * Accept source and save the corresponding document in the database.
+ *
+ * @param topic A topic (arbitrary string).
+ * @param source Markdown source to store at this topic.
+ * @return The xhtml for the page that has just been saved.
+ */
+function save_source(topic, source) {
+ /wiki[topic] <- source
+ load_rendered(topic)
+}
+
+/**
+ * {1 User interface}
+ */
+
+/**
+ * Set the user interface in edition mode.
+ *
+ * Load the Markdown source for a topic, display an editable zone
+ * for this markdown.
+ *
+ * @param topic The topic to edit.
+ */
+function edit(topic) {
+ Dom.set_value(#edit_content, load_source(topic))
+ Dom.hide(#show_content)
+ Dom.show(#edit_content)
+ Dom.give_focus(#edit_content)
+}
+
+/**
+ * Set the user interface in reading mode.
+ *
+ * Save the Markdown source for a topic (extracted from [#edit_content]),
+ * display the rendered version.
+ *
+ * @param topic The topic to save.
+ */
+function save(topic) {
+ content = save_source(topic, Dom.get_value(#edit_content))
+ #show_content = content
+ Dom.hide(#edit_content)
+ Dom.show(#show_content)
+}
+
+/**
+ * Main user interface
+ *
+ * @param topic The topic being consulted
+ * @return A resource, ready to be passed to a dispatcher.
+ */
+function display(topic) {
+ Resource.styled_page("About {topic}", ["/resources/css.css"],
+ <div class="topbar"><div class="fill"><div class="container"><div id=#logo></div></div></div></div>
+ <div class="content container">
+ <div class="page-header"><h1>About {topic}</></>
+ <div class="well" id=#show_content ondblclick={function(_) { edit(topic) }}>{load_rendered(topic)}</>
+ <textarea rows="30" id=#edit_content onblur={function(_) { save(topic) }}></>
+ </div>
+ )
+}
+
+/**
+ * {1 Main application}
+ */
+
+/**
+ * Dispatch requests to the user interface
+ *
+ * Note: The empty request is dispatched as if it were "Hello".
+ */
+function start(url) {
+ match (url) {
+ case {path:[] ... } :
+ { display("Hello") }
+ case {~path ...} :
+ { display(String.capitalize(String.to_lower(String.concat("::", path)))) }
+ }
+}
+
+/**
+ * Start the wiki server
+ */
+Server.start(Server.http,
+ /** Statically embed a bundle of resources */
+ [ {bundle: @static_include_directory("resources")}
+ /** Launch the [start] dispatcher */
+ , {filter: Server.Filter.anywhere, dispatch: start}
+ ]
+)
13 resources/css.css
@@ -0,0 +1,13 @@
+/***Header***/
+#logo {
+ background: url("/resources/opa-logo.png") no-repeat scroll 0 0 transparent;
+ height: 32px;
+ margin: 10px 0 5px;
+ width: 61px;
+}
+/***Editing area***/
+.content {margin-top:60px;}
+#edit_content {
+ width:100%;
+ display: none; /* initially hidden */
+}
BIN resources/opa-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit da4886b

Please sign in to comment.