From 01f1b8481feaa6a8f72e838b6cdb402658caa50b Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 11 Aug 2020 13:58:52 +0200 Subject: [PATCH] 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 --- lib/core/processors/inline_tags.dart | 28 +++++++++++++++++++++++++++ test/processors/inline_tags_test.dart | 28 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 lib/core/processors/inline_tags.dart create mode 100644 test/processors/inline_tags_test.dart 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 +}