Skip to content

Commit

Permalink
Support non-ASCII tags
Browse files Browse the repository at this point in the history
  • Loading branch information
amake committed Oct 12, 2024
1 parent 56e1600 commit a3795a1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
12 changes: 2 additions & 10 deletions lib/src/org/grammar.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:more/char_matcher.dart';
import 'package:org_parser/src/todo/model.dart';
import 'package:org_parser/src/util/util.dart';
import 'package:petitparser/petitparser.dart';
Expand Down Expand Up @@ -73,7 +72,7 @@ class OrgGrammarDefinition extends GrammarDefinition {
char(':') &
lineEnd().and();

Parser tag() => pattern('a-zA-Z0-9_@#%').plusString('Tags expected');
Parser tag() => (alnum() | anyOf('_@#%')).plus().flatten('Tags expected');

Parser content() => ref0(_content).flatten('Content expected');

Expand Down Expand Up @@ -313,14 +312,7 @@ class OrgContentGrammarDefinition extends GrammarDefinition {
ref1(sexpList, '{}') |
ref0(sexpList) |
char('*') |
(anyOf('+-').optional() &
reverseId(ref0(alnum) | anyOf(r'.,\'), ref0(alnum)));

Parser alnum() => any().where((char) {
final c = char.codeUnitAt(0);
return UnicodeCharMatcher.letter().match(c) ||
UnicodeCharMatcher.number().match(c);
});
(anyOf('+-').optional() & reverseId(alnum() | anyOf(r'.,\'), alnum()));

Parser macroReference() =>
string('{{{') &
Expand Down
2 changes: 1 addition & 1 deletion lib/src/query/grammar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class OrgQueryGrammarDefinition extends GrammarDefinition {
// TODO(aaron): Support regex element: {^boss.*}
Parser element() => ref0(propertyExpression) | ref0(tag);

Parser tag() => pattern('a-zA-Z0-9_@#%').plusString('Tag expected');
Parser tag() => (alnum() | anyOf('_@#%')).plus().flatten('Tag expected');

Parser propertyExpression() =>
ref0(propertyName) & ref0(op) & ref0(propertyValue);
Expand Down
8 changes: 8 additions & 0 deletions lib/src/util/alnum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:more/char_matcher.dart';
import 'package:petitparser/petitparser.dart';

Parser alnum() => any().where((char) {
final c = char.codeUnitAt(0);
return UnicodeCharMatcher.letter().match(c) ||
UnicodeCharMatcher.number().match(c);
});
1 change: 1 addition & 0 deletions lib/src/util/util.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'alnum.dart';
export 'block.dart';
export 'bof.dart';
export 'drop.dart';
Expand Down
7 changes: 7 additions & 0 deletions test/org/model/header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ void main() {
final headline = result.value as OrgHeadline;
expect(headline.toMarkup(), markup);
});
test('non-ASCII tags', () {
final markup = '* TODO foo :あ:';
final result = parser.parse(markup);
final headline = result.value as OrgHeadline;
expect(headline.tags!.values, ['あ']);
expect(headline.toMarkup(), markup);
});
});
}
4 changes: 4 additions & 0 deletions test/query/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ void main() {
OrgQueryNotMatcher(OrgQueryTagMatcher('foo')),
);
});
test('non-ASCII tag', () {
expect(orgQuery.parse('あ').value, OrgQueryTagMatcher('あ'));
expect(orgQuery.parse('+あ').value, OrgQueryTagMatcher('あ'));
});
test('implicit and', () {
expect(
orgQuery.parse('foo+bar').value,
Expand Down

1 comment on commit a3795a1

@llcc
Copy link

@llcc llcc commented on a3795a1 Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue not directly related to this commit, but since you delved into the tags improvement, I would like to bring it up here. Multiple tags are being truncated based on the screen width, and there is no way to view the entire tags. See the example below.

Org-mode source:

* multiple headline tags  :BAR:FOO:BAZ:タグ:QUX:Archive:

Image

Please sign in to comment.