Skip to content

Commit

Permalink
Merge pull request #64 from rasendubi/feat-sub-superscripts
Browse files Browse the repository at this point in the history
feat(uniorg-parse): support useSubSuperscripts options
  • Loading branch information
rasendubi authored Jan 29, 2023
2 parents 6617c95 + 2325611 commit 5d8999d
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 28 deletions.
204 changes: 204 additions & 0 deletions packages/uniorg-parse/src/__snapshots__/parser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3239,6 +3239,210 @@ children:
value: "hello\\n#+BLAH[hi]: heh\\nhi\\n"
`;
exports[`org/parser options.useSubSuperscript = {} ignores subscript with number 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 3
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 3
children:
- type: "text"
value: "x_2"
`;
exports[`org/parser options.useSubSuperscript = {} ignores superscript with number 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 3
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 3
children:
- type: "text"
value: "x^2"
`;
exports[`org/parser options.useSubSuperscript = {} parses subscript with braces 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 5
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 5
children:
- type: "text"
value: "x"
- type: "subscript"
contentsBegin: 3
contentsEnd: 4
children:
- type: "text"
value: "2"
`;
exports[`org/parser options.useSubSuperscript = {} parses superscript with braces 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 5
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 5
children:
- type: "text"
value: "x"
- type: "superscript"
contentsBegin: 3
contentsEnd: 4
children:
- type: "text"
value: "2"
`;
exports[`org/parser options.useSubSuperscript = false ignores subscript with braces 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 5
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 5
children:
- type: "text"
value: "x_{2}"
`;
exports[`org/parser options.useSubSuperscript = false ignores subscript with number 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 3
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 3
children:
- type: "text"
value: "x_2"
`;
exports[`org/parser options.useSubSuperscript = false ignores superscript with braces 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 5
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 5
children:
- type: "text"
value: "x^{2}"
`;
exports[`org/parser options.useSubSuperscript = false ignores superscript with number 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 3
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 3
children:
- type: "text"
value: "x^2"
`;
exports[`org/parser options.useSubSuperscript = true parses subscript with braces 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 5
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 5
children:
- type: "text"
value: "x"
- type: "subscript"
contentsBegin: 3
contentsEnd: 4
children:
- type: "text"
value: "2"
`;
exports[`org/parser options.useSubSuperscript = true parses subscript with number 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 3
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 3
children:
- type: "text"
value: "x"
- type: "subscript"
contentsBegin: 2
contentsEnd: 3
children:
- type: "text"
value: "2"
`;
exports[`org/parser options.useSubSuperscript = true parses superscript with braces 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 5
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 5
children:
- type: "text"
value: "x"
- type: "superscript"
contentsBegin: 3
contentsEnd: 4
children:
- type: "text"
value: "2"
`;
exports[`org/parser options.useSubSuperscript = true parses superscript with number 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 3
children:
- type: "paragraph"
affiliated: {}
contentsBegin: 0
contentsEnd: 3
children:
- type: "text"
value: "x"
- type: "superscript"
contentsBegin: 2
contentsEnd: 3
children:
- type: "text"
value: "2"
`;
exports[`org/parser paragraph split by empty line 1`] = `
type: "org-data"
contentsBegin: 0
Expand Down
9 changes: 9 additions & 0 deletions packages/uniorg-parse/src/parse-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ export interface ParseOptions {
* plain list of keywords.
*/
todoKeywords: string[];
/**
* Same as `org-use-sub-superscripts` in Emacs.
*
* - `true` (default) - parse sub-/superscripts
* - `false` - do not parse sub-/superscripts
* - `'{}'` - only parse sub-/superscripts when enclosed in braces
*/
useSubSuperscripts: true | false | '{}';
/**
* Allows overriding parameters for emphasis regex. Corresponds to
* `org-emphasis-regex-components` in Emacs.
Expand All @@ -29,6 +37,7 @@ export interface ParseOptions {

export const defaultOptions: ParseOptions = {
todoKeywords: ['TODO', 'DONE'],
useSubSuperscripts: true,
emphasisRegexpComponents: {
// deviates from org mode default to allow ndash, mdash, and
// quotes (’“”)
Expand Down
47 changes: 47 additions & 0 deletions packages/uniorg-parse/src/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,53 @@ not a block
});
});

describe('options.useSubSuperscript', () => {
describe('= true', () => {
itParses('parses superscript with number', 'x^2', {
useSubSuperscripts: true,
});
itParses('parses subscript with number', 'x_2', {
useSubSuperscripts: true,
});
itParses('parses superscript with braces', 'x^{2}', {
useSubSuperscripts: true,
});
itParses('parses subscript with braces', 'x_{2}', {
useSubSuperscripts: true,
});
});

describe('= false', () => {
itParses('ignores superscript with number', 'x^2', {
useSubSuperscripts: false,
});
itParses('ignores subscript with number', 'x_2', {
useSubSuperscripts: false,
});
itParses('ignores superscript with braces', 'x^{2}', {
useSubSuperscripts: false,
});
itParses('ignores subscript with braces', 'x_{2}', {
useSubSuperscripts: false,
});
});

describe('= {}', () => {
itParses('ignores superscript with number', 'x^2', {
useSubSuperscripts: '{}',
});
itParses('ignores subscript with number', 'x_2', {
useSubSuperscripts: '{}',
});
itParses('parses superscript with braces', 'x^{2}', {
useSubSuperscripts: '{}',
});
itParses('parses subscript with braces', 'x_{2}', {
useSubSuperscripts: '{}',
});
});
});

itParses(
'table',
`
Expand Down
24 changes: 22 additions & 2 deletions packages/uniorg-parse/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1393,9 +1393,19 @@ class Parser {
// Object parsers.

private parseSuperscript(): Superscript | null {
if (!this.options.useSubSuperscripts) {
return null;
}

this.r.backoff(1); // backoff by one, to match previous char (should be non-space)
const start = this.r.offset();
const m = this.r.advance(this.r.lookingAt(this.re.subsuperscriptRe()));
const m = this.r.advance(
this.r.lookingAt(
this.options.useSubSuperscripts === '{}'
? this.re.matchSubstringWithBracesRegex()
: this.re.matchSubstringRegex()
)
);
if (!m) return null;

const inside = m.groups!['inBraces'] || m.groups!['inBrackets'];
Expand All @@ -1409,9 +1419,19 @@ class Parser {
}

private parseSubscript(): Subscript | null {
if (!this.options.useSubSuperscripts) {
return null;
}

this.r.backoff(1); // backoff by one, to match previous char (should be non-space)
const start = this.r.offset();
const m = this.r.advance(this.r.lookingAt(this.re.subsuperscriptRe()));
const m = this.r.advance(
this.r.lookingAt(
this.options.useSubSuperscripts === '{}'
? this.re.matchSubstringWithBracesRegex()
: this.re.matchSubstringRegex()
)
);
if (!m) return null;

const inside = m.groups!['inBraces'] || m.groups!['inBrackets'];
Expand Down
Loading

0 comments on commit 5d8999d

Please sign in to comment.