Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communication: Match mentions #54

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions Sources/ArtemisMarkdown/RegexReplacementVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,33 @@ struct RegexReplacementVisitor<Output> {
}

enum RegexReplacementVisitors {
static let channels = RegexReplacementVisitor(regex: #/\[channel\](?<name>.*?)\((?<id>.*?)\)\[/channel\]/#) { match in

// (?<ATTACHMENT>\[attachment].*?\[\/attachment])
static let attachments = RegexReplacementVisitor(
regex: #/\[attachment\](?<name>.*?)\((?<path>lecture/\d+/.*?)\)\[/attachment\]/#
) { match in
"[Attachment \(match.name)](mention://attachment/\(match.path))"
}

// (?<ATTACHMENT_UNITS>\[lecture-unit].*?\[\/lecture-unit])
static let attachmentUnits = RegexReplacementVisitor(
regex: #/\[lecture-unit\](?<name>.*?)\((?<path>attachment-unit/\d+/.*?)\)\[/lecture-unit\]/#
) { match in
"[Lecture unit \(match.name)](mention://lecture-unit/\(match.path))"
}

// (?<CHANNEL>\[channel].*?\[\/channel])
static let channels = RegexReplacementVisitor(
regex: #/\[channel\](?<name>.*?)\((?<id>.*?)\)\[/channel\]/#
) { match in
"[#\(match.name)](mention://channel/\(match.id))"
}

// (?<PROGRAMMING>\[programming].*?\[\/programming])|
// (?<MODELING>\[modeling].*?\[\/modeling])|
// (?<QUIZ>\[quiz].*?\[\/quiz])|
// (?<TEXT>\[text].*?\[\/text])|
// (?<FILE_UPLOAD>\[file-upload].*?\[\/file-upload])
static let exercises = RegexReplacementVisitor(
regex: #/\[(?<start>[\w-]*?)\](?<name>.*?)\((?<path>/courses/\d+/exercises/\d+)\)\[/(?<stop>[\w-]*?)\]/#
) { match in
Expand All @@ -38,6 +61,7 @@ enum RegexReplacementVisitors {
String(match.ins)
}

// (?<LECTURE>\[lecture].*?\[\/lecture])
static let lectures = RegexReplacementVisitor(
regex: #/\[lecture\](?<name>.*?)\((?<path>/courses/\d+/lectures/\d+)\)\[/lecture\]/#
) { match in
Expand All @@ -48,17 +72,34 @@ enum RegexReplacementVisitors {
return "![Lecture](fa-chalkboard-user) [\(match.name)](mention://lecture/\(url.lastPathComponent))"
}

// (?<USER>\[user].*?\[\/user])
static let members = RegexReplacementVisitor(regex: #/\[user\](?<name>.*?)\((?<login>.*?)\)\[/user\]/#) { match in
"[@\(match.name)](mention://member/\(match.login))"
}

// (?<POST>#\d+)
static let messages = RegexReplacementVisitor(regex: #/\#(?<id>\d+)/#) { match in
return "[Message #\(match.id)](mention://message/\(match.id))"
}

// (?<SLIDE>\[slide].*?\[\/slide])
static let slides = RegexReplacementVisitor(
regex: #/\[slide\](?<name>.*?)\((?<path>attachment-unit/\d+/slide/\d+)\)\[/slide\]/#
) { match in
"[Slide \(match.name)](mention://slide/\(match.path))"
}

static func visitAll(input: inout String) {
for visit in [
attachments.visit(input:),
attachmentUnits.visit(input:),
channels.visit(input:),
exercises.visit(input:),
ins.visit(input:),
lectures.visit(input:),
members.visit(input:)
members.visit(input:),
messages.visit(input:),
slides.visit(input:)
] {
visit(&input)
}
Expand Down
29 changes: 28 additions & 1 deletion Tests/ArtemisMarkdownTests/ArtemisMarkdownTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@ import XCTest
@testable import ArtemisMarkdown

final class ArtemisMarkdownTests: XCTestCase {
func testExerciseRegex() throws {
func testAttachmentsRegex() throws {
let inputString = "[attachment]uml-blue(lecture/3/LectureAttachment_2024-05-24T21-05-08-351_d37182b7.png)[/attachment]"
let match = try XCTUnwrap(RegexReplacementVisitors.attachments.regex.wholeMatch(in: inputString))
XCTAssertEqual(match.name, "uml-blue")
XCTAssertEqual(match.path, "lecture/3/LectureAttachment_2024-05-24T21-05-08-351_d37182b7.png")
}

func testAttachmentUnitsRegex() throws {
let inputString = "[lecture-unit]Inheritance (part 1)(attachment-unit/7/AttachmentUnit_2024-05-24T21-12-25-915_Inheritance__part_1_.pdf)[/lecture-unit]"
let match = try XCTUnwrap(RegexReplacementVisitors.attachmentUnits.regex.wholeMatch(in: inputString))
XCTAssertEqual(match.name, "Inheritance (part 1)")
XCTAssertEqual(match.path, "attachment-unit/7/AttachmentUnit_2024-05-24T21-12-25-915_Inheritance__part_1_.pdf")
}

func testExercisesRegex() throws {
let inputString = "[file-upload]An Exercise(/courses/1/exercises/2)[/file-upload]"
let match = try XCTUnwrap(RegexReplacementVisitors.exercises.regex.wholeMatch(in: inputString))
XCTAssertEqual(match.name, "An Exercise")
Expand All @@ -28,4 +42,17 @@ final class ArtemisMarkdownTests: XCTestCase {
XCTAssertEqual(match.name, "Full Name")
XCTAssertEqual(match.login, "login")
}

func testMessagesRegex() throws {
let inputString = "#13"
let match = try XCTUnwrap(RegexReplacementVisitors.messages.regex.wholeMatch(in: inputString))
XCTAssertEqual(match.id, "13")
}

func testSlidesRegex() throws {
let inputString = "[slide]Polymorphism Slide 1(attachment-unit/10/slide/1)[/slide]"
let match = try XCTUnwrap(RegexReplacementVisitors.slides.regex.wholeMatch(in: inputString))
XCTAssertEqual(match.name, "Polymorphism Slide 1")
XCTAssertEqual(match.path, "attachment-unit/10/slide/1")
}
}