diff --git a/README.md b/README.md index 5710f46..e92621e 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,19 @@ instead of ## Syntax -Every Ecmarkdown fragment is a **numeric list**. They are written as a series of lines, each starting with `1. `. Lines can be indented by multiples of exactly two spaces to indicate nesting. +### Top-Level Constructs + +Every Ecmarkdown fragment is a either a **numeric list** or a **paragraph**. These two base productions can contain other productions. + +**Numeric lists** are written as a series of lines, each starting with `1. `. Lines can be indented by multiples of exactly two spaces to indicate nesting. To convert a numeric list, use `ecmarkdown.list(stringOfText)` to get back HTML for the list (with root element `
    `). + +**Paragraphs** are a single line of text. To convert a paragraph, use `ecmarkdown.paragraph(stringOfText)` to get back HTML for the paragraph (with root element `

    `). + +In the future we will unify these into a single parser that allows multiple paragraphs, multiple lists, and any combination thereof. But for now the above is what's fallen out of the work done so far. + +### Inline Constructs + +Within a paragraph or list item line, the following can be used: **Variables** are written as `_x_` and are translated to `x`. Variables cannot contain spaces, but can contain underscores. @@ -59,4 +71,4 @@ Every Ecmarkdown fragment is a **numeric list**. They are written as a series of Ecmarkdown is meant to be used together with [Ecmarkup](https://github.com/bterlson/ecmarkup/). Ecmarkup has an `` element within which Ecmarkdown can be used; additionally, several Ecmarkdown productions produce Ecmarkup elements, as noted above. -In short, we expect Ecmarkdown to be embedded within a larger Ecmarkup document, used for writing algorithm steps (and perhaps paragraphs) in a concise format. +In short, we expect Ecmarkdown to be embedded within a larger Ecmarkup document, used for writing algorithm steps and paragraphs in a concise format. diff --git a/lib/actions.js b/lib/actions.js index a93d357..1a7187d 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -58,6 +58,15 @@ exports.listItem = function (atoms) { return li; }; +exports.paragraph = function (atoms) { + var p = $('

    '); + atoms.forEach(function (atom) { + p.append(atom); + }); + + return beautify($.html(p)); +}; + exports.variable = function (first, rest) { return $('').text(first + rest); }; diff --git a/lib/ecmarkdown.js b/lib/ecmarkdown.js index fb1e261..e247b97 100644 --- a/lib/ecmarkdown.js +++ b/lib/ecmarkdown.js @@ -1,6 +1,10 @@ 'use strict'; var parser = require('./generated-parser.js'); -module.exports = function translate(ecmarkdown) { - return parser.parse(ecmarkdown.trim() + '\n'); +exports.list = function (ecmarkdown) { + return parser.parse(ecmarkdown.trim() + '\n', { startRule: 'list' }); +}; + +exports.paragraph = function (ecmarkdown) { + return parser.parse(ecmarkdown.trim(), { startRule: 'paragraph' }); }; diff --git a/lib/grammar.pegjs b/lib/grammar.pegjs index 6b95a19..0af5606 100644 --- a/lib/grammar.pegjs +++ b/lib/grammar.pegjs @@ -3,8 +3,14 @@ var state = actions.makeInitialState(); } +// Entry points + list = lines:(dents $[0-9]+ ". " listItem "\n")+ { return actions.list(lines, state); } +paragraph = atoms:atom+ { return actions.paragraph(atoms); } + +// Supporting productions + dents = spaces:" "* { return actions.dents(spaces, state); } listItem = atoms:atom+ { return actions.listItem(atoms); } diff --git a/package.json b/package.json index 277ea45..006a414 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ }, "devDependencies": { "baseline-tester": "^1.1.1", + "bluebird": "^2.6.2", "glob": "^4.0.5", "jscs": "^1.6.1", "jshint": "^2.5.5" diff --git a/scripts/generate-grammar.js b/scripts/generate-grammar.js index 31f6c9e..aef22c2 100644 --- a/scripts/generate-grammar.js +++ b/scripts/generate-grammar.js @@ -6,7 +6,7 @@ process.stdin.on('data', function (data) { grammarText += data; }); process.stdin.on('end', function () { - var parserSource = pegjs.buildParser(grammarText, { output: 'source' }); + var parserSource = pegjs.buildParser(grammarText, { output: 'source', allowedStartRules: ['list', 'paragraph'] }); process.stdout.write('module.exports = '); process.stdout.write(parserSource); }); diff --git a/test/cases/code.ecmarkdown b/test/list-cases/code.ecmarkdown similarity index 100% rename from test/cases/code.ecmarkdown rename to test/list-cases/code.ecmarkdown diff --git a/test/cases/code.html b/test/list-cases/code.html similarity index 100% rename from test/cases/code.html rename to test/list-cases/code.html diff --git a/test/cases/iterator-close.ecmarkdown b/test/list-cases/iterator-close.ecmarkdown similarity index 100% rename from test/cases/iterator-close.ecmarkdown rename to test/list-cases/iterator-close.ecmarkdown diff --git a/test/cases/iterator-close.html b/test/list-cases/iterator-close.html similarity index 100% rename from test/cases/iterator-close.html rename to test/list-cases/iterator-close.html diff --git a/test/cases/list-numbering.ecmarkdown b/test/list-cases/list-numbering.ecmarkdown similarity index 100% rename from test/cases/list-numbering.ecmarkdown rename to test/list-cases/list-numbering.ecmarkdown diff --git a/test/cases/list-numbering.html b/test/list-cases/list-numbering.html similarity index 100% rename from test/cases/list-numbering.html rename to test/list-cases/list-numbering.html diff --git a/test/cases/list.ecmarkdown b/test/list-cases/list.ecmarkdown similarity index 100% rename from test/cases/list.ecmarkdown rename to test/list-cases/list.ecmarkdown diff --git a/test/cases/list.html b/test/list-cases/list.html similarity index 100% rename from test/cases/list.html rename to test/list-cases/list.html diff --git a/test/cases/nested-list.ecmarkdown b/test/list-cases/nested-list.ecmarkdown similarity index 100% rename from test/cases/nested-list.ecmarkdown rename to test/list-cases/nested-list.ecmarkdown diff --git a/test/cases/nested-list.html b/test/list-cases/nested-list.html similarity index 100% rename from test/cases/nested-list.html rename to test/list-cases/nested-list.html diff --git a/test/cases/nonterminals.ecmarkdown b/test/list-cases/nonterminals.ecmarkdown similarity index 100% rename from test/cases/nonterminals.ecmarkdown rename to test/list-cases/nonterminals.ecmarkdown diff --git a/test/cases/nonterminals.html b/test/list-cases/nonterminals.html similarity index 100% rename from test/cases/nonterminals.html rename to test/list-cases/nonterminals.html diff --git a/test/cases/spec-constants.ecmarkdown b/test/list-cases/spec-constants.ecmarkdown similarity index 100% rename from test/cases/spec-constants.ecmarkdown rename to test/list-cases/spec-constants.ecmarkdown diff --git a/test/cases/spec-constants.html b/test/list-cases/spec-constants.html similarity index 100% rename from test/cases/spec-constants.html rename to test/list-cases/spec-constants.html diff --git a/test/cases/values.ecmarkdown b/test/list-cases/values.ecmarkdown similarity index 100% rename from test/cases/values.ecmarkdown rename to test/list-cases/values.ecmarkdown diff --git a/test/cases/values.html b/test/list-cases/values.html similarity index 100% rename from test/cases/values.html rename to test/list-cases/values.html diff --git a/test/cases/variables.ecmarkdown b/test/list-cases/variables.ecmarkdown similarity index 100% rename from test/cases/variables.ecmarkdown rename to test/list-cases/variables.ecmarkdown diff --git a/test/cases/variables.html b/test/list-cases/variables.html similarity index 100% rename from test/cases/variables.html rename to test/list-cases/variables.html diff --git a/test/paragraph-cases/beginning-value.ecmarkdown b/test/paragraph-cases/beginning-value.ecmarkdown new file mode 100644 index 0000000..39b1b6f --- /dev/null +++ b/test/paragraph-cases/beginning-value.ecmarkdown @@ -0,0 +1 @@ +*beginning* value diff --git a/test/paragraph-cases/beginning-value.html b/test/paragraph-cases/beginning-value.html new file mode 100644 index 0000000..27c3ccf --- /dev/null +++ b/test/paragraph-cases/beginning-value.html @@ -0,0 +1 @@ +

    beginning value

    diff --git a/test/run-cases.js b/test/run-cases.js index eab06fe..de60961 100644 --- a/test/run-cases.js +++ b/test/run-cases.js @@ -1,10 +1,21 @@ 'use strict'; var path = require('path'); -var baselineTester = require('baseline-tester'); -var generate = require('..'); +var Bluebird = require('bluebird'); +var baselineTester = Bluebird.promisify(require('baseline-tester')); +var ecmarkdown = require('..'); -baselineTester(generate, { - casesDirectory: path.resolve(__dirname, 'cases'), - inputExtension: 'ecmarkdown', - outputExtension: 'html' -}); +Bluebird.try(function () { + return baselineTester(ecmarkdown.list, { + casesDirectory: path.resolve(__dirname, 'list-cases'), + inputExtension: 'ecmarkdown', + outputExtension: 'html' + }); +}) +.then(function () { + return baselineTester(ecmarkdown.paragraph, { + casesDirectory: path.resolve(__dirname, 'paragraph-cases'), + inputExtension: 'ecmarkdown', + outputExtension: 'html' + }); +}) +.done();