Skip to content

Commit

Permalink
Add support for paragraphs as a top-level production
Browse files Browse the repository at this point in the history
This changes the API to export `list` and `paragraph` methods, which are now documented in the README.
  • Loading branch information
domenic committed Jan 9, 2015
1 parent 63ad249 commit 4424df1
Show file tree
Hide file tree
Showing 27 changed files with 57 additions and 12 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<ol>`).

**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 `<p>`).

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 `<var>x</var>`. Variables cannot contain spaces, but can contain underscores.

Expand All @@ -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 `<emu-alg>` 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.
9 changes: 9 additions & 0 deletions lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ exports.listItem = function (atoms) {
return li;
};

exports.paragraph = function (atoms) {
var p = $('<p>');
atoms.forEach(function (atom) {
p.append(atom);
});

return beautify($.html(p));
};

exports.variable = function (first, rest) {
return $('<var>').text(first + rest);
};
Expand Down
8 changes: 6 additions & 2 deletions lib/ecmarkdown.js
Original file line number Diff line number Diff line change
@@ -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' });
};
6 changes: 6 additions & 0 deletions lib/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/paragraph-cases/beginning-value.ecmarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*beginning* value
1 change: 1 addition & 0 deletions test/paragraph-cases/beginning-value.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><emu-val>beginning</emu-val> value</p>
25 changes: 18 additions & 7 deletions test/run-cases.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 4424df1

Please sign in to comment.