Add scaffolding for an inline tag processor

This is just a simple test to extract the inline tags, it hasn't been
hooked up to anything.

Related to #44
This commit is contained in:
Vishesh Handa
2020-08-11 13:58:52 +02:00
parent 71b3d841da
commit 01f1b8481f
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,28 @@
import 'package:gitjournal/core/note.dart';
class InlineTagsProcessor {
// FIXME: Make me configurable
final List<String> tagPrefixes = ['#'];
void process(Note note) {}
Set<String> extractTags(String text) {
var tags = <String>{};
for (var prefix in tagPrefixes) {
var regexp = RegExp(r'(^|\s)' + prefix + r'([^ ]+)(\s|$)');
var matches = regexp.allMatches(text);
for (var match in matches) {
var tag = match.group(2);
if (tag.endsWith('.') || tag.endsWith('!') || tag.endsWith('?')) {
tag = tag.substring(0, tag.length - 1);
}
tags.add(tag);
}
}
return tags;
}
}

View File

@ -0,0 +1,28 @@
import 'package:test/test.dart';
import 'package:gitjournal/core/processors/inline_tags.dart';
void main() {
test('Should parse simple tags', () {
var body = "#hello Hi\nthere how#are you #doing now? #dog";
var p = InlineTagsProcessor();
var tags = p.extractTags(body);
expect(tags, {'hello', 'doing', 'dog'});
});
test('Ignore . at the end of a tag', () {
var body = "Hi there #tag.";
var p = InlineTagsProcessor();
var tags = p.extractTags(body);
expect(tags, {'tag'});
});
// #a#b should be counted as two tags
// + should work as a prefix
// @ should work as a prefix
// test for tags with non-ascii words
}