r9907 - apps/gobi/trunk/code/Global

[email protected] Wed, 16 Sep 2009 11:20:25 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090916092025.20A203D0E3@mia>
Author: hannes
Date: 2009-09-16 11:20:25 +0200 (Wed, 16 Sep 2009)
New Revision: 9907

Modified:
   apps/gobi/trunk/code/Global/WikiMarkup.js
Log:
Implement optional Markdown format (begin wiki text with #MARKDOWN).

Details at http://dev.helma.org/trac/helma/changeset/9907

Modified: apps/gobi/trunk/code/Global/WikiMarkup.js
===================================================================
--- apps/gobi/trunk/code/Global/WikiMarkup.js	2009-09-16 09:15:37 UTC (rev 9906)
+++ apps/gobi/trunk/code/Global/WikiMarkup.js	2009-09-16 09:20:25 UTC (rev 9907)
@@ -13,6 +13,12 @@
         if (!src)
             return;
 
+        // hack: allow pages to use Markdown format by starting with #MARKDOWN
+        if (src.startsWith('#MARKDOWN')) {
+            this.renderMarkdown(src.substring(9), page);
+            return;
+        }
+
         // render header tags
         src = this.renderMarkup(page, src);
 
@@ -21,7 +27,7 @@
 
         // headers
         src = WikiMarkup.renderHeaders(src, page, new java.lang.StringBuffer());
-        
+
         // give choice over format method
         if (param && param.format == "plain") {
             src = format(src);
@@ -34,6 +40,51 @@
     },
 
     /**
+     *  Render the text of a page using the Markdown format.
+     *  This is an alternative for (and will eventually replace) the render function above.
+     */
+    renderMarkdown: function(src, page) {
+        if (!src)
+            return;
+
+        // create auto-toc from headers
+        src = WikiMarkup.renderHeaders(src, page, new java.lang.StringBuffer());
+
+        // set up wiki page search path
+        if (!page.isPersistent()) {
+            page = path.page;
+        }
+        var wikis = getServices("wiki", page);
+        // this is the wiki we're going to create a new page in if required
+        var mainWiki = wikis[0] || page;
+        // add this page as first element to wiki search path
+        wikis.unshift(page);
+
+        // do the markdown
+        var markdown = new JavaAdapter(Packages.helma.util.MarkdownProcessor, {
+            lookupLink: function(id) {
+                if (!id.isUrl()) {
+                    var wikipage;
+                    for each (var wiki in wikis) {
+                        wikipage = wiki.getWikiPage(id);
+                        if (wikipage) break;
+                    }
+                    if (!wikipage) {
+                        return [mainWiki.href("create") + "?name=" + id, "Create new wiki page"];
+                    } else {
+                        return [wikipage.href(), "Internal wiki link"];
+                    }
+                }
+                return null;
+            }
+        });
+        src = markdown.process(src);
+
+        var skin = createSkin(src);
+        page.renderSkin(skin);
+    },
+
+    /**
      * Like render(), but return result as a string.
      */
     renderAsString: function(src, page, param) {