From 596892164684a462dc3be8dac2cc14e681daff4c Mon Sep 17 00:00:00 2001 From: Sergey Kryzhanovsky Date: Fri, 23 Dec 2011 10:43:37 +0200 Subject: [PATCH] toc rewrite/refactoring --- GNUmakefile | 4 +- lib/_shmakowiki.js | 2203 ++++++++++++++++ lib/extensions/shmakowiki-toc/GNUmakefile | 16 + .../shmakowiki-toc/lib/_shmakowikitoc.js | 301 +++ .../shmakowiki-toc/lib/shmakowikitoc.js | 6 + .../shmakowiki-toc/src/shmakowikitoc.js | 1 + .../shmakowiki-toc/src/shmakowikitoc.ometajs | 61 + .../src/shmakowikitoc.ometajs.js | 242 ++ lib/extensions/shmakowiki-toc/src/toc2list.js | 60 + lib/extensions/shmakowiki-toc/tests/tests.js | 29 + lib/shmakowiki.js | 2237 +---------------- src/shmakowiki.js | 3 +- src/shmakowiki.ometajs | 3 +- src/shmakowiki.ometajs.js | 3 +- src/shmakowiki2html.ometajs | 6 +- src/shmakowiki2html.ometajs.js | 4 + 16 files changed, 2972 insertions(+), 2207 deletions(-) create mode 100644 lib/_shmakowiki.js create mode 100644 lib/extensions/shmakowiki-toc/GNUmakefile create mode 100644 lib/extensions/shmakowiki-toc/lib/_shmakowikitoc.js create mode 100644 lib/extensions/shmakowiki-toc/lib/shmakowikitoc.js create mode 100644 lib/extensions/shmakowiki-toc/src/shmakowikitoc.js create mode 100644 lib/extensions/shmakowiki-toc/src/shmakowikitoc.ometajs create mode 100644 lib/extensions/shmakowiki-toc/src/shmakowikitoc.ometajs.js create mode 100644 lib/extensions/shmakowiki-toc/src/toc2list.js create mode 100644 lib/extensions/shmakowiki-toc/tests/tests.js diff --git a/GNUmakefile b/GNUmakefile index bdc22dd..7495a50 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -6,9 +6,9 @@ src: $(patsubst %.ometajs,%.ometajs.js,$(wildcard src/*.ometajs)) %.ometajs.js: %.ometajs ometajs2js -i $< -o $@ -lib: lib/shmakowiki.js +lib: lib/_shmakowiki.js -lib/shmakowiki.js: src +lib/_shmakowiki.js: src -rm $@ for i in \ shmakowiki.js \ diff --git a/lib/_shmakowiki.js b/lib/_shmakowiki.js new file mode 100644 index 0000000..59dbc98 --- /dev/null +++ b/lib/_shmakowiki.js @@ -0,0 +1,2203 @@ +var ometajs = require('ometajs'), + OMeta = ometajs.OMeta, + ohl = require('ometa-highlighter'), + utils = require('./utils.js'), + shtoc = require('shmakowiki-toc'), + OmetaHighlighter = ohl.OmetaHighlighter, + OmetaHighlighterToHtml = ohl.OmetaHighlighterToHtml, + OmetaHighlighterToBemjson = ohl.OmetaHighlighterToBemjson, + ShmakoWikiToc = shtoc.ShmakoWikiToc, + ShmakoWikiTocToBemjson = shtoc.ShmakoWikiTocToBemjson, + ShmakoWikiTocToHtml = shtoc.ShmakoWikiTocToHtml; +var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; +var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; +var isImmutable = ometajs_.isImmutable; +var digitValue = ometajs_.digitValue; +var isSequenceable = ometajs_.isSequenceable; +var escapeChar = ometajs_.escapeChar; +var unescape = ometajs_.unescape; +var getTag = ometajs_.getTag; +var inspect = ometajs_.inspect; +var lift = ometajs_.lift; +var clone = ometajs_.clone; +var Parser = ometajs_.Parser; +var fail = ometajs_.fail; +var OMeta = ometajs_.OMeta; +var BSNullOptimization = ometajs_.BSNullOptimization; +var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; +var BSSeqInliner = ometajs_.BSSeqInliner; +var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; +var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; +var BSOMetaParser = ometajs_.BSOMetaParser; +var BSOMetaTranslator = ometajs_.BSOMetaTranslator; +var BSJSParser = ometajs_.BSJSParser; +var BSSemActionParser = ometajs_.BSSemActionParser; +var BSJSIdentity = ometajs_.BSJSIdentity; +var BSJSTranslator = ometajs_.BSJSTranslator; +var BSOMetaJSParser = ometajs_.BSOMetaJSParser; +var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; +if (global === ometajs_) { + fail = (function(fail) { + return function() { return fail }; + })(fail); + OMeta = require('ometajs').OMeta; +}{ + var ShmakoWiki = exports.ShmakoWiki = objectThatDelegatesTo(OMeta, { + oneOf: function() { + var $elf = this, _fromIdx = this.input.idx, a, a; + return this._or(function() { + return function() { + a = this._apply("anything"); + this._pred(a["length"] == 1); + return this._applyWithArgs("apply", a.pop()); + }.call(this); + }, function() { + return function() { + a = this._apply("anything"); + this._pred(a["length"] > 1); + return this._or(function() { + return this._applyWithArgs("apply", a.pop()); + }, function() { + return this._applyWithArgs("oneOf", a); + }); + }.call(this); + }); + }, + escapedChar: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + this._applyWithArgs("exactly", "~"); + c = this._apply("char"); + return c; + }.call(this); + }, + escaped: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._many1(function() { + return this._apply("escapedChar"); + }); + return [ "escaped", c.join("") ]; + }.call(this); + }, + b: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", "*"); + this._applyWithArgs("exactly", "*"); + return "**"; + }.call(this); + }, + i: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", "/"); + this._applyWithArgs("exactly", "/"); + return "//"; + }.call(this); + }, + u: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", "_"); + this._applyWithArgs("exactly", "_"); + return "__"; + }.call(this); + }, + s: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._not(function() { + return this._applyWithArgs("exactly", " "); + }); + this._applyWithArgs("exactly", "-"); + this._applyWithArgs("exactly", "-"); + "--"; + return this._not(function() { + return this._applyWithArgs("exactly", " "); + }); + }.call(this); + }, + m: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", "#"); + this._applyWithArgs("exactly", "#"); + return "##"; + }.call(this); + }, + sup: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", "^"); + this._applyWithArgs("exactly", "^"); + return "^^"; + }.call(this); + }, + sub: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", ","); + this._applyWithArgs("exactly", ","); + return ",,"; + }.call(this); + }, + ext: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", "%"); + this._applyWithArgs("exactly", "%"); + return "%%"; + }.call(this); + }, + linkStart: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "[": + return function() { + this._applyWithArgs("exactly", "["); + return "[["; + }.call(this); + case "(": + return function() { + this._applyWithArgs("exactly", "("); + return "(("; + }.call(this); + default: + throw fail(); + } + }.call(this); + }, + linkEnd: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "]": + return function() { + this._applyWithArgs("exactly", "]"); + return "]]"; + }.call(this); + case ")": + return function() { + this._applyWithArgs("exactly", ")"); + return "))"; + }.call(this); + default: + throw fail(); + } + }.call(this); + }, + l: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._or(function() { + return this._apply("linkStart"); + }, function() { + return this._apply("linkEnd"); + }); + }, + lineBreak: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", "\\"); + this._applyWithArgs("exactly", "\\"); + "\\\\"; + return [ "lineBreak", [] ]; + }.call(this); + }, + special: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._or(function() { + return this._apply("b"); + }, function() { + return this._apply("i"); + }, function() { + return this._apply("u"); + }, function() { + return this._apply("s"); + }, function() { + return this._apply("m"); + }, function() { + return this._apply("sup"); + }, function() { + return this._apply("sub"); + }, function() { + return this._apply("l"); + }, function() { + return this._apply("lineBreak"); + }); + return [ "special", c ]; + }.call(this); + }, + between: function() { + var $elf = this, _fromIdx = this.input.idx, t, n, nn, c; + return function() { + t = this._apply("anything"); + n = this._apply("anything"); + nn = function() { + this._applyWithArgs("apply", t); + return ShmakoWiki.arrAdd(n, t); + }.call(this); + c = this._many(function() { + return function() { + this._not(function() { + return this._applyWithArgs("oneOf", ShmakoWiki.arrCopy(nn)); + }); + return this._applyWithArgs("allInline", nn); + }.call(this); + }); + this._applyWithArgs("apply", t); + return c; + }.call(this); + }, + between_: function() { + var $elf = this, _fromIdx = this.input.idx, t, c; + return function() { + t = this._apply("anything"); + this._applyWithArgs("apply", t); + c = this._many(function() { + return function() { + this._not(function() { + return this._apply("special"); + }); + return this._apply("text"); + }.call(this); + }); + this._or(function() { + return this._not(function() { + return this._not(function() { + return this._apply("special"); + }); + }); + }, function() { + return this._apply("end"); + }); + return c; + }.call(this); + }, + bold: function() { + var $elf = this, _fromIdx = this.input.idx, n, c; + return function() { + n = this._apply("anything"); + c = this._applyWithArgs("between", "b", n); + return [ "bold", c ]; + }.call(this); + }, + bold_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._applyWithArgs("between_", "b"); + return [ "bold_", c ]; + }.call(this); + }, + italic: function() { + var $elf = this, _fromIdx = this.input.idx, n, c; + return function() { + n = this._apply("anything"); + c = this._applyWithArgs("between", "i", n); + return [ "italic", c ]; + }.call(this); + }, + italic_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._applyWithArgs("between_", "i"); + return [ "italic_", c ]; + }.call(this); + }, + underline: function() { + var $elf = this, _fromIdx = this.input.idx, n, c; + return function() { + n = this._apply("anything"); + c = this._applyWithArgs("between", "u", n); + return [ "underline", c ]; + }.call(this); + }, + underline_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._applyWithArgs("between_", "u"); + return [ "underline_", c ]; + }.call(this); + }, + strike: function() { + var $elf = this, _fromIdx = this.input.idx, n, c; + return function() { + n = this._apply("anything"); + c = this._applyWithArgs("between", "s", n); + return [ "strike", c ]; + }.call(this); + }, + strike_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._applyWithArgs("between_", "s"); + return [ "strike_", c ]; + }.call(this); + }, + monospace: function() { + var $elf = this, _fromIdx = this.input.idx, n, c; + return function() { + n = this._apply("anything"); + c = this._applyWithArgs("between", "m", n); + return [ "monospace", c ]; + }.call(this); + }, + monospace_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._applyWithArgs("between_", "m"); + return [ "monospace_", c ]; + }.call(this); + }, + superscript: function() { + var $elf = this, _fromIdx = this.input.idx, n, c; + return function() { + n = this._apply("anything"); + c = this._applyWithArgs("between", "sup", n); + return [ "superscript", c ]; + }.call(this); + }, + superscript_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._applyWithArgs("between_", "sup"); + return [ "superscript_", c ]; + }.call(this); + }, + subscript: function() { + var $elf = this, _fromIdx = this.input.idx, n, c; + return function() { + n = this._apply("anything"); + c = this._applyWithArgs("between", "sub", n); + return [ "subscript", c ]; + }.call(this); + }, + subscript_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._applyWithArgs("between_", "sub"); + return [ "subscript_", c ]; + }.call(this); + }, + link: function() { + var $elf = this, _fromIdx = this.input.idx, n, nn, c, cc; + return function() { + n = this._apply("anything"); + nn = function() { + this._apply("linkStart"); + return ShmakoWiki.arrAdd(n, "l"); + }.call(this); + c = this._many1(function() { + return function() { + this._not(function() { + return this._apply("space"); + }); + this._not(function() { + return this._apply("linkEnd"); + }); + return this._apply("char"); + }.call(this); + }); + this._apply("spacesNoNl"); + cc = this._many(function() { + return function() { + this._not(function() { + return this._applyWithArgs("oneOf", ShmakoWiki.arrCopy(nn)); + }); + return this._applyWithArgs("allInline", nn); + }.call(this); + }); + this._apply("linkEnd"); + return [ "link", c.join(""), cc ]; + }.call(this); + }, + link_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return this._or(function() { + return function() { + this._apply("linkStart"); + c = this._many1(function() { + return function() { + this._not(function() { + return this._apply("space"); + }); + return this._apply("char"); + }.call(this); + }); + this._or(function() { + return this._not(function() { + return this._not(function() { + return this._apply("space"); + }); + }); + }, function() { + return this._not(function() { + return this._not(function() { + return this._apply("special"); + }); + }); + }, function() { + return this._apply("end"); + }); + return [ "link_", c.join("") ]; + }.call(this); + }, function() { + return function() { + this._apply("linkEnd"); + return ""; + }.call(this); + }); + }, + mdash: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", " "); + this._applyWithArgs("exactly", "-"); + this._applyWithArgs("exactly", "-"); + this._applyWithArgs("exactly", " "); + " -- "; + return " – "; + }.call(this); + }, + text: function() { + var $elf = this, _fromIdx = this.input.idx, c, c; + return this._or(function() { + return function() { + c = this._many1(function() { + return function() { + this._not(function() { + return this._apply("special"); + }); + this._not(function() { + return this._apply("escapedChar"); + }); + return this._or(function() { + return this._apply("mdash"); + }, function() { + return this._apply("char"); + }); + }.call(this); + }); + return c.join(""); + }.call(this); + }, function() { + return function() { + c = this._apply("escaped"); + return c; + }.call(this); + }); + }, + inline: function() { + var $elf = this, _fromIdx = this.input.idx, n; + return function() { + n = this._apply("anything"); + return this._or(function() { + return this._applyWithArgs("bold", n); + }, function() { + return this._applyWithArgs("italic", n); + }, function() { + return this._applyWithArgs("underline", n); + }, function() { + return this._applyWithArgs("strike", n); + }, function() { + return this._applyWithArgs("monospace", n); + }, function() { + return this._applyWithArgs("superscript", n); + }, function() { + return this._applyWithArgs("subscript", n); + }, function() { + return this._applyWithArgs("link", n); + }, function() { + return this._apply("lineBreak"); + }, function() { + return this._apply("text"); + }); + }.call(this); + }, + inline_: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._or(function() { + return this._apply("bold_"); + }, function() { + return this._apply("italic_"); + }, function() { + return this._apply("underline_"); + }, function() { + return this._apply("superscript_"); + }, function() { + return this._apply("subscript_"); + }, function() { + return this._apply("strike_"); + }, function() { + return this._apply("monospace_"); + }, function() { + return this._apply("link_"); + }); + }, + allInline: function() { + var $elf = this, _fromIdx = this.input.idx, n; + return function() { + n = this._apply("anything"); + return this._or(function() { + return this._applyWithArgs("inline", n); + }, function() { + return function() { + this._not(function() { + return this._applyWithArgs("inline", n); + }); + return this._apply("inline_"); + }.call(this); + }); + }.call(this); + }, + topInline: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._many1(function() { + return this._applyWithArgs("allInline", []); + }); + }, + spacesNoNl: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._many(function() { + return function() { + this._not(function() { + return this._applyWithArgs("exactly", "\n"); + }); + return this._apply("space"); + }.call(this); + }); + }, + spacesNlSpaces: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._apply("spacesNoNl"); + this._applyWithArgs("exactly", "\n"); + this._apply("spacesNoNl"); + this._not(function() { + return this._apply("end"); + }); + return " "; + }.call(this); + }, + nl: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + this._applyWithArgs("exactly", "\n"); + return this._many1(function() { + return function() { + this._apply("spacesNoNl"); + return this._applyWithArgs("exactly", "\n"); + }.call(this); + }); + }.call(this); + }, + headerStart: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._many1(function() { + return this._applyWithArgs("exactly", "="); + }); + this._apply("spacesNoNl"); + return c.join("")["length"] - 1; + }.call(this); + }, + headerEnd: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._or(function() { + return function() { + switch (this._apply("anything")) { + case "\n": + return this._many(function() { + return function() { + this._apply("spacesNoNl"); + return this._applyWithArgs("exactly", "\n"); + }.call(this); + }); + default: + throw fail(); + } + }.call(this); + }, function() { + return this._apply("end"); + }); + }, + headerAnchor: function() { + var $elf = this, _fromIdx = this.input.idx, a; + return this._or(function() { + return function() { + this._many1(function() { + return this._applyWithArgs("exactly", "="); + }); + a = this._many(function() { + return function() { + this._not(function() { + return this._applyWithArgs("exactly", "\n"); + }); + return this._apply("char"); + }.call(this); + }); + return a.join(""); + }.call(this); + }, function() { + return function() { + this._apply("empty"); + return ""; + }.call(this); + }); + }, + header: function() { + var $elf = this, _fromIdx = this.input.idx, l, cc, c, anchor; + return function() { + l = this._apply("headerStart"); + c = function() { + cc = this._many1(function() { + return function() { + this._not(function() { + return this._apply("headerEnd"); + }); + this._not(function() { + return this._applyWithArgs("exactly", "="); + }); + return this._apply("char"); + }.call(this); + }); + return cc.join(""); + }.call(this); + anchor = this._apply("headerAnchor"); + this._apply("headerEnd"); + return function() { + var hAST = ShmakoWiki.matchAll(c, "topLevel"), hAnchor = utils.transliterate("ru", anchor["length"] ? anchor : ShmakoWikiToPlain.match(hAST, "topLevel")); + return [ "header" + (l <= 6 ? l : 6), hAST, hAnchor ]; + }.call(this); + }.call(this); + }, + blockEnd: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._or(function() { + return function() { + switch (this._apply("anything")) { + case "\n": + return this._not(function() { + return this._not(function() { + return this._apply("extBlockStart"); + }); + }); + default: + throw fail(); + } + }.call(this); + }, function() { + return this._apply("nl"); + }, function() { + return function() { + switch (this._apply("anything")) { + case "\n": + return this._or(function() { + return this._not(function() { + return this._not(function() { + return this._apply("anyLi"); + }); + }); + }, function() { + return this._not(function() { + return this._not(function() { + return this._apply("headerStart"); + }); + }); + }); + default: + throw fail(); + } + }.call(this); + }, function() { + return function() { + this._many(function() { + return this._applyWithArgs("exactly", "\n"); + }); + return this._apply("end"); + }.call(this); + }); + }, + para: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._many1(function() { + return function() { + this._not(function() { + return this._apply("blockEnd"); + }); + return this._or(function() { + return this._apply("spacesNlSpaces"); + }, function() { + return this._apply("char"); + }); + }.call(this); + }); + c.join(""); + this._apply("blockEnd"); + return [ "para", ShmakoWiki.matchAll(c, "topInline") ]; + }.call(this); + }, + uli: function() { + var $elf = this, _fromIdx = this.input.idx, s; + return function() { + s = this._apply("spacesNoNl"); + this._applyWithArgs("exactly", "*"); + this._not(function() { + return this._applyWithArgs("exactly", "*"); + }); + this._apply("spacesNoNl"); + return s.join(""); + }.call(this); + }, + oli: function() { + var $elf = this, _fromIdx = this.input.idx, s; + return function() { + s = this._apply("spacesNoNl"); + this._many1(function() { + return this._apply("digit"); + }); + this._applyWithArgs("exactly", "."); + this._apply("spacesNoNl"); + return s.join(""); + }.call(this); + }, + anyLi: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._or(function() { + return this._apply("uli"); + }, function() { + return this._apply("oli"); + }); + }, + listItemContent: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._many1(function() { + return function() { + this._not(function() { + return this._apply("blockEnd"); + }); + return this._apply("char"); + }.call(this); + }); + return ShmakoWiki.matchAll(c.join(""), "topInline"); + }.call(this); + }, + anySubList: function() { + var $elf = this, _fromIdx = this.input.idx, l, s, sl; + return function() { + l = this._apply("anything"); + this._applyWithArgs("exactly", "\n"); + this._not(function() { + return this._not(function() { + return s = this._apply("anyLi"); + }); + }); + this._pred(s["length"] > l["length"]); + sl = this._apply("anyList"); + return sl; + }.call(this); + }, + listItem: function() { + var $elf = this, _fromIdx = this.input.idx, t, l, s, c, cc; + return function() { + t = this._apply("anything"); + l = this._apply("anything"); + s = this._applyWithArgs("apply", t); + this._pred(s["length"] == l["length"]); + c = this._apply("listItemContent"); + cc = this._or(function() { + return this._many1(function() { + return this._applyWithArgs("anySubList", s); + }); + }, function() { + return function() { + this._or(function() { + return function() { + switch (this._apply("anything")) { + case "\n": + return this._not(function() { + return this._not(function() { + return this._applyWithArgs("apply", t); + }); + }); + default: + throw fail(); + } + }.call(this); + }, function() { + return this._not(function() { + return this._not(function() { + return this._apply("blockEnd"); + }); + }); + }); + return ""; + }.call(this); + }); + return function() { + if (cc) { + c = c.concat(cc); + } else { + undefined; + } + return [ t + "stItem", c ]; + }.call(this); + }.call(this); + }, + list: function() { + var $elf = this, _fromIdx = this.input.idx, t, s, c; + return function() { + t = this._apply("anything"); + this._not(function() { + return this._not(function() { + return s = this._applyWithArgs("apply", t); + }); + }); + c = this._many1(function() { + return this._applyWithArgs("listItem", t, s); + }); + return [ t + "st", c ]; + }.call(this); + }, + anyList: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._or(function() { + return this._applyWithArgs("list", "uli"); + }, function() { + return this._applyWithArgs("list", "oli"); + }); + }, + extBlockStart: function() { + var $elf = this, _fromIdx = this.input.idx, t, tt, c, cc; + return function() { + tt = this._or(function() { + return function() { + this._apply("ext"); + t = this._apply("char"); + this._applyWithArgs("exactly", "%"); + return t; + }.call(this); + }, function() { + return function() { + this._apply("ext"); + return ""; + }.call(this); + }); + c = this._many(function() { + return function() { + this._not(function() { + return this._apply("space"); + }); + return this._apply("char"); + }.call(this); + }); + this._apply("spacesNoNl"); + cc = this._many(function() { + return function() { + this._not(function() { + return this._applyWithArgs("exactly", "\n"); + }); + return this._apply("char"); + }.call(this); + }); + this._applyWithArgs("exactly", "\n"); + return [ tt, c.join(""), cc.join("") ]; + }.call(this); + }, + extBlockEnd: function() { + var $elf = this, _fromIdx = this.input.idx, t, t; + return this._or(function() { + return function() { + t = this._apply("anything"); + this._pred(t["length"] == 0); + this._many(function() { + return this._apply("space"); + }); + return this._apply("ext"); + }.call(this); + }, function() { + return function() { + t = this._apply("char"); + this._pred(t["length"] > 0); + this._many(function() { + return this._apply("space"); + }); + this._applyWithArgs("exactly", "%"); + this._applyWithArgs("seq", t); + return this._apply("ext"); + }.call(this); + }); + }, + extBlock: function() { + var $elf = this, _fromIdx = this.input.idx, s, cc, c; + return function() { + s = this._apply("extBlockStart"); + c = function() { + cc = this._many1(function() { + return function() { + this._not(function() { + return this._applyWithArgs("extBlockEnd", s[0]); + }); + return this._apply("char"); + }.call(this); + }); + return cc.join(""); + }.call(this); + this._applyWithArgs("extBlockEnd", s[0]); + this._or(function() { + return this._apply("blockEnd"); + }, function() { + return function() { + switch (this._apply("anything")) { + case "\n": + return "\n"; + default: + throw fail(); + } + }.call(this); + }); + return [ "extension", s[1], ShmakoWiki["extensions"].hasOwnProperty(s[1]) ? ShmakoWiki["extensions"][s[1]](c, s[2]) : c, s[2] ]; + }.call(this); + }, + allBlock: function() { + var $elf = this, _fromIdx = this.input.idx, l; + return this._or(function() { + return this._apply("extBlock"); + }, function() { + return function() { + l = this._apply("anyList"); + this._apply("blockEnd"); + return l; + }.call(this); + }, function() { + return this._apply("header"); + }, function() { + return this._apply("para"); + }); + }, + topLevel: function() { + var $elf = this, _fromIdx = this.input.idx; + return this._many1(function() { + return this._apply("allBlock"); + }); + } + }); + ShmakoWiki["extensions"] = { + ohl: function(c, p) { + return OmetaHighlighter.matchAll(c, p); + }, + toc: function(c, p) { + return ShmakoWiki.matchAll(c, "topLevel"); + } + }; + ShmakoWiki["extensions"]["hl"] = ShmakoWiki["extensions"]["ohl"]; + ShmakoWiki["arrJoin"] = function(arr1, arr2) { + var newArr = ShmakoWiki.arrCopy(arr1); + for (var i = 0; i < arr2["length"]; i++) { + newArr[newArr["length"]] = arr2[i]; + } + return newArr; + }; + ShmakoWiki["arrCopy"] = function(arr) { + var newArr = []; + for (var i = 0; i < arr["length"]; i++) { + newArr[newArr["length"]] = arr[i]; + } + return newArr; + }; + ShmakoWiki["arrAdd"] = function(arr, elem) { + for (var i = 0; i < arr["length"]; i++) { + if (arr[i] == elem) { + return arr; + } else { + undefined; + } + } + arr[arr["length"]] = elem; + return arr; + }; +}var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; +var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; +var isImmutable = ometajs_.isImmutable; +var digitValue = ometajs_.digitValue; +var isSequenceable = ometajs_.isSequenceable; +var escapeChar = ometajs_.escapeChar; +var unescape = ometajs_.unescape; +var getTag = ometajs_.getTag; +var inspect = ometajs_.inspect; +var lift = ometajs_.lift; +var clone = ometajs_.clone; +var Parser = ometajs_.Parser; +var fail = ometajs_.fail; +var OMeta = ometajs_.OMeta; +var BSNullOptimization = ometajs_.BSNullOptimization; +var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; +var BSSeqInliner = ometajs_.BSSeqInliner; +var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; +var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; +var BSOMetaParser = ometajs_.BSOMetaParser; +var BSOMetaTranslator = ometajs_.BSOMetaTranslator; +var BSJSParser = ometajs_.BSJSParser; +var BSSemActionParser = ometajs_.BSSemActionParser; +var BSJSIdentity = ometajs_.BSJSIdentity; +var BSJSTranslator = ometajs_.BSJSTranslator; +var BSOMetaJSParser = ometajs_.BSOMetaJSParser; +var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; +if (global === ometajs_) { + fail = (function(fail) { + return function() { return fail }; + })(fail); + OMeta = require('ometajs').OMeta; +}{ + var ShmakoWikiToHtml = exports.ShmakoWikiToHtml = objectThatDelegatesTo(OMeta, { + keyword: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "monospace": + return "monospace"; + case "strike": + return "strike"; + case "header6": + return "header6"; + case "header3": + return "header3"; + case "para": + return "para"; + case "italic": + return "italic"; + case "underline_": + return "underline_"; + case "header2": + return "header2"; + case "header4": + return "header4"; + case "italic_": + return "italic_"; + case "underline": + return "underline"; + case "header5": + return "header5"; + case "strike_": + return "strike_"; + case "link_": + return "link_"; + case "subscript": + return "subscript"; + case "escaped": + return "escaped"; + case "superscript": + return "superscript"; + case "ulistItem": + return "ulistItem"; + case "olistItem": + return "olistItem"; + case "header1": + return "header1"; + case "subscript_": + return "subscript_"; + case "bold_": + return "bold_"; + case "monospace_": + return "monospace_"; + case "olist": + return "olist"; + case "ulist": + return "ulist"; + case "lineBreak": + return "lineBreak"; + case "superscript_": + return "superscript_"; + case "link": + return "link"; + case "extension": + return "extension"; + case "bold": + return "bold"; + default: + throw fail(); + } + }.call(this); + }, + token: function() { + var $elf = this, _fromIdx = this.input.idx, t, ans, c; + return this._or(function() { + return function() { + this._form(function() { + return function() { + t = this._apply("keyword"); + return ans = this._applyWithArgs("apply", t); + }.call(this); + }); + return ans; + }.call(this); + }, function() { + return function() { + c = this._apply("anything"); + return ShmakoWikiToHtml._escape(c); + }.call(this); + }); + }, + tokens: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + this._form(function() { + return c = this._many(function() { + return this._apply("token"); + }); + }); + return c.join(""); + }.call(this); + }, + bold: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + bold_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + italic: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + italic_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + underline: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + underline_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + strike: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + strike_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + monospace: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + monospace_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + superscript: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + superscript_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + subscript: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + subscript_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + link: function() { + var $elf = this, _fromIdx = this.input.idx, c, c, cc; + return this._or(function() { + return function() { + c = this._apply("token"); + this._form(function() { + return undefined; + }); + return '' + c + ""; + }.call(this); + }, function() { + return function() { + c = this._apply("token"); + cc = this._apply("tokens"); + return '' + cc + ""; + }.call(this); + }); + }, + link_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("token"); + return '' + c + ""; + }.call(this); + }, + lineBreak: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("anything"); + return "
"; + }.call(this); + }, + escaped: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "" + c + ""; + }.call(this); + }, + para: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "

" + c + "

"; + }.call(this); + }, + header1: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return '

' + c + "

"; + }.call(this); + }, + header2: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return '

' + c + "

"; + }.call(this); + }, + header3: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return '

' + c + "

"; + }.call(this); + }, + header4: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return '

' + c + "

"; + }.call(this); + }, + header5: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return '
' + c + "
"; + }.call(this); + }, + header6: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return '
' + c + "
"; + }.call(this); + }, + olist: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "
    " + c + "
"; + }.call(this); + }, + ulist: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return ""; + }.call(this); + }, + olistItem: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "
  • " + c + "
  • "; + }.call(this); + }, + ulistItem: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return "
  • " + c + "
  • "; + }.call(this); + }, + extension: function() { + var $elf = this, _fromIdx = this.input.idx, t, c, p; + return function() { + t = this._apply("anything"); + c = this._apply("anything"); + p = this._apply("anything"); + return ShmakoWikiToHtml["extensions"].hasOwnProperty(t) ? ShmakoWikiToHtml["extensions"][t](c, p) : "
    " + ShmakoWikiToHtml._escape(c) + "
    "; + }.call(this); + }, + topLevel: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return '
    ' + c + "
    "; + }.call(this); + } + }); + ShmakoWikiToHtml["extensions"] = { + html: function(c) { + return c; + }, + ohl: function(c, p) { + return OmetaHighlighterToHtml.match(c, "topLevel"); + }, + hljs: function(c, p) { + return '
    ' + ShmakoWikiToHtml._escape(c) + "
    "; + }, + toc: function(c, p) { + console.log(ShmakoWikiTocToHtml.tocToHtml(c[0], ShmakoWikiToHtml)); + return ShmakoWikiTocToHtml.tocToHtml(c[0], ShmakoWikiToHtml); + } + }; + ShmakoWikiToHtml["extensions"]["hl"] = ShmakoWikiToHtml["extensions"]["ohl"]; + ShmakoWikiToHtml["_escape"] = function() { + var amp = new RegExp("&", "g"), lt = new RegExp("<", "g"), gt = new RegExp(">", "g"), apos = new RegExp("'", "g"), quot = new RegExp('"', "g"); + return function(s) { + return String(s).replace(amp, "&").replace(lt, "<").replace(gt, ">").replace(apos, "'").replace(quot, """); + }; + }(); +}var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; +var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; +var isImmutable = ometajs_.isImmutable; +var digitValue = ometajs_.digitValue; +var isSequenceable = ometajs_.isSequenceable; +var escapeChar = ometajs_.escapeChar; +var unescape = ometajs_.unescape; +var getTag = ometajs_.getTag; +var inspect = ometajs_.inspect; +var lift = ometajs_.lift; +var clone = ometajs_.clone; +var Parser = ometajs_.Parser; +var fail = ometajs_.fail; +var OMeta = ometajs_.OMeta; +var BSNullOptimization = ometajs_.BSNullOptimization; +var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; +var BSSeqInliner = ometajs_.BSSeqInliner; +var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; +var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; +var BSOMetaParser = ometajs_.BSOMetaParser; +var BSOMetaTranslator = ometajs_.BSOMetaTranslator; +var BSJSParser = ometajs_.BSJSParser; +var BSSemActionParser = ometajs_.BSSemActionParser; +var BSJSIdentity = ometajs_.BSJSIdentity; +var BSJSTranslator = ometajs_.BSJSTranslator; +var BSOMetaJSParser = ometajs_.BSOMetaJSParser; +var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; +if (global === ometajs_) { + fail = (function(fail) { + return function() { return fail }; + })(fail); + OMeta = require('ometajs').OMeta; +}{ + var ShmakoWikiToBemjson = exports.ShmakoWikiToBemjson = objectThatDelegatesTo(OMeta, { + keyword: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "monospace": + return "monospace"; + case "strike": + return "strike"; + case "header6": + return "header6"; + case "header3": + return "header3"; + case "para": + return "para"; + case "italic": + return "italic"; + case "underline_": + return "underline_"; + case "header2": + return "header2"; + case "header4": + return "header4"; + case "italic_": + return "italic_"; + case "underline": + return "underline"; + case "header5": + return "header5"; + case "strike_": + return "strike_"; + case "link_": + return "link_"; + case "subscript": + return "subscript"; + case "escaped": + return "escaped"; + case "superscript": + return "superscript"; + case "ulistItem": + return "ulistItem"; + case "olistItem": + return "olistItem"; + case "header1": + return "header1"; + case "subscript_": + return "subscript_"; + case "bold_": + return "bold_"; + case "monospace_": + return "monospace_"; + case "olist": + return "olist"; + case "ulist": + return "ulist"; + case "lineBreak": + return "lineBreak"; + case "superscript_": + return "superscript_"; + case "link": + return "link"; + case "extension": + return "extension"; + case "bold": + return "bold"; + default: + throw fail(); + } + }.call(this); + }, + token: function() { + var $elf = this, _fromIdx = this.input.idx, t, ans, c; + return this._or(function() { + return function() { + this._form(function() { + return function() { + t = this._apply("keyword"); + return ans = this._applyWithArgs("apply", t); + }.call(this); + }); + return ans; + }.call(this); + }, function() { + return function() { + c = this._apply("anything"); + return ShmakoWikiToBemjson._escape(c); + }.call(this); + }); + }, + tokens: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + this._form(function() { + return c = this._many(function() { + return this._apply("token"); + }); + }); + return c; + }.call(this); + }, + bold: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "b", + content: c + }; + }.call(this); + }, + bold_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "b", + content: c + }; + }.call(this); + }, + italic: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "i", + content: c + }; + }.call(this); + }, + italic_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "i", + content: c + }; + }.call(this); + }, + underline: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "u", + content: c + }; + }.call(this); + }, + underline_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "u", + content: c + }; + }.call(this); + }, + strike: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "s", + content: c + }; + }.call(this); + }, + strike_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "s", + content: c + }; + }.call(this); + }, + monospace: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "tt", + content: c + }; + }.call(this); + }, + monospace_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "tt", + content: c + }; + }.call(this); + }, + superscript: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "sup", + content: c + }; + }.call(this); + }, + superscript_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "sup", + content: c + }; + }.call(this); + }, + subscript: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "sub", + content: c + }; + }.call(this); + }, + subscript_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "sub", + content: c + }; + }.call(this); + }, + link: function() { + var $elf = this, _fromIdx = this.input.idx, c, c, cc; + return this._or(function() { + return function() { + c = this._apply("token"); + this._form(function() { + return undefined; + }); + return { + block: "b-link", + url: c, + content: c + }; + }.call(this); + }, function() { + return function() { + c = this._apply("token"); + cc = this._apply("tokens"); + return { + block: "b-link", + url: c, + content: cc + }; + }.call(this); + }); + }, + link_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("token"); + return { + block: "b-link", + url: c, + content: c + }; + }.call(this); + }, + lineBreak: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("anything"); + return { + tag: "br" + }; + }.call(this); + }, + escaped: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + tag: "span", + content: c + }; + }.call(this); + }, + para: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + elem: "p", + content: c + }; + }.call(this); + }, + header1: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return { + elem: "h1", + attrs: { + id: a + }, + content: c + }; + }.call(this); + }, + header2: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return { + elem: "h2", + attrs: { + id: a + }, + content: c + }; + }.call(this); + }, + header3: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return { + elem: "h3", + attrs: { + id: a + }, + content: c + }; + }.call(this); + }, + header4: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return { + elem: "h4", + attrs: { + id: a + }, + content: c + }; + }.call(this); + }, + header5: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return { + elem: "h5", + attrs: { + id: a + }, + content: c + }; + }.call(this); + }, + header6: function() { + var $elf = this, _fromIdx = this.input.idx, c, a; + return function() { + c = this._apply("tokens"); + a = this._apply("anything"); + return { + elem: "h6", + attrs: { + id: a + }, + content: c + }; + }.call(this); + }, + olist: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + elem: "ol", + content: c + }; + }.call(this); + }, + ulist: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + elem: "ul", + content: c + }; + }.call(this); + }, + olistItem: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + elem: "li", + content: c + }; + }.call(this); + }, + ulistItem: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + elem: "li", + content: c + }; + }.call(this); + }, + extension: function() { + var $elf = this, _fromIdx = this.input.idx, t, c, p; + return function() { + t = this._apply("anything"); + c = this._apply("anything"); + p = this._apply("anything"); + return ShmakoWikiToBemjson["extensions"].hasOwnProperty(t) ? ShmakoWikiToBemjson["extensions"][t](c, p) : { + tag: "div", + content: ShmakoWikiToBemjson._escape(c) + }; + }.call(this); + }, + topLevel: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return { + block: "b-text", + content: c + }; + }.call(this); + } + }); + ShmakoWikiToBemjson["extensions"] = { + html: function(c) { + return c; + }, + ohl: function(c, p) { + return OmetaHighlighterToBemjson.match(c, "topLevel"); + }, + hljs: function(c, p) { + return { + block: "b-code", + cls: p, + content: ShmakoWikiToBemjson._escape(c) + }; + }, + toc: function(c, p) { + return ShmakoWikiTocToBemjson.tocToBemjson(c[0], ShmakoWikiToBemjson); + } + }; + ShmakoWikiToBemjson["extensions"]["hl"] = ShmakoWikiToBemjson["extensions"]["ohl"]; + ShmakoWikiToBemjson["_escape"] = function() { + var amp = new RegExp("&", "g"), lt = new RegExp("<", "g"), gt = new RegExp(">", "g"), apos = new RegExp("'", "g"), quot = new RegExp('"', "g"); + return function(s) { + return String(s).replace(amp, "&").replace(lt, "<").replace(gt, ">").replace(apos, "'").replace(quot, """); + }; + }(); +}var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; +var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; +var isImmutable = ometajs_.isImmutable; +var digitValue = ometajs_.digitValue; +var isSequenceable = ometajs_.isSequenceable; +var escapeChar = ometajs_.escapeChar; +var unescape = ometajs_.unescape; +var getTag = ometajs_.getTag; +var inspect = ometajs_.inspect; +var lift = ometajs_.lift; +var clone = ometajs_.clone; +var Parser = ometajs_.Parser; +var fail = ometajs_.fail; +var OMeta = ometajs_.OMeta; +var BSNullOptimization = ometajs_.BSNullOptimization; +var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; +var BSSeqInliner = ometajs_.BSSeqInliner; +var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; +var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; +var BSOMetaParser = ometajs_.BSOMetaParser; +var BSOMetaTranslator = ometajs_.BSOMetaTranslator; +var BSJSParser = ometajs_.BSJSParser; +var BSSemActionParser = ometajs_.BSSemActionParser; +var BSJSIdentity = ometajs_.BSJSIdentity; +var BSJSTranslator = ometajs_.BSJSTranslator; +var BSOMetaJSParser = ometajs_.BSOMetaJSParser; +var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; +if (global === ometajs_) { + fail = (function(fail) { + return function() { return fail }; + })(fail); + OMeta = require('ometajs').OMeta; +}var ShmakoWikiToPlain = exports.ShmakoWikiToPlain = objectThatDelegatesTo(OMeta, { + keyword: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "monospace": + return "monospace"; + case "strike": + return "strike"; + case "underline": + return "underline"; + case "header3": + return "header3"; + case "para": + return "para"; + case "italic": + return "italic"; + case "underline_": + return "underline_"; + case "header6": + return "header6"; + case "header4": + return "header4"; + case "italic_": + return "italic_"; + case "strike_": + return "strike_"; + case "header5": + return "header5"; + case "escaped": + return "escaped"; + case "header2": + return "header2"; + case "subscript": + return "subscript"; + case "superscript": + return "superscript"; + case "header1": + return "header1"; + case "subscript_": + return "subscript_"; + case "bold_": + return "bold_"; + case "monospace_": + return "monospace_"; + case "superscript_": + return "superscript_"; + case "bold": + return "bold"; + default: + throw fail(); + } + }.call(this); + }, + token: function() { + var $elf = this, _fromIdx = this.input.idx, t, ans, c; + return this._or(function() { + return function() { + this._form(function() { + return function() { + t = this._apply("keyword"); + return ans = this._applyWithArgs("apply", t); + }.call(this); + }); + return ans; + }.call(this); + }, function() { + return function() { + c = this._apply("anything"); + return c; + }.call(this); + }); + }, + tokens: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + this._form(function() { + return c = this._many(function() { + return this._apply("token"); + }); + }); + return c; + }.call(this); + }, + bold: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + bold_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + italic: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + italic_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + underline: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + underline_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + strike: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + strike_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + monospace: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + monospace_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + superscript: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + superscript_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + subscript: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + subscript_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + escaped: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + para: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + header1: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + header2: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + header3: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + header4: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + header5: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + header6: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + }, + topLevel: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c.join(""); + }.call(this); + } +}); \ No newline at end of file diff --git a/lib/extensions/shmakowiki-toc/GNUmakefile b/lib/extensions/shmakowiki-toc/GNUmakefile new file mode 100644 index 0000000..ca3c292 --- /dev/null +++ b/lib/extensions/shmakowiki-toc/GNUmakefile @@ -0,0 +1,16 @@ +all: lib + +src: $(patsubst %.ometajs,%.ometajs.js,$(wildcard src/*.ometajs)) + +%.ometajs.js: %.ometajs + ometajs2js -i $< -o $@ + +lib: src + cat src/toc2list.js > lib/_shmakowikitoc.js + cat src/shmakowikitoc.js >> lib/_shmakowikitoc.js + cat src/shmakowikitoc.ometajs.js >> lib/_shmakowikitoc.js + +tests: + node $@/tests.js + +.PHONY: all tests diff --git a/lib/extensions/shmakowiki-toc/lib/_shmakowikitoc.js b/lib/extensions/shmakowiki-toc/lib/_shmakowikitoc.js new file mode 100644 index 0000000..d2ba20d --- /dev/null +++ b/lib/extensions/shmakowiki-toc/lib/_shmakowikitoc.js @@ -0,0 +1,301 @@ +var TocToList = { + + createChain: function(levels, header, n, prevn) { + var lh = levels[prevn], ul, li; + + for (var i = prevn + 1; i <= n; i++) { + ul = this.createUl(this.createLi()); + this.getLi(lh)[1].push(ul); + lh = levels[i] = ul; + } + + this.getLi(lh)[1].push(this.createLink(header[1], header[2])); + }, + + getLi: function(ul) { + var li = ul[1][ul[1].length - 1]; + + if (!li) { + li = this.createLi(); + ul[1].push(li); + } + + return li; + }, + + convert: function(headers) { + var levels = { 1: this.createUl() }, + h, n, prev = 1; + + for (var i = 0; i < headers.length; i++) { + h = headers[i]; + n = this.getLevelNumber(h[0]); + if (n > prev) { + this.createChain(levels, h, n, prev); + } else { + levels[n][1].push(this.createLi(h[1], h[2])); + } + prev = n; + } + + return levels[1]; + }, + + createUl: function(li) { + return ['ulist', li ? [li] : []]; + }, + + createLi: function(content, a) { + return ['ulistItem', ['listItem', content ? this.createLink(content, a) : null]]; + }, + + createLink: function(content, a) { + return ['link', '#' + a, content]; + }, + + getLevelNumber: function(text) { + return text.charCodeAt(text.length - 1) - 48; + } + +};var api = require('../../../shmakowiki.js');var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; +var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; +var isImmutable = ometajs_.isImmutable; +var digitValue = ometajs_.digitValue; +var isSequenceable = ometajs_.isSequenceable; +var escapeChar = ometajs_.escapeChar; +var unescape = ometajs_.unescape; +var getTag = ometajs_.getTag; +var inspect = ometajs_.inspect; +var lift = ometajs_.lift; +var clone = ometajs_.clone; +var Parser = ometajs_.Parser; +var fail = ometajs_.fail; +var OMeta = ometajs_.OMeta; +var BSNullOptimization = ometajs_.BSNullOptimization; +var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; +var BSSeqInliner = ometajs_.BSSeqInliner; +var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; +var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; +var BSOMetaParser = ometajs_.BSOMetaParser; +var BSOMetaTranslator = ometajs_.BSOMetaTranslator; +var BSJSParser = ometajs_.BSJSParser; +var BSSemActionParser = ometajs_.BSSemActionParser; +var BSJSIdentity = ometajs_.BSJSIdentity; +var BSJSTranslator = ometajs_.BSJSTranslator; +var BSOMetaJSParser = ometajs_.BSOMetaJSParser; +var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; +if (global === ometajs_) { + fail = (function(fail) { + return function() { return fail }; + })(fail); + OMeta = require('ometajs').OMeta; +}{ + var ShmakoWikiToc = exports.ShmakoWikiToc = objectThatDelegatesTo(OMeta, { + keyword: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "monospace": + return "monospace"; + case "strike": + return "strike"; + case "underline": + return "underline"; + case "link_": + return "link_"; + case "para": + return "para"; + case "italic": + return "italic"; + case "underline_": + return "underline_"; + case "italic_": + return "italic_"; + case "strike_": + return "strike_"; + case "escaped": + return "escaped"; + case "subscript": + return "subscript"; + case "superscript": + return "superscript"; + case "ulistItem": + return "ulistItem"; + case "olistItem": + return "olistItem"; + case "subscript_": + return "subscript_"; + case "bold_": + return "bold_"; + case "monospace_": + return "monospace_"; + case "olist": + return "olist"; + case "ulist": + return "ulist"; + case "lineBreak": + return "lineBreak"; + case "superscript_": + return "superscript_"; + case "bold": + return "bold"; + default: + throw fail(); + } + }.call(this); + }, + headertag: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "header4": + return "header4"; + case "header1": + return "header1"; + case "header3": + return "header3"; + case "header6": + return "header6"; + case "header2": + return "header2"; + case "header5": + return "header5"; + default: + throw fail(); + } + }.call(this); + }, + header: function() { + var $elf = this, _fromIdx = this.input.idx, t, c, a; + return function() { + this._form(function() { + return function() { + t = this._apply("headertag"); + c = this._apply("tokens"); + return a = this._apply("anything"); + }.call(this); + }); + return function() { + var h = [ t, c, a ]; + ShmakoWikiToc.addTocHeader(h); + return h; + }.call(this); + }.call(this); + }, + token: function() { + var $elf = this, _fromIdx = this.input.idx, t, ans, c; + return this._or(function() { + return this._apply("header"); + }, function() { + return this._apply("extension"); + }, function() { + return function() { + this._form(function() { + return function() { + t = this._apply("keyword"); + return ans = this._apply("tokens"); + }.call(this); + }); + return [ t, ans ]; + }.call(this); + }, function() { + return this._apply("link"); + }, function() { + return c = this._apply("anything"); + }); + }, + tokens: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + this._form(function() { + return c = this._many(function() { + return this._apply("token"); + }); + }); + return c; + }.call(this); + }, + link: function() { + var $elf = this, _fromIdx = this.input.idx, c, c, cc; + return this._or(function() { + return function() { + c = this._apply("token"); + this._form(function() { + return undefined; + }); + return [ "link", c, [] ]; + }.call(this); + }, function() { + return function() { + c = this._apply("token"); + cc = this._apply("tokens"); + return [ "link", c, cc ]; + }.call(this); + }); + }, + link_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("token"); + return c; + }.call(this); + }, + extension: function() { + var $elf = this, _fromIdx = this.input.idx, t, tt, c, p, t, tt, c, p; + return this._or(function() { + return function() { + this._form(function() { + return function() { + t = this._applyWithArgs("exactly", "extension"); + tt = this._applyWithArgs("exactly", "toc"); + c = this._apply("anything"); + return p = this._apply("anything"); + }.call(this); + }); + return [ t, tt, [ this.transform(c, p), c ], p ]; + }.call(this); + }, function() { + return function() { + this._form(function() { + return function() { + t = this._applyWithArgs("exactly", "extension"); + tt = this._apply("anything"); + c = this._apply("tokens"); + return p = this._apply("anything"); + }.call(this); + }); + return [ t, tt, c, p ]; + }.call(this); + }); + }, + topLevel: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c; + }.call(this); + } + }); + ShmakoWikiToc["tocs"] = []; + ShmakoWikiToc["transform"] = function(ast, p) { + this.startToc(); + this.match(ast, "topLevel"); + p === "nested" && this.addNestedToc(); + return this.finishToc(); + }; + ShmakoWikiToc["startToc"] = function() { + this["tocs"].push({ + toc: [] + }); + }; + ShmakoWikiToc["finishToc"] = function() { + return TocToList.convert(this["tocs"].pop()["toc"]); + }; + ShmakoWikiToc["addTocHeader"] = function(header) { + this["tocs"][this["tocs"]["length"] - 1]["toc"].push([ header[0], api.astToPlain(header[1]), header[2] ]); + }; + ShmakoWikiToc["addNestedToc"] = function() { + var headers = this["tocs"][this["tocs"]["length"] - 1]["toc"], parent = this["tocs"][this["tocs"]["length"] - 2]; + parent && parent["toc"] = parent["toc"].concat(headers); + }; +} \ No newline at end of file diff --git a/lib/extensions/shmakowiki-toc/lib/shmakowikitoc.js b/lib/extensions/shmakowiki-toc/lib/shmakowikitoc.js new file mode 100644 index 0000000..725f019 --- /dev/null +++ b/lib/extensions/shmakowiki-toc/lib/shmakowikitoc.js @@ -0,0 +1,6 @@ +var shmakowikitoc = require('./_shmakowikitoc.js'); + +exports.transform = function(ast) { + console.log(ast[0]); + return shmakowikitoc.ShmakoWikiToc.match(ast, 'topLevel'); +}; \ No newline at end of file diff --git a/lib/extensions/shmakowiki-toc/src/shmakowikitoc.js b/lib/extensions/shmakowiki-toc/src/shmakowikitoc.js new file mode 100644 index 0000000..111f65c --- /dev/null +++ b/lib/extensions/shmakowiki-toc/src/shmakowikitoc.js @@ -0,0 +1 @@ +var api = require('../../../shmakowiki.js'); \ No newline at end of file diff --git a/lib/extensions/shmakowiki-toc/src/shmakowikitoc.ometajs b/lib/extensions/shmakowiki-toc/src/shmakowikitoc.ometajs new file mode 100644 index 0000000..39694ad --- /dev/null +++ b/lib/extensions/shmakowiki-toc/src/shmakowikitoc.ometajs @@ -0,0 +1,61 @@ +ometa ShmakoWikiToc { + keyword = 'para' + | 'olist' | 'ulist' | 'olistItem' | 'ulistItem' + | 'bold' | 'bold_' + | 'italic' | 'italic_' + | 'underline' | 'underline_' + | 'strike' | 'strike_' + | 'monospace' | 'monospace_' + | 'superscript' | 'superscript_' + | 'subscript' | 'subscript_' + | 'link_' + | 'lineBreak' + | 'escaped', + + headertag = 'header1' | 'header2' | 'header3' | 'header4' | 'header5' | 'header6', + + header = [headertag:t tokens:c :a] -> { var h = [t, c, a]; ShmakoWikiToc.addTocHeader(h); h; }, + + token = header + | extension + | [keyword:t tokens:ans] -> [t, ans] + | link + | :c, + tokens = [token*:c] -> c, + + link token:c [] -> ['link', c, []], + link token:c tokens:cc -> ['link', c, cc], + link_ token:c -> c, + + extension = ['extension':t 'toc':tt :c :p] -> [t, tt, [this.transform(c, p), c], p], + extension = ['extension':t :tt tokens:c :p] -> [t, tt, c, p], + + topLevel = tokens:c -> c +} + +ShmakoWikiToc.tocs = []; + +ShmakoWikiToc.transform = function(ast, p) { + this.startToc(); + this.match(ast, 'topLevel'); + p === 'nested' && this.addNestedToc(); + return this.finishToc(); +}; + +ShmakoWikiToc.startToc = function() { + this.tocs.push({ toc: [] }); +}; + +ShmakoWikiToc.finishToc = function() { + return TocToList.convert(this.tocs.pop().toc); +}; + +ShmakoWikiToc.addTocHeader = function(header) { + this.tocs[this.tocs.length - 1].toc.push([header[0], api.astToPlain(header[1]), header[2]]); +}; + +ShmakoWikiToc.addNestedToc = function() { + var headers = this.tocs[this.tocs.length - 1].toc, + parent = this.tocs[this.tocs.length - 2]; + parent && parent.toc = parent.toc.concat(headers); +}; \ No newline at end of file diff --git a/lib/extensions/shmakowiki-toc/src/shmakowikitoc.ometajs.js b/lib/extensions/shmakowiki-toc/src/shmakowikitoc.ometajs.js new file mode 100644 index 0000000..e795e9b --- /dev/null +++ b/lib/extensions/shmakowiki-toc/src/shmakowikitoc.ometajs.js @@ -0,0 +1,242 @@ +var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; +var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; +var isImmutable = ometajs_.isImmutable; +var digitValue = ometajs_.digitValue; +var isSequenceable = ometajs_.isSequenceable; +var escapeChar = ometajs_.escapeChar; +var unescape = ometajs_.unescape; +var getTag = ometajs_.getTag; +var inspect = ometajs_.inspect; +var lift = ometajs_.lift; +var clone = ometajs_.clone; +var Parser = ometajs_.Parser; +var fail = ometajs_.fail; +var OMeta = ometajs_.OMeta; +var BSNullOptimization = ometajs_.BSNullOptimization; +var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; +var BSSeqInliner = ometajs_.BSSeqInliner; +var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; +var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; +var BSOMetaParser = ometajs_.BSOMetaParser; +var BSOMetaTranslator = ometajs_.BSOMetaTranslator; +var BSJSParser = ometajs_.BSJSParser; +var BSSemActionParser = ometajs_.BSSemActionParser; +var BSJSIdentity = ometajs_.BSJSIdentity; +var BSJSTranslator = ometajs_.BSJSTranslator; +var BSOMetaJSParser = ometajs_.BSOMetaJSParser; +var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; +if (global === ometajs_) { + fail = (function(fail) { + return function() { return fail }; + })(fail); + OMeta = require('ometajs').OMeta; +}{ + var ShmakoWikiToc = exports.ShmakoWikiToc = objectThatDelegatesTo(OMeta, { + keyword: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "monospace": + return "monospace"; + case "strike": + return "strike"; + case "underline": + return "underline"; + case "link_": + return "link_"; + case "para": + return "para"; + case "italic": + return "italic"; + case "underline_": + return "underline_"; + case "italic_": + return "italic_"; + case "strike_": + return "strike_"; + case "escaped": + return "escaped"; + case "subscript": + return "subscript"; + case "superscript": + return "superscript"; + case "ulistItem": + return "ulistItem"; + case "olistItem": + return "olistItem"; + case "subscript_": + return "subscript_"; + case "bold_": + return "bold_"; + case "monospace_": + return "monospace_"; + case "olist": + return "olist"; + case "ulist": + return "ulist"; + case "lineBreak": + return "lineBreak"; + case "superscript_": + return "superscript_"; + case "bold": + return "bold"; + default: + throw fail(); + } + }.call(this); + }, + headertag: function() { + var $elf = this, _fromIdx = this.input.idx; + return function() { + switch (this._apply("anything")) { + case "header4": + return "header4"; + case "header1": + return "header1"; + case "header3": + return "header3"; + case "header6": + return "header6"; + case "header2": + return "header2"; + case "header5": + return "header5"; + default: + throw fail(); + } + }.call(this); + }, + header: function() { + var $elf = this, _fromIdx = this.input.idx, t, c, a; + return function() { + this._form(function() { + return function() { + t = this._apply("headertag"); + c = this._apply("tokens"); + return a = this._apply("anything"); + }.call(this); + }); + return function() { + var h = [ t, c, a ]; + ShmakoWikiToc.addTocHeader(h); + return h; + }.call(this); + }.call(this); + }, + token: function() { + var $elf = this, _fromIdx = this.input.idx, t, ans, c; + return this._or(function() { + return this._apply("header"); + }, function() { + return this._apply("extension"); + }, function() { + return function() { + this._form(function() { + return function() { + t = this._apply("keyword"); + return ans = this._apply("tokens"); + }.call(this); + }); + return [ t, ans ]; + }.call(this); + }, function() { + return this._apply("link"); + }, function() { + return c = this._apply("anything"); + }); + }, + tokens: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + this._form(function() { + return c = this._many(function() { + return this._apply("token"); + }); + }); + return c; + }.call(this); + }, + link: function() { + var $elf = this, _fromIdx = this.input.idx, c, c, cc; + return this._or(function() { + return function() { + c = this._apply("token"); + this._form(function() { + return undefined; + }); + return [ "link", c, [] ]; + }.call(this); + }, function() { + return function() { + c = this._apply("token"); + cc = this._apply("tokens"); + return [ "link", c, cc ]; + }.call(this); + }); + }, + link_: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("token"); + return c; + }.call(this); + }, + extension: function() { + var $elf = this, _fromIdx = this.input.idx, t, tt, c, p, t, tt, c, p; + return this._or(function() { + return function() { + this._form(function() { + return function() { + t = this._applyWithArgs("exactly", "extension"); + tt = this._applyWithArgs("exactly", "toc"); + c = this._apply("anything"); + return p = this._apply("anything"); + }.call(this); + }); + return [ t, tt, [ this.transform(c, p), c ], p ]; + }.call(this); + }, function() { + return function() { + this._form(function() { + return function() { + t = this._applyWithArgs("exactly", "extension"); + tt = this._apply("anything"); + c = this._apply("tokens"); + return p = this._apply("anything"); + }.call(this); + }); + return [ t, tt, c, p ]; + }.call(this); + }); + }, + topLevel: function() { + var $elf = this, _fromIdx = this.input.idx, c; + return function() { + c = this._apply("tokens"); + return c; + }.call(this); + } + }); + ShmakoWikiToc["tocs"] = []; + ShmakoWikiToc["transform"] = function(ast, p) { + this.startToc(); + this.match(ast, "topLevel"); + p === "nested" && this.addNestedToc(); + return this.finishToc(); + }; + ShmakoWikiToc["startToc"] = function() { + this["tocs"].push({ + toc: [] + }); + }; + ShmakoWikiToc["finishToc"] = function() { + return TocToList.convert(this["tocs"].pop()["toc"]); + }; + ShmakoWikiToc["addTocHeader"] = function(header) { + this["tocs"][this["tocs"]["length"] - 1]["toc"].push([ header[0], api.astToPlain(header[1]), header[2] ]); + }; + ShmakoWikiToc["addNestedToc"] = function() { + var headers = this["tocs"][this["tocs"]["length"] - 1]["toc"], parent = this["tocs"][this["tocs"]["length"] - 2]; + parent && parent["toc"] = parent["toc"].concat(headers); + }; +} \ No newline at end of file diff --git a/lib/extensions/shmakowiki-toc/src/toc2list.js b/lib/extensions/shmakowiki-toc/src/toc2list.js new file mode 100644 index 0000000..ec6c473 --- /dev/null +++ b/lib/extensions/shmakowiki-toc/src/toc2list.js @@ -0,0 +1,60 @@ +var TocToList = { + + createChain: function(levels, header, n, prevn) { + var lh = levels[prevn], ul, li; + + for (var i = prevn + 1; i <= n; i++) { + ul = this.createUl(this.createLi()); + this.getLi(lh)[1].push(ul); + lh = levels[i] = ul; + } + + this.getLi(lh)[1].push(this.createLink(header[1], header[2])); + }, + + getLi: function(ul) { + var li = ul[1][ul[1].length - 1]; + + if (!li) { + li = this.createLi(); + ul[1].push(li); + } + + return li; + }, + + convert: function(headers) { + var levels = { 1: this.createUl() }, + h, n, prev = 1; + + for (var i = 0; i < headers.length; i++) { + h = headers[i]; + n = this.getLevelNumber(h[0]); + if (n > prev) { + this.createChain(levels, h, n, prev); + } else { + levels[n][1].push(this.createLi(h[1], h[2])); + } + prev = n; + } + + return levels[1]; + }, + + createUl: function(li) { + return ['ulist', li ? [li] : []]; + }, + + createLi: function(content, a) { + return ['ulistItem', ['listItem', content ? this.createLink(content, a) : null]]; + }, + + createLink: function(content, a) { + return ['link', '#' + a, content]; + }, + + getLevelNumber: function(text) { + return text.charCodeAt(text.length - 1) - 48; + } + +}; \ No newline at end of file diff --git a/lib/extensions/shmakowiki-toc/tests/tests.js b/lib/extensions/shmakowiki-toc/tests/tests.js new file mode 100644 index 0000000..5854968 --- /dev/null +++ b/lib/extensions/shmakowiki-toc/tests/tests.js @@ -0,0 +1,29 @@ +var tests = [ + { + 'in': '%%toc\n==test1\n==test2%%', + 'out': [] + } +]; + +var api = require('../../../shmakowiki.js'), + totalFail = 0; + +for (var i = 0; i < tests.length; i++) { + var test = tests[i]; + + test.res = api.shmakowikiToAST(test['in']); + + var res = JSON.stringify(test.res), + out = JSON.stringify(test.out), + isOk = res == out; + + console.log('Test in:\n' + test['in'] + '\n: ' + (isOk ? 'ok' : 'FAIL')); + console.log('Test result:\n' + res); + if (!isOk) { + totalFail++; + console.log('Test out:\n' + out); + } + + console.log('-----------------------------------------------------'); +} +console.log('\n' + (totalFail ? 'Total FAIL: ' + totalFail : 'All Ok')); diff --git a/lib/shmakowiki.js b/lib/shmakowiki.js index 25886fc..9477e4a 100644 --- a/lib/shmakowiki.js +++ b/lib/shmakowiki.js @@ -1,2199 +1,38 @@ -var ometajs = require('ometajs'), - OMeta = ometajs.OMeta, - ohl = require('ometa-highlighter'), - utils = require('./utils.js'), - shtoc = require('shmakowiki-toc'), - OmetaHighlighter = ohl.OmetaHighlighter, - OmetaHighlighterToHtml = ohl.OmetaHighlighterToHtml, - OmetaHighlighterToBemjson = ohl.OmetaHighlighterToBemjson, - ShmakoWikiToc = shtoc.ShmakoWikiToc, - ShmakoWikiTocToBemjson = shtoc.ShmakoWikiTocToBemjson; -var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; -var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; -var isImmutable = ometajs_.isImmutable; -var digitValue = ometajs_.digitValue; -var isSequenceable = ometajs_.isSequenceable; -var escapeChar = ometajs_.escapeChar; -var unescape = ometajs_.unescape; -var getTag = ometajs_.getTag; -var inspect = ometajs_.inspect; -var lift = ometajs_.lift; -var clone = ometajs_.clone; -var Parser = ometajs_.Parser; -var fail = ometajs_.fail; -var OMeta = ometajs_.OMeta; -var BSNullOptimization = ometajs_.BSNullOptimization; -var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; -var BSSeqInliner = ometajs_.BSSeqInliner; -var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; -var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; -var BSOMetaParser = ometajs_.BSOMetaParser; -var BSOMetaTranslator = ometajs_.BSOMetaTranslator; -var BSJSParser = ometajs_.BSJSParser; -var BSSemActionParser = ometajs_.BSSemActionParser; -var BSJSIdentity = ometajs_.BSJSIdentity; -var BSJSTranslator = ometajs_.BSJSTranslator; -var BSOMetaJSParser = ometajs_.BSOMetaJSParser; -var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; -if (global === ometajs_) { - fail = (function(fail) { - return function() { return fail }; - })(fail); - OMeta = require('ometajs').OMeta; -}{ - var ShmakoWiki = exports.ShmakoWiki = objectThatDelegatesTo(OMeta, { - oneOf: function() { - var $elf = this, _fromIdx = this.input.idx, a, a; - return this._or(function() { - return function() { - a = this._apply("anything"); - this._pred(a["length"] == 1); - return this._applyWithArgs("apply", a.pop()); - }.call(this); - }, function() { - return function() { - a = this._apply("anything"); - this._pred(a["length"] > 1); - return this._or(function() { - return this._applyWithArgs("apply", a.pop()); - }, function() { - return this._applyWithArgs("oneOf", a); - }); - }.call(this); - }); - }, - escapedChar: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - this._applyWithArgs("exactly", "~"); - c = this._apply("char"); - return c; - }.call(this); - }, - escaped: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._many1(function() { - return this._apply("escapedChar"); - }); - return [ "escaped", c.join("") ]; - }.call(this); - }, - b: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", "*"); - this._applyWithArgs("exactly", "*"); - return "**"; - }.call(this); - }, - i: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", "/"); - this._applyWithArgs("exactly", "/"); - return "//"; - }.call(this); - }, - u: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", "_"); - this._applyWithArgs("exactly", "_"); - return "__"; - }.call(this); - }, - s: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._not(function() { - return this._applyWithArgs("exactly", " "); - }); - this._applyWithArgs("exactly", "-"); - this._applyWithArgs("exactly", "-"); - "--"; - return this._not(function() { - return this._applyWithArgs("exactly", " "); - }); - }.call(this); - }, - m: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", "#"); - this._applyWithArgs("exactly", "#"); - return "##"; - }.call(this); - }, - sup: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", "^"); - this._applyWithArgs("exactly", "^"); - return "^^"; - }.call(this); - }, - sub: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", ","); - this._applyWithArgs("exactly", ","); - return ",,"; - }.call(this); - }, - ext: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", "%"); - this._applyWithArgs("exactly", "%"); - return "%%"; - }.call(this); - }, - linkStart: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - switch (this._apply("anything")) { - case "[": - return function() { - this._applyWithArgs("exactly", "["); - return "[["; - }.call(this); - case "(": - return function() { - this._applyWithArgs("exactly", "("); - return "(("; - }.call(this); - default: - throw fail(); - } - }.call(this); - }, - linkEnd: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - switch (this._apply("anything")) { - case "]": - return function() { - this._applyWithArgs("exactly", "]"); - return "]]"; - }.call(this); - case ")": - return function() { - this._applyWithArgs("exactly", ")"); - return "))"; - }.call(this); - default: - throw fail(); - } - }.call(this); - }, - l: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._or(function() { - return this._apply("linkStart"); - }, function() { - return this._apply("linkEnd"); - }); - }, - lineBreak: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", "\\"); - this._applyWithArgs("exactly", "\\"); - "\\\\"; - return [ "lineBreak", [] ]; - }.call(this); - }, - special: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._or(function() { - return this._apply("b"); - }, function() { - return this._apply("i"); - }, function() { - return this._apply("u"); - }, function() { - return this._apply("s"); - }, function() { - return this._apply("m"); - }, function() { - return this._apply("sup"); - }, function() { - return this._apply("sub"); - }, function() { - return this._apply("l"); - }, function() { - return this._apply("lineBreak"); - }); - return [ "special", c ]; - }.call(this); - }, - between: function() { - var $elf = this, _fromIdx = this.input.idx, t, n, nn, c; - return function() { - t = this._apply("anything"); - n = this._apply("anything"); - nn = function() { - this._applyWithArgs("apply", t); - return ShmakoWiki.arrAdd(n, t); - }.call(this); - c = this._many(function() { - return function() { - this._not(function() { - return this._applyWithArgs("oneOf", ShmakoWiki.arrCopy(nn)); - }); - return this._applyWithArgs("allInline", nn); - }.call(this); - }); - this._applyWithArgs("apply", t); - return c; - }.call(this); - }, - between_: function() { - var $elf = this, _fromIdx = this.input.idx, t, c; - return function() { - t = this._apply("anything"); - this._applyWithArgs("apply", t); - c = this._many(function() { - return function() { - this._not(function() { - return this._apply("special"); - }); - return this._apply("text"); - }.call(this); - }); - this._or(function() { - return this._not(function() { - return this._not(function() { - return this._apply("special"); - }); - }); - }, function() { - return this._apply("end"); - }); - return c; - }.call(this); - }, - bold: function() { - var $elf = this, _fromIdx = this.input.idx, n, c; - return function() { - n = this._apply("anything"); - c = this._applyWithArgs("between", "b", n); - return [ "bold", c ]; - }.call(this); - }, - bold_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._applyWithArgs("between_", "b"); - return [ "bold_", c ]; - }.call(this); - }, - italic: function() { - var $elf = this, _fromIdx = this.input.idx, n, c; - return function() { - n = this._apply("anything"); - c = this._applyWithArgs("between", "i", n); - return [ "italic", c ]; - }.call(this); - }, - italic_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._applyWithArgs("between_", "i"); - return [ "italic_", c ]; - }.call(this); - }, - underline: function() { - var $elf = this, _fromIdx = this.input.idx, n, c; - return function() { - n = this._apply("anything"); - c = this._applyWithArgs("between", "u", n); - return [ "underline", c ]; - }.call(this); - }, - underline_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._applyWithArgs("between_", "u"); - return [ "underline_", c ]; - }.call(this); - }, - strike: function() { - var $elf = this, _fromIdx = this.input.idx, n, c; - return function() { - n = this._apply("anything"); - c = this._applyWithArgs("between", "s", n); - return [ "strike", c ]; - }.call(this); - }, - strike_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._applyWithArgs("between_", "s"); - return [ "strike_", c ]; - }.call(this); - }, - monospace: function() { - var $elf = this, _fromIdx = this.input.idx, n, c; - return function() { - n = this._apply("anything"); - c = this._applyWithArgs("between", "m", n); - return [ "monospace", c ]; - }.call(this); - }, - monospace_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._applyWithArgs("between_", "m"); - return [ "monospace_", c ]; - }.call(this); - }, - superscript: function() { - var $elf = this, _fromIdx = this.input.idx, n, c; - return function() { - n = this._apply("anything"); - c = this._applyWithArgs("between", "sup", n); - return [ "superscript", c ]; - }.call(this); - }, - superscript_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._applyWithArgs("between_", "sup"); - return [ "superscript_", c ]; - }.call(this); - }, - subscript: function() { - var $elf = this, _fromIdx = this.input.idx, n, c; - return function() { - n = this._apply("anything"); - c = this._applyWithArgs("between", "sub", n); - return [ "subscript", c ]; - }.call(this); - }, - subscript_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._applyWithArgs("between_", "sub"); - return [ "subscript_", c ]; - }.call(this); - }, - link: function() { - var $elf = this, _fromIdx = this.input.idx, n, nn, c, cc; - return function() { - n = this._apply("anything"); - nn = function() { - this._apply("linkStart"); - return ShmakoWiki.arrAdd(n, "l"); - }.call(this); - c = this._many1(function() { - return function() { - this._not(function() { - return this._apply("space"); - }); - this._not(function() { - return this._apply("linkEnd"); - }); - return this._apply("char"); - }.call(this); - }); - this._apply("spacesNoNl"); - cc = this._many(function() { - return function() { - this._not(function() { - return this._applyWithArgs("oneOf", ShmakoWiki.arrCopy(nn)); - }); - return this._applyWithArgs("allInline", nn); - }.call(this); - }); - this._apply("linkEnd"); - return [ "link", c.join(""), cc ]; - }.call(this); - }, - link_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return this._or(function() { - return function() { - this._apply("linkStart"); - c = this._many1(function() { - return function() { - this._not(function() { - return this._apply("space"); - }); - return this._apply("char"); - }.call(this); - }); - this._or(function() { - return this._not(function() { - return this._not(function() { - return this._apply("space"); - }); - }); - }, function() { - return this._not(function() { - return this._not(function() { - return this._apply("special"); - }); - }); - }, function() { - return this._apply("end"); - }); - return [ "link_", c.join("") ]; - }.call(this); - }, function() { - return function() { - this._apply("linkEnd"); - return ""; - }.call(this); - }); - }, - mdash: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", " "); - this._applyWithArgs("exactly", "-"); - this._applyWithArgs("exactly", "-"); - this._applyWithArgs("exactly", " "); - " -- "; - return " – "; - }.call(this); - }, - text: function() { - var $elf = this, _fromIdx = this.input.idx, c, c; - return this._or(function() { - return function() { - c = this._many1(function() { - return function() { - this._not(function() { - return this._apply("special"); - }); - this._not(function() { - return this._apply("escapedChar"); - }); - return this._or(function() { - return this._apply("mdash"); - }, function() { - return this._apply("char"); - }); - }.call(this); - }); - return c.join(""); - }.call(this); - }, function() { - return function() { - c = this._apply("escaped"); - return c; - }.call(this); - }); - }, - inline: function() { - var $elf = this, _fromIdx = this.input.idx, n; - return function() { - n = this._apply("anything"); - return this._or(function() { - return this._applyWithArgs("bold", n); - }, function() { - return this._applyWithArgs("italic", n); - }, function() { - return this._applyWithArgs("underline", n); - }, function() { - return this._applyWithArgs("strike", n); - }, function() { - return this._applyWithArgs("monospace", n); - }, function() { - return this._applyWithArgs("superscript", n); - }, function() { - return this._applyWithArgs("subscript", n); - }, function() { - return this._applyWithArgs("link", n); - }, function() { - return this._apply("lineBreak"); - }, function() { - return this._apply("text"); - }); - }.call(this); - }, - inline_: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._or(function() { - return this._apply("bold_"); - }, function() { - return this._apply("italic_"); - }, function() { - return this._apply("underline_"); - }, function() { - return this._apply("superscript_"); - }, function() { - return this._apply("subscript_"); - }, function() { - return this._apply("strike_"); - }, function() { - return this._apply("monospace_"); - }, function() { - return this._apply("link_"); - }); - }, - allInline: function() { - var $elf = this, _fromIdx = this.input.idx, n; - return function() { - n = this._apply("anything"); - return this._or(function() { - return this._applyWithArgs("inline", n); - }, function() { - return function() { - this._not(function() { - return this._applyWithArgs("inline", n); - }); - return this._apply("inline_"); - }.call(this); - }); - }.call(this); - }, - topInline: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._many1(function() { - return this._applyWithArgs("allInline", []); - }); - }, - spacesNoNl: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._many(function() { - return function() { - this._not(function() { - return this._applyWithArgs("exactly", "\n"); - }); - return this._apply("space"); - }.call(this); - }); - }, - spacesNlSpaces: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._apply("spacesNoNl"); - this._applyWithArgs("exactly", "\n"); - this._apply("spacesNoNl"); - this._not(function() { - return this._apply("end"); - }); - return " "; - }.call(this); - }, - nl: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - this._applyWithArgs("exactly", "\n"); - return this._many1(function() { - return function() { - this._apply("spacesNoNl"); - return this._applyWithArgs("exactly", "\n"); - }.call(this); - }); - }.call(this); - }, - headerStart: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._many1(function() { - return this._applyWithArgs("exactly", "="); - }); - this._apply("spacesNoNl"); - return c.join("")["length"] - 1; - }.call(this); - }, - headerEnd: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._or(function() { - return function() { - switch (this._apply("anything")) { - case "\n": - return this._many(function() { - return function() { - this._apply("spacesNoNl"); - return this._applyWithArgs("exactly", "\n"); - }.call(this); - }); - default: - throw fail(); - } - }.call(this); - }, function() { - return this._apply("end"); - }); - }, - headerAnchor: function() { - var $elf = this, _fromIdx = this.input.idx, a; - return this._or(function() { - return function() { - this._many1(function() { - return this._applyWithArgs("exactly", "="); - }); - a = this._many(function() { - return function() { - this._not(function() { - return this._applyWithArgs("exactly", "\n"); - }); - return this._apply("char"); - }.call(this); - }); - return a.join(""); - }.call(this); - }, function() { - return function() { - this._apply("empty"); - return ""; - }.call(this); - }); - }, - header: function() { - var $elf = this, _fromIdx = this.input.idx, l, cc, c, anchor; - return function() { - l = this._apply("headerStart"); - c = function() { - cc = this._many1(function() { - return function() { - this._not(function() { - return this._apply("headerEnd"); - }); - this._not(function() { - return this._applyWithArgs("exactly", "="); - }); - return this._apply("char"); - }.call(this); - }); - return cc.join(""); - }.call(this); - anchor = this._apply("headerAnchor"); - this._apply("headerEnd"); - return function() { - var hAST = ShmakoWiki.matchAll(c, "topLevel"), hAnchor = utils.transliterate("ru", anchor["length"] ? anchor : ShmakoWikiToPlain.match(hAST, "topLevel")); - return [ "header" + (l <= 6 ? l : 6), hAST, hAnchor ]; - }.call(this); - }.call(this); - }, - blockEnd: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._or(function() { - return function() { - switch (this._apply("anything")) { - case "\n": - return this._not(function() { - return this._not(function() { - return this._apply("extBlockStart"); - }); - }); - default: - throw fail(); - } - }.call(this); - }, function() { - return this._apply("nl"); - }, function() { - return function() { - switch (this._apply("anything")) { - case "\n": - return this._or(function() { - return this._not(function() { - return this._not(function() { - return this._apply("anyLi"); - }); - }); - }, function() { - return this._not(function() { - return this._not(function() { - return this._apply("headerStart"); - }); - }); - }); - default: - throw fail(); - } - }.call(this); - }, function() { - return function() { - this._many(function() { - return this._applyWithArgs("exactly", "\n"); - }); - return this._apply("end"); - }.call(this); - }); - }, - para: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._many1(function() { - return function() { - this._not(function() { - return this._apply("blockEnd"); - }); - return this._or(function() { - return this._apply("spacesNlSpaces"); - }, function() { - return this._apply("char"); - }); - }.call(this); - }); - c.join(""); - this._apply("blockEnd"); - return [ "para", ShmakoWiki.matchAll(c, "topInline") ]; - }.call(this); - }, - uli: function() { - var $elf = this, _fromIdx = this.input.idx, s; - return function() { - s = this._apply("spacesNoNl"); - this._applyWithArgs("exactly", "*"); - this._not(function() { - return this._applyWithArgs("exactly", "*"); - }); - this._apply("spacesNoNl"); - return s.join(""); - }.call(this); - }, - oli: function() { - var $elf = this, _fromIdx = this.input.idx, s; - return function() { - s = this._apply("spacesNoNl"); - this._many1(function() { - return this._apply("digit"); - }); - this._applyWithArgs("exactly", "."); - this._apply("spacesNoNl"); - return s.join(""); - }.call(this); - }, - anyLi: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._or(function() { - return this._apply("uli"); - }, function() { - return this._apply("oli"); - }); - }, - listItemContent: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._many1(function() { - return function() { - this._not(function() { - return this._apply("blockEnd"); - }); - return this._apply("char"); - }.call(this); - }); - return ShmakoWiki.matchAll(c.join(""), "topInline"); - }.call(this); - }, - anySubList: function() { - var $elf = this, _fromIdx = this.input.idx, l, s, sl; - return function() { - l = this._apply("anything"); - this._applyWithArgs("exactly", "\n"); - this._not(function() { - return this._not(function() { - return s = this._apply("anyLi"); - }); - }); - this._pred(s["length"] > l["length"]); - sl = this._apply("anyList"); - return sl; - }.call(this); - }, - listItem: function() { - var $elf = this, _fromIdx = this.input.idx, t, l, s, c, cc; - return function() { - t = this._apply("anything"); - l = this._apply("anything"); - s = this._applyWithArgs("apply", t); - this._pred(s["length"] == l["length"]); - c = this._apply("listItemContent"); - cc = this._or(function() { - return this._many1(function() { - return this._applyWithArgs("anySubList", s); - }); - }, function() { - return function() { - this._or(function() { - return function() { - switch (this._apply("anything")) { - case "\n": - return this._not(function() { - return this._not(function() { - return this._applyWithArgs("apply", t); - }); - }); - default: - throw fail(); - } - }.call(this); - }, function() { - return this._not(function() { - return this._not(function() { - return this._apply("blockEnd"); - }); - }); - }); - return ""; - }.call(this); - }); - return function() { - if (cc) { - c = c.concat(cc); - } else { - undefined; - } - return [ t + "stItem", c ]; - }.call(this); - }.call(this); - }, - list: function() { - var $elf = this, _fromIdx = this.input.idx, t, s, c; - return function() { - t = this._apply("anything"); - this._not(function() { - return this._not(function() { - return s = this._applyWithArgs("apply", t); - }); - }); - c = this._many1(function() { - return this._applyWithArgs("listItem", t, s); - }); - return [ t + "st", c ]; - }.call(this); - }, - anyList: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._or(function() { - return this._applyWithArgs("list", "uli"); - }, function() { - return this._applyWithArgs("list", "oli"); - }); - }, - extBlockStart: function() { - var $elf = this, _fromIdx = this.input.idx, t, tt, c, cc; - return function() { - tt = this._or(function() { - return function() { - this._apply("ext"); - t = this._apply("char"); - this._applyWithArgs("exactly", "%"); - return t; - }.call(this); - }, function() { - return function() { - this._apply("ext"); - return ""; - }.call(this); - }); - c = this._many(function() { - return function() { - this._not(function() { - return this._apply("space"); - }); - return this._apply("char"); - }.call(this); - }); - this._apply("spacesNoNl"); - cc = this._many(function() { - return function() { - this._not(function() { - return this._applyWithArgs("exactly", "\n"); - }); - return this._apply("char"); - }.call(this); - }); - this._applyWithArgs("exactly", "\n"); - return [ tt, c.join(""), cc.join("") ]; - }.call(this); - }, - extBlockEnd: function() { - var $elf = this, _fromIdx = this.input.idx, t, t; - return this._or(function() { - return function() { - t = this._apply("anything"); - this._pred(t["length"] == 0); - this._many(function() { - return this._apply("space"); - }); - return this._apply("ext"); - }.call(this); - }, function() { - return function() { - t = this._apply("char"); - this._pred(t["length"] > 0); - this._many(function() { - return this._apply("space"); - }); - this._applyWithArgs("exactly", "%"); - this._applyWithArgs("seq", t); - return this._apply("ext"); - }.call(this); - }); - }, - extBlock: function() { - var $elf = this, _fromIdx = this.input.idx, s, cc, c; - return function() { - s = this._apply("extBlockStart"); - c = function() { - cc = this._many1(function() { - return function() { - this._not(function() { - return this._applyWithArgs("extBlockEnd", s[0]); - }); - return this._apply("char"); - }.call(this); - }); - return cc.join(""); - }.call(this); - this._applyWithArgs("extBlockEnd", s[0]); - this._or(function() { - return this._apply("blockEnd"); - }, function() { - return function() { - switch (this._apply("anything")) { - case "\n": - return "\n"; - default: - throw fail(); - } - }.call(this); - }); - return [ "extension", s[1], ShmakoWiki["extensions"].hasOwnProperty(s[1]) ? ShmakoWiki["extensions"][s[1]](c, s[2]) : c, s[2] ]; - }.call(this); - }, - allBlock: function() { - var $elf = this, _fromIdx = this.input.idx, l; - return this._or(function() { - return this._apply("extBlock"); - }, function() { - return function() { - l = this._apply("anyList"); - this._apply("blockEnd"); - return l; - }.call(this); - }, function() { - return this._apply("header"); - }, function() { - return this._apply("para"); - }); - }, - topLevel: function() { - var $elf = this, _fromIdx = this.input.idx; - return this._many1(function() { - return this._apply("allBlock"); - }); - } - }); - ShmakoWiki["extensions"] = { - ohl: function(c, p) { - return OmetaHighlighter.matchAll(c, p); - }, - toc: function(c, p) { - var x = ShmakoWikiToc.parse(c, p, ShmakoWiki); - return [ x["toc"], x["ast"] ]; - } - }; - ShmakoWiki["extensions"]["hl"] = ShmakoWiki["extensions"]["ohl"]; - ShmakoWiki["arrJoin"] = function(arr1, arr2) { - var newArr = ShmakoWiki.arrCopy(arr1); - for (var i = 0; i < arr2["length"]; i++) { - newArr[newArr["length"]] = arr2[i]; - } - return newArr; - }; - ShmakoWiki["arrCopy"] = function(arr) { - var newArr = []; - for (var i = 0; i < arr["length"]; i++) { - newArr[newArr["length"]] = arr[i]; - } - return newArr; - }; - ShmakoWiki["arrAdd"] = function(arr, elem) { - for (var i = 0; i < arr["length"]; i++) { - if (arr[i] == elem) { - return arr; - } else { - undefined; - } - } - arr[arr["length"]] = elem; - return arr; - }; -}var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; -var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; -var isImmutable = ometajs_.isImmutable; -var digitValue = ometajs_.digitValue; -var isSequenceable = ometajs_.isSequenceable; -var escapeChar = ometajs_.escapeChar; -var unescape = ometajs_.unescape; -var getTag = ometajs_.getTag; -var inspect = ometajs_.inspect; -var lift = ometajs_.lift; -var clone = ometajs_.clone; -var Parser = ometajs_.Parser; -var fail = ometajs_.fail; -var OMeta = ometajs_.OMeta; -var BSNullOptimization = ometajs_.BSNullOptimization; -var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; -var BSSeqInliner = ometajs_.BSSeqInliner; -var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; -var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; -var BSOMetaParser = ometajs_.BSOMetaParser; -var BSOMetaTranslator = ometajs_.BSOMetaTranslator; -var BSJSParser = ometajs_.BSJSParser; -var BSSemActionParser = ometajs_.BSSemActionParser; -var BSJSIdentity = ometajs_.BSJSIdentity; -var BSJSTranslator = ometajs_.BSJSTranslator; -var BSOMetaJSParser = ometajs_.BSOMetaJSParser; -var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; -if (global === ometajs_) { - fail = (function(fail) { - return function() { return fail }; - })(fail); - OMeta = require('ometajs').OMeta; -}{ - var ShmakoWikiToHtml = exports.ShmakoWikiToHtml = objectThatDelegatesTo(OMeta, { - keyword: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - switch (this._apply("anything")) { - case "monospace": - return "monospace"; - case "strike": - return "strike"; - case "header6": - return "header6"; - case "header3": - return "header3"; - case "para": - return "para"; - case "italic": - return "italic"; - case "underline_": - return "underline_"; - case "header2": - return "header2"; - case "header4": - return "header4"; - case "italic_": - return "italic_"; - case "underline": - return "underline"; - case "header5": - return "header5"; - case "strike_": - return "strike_"; - case "link_": - return "link_"; - case "subscript": - return "subscript"; - case "escaped": - return "escaped"; - case "superscript": - return "superscript"; - case "ulistItem": - return "ulistItem"; - case "olistItem": - return "olistItem"; - case "header1": - return "header1"; - case "subscript_": - return "subscript_"; - case "bold_": - return "bold_"; - case "monospace_": - return "monospace_"; - case "olist": - return "olist"; - case "ulist": - return "ulist"; - case "lineBreak": - return "lineBreak"; - case "superscript_": - return "superscript_"; - case "link": - return "link"; - case "extension": - return "extension"; - case "bold": - return "bold"; - default: - throw fail(); - } - }.call(this); - }, - token: function() { - var $elf = this, _fromIdx = this.input.idx, t, ans, c; - return this._or(function() { - return function() { - this._form(function() { - return function() { - t = this._apply("keyword"); - return ans = this._applyWithArgs("apply", t); - }.call(this); - }); - return ans; - }.call(this); - }, function() { - return function() { - c = this._apply("anything"); - return ShmakoWikiToHtml._escape(c); - }.call(this); - }); - }, - tokens: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - this._form(function() { - return c = this._many(function() { - return this._apply("token"); - }); - }); - return c.join(""); - }.call(this); - }, - bold: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - bold_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - italic: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - italic_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - underline: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - underline_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - strike: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - strike_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - monospace: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - monospace_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - superscript: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - superscript_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - subscript: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - subscript_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - link: function() { - var $elf = this, _fromIdx = this.input.idx, c, c, cc; - return this._or(function() { - return function() { - c = this._apply("token"); - this._form(function() { - return undefined; - }); - return '' + c + ""; - }.call(this); - }, function() { - return function() { - c = this._apply("token"); - cc = this._apply("tokens"); - return '' + cc + ""; - }.call(this); - }); - }, - link_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("token"); - return '' + c + ""; - }.call(this); - }, - lineBreak: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("anything"); - return "
    "; - }.call(this); - }, - escaped: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "" + c + ""; - }.call(this); - }, - para: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "

    " + c + "

    "; - }.call(this); - }, - header1: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return '

    ' + c + "

    "; - }.call(this); - }, - header2: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return '

    ' + c + "

    "; - }.call(this); - }, - header3: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return '

    ' + c + "

    "; - }.call(this); - }, - header4: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return '

    ' + c + "

    "; - }.call(this); - }, - header5: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return '
    ' + c + "
    "; - }.call(this); - }, - header6: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return '
    ' + c + "
    "; - }.call(this); - }, - olist: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "
      " + c + "
    "; - }.call(this); - }, - ulist: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return ""; - }.call(this); - }, - olistItem: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "
  • " + c + "
  • "; - }.call(this); - }, - ulistItem: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return "
  • " + c + "
  • "; - }.call(this); - }, - extension: function() { - var $elf = this, _fromIdx = this.input.idx, t, c, p; - return function() { - t = this._apply("anything"); - c = this._apply("anything"); - p = this._apply("anything"); - return ShmakoWikiToHtml["extensions"].hasOwnProperty(t) ? ShmakoWikiToHtml["extensions"][t](c, p) : "
    " + ShmakoWikiToHtml._escape(c) + "
    "; - }.call(this); - }, - topLevel: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return '
    ' + c + "
    "; - }.call(this); - } - }); - ShmakoWikiToHtml["extensions"] = { - html: function(c) { - return c; - }, - ohl: function(c, p) { - return OmetaHighlighterToHtml.match(c, "topLevel"); - }, - hljs: function(c, p) { - return '
    ' + ShmakoWikiToHtml._escape(c) + "
    "; - } - }; - ShmakoWikiToHtml["extensions"]["hl"] = ShmakoWikiToHtml["extensions"]["ohl"]; - ShmakoWikiToHtml["_escape"] = function() { - var amp = new RegExp("&", "g"), lt = new RegExp("<", "g"), gt = new RegExp(">", "g"), apos = new RegExp("'", "g"), quot = new RegExp('"', "g"); - return function(s) { - return String(s).replace(amp, "&").replace(lt, "<").replace(gt, ">").replace(apos, "'").replace(quot, """); - }; - }(); -}var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; -var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; -var isImmutable = ometajs_.isImmutable; -var digitValue = ometajs_.digitValue; -var isSequenceable = ometajs_.isSequenceable; -var escapeChar = ometajs_.escapeChar; -var unescape = ometajs_.unescape; -var getTag = ometajs_.getTag; -var inspect = ometajs_.inspect; -var lift = ometajs_.lift; -var clone = ometajs_.clone; -var Parser = ometajs_.Parser; -var fail = ometajs_.fail; -var OMeta = ometajs_.OMeta; -var BSNullOptimization = ometajs_.BSNullOptimization; -var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; -var BSSeqInliner = ometajs_.BSSeqInliner; -var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; -var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; -var BSOMetaParser = ometajs_.BSOMetaParser; -var BSOMetaTranslator = ometajs_.BSOMetaTranslator; -var BSJSParser = ometajs_.BSJSParser; -var BSSemActionParser = ometajs_.BSSemActionParser; -var BSJSIdentity = ometajs_.BSJSIdentity; -var BSJSTranslator = ometajs_.BSJSTranslator; -var BSOMetaJSParser = ometajs_.BSOMetaJSParser; -var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; -if (global === ometajs_) { - fail = (function(fail) { - return function() { return fail }; - })(fail); - OMeta = require('ometajs').OMeta; -}{ - var ShmakoWikiToBemjson = exports.ShmakoWikiToBemjson = objectThatDelegatesTo(OMeta, { - keyword: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - switch (this._apply("anything")) { - case "monospace": - return "monospace"; - case "strike": - return "strike"; - case "header6": - return "header6"; - case "header3": - return "header3"; - case "para": - return "para"; - case "italic": - return "italic"; - case "underline_": - return "underline_"; - case "header2": - return "header2"; - case "header4": - return "header4"; - case "italic_": - return "italic_"; - case "underline": - return "underline"; - case "header5": - return "header5"; - case "strike_": - return "strike_"; - case "link_": - return "link_"; - case "subscript": - return "subscript"; - case "escaped": - return "escaped"; - case "superscript": - return "superscript"; - case "ulistItem": - return "ulistItem"; - case "olistItem": - return "olistItem"; - case "header1": - return "header1"; - case "subscript_": - return "subscript_"; - case "bold_": - return "bold_"; - case "monospace_": - return "monospace_"; - case "olist": - return "olist"; - case "ulist": - return "ulist"; - case "lineBreak": - return "lineBreak"; - case "superscript_": - return "superscript_"; - case "link": - return "link"; - case "extension": - return "extension"; - case "bold": - return "bold"; - default: - throw fail(); - } - }.call(this); - }, - token: function() { - var $elf = this, _fromIdx = this.input.idx, t, ans, c; - return this._or(function() { - return function() { - this._form(function() { - return function() { - t = this._apply("keyword"); - return ans = this._applyWithArgs("apply", t); - }.call(this); - }); - return ans; - }.call(this); - }, function() { - return function() { - c = this._apply("anything"); - return ShmakoWikiToBemjson._escape(c); - }.call(this); - }); - }, - tokens: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - this._form(function() { - return c = this._many(function() { - return this._apply("token"); - }); - }); - return c; - }.call(this); - }, - bold: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "b", - content: c - }; - }.call(this); - }, - bold_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "b", - content: c - }; - }.call(this); - }, - italic: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "i", - content: c - }; - }.call(this); - }, - italic_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "i", - content: c - }; - }.call(this); - }, - underline: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "u", - content: c - }; - }.call(this); - }, - underline_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "u", - content: c - }; - }.call(this); - }, - strike: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "s", - content: c - }; - }.call(this); - }, - strike_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "s", - content: c - }; - }.call(this); - }, - monospace: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "tt", - content: c - }; - }.call(this); - }, - monospace_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "tt", - content: c - }; - }.call(this); - }, - superscript: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "sup", - content: c - }; - }.call(this); - }, - superscript_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "sup", - content: c - }; - }.call(this); - }, - subscript: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "sub", - content: c - }; - }.call(this); - }, - subscript_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "sub", - content: c - }; - }.call(this); - }, - link: function() { - var $elf = this, _fromIdx = this.input.idx, c, c, cc; - return this._or(function() { - return function() { - c = this._apply("token"); - this._form(function() { - return undefined; - }); - return { - block: "b-link", - url: c, - content: c - }; - }.call(this); - }, function() { - return function() { - c = this._apply("token"); - cc = this._apply("tokens"); - return { - block: "b-link", - url: c, - content: cc - }; - }.call(this); - }); - }, - link_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("token"); - return { - block: "b-link", - url: c, - content: c - }; - }.call(this); - }, - lineBreak: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("anything"); - return { - tag: "br" - }; - }.call(this); - }, - escaped: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - tag: "span", - content: c - }; - }.call(this); - }, - para: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - elem: "p", - content: c - }; - }.call(this); - }, - header1: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return { - elem: "h1", - attrs: { - id: a - }, - content: c - }; - }.call(this); - }, - header2: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return { - elem: "h2", - attrs: { - id: a - }, - content: c - }; - }.call(this); - }, - header3: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return { - elem: "h3", - attrs: { - id: a - }, - content: c - }; - }.call(this); - }, - header4: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return { - elem: "h4", - attrs: { - id: a - }, - content: c - }; - }.call(this); - }, - header5: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return { - elem: "h5", - attrs: { - id: a - }, - content: c - }; - }.call(this); - }, - header6: function() { - var $elf = this, _fromIdx = this.input.idx, c, a; - return function() { - c = this._apply("tokens"); - a = this._apply("anything"); - return { - elem: "h6", - attrs: { - id: a - }, - content: c - }; - }.call(this); - }, - olist: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - elem: "ol", - content: c - }; - }.call(this); - }, - ulist: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - elem: "ul", - content: c - }; - }.call(this); - }, - olistItem: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - elem: "li", - content: c - }; - }.call(this); - }, - ulistItem: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - elem: "li", - content: c - }; - }.call(this); - }, - extension: function() { - var $elf = this, _fromIdx = this.input.idx, t, c, p; - return function() { - t = this._apply("anything"); - c = this._apply("anything"); - p = this._apply("anything"); - return ShmakoWikiToBemjson["extensions"].hasOwnProperty(t) ? ShmakoWikiToBemjson["extensions"][t](c, p) : { - tag: "div", - content: ShmakoWikiToBemjson._escape(c) - }; - }.call(this); - }, - topLevel: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return { - block: "b-text", - content: c - }; - }.call(this); - } - }); - ShmakoWikiToBemjson["extensions"] = { - html: function(c) { - return c; - }, - ohl: function(c, p) { - return OmetaHighlighterToBemjson.match(c, "topLevel"); - }, - hljs: function(c, p) { - return { - block: "b-code", - cls: p, - content: ShmakoWikiToBemjson._escape(c) - }; - }, - toc: function(c, p) { - return ShmakoWikiTocToBemjson.tocToBemjson(c[0], ShmakoWikiToBemjson); - } - }; - ShmakoWikiToBemjson["extensions"]["hl"] = ShmakoWikiToBemjson["extensions"]["ohl"]; - ShmakoWikiToBemjson["_escape"] = function() { - var amp = new RegExp("&", "g"), lt = new RegExp("<", "g"), gt = new RegExp(">", "g"), apos = new RegExp("'", "g"), quot = new RegExp('"', "g"); - return function(s) { - return String(s).replace(amp, "&").replace(lt, "<").replace(gt, ">").replace(apos, "'").replace(quot, """); - }; - }(); -}var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; -var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; -var isImmutable = ometajs_.isImmutable; -var digitValue = ometajs_.digitValue; -var isSequenceable = ometajs_.isSequenceable; -var escapeChar = ometajs_.escapeChar; -var unescape = ometajs_.unescape; -var getTag = ometajs_.getTag; -var inspect = ometajs_.inspect; -var lift = ometajs_.lift; -var clone = ometajs_.clone; -var Parser = ometajs_.Parser; -var fail = ometajs_.fail; -var OMeta = ometajs_.OMeta; -var BSNullOptimization = ometajs_.BSNullOptimization; -var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; -var BSSeqInliner = ometajs_.BSSeqInliner; -var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; -var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; -var BSOMetaParser = ometajs_.BSOMetaParser; -var BSOMetaTranslator = ometajs_.BSOMetaTranslator; -var BSJSParser = ometajs_.BSJSParser; -var BSSemActionParser = ometajs_.BSSemActionParser; -var BSJSIdentity = ometajs_.BSJSIdentity; -var BSJSTranslator = ometajs_.BSJSTranslator; -var BSOMetaJSParser = ometajs_.BSOMetaJSParser; -var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; -if (global === ometajs_) { - fail = (function(fail) { - return function() { return fail }; - })(fail); - OMeta = require('ometajs').OMeta; -}var ShmakoWikiToPlain = exports.ShmakoWikiToPlain = objectThatDelegatesTo(OMeta, { - keyword: function() { - var $elf = this, _fromIdx = this.input.idx; - return function() { - switch (this._apply("anything")) { - case "monospace": - return "monospace"; - case "strike": - return "strike"; - case "underline": - return "underline"; - case "header3": - return "header3"; - case "para": - return "para"; - case "italic": - return "italic"; - case "underline_": - return "underline_"; - case "header6": - return "header6"; - case "header4": - return "header4"; - case "italic_": - return "italic_"; - case "strike_": - return "strike_"; - case "header5": - return "header5"; - case "escaped": - return "escaped"; - case "header2": - return "header2"; - case "subscript": - return "subscript"; - case "superscript": - return "superscript"; - case "header1": - return "header1"; - case "subscript_": - return "subscript_"; - case "bold_": - return "bold_"; - case "monospace_": - return "monospace_"; - case "superscript_": - return "superscript_"; - case "bold": - return "bold"; - default: - throw fail(); - } - }.call(this); - }, - token: function() { - var $elf = this, _fromIdx = this.input.idx, t, ans, c; - return this._or(function() { - return function() { - this._form(function() { - return function() { - t = this._apply("keyword"); - return ans = this._applyWithArgs("apply", t); - }.call(this); - }); - return ans; - }.call(this); - }, function() { - return function() { - c = this._apply("anything"); - return c; - }.call(this); - }); - }, - tokens: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - this._form(function() { - return c = this._many(function() { - return this._apply("token"); - }); - }); - return c; - }.call(this); - }, - bold: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - bold_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - italic: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - italic_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - underline: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - underline_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - strike: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - strike_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - monospace: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - monospace_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - superscript: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - superscript_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - subscript: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - subscript_: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - escaped: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - para: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - header1: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - header2: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - header3: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - header4: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - header5: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - header6: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - }, - topLevel: function() { - var $elf = this, _fromIdx = this.input.idx, c; - return function() { - c = this._apply("tokens"); - return c.join(""); - }.call(this); - } -}); \ No newline at end of file +var shmakowiki = require('./_shmakowiki.js'), + ShmakoWiki = shmakowiki.ShmakoWiki, + ShmakoWikiToBemjson = shmakowiki.ShmakoWikiToBemjson, + ShmakoWikiToHtml = shmakowiki.ShmakoWikiToHtml, + ShmakoWikiToPlain = shmakowiki.ShmakoWikiToPlain, + ShmakoWikiToc = require('./extensions/shmakowiki-toc/lib/shmakowikitoc.js'); + +var shmakowikiToAST = exports.shmakowikiToAST = function(text, level) { + return postprocessAST(ShmakoWiki.matchAll(text, level || "topLevel"), level); +}; + +var shmakowikiToBemjson = exports.shmakowikiToBemjson = function(text, level) { + return astToBemjson(shmakowikiToAST(text, level)); +}; + +var shmakowikiToHtml = exports.shmakowikiToHtml = function(text, level) { + return astToHtml(shmakowikiToAST(text, level), level); +}; + +var shmakowikiToPlain = exports.shmakowikiToPlain = function(text, level) { + return astToPlain(shmakowikiToAST(text, level), level); +}; + +var astToBemjson = exports.astToBemjson = function(ast, level) { + return ShmakoWikiToBemjson.match(ast, level || 'topLevel'); +}; + +var astToHtml = exports.astToHtml = function(ast, level) { + return ShmakoWikiToHtml.match(ast, level || 'topLevel'); +}; + +var astToPlain = exports.astToPlain = function(ast, level) { + return ShmakoWikiToPlain.match(ast, level || 'topLevel'); +}; + +var postprocessAST = function(ast, level) { + return ShmakoWikiToc.transform(ast); +}; \ No newline at end of file diff --git a/src/shmakowiki.js b/src/shmakowiki.js index ddf0248..af05d78 100644 --- a/src/shmakowiki.js +++ b/src/shmakowiki.js @@ -7,4 +7,5 @@ var ometajs = require('ometajs'), OmetaHighlighterToHtml = ohl.OmetaHighlighterToHtml, OmetaHighlighterToBemjson = ohl.OmetaHighlighterToBemjson, ShmakoWikiToc = shtoc.ShmakoWikiToc, - ShmakoWikiTocToBemjson = shtoc.ShmakoWikiTocToBemjson; + ShmakoWikiTocToBemjson = shtoc.ShmakoWikiTocToBemjson, + ShmakoWikiTocToHtml = shtoc.ShmakoWikiTocToHtml; diff --git a/src/shmakowiki.ometajs b/src/shmakowiki.ometajs index d1dfe3e..75842ef 100644 --- a/src/shmakowiki.ometajs +++ b/src/shmakowiki.ometajs @@ -124,8 +124,7 @@ ShmakoWiki.extensions = { return OmetaHighlighter.matchAll(c, p); }, 'toc': function(c, p) { - var x = ShmakoWikiToc.parse(c, p, ShmakoWiki); - return [x.toc, x.ast]; + return ShmakoWiki.matchAll(c, 'topLevel'); } }; diff --git a/src/shmakowiki.ometajs.js b/src/shmakowiki.ometajs.js index 22ad17c..3a48a51 100644 --- a/src/shmakowiki.ometajs.js +++ b/src/shmakowiki.ometajs.js @@ -980,8 +980,7 @@ if (global === ometajs_) { return OmetaHighlighter.matchAll(c, p); }, toc: function(c, p) { - var x = ShmakoWikiToc.parse(c, p, ShmakoWiki); - return [ x["toc"], x["ast"] ]; + return ShmakoWiki.matchAll(c, "topLevel"); } }; ShmakoWiki["extensions"]["hl"] = ShmakoWiki["extensions"]["ohl"]; diff --git a/src/shmakowiki2html.ometajs b/src/shmakowiki2html.ometajs index ea8dfcf..d1f55df 100644 --- a/src/shmakowiki2html.ometajs +++ b/src/shmakowiki2html.ometajs @@ -77,7 +77,11 @@ ShmakoWikiToHtml.extensions = { return '
    ' +
                         ShmakoWikiToHtml._escape(c) +
                     '
    ' - } + }, + 'toc': function(c, p) { + console.log(ShmakoWikiTocToHtml.tocToHtml(c[0], ShmakoWikiToHtml)); + return ShmakoWikiTocToHtml.tocToHtml(c[0], ShmakoWikiToHtml); + } }; ShmakoWikiToHtml.extensions.hl = ShmakoWikiToHtml.extensions.ohl; diff --git a/src/shmakowiki2html.ometajs.js b/src/shmakowiki2html.ometajs.js index 4ed6d4a..03389f0 100644 --- a/src/shmakowiki2html.ometajs.js +++ b/src/shmakowiki2html.ometajs.js @@ -377,6 +377,10 @@ if (global === ometajs_) { }, hljs: function(c, p) { return '
    ' + ShmakoWikiToHtml._escape(c) + "
    "; + }, + toc: function(c, p) { + console.log(ShmakoWikiTocToHtml.tocToHtml(c[0], ShmakoWikiToHtml)); + return ShmakoWikiTocToHtml.tocToHtml(c[0], ShmakoWikiToHtml); } }; ShmakoWikiToHtml["extensions"]["hl"] = ShmakoWikiToHtml["extensions"]["ohl"];