mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 04:07:53 +08:00
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:
28
lib/core/processors/inline_tags.dart
Normal file
28
lib/core/processors/inline_tags.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
28
test/processors/inline_tags_test.dart
Normal file
28
test/processors/inline_tags_test.dart
Normal 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
|
||||||
|
}
|
Reference in New Issue
Block a user