From 036434dd7a3bc07707aa9d82e0edc0687551af23 Mon Sep 17 00:00:00 2001 From: Matthew Scragg Date: Fri, 5 Sep 2014 22:41:22 -0500 Subject: [PATCH] switch from showdown to marked add better yaml support for metadata --- bower.json | 20 +++---- realms/__init__.py | 10 ++-- realms/static/js/main.js | 83 +++++++++++++++++++++++++++--- realms/templates/layout.html | 6 ++- realms/templates/wiki/history.html | 2 +- 5 files changed, 97 insertions(+), 24 deletions(-) diff --git a/bower.json b/bower.json index a9256fd..c2ade71 100644 --- a/bower.json +++ b/bower.json @@ -2,16 +2,18 @@ "name": "realms", "version": "0.1.1", "dependencies": { - "components-bootstrap": "3.0.0", - "jquery": "1.9.1", - "components-underscore": "~1.5.1", - "requirejs": "~2.1.8", - "highlightjs": "~7.3.0", - "handlebars": "~1.0.0", - "components-font-awesome": "~3.2.1", + "components-bootstrap": "~3.2.0", + "jquery": "~1.11.1", + "components-underscore": "~1.5.2", + "requirejs": "~2.1.14", + "highlightjs": "~8.0.0", + "handlebars": "~2.0.0", + "components-font-awesome": "~4.1.0", "showdown": "~0.3.1", "keymaster": "madrobby/keymaster", - "ace": "~1.1.0", - "parsleyjs": "~2.0.3" + "ace": "~1.1.6", + "parsleyjs": "~2.0.3", + "marked": "~0.3.2", + "js-yaml": "~3.2.1" } } \ No newline at end of file diff --git a/realms/__init__.py b/realms/__init__.py index 787c8a1..29db79b 100644 --- a/realms/__init__.py +++ b/realms/__init__.py @@ -184,13 +184,13 @@ cache = Cache(app) assets = Assets(app) assets.register('main.js', - 'vendor/jquery/jquery.js', - 'vendor/components-underscore/underscore.js', + #'vendor/requirejs/require.js', + 'vendor/jquery/dist/jquery.js', 'vendor/components-bootstrap/js/bootstrap.js', 'vendor/handlebars/handlebars.js', - 'vendor/showdown/src/showdown.js', - 'vendor/showdown/src/extensions/table.js', - 'js/wmd.js', + 'vendor/js-yaml/dist/js-yaml.js', + 'vendor/marked/lib/marked.js', + #'js/wmd.js', 'js/html-sanitizer-minified.js', # don't minify? 'vendor/highlightjs/highlight.pack.js', 'vendor/parsleyjs/dist/parsley.js', diff --git a/realms/static/js/main.js b/realms/static/js/main.js index a36385a..e565661 100644 --- a/realms/static/js/main.js +++ b/realms/static/js/main.js @@ -1,17 +1,71 @@ +/* © 2013 j201 + * https://github.com/j201/meta-marked */ + +// Splits the given string into a meta section and a markdown section if a meta section is present, else returns null +function splitInput(str) { + if (str.slice(0, 3) !== '---') return; + + var matcher = /\n(\.{3}|\-{3})/g; + var metaEnd = matcher.exec(str); + + return metaEnd && [str.slice(0, metaEnd.index), str.slice(matcher.lastIndex)]; +} + +var metaMarked = function(src, opt, callback) { + if (Object.prototype.toString.call(src) !== '[object String]') + throw new TypeError('First parameter must be a string.'); + + var mySplitInput = splitInput(src); + if (mySplitInput) { + var meta; + try { + meta = jsyaml.safeLoad(mySplitInput[0]); + } catch(e) { + meta = null; + } + return { + meta: meta, + md: mySplitInput[1] + }; + } else { + return { + meta: null, + md: src + } + } +}; + +marked.setOptions({ + renderer: new marked.Renderer(), + gfm: true, + tables: true, + breaks: false, + pedantic: false, + sanitize: false, + smartLists: true, + smartypants: false +}); + // Init highlight JS hljs.initHighlightingOnLoad(); // Markdown Renderer -MDR = { - doc: null, - callback: WMD.convert, +var MDR = { + meta: null, + md: null, sanitize: true, // Override + parse: function(md){ return marked(md); }, convert: function(md, sanitize){ if (this.sanitize !== null) { sanitize = this.sanitize; } - this.doc = this.callback(md); - var html = this.doc.html; + this.md = md; + this.processMeta(); + try { + var html = this.parse(this.md); + } catch(e) { + return this.md; + } if (sanitize) { // Causes some problems with inline styles html = html_sanitize(html, function(url) { @@ -22,16 +76,29 @@ MDR = { return id; }); } - html = this.hook(html); + //html = this.hook(html); return html; }, + + processMeta: function() { + var doc = metaMarked(this.md); + this.md = doc.md; + this.meta = doc.meta; + if (this.meta) { + try { + var template = Handlebars.compile(this.md); + this.md = template(this.meta); + } catch(e) {} + } + }, + hook: function(html) { - if (!this.doc.metadata) { + if (!this.doc.meta) { return html; } try { var template = Handlebars.compile(html); - return template(this.doc.metadata); + return template(this.doc.meta); } catch(e) { return html; } diff --git a/realms/templates/layout.html b/realms/templates/layout.html index 2f25f41..8bb5e17 100644 --- a/realms/templates/layout.html +++ b/realms/templates/layout.html @@ -90,7 +90,11 @@ {% for bundle in g.assets['js'] %} {% assets bundle %} - + {% if bundle == 'editor.js' %} + + {% else %} + + {% endif %} {% endassets %} {% endfor %} {% block js %}{% endblock %} diff --git a/realms/templates/wiki/history.html b/realms/templates/wiki/history.html index c5b4588..90bf6ee 100644 --- a/realms/templates/wiki/history.html +++ b/realms/templates/wiki/history.html @@ -51,7 +51,7 @@ }); revs.reverse(); revs = revs.join(".."); - location.href = "{{ config.BASE_URL }}/_compare/{{ name }}/" + revs; + location.href = "{{ config.RELATIVE_PATH }}/_compare/{{ name }}/" + revs; }); });