Skip to content

Commit

Permalink
Support custom headline keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
amake committed Mar 4, 2024
1 parent 0410732 commit 84a6a00
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/src/org/grammar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ library org_parser;
import 'package:org_parser/src/util/util.dart';
import 'package:petitparser/petitparser.dart';

/// Equivalent to `org-todo-keywords`
const defaultTodoKeywords = ['TODO', 'DONE'];

// See https://orgmode.org/worg/dev/org-syntax.html

/// Top-level grammar definition
Expand All @@ -18,6 +21,13 @@ import 'package:petitparser/petitparser.dart';
/// The structure and the content turned out to be hard to define together, so
/// the content rules are defined separately in [OrgContentGrammarDefinition].
class OrgGrammarDefinition extends GrammarDefinition {
const OrgGrammarDefinition({this.todoKeywords});

/// Equivalent to `org-todo-keywords`. If not provided, defaults to
/// [defaultTodoKeywords].
final List<String>? todoKeywords;
List<String> get _todoKeywords => todoKeywords ?? defaultTodoKeywords;

@override
Parser start() => ref0(document).end();

Expand All @@ -37,7 +47,7 @@ class OrgGrammarDefinition extends GrammarDefinition {
Parser stars() => char('*').plusString() & char(' ').plusString();

Parser todoKeyword() =>
(string('TODO') | string('DONE')) & char(' ').starString();
_todoKeywords.map(string).toChoiceParser() & char(' ').starString();

Parser priority() =>
string('[#') &
Expand Down
2 changes: 2 additions & 0 deletions lib/src/org/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ final org = OrgParserDefinition().build();

/// Top-level parser definitions
class OrgParserDefinition extends OrgGrammarDefinition {
const OrgParserDefinition({super.todoKeywords});

@override
Parser start() => super.start().map((items) {
final topContent = items[0] as OrgContent?;
Expand Down
16 changes: 16 additions & 0 deletions test/org/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -651,4 +651,20 @@ bazoonga''');
expect(doc.sections[0].headline.keyword?.value, 'TODO');
});
});
group('custom parser', () {
test('todo keywords', () {
final parser = OrgParserDefinition(todoKeywords: ['FOO', 'BAR']).build();
{
final doc = parser.parse('''* FOO [#A] foo bar
baz buzz''').value as OrgDocument;
expect(doc.sections[0].headline.keyword?.value, 'FOO');
}
{
final doc = parser.parse('''* TODO [#A] foo bar
baz buzz''').value as OrgDocument;
expect(doc.sections[0].headline.keyword?.value, isNull);
expect(doc.sections[0].headline.rawTitle, 'TODO [#A] foo bar');
}
});
});
}

0 comments on commit 84a6a00

Please sign in to comment.