Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equations upgrade #573

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/idyll-compiler/src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,47 @@ const lex = function(options) {
.concat(['CLOSE_BRACKET']);
}
);
// Standalone, centered equation inside $$...$$
lexer.addRule(
/()\[\s*\${2}\s*([^\/\$]*)\s[\n\s\t]*(((?!(\${2}\s*\])).)*)[\n\s\t]*\s*\${2}\s*\]/i,
function(lexeme, props, innerText) {
inComponent = false;
if (this.reject) return;
updatePosition(lexeme);
return ['OPEN_BRACKET', 'COMPONENT_NAME']
.concat(formatToken('equation'))
.concat(recurse(props, { inComponent: true, gotName: true }))
.concat(['COMPONENT_WORD'])
.concat(formatToken('display'))
.concat(['PARAM_SEPARATOR'])
.concat(['BOOLEAN'])
.concat(formatToken('true'))
.concat(['CLOSE_BRACKET'])
.concat(['WORDS'])
.concat(formatToken(innerText))
.concat(['OPEN_BRACKET', 'FORWARD_SLASH', 'COMPONENT_NAME'])
.concat(formatToken('equation'))
.concat(['CLOSE_BRACKET']);
}
);
// Inline equation inside $...$
lexer.addRule(
/()\[\s*\${1}\s*([^\/\$]*)\s[\n\s\t]*(((?!(\${1}\s*\])).)*)[\n\s\t]*\s*\${1}\s*\]/i,
function(lexeme, props, innerText) {
inComponent = false;
if (this.reject) return;
updatePosition(lexeme);
return ['OPEN_BRACKET', 'COMPONENT_NAME']
.concat(formatToken('equation'))
.concat(recurse(props, { inComponent: true, gotName: true }))
.concat(['CLOSE_BRACKET'])
.concat(['WORDS'])
.concat(formatToken(innerText))
.concat(['OPEN_BRACKET', 'FORWARD_SLASH', 'COMPONENT_NAME'])
.concat(formatToken('equation'))
.concat(['CLOSE_BRACKET']);
}
);
lexer.addRule(
/\[\s*code\s*([^\/\]]*)\s*\][\n\s\t]*(((?!(\[\s*code\s*\])).)*)[\n\s\t]*\[\s*\/\s*code\s*\]/i,
function(lexeme, props, innerText) {
Expand Down
48 changes: 48 additions & 0 deletions packages/idyll-compiler/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ describe('compiler', function() {
);
});

it('should handle standalone, centered equations inside $$...$$', function() {
var lex = Lexer();
var results = lex('some equation: [$$ y = 0 $$]');
expect(results.tokens.join(' ')).to.eql(
'WORDS TOKEN_VALUE_START "some equation: " TOKEN_VALUE_END OPEN_BRACKET COMPONENT_NAME TOKEN_VALUE_START "equation" TOKEN_VALUE_END COMPONENT_WORD TOKEN_VALUE_START "display" TOKEN_VALUE_END PARAM_SEPARATOR BOOLEAN TOKEN_VALUE_START "true" TOKEN_VALUE_END CLOSE_BRACKET WORDS TOKEN_VALUE_START "y = 0" TOKEN_VALUE_END OPEN_BRACKET FORWARD_SLASH COMPONENT_NAME TOKEN_VALUE_START "equation" TOKEN_VALUE_END CLOSE_BRACKET EOF'
);
});

it('should handle inline equations inside $...$', function() {
var lex = Lexer();
var results = lex('some inline equation: [$ y = 0 $]');
expect(results.tokens.join(' ')).to.eql(
'WORDS TOKEN_VALUE_START "some inline equation: " TOKEN_VALUE_END OPEN_BRACKET COMPONENT_NAME TOKEN_VALUE_START "equation" TOKEN_VALUE_END CLOSE_BRACKET WORDS TOKEN_VALUE_START "y = 0" TOKEN_VALUE_END OPEN_BRACKET FORWARD_SLASH COMPONENT_NAME TOKEN_VALUE_START "equation" TOKEN_VALUE_END CLOSE_BRACKET EOF'
);
});

it('should handle backticks in a paragraph', function() {
var lex = Lexer();
var results = lex('regular text and stuff, then some `code`');
Expand Down Expand Up @@ -1140,6 +1156,38 @@ End text
);
});

it('should handle equations within $$...$$', function() {
const input = `[$$ \sum_{j=0}^n x^{j} + \sum_{k=0}^n x^{k} $$]`;
expect(compile(input, { async: false })).to.eql(
AST.convertV1ToV2([
[
'TextContainer',
[],
[
[
'equation',
[['display', ['value', true]]],
['sum_{j=0}^n x^{j} + sum_{k=0}^n x^{k}']
]
]
]
])
);
});

it('should handle equations within $...$', function() {
const input = `[$ \sum_{j=0}^n x^{j} + \sum_{k=0}^n x^{k} $]`;
expect(compile(input, { async: false })).to.eql(
AST.convertV1ToV2([
[
'TextContainer',
[],
[['equation', [], ['sum_{j=0}^n x^{j} + sum_{k=0}^n x^{k}']]]
]
])
);
});

it('should handle code blocks with parens inside', function() {
const input = `[code](n - 1)!/2 possible paths[/code]`;
expect(compile(input, { async: false })).to.eql(
Expand Down