diff --git a/lib/core/processors/inline_tags.dart b/lib/core/processors/inline_tags.dart new file mode 100644 index 00000000..b7fcfa5a --- /dev/null +++ b/lib/core/processors/inline_tags.dart @@ -0,0 +1,28 @@ +import 'package:gitjournal/core/note.dart'; + +class InlineTagsProcessor { + // FIXME: Make me configurable + final List tagPrefixes = ['#']; + + void process(Note note) {} + + Set extractTags(String text) { + var tags = {}; + + 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; + } +} diff --git a/test/processors/inline_tags_test.dart b/test/processors/inline_tags_test.dart new file mode 100644 index 00000000..9aa9d9bb --- /dev/null +++ b/test/processors/inline_tags_test.dart @@ -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 +}