Skip to content

Commit

Permalink
Merge pull request #4 from ddanilyuk/fix/groupParser
Browse files Browse the repository at this point in the history
Fix/group parser
  • Loading branch information
ddanilyuk authored Jun 14, 2022
2 parents 42c84cb + a28cffd commit e275b6e
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 109 deletions.
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/swift-parsing", revision: "0.9.2"),
.package(url: "https://github.com/pointfreeco/swift-url-routing", from: "0.1.0"),
.package(url: "https://github.com/pointfreeco/vapor-routing", from: "0.1.0"),
// .package(url: "https://github.com/google/swift-benchmark", from: "0.1.1"),
],
targets: [
.target(
Expand Down
6 changes: 3 additions & 3 deletions Sources/App/Controllers/LessonsController.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// LessonsController.swift
//
//
// Created by Denys Danyliuk on 27.05.2022.
Expand All @@ -10,9 +10,9 @@ import KPIHubParser

final class LessonsController {

func getGroups(for uuid: UUID, request: Request) async throws -> LessonsResponse {
func getLessons(for groupUUID: UUID, request: Request) async throws -> LessonsResponse {
let response = try await request.client.get(
"http://rozklad.kpi.ua/Schedules/ViewSchedule.aspx?g=\(uuid.uuidString)"
"http://rozklad.kpi.ua/Schedules/ViewSchedule.aspx?g=\(groupUUID.uuidString)"
)
let html = try (response.body).htmlString(encoding: .utf8)
let lessons = try LessonsParser().parse(html)
Expand Down
3 changes: 1 addition & 2 deletions Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func apiHandler(
case let .campus(route):
return try await campusHandler(request: request, route: route)


case let .groups(route):
return try await groupsHandler(request: request, route: route)

Expand Down Expand Up @@ -84,6 +83,6 @@ func groupHandler(
switch route {
case .lessons:
let controller = LessonsController()
return try await controller.getGroups(for: uuid, request: request)
return try await controller.getLessons(for: uuid, request: request)
}
}
12 changes: 10 additions & 2 deletions Sources/KPIHubParser/GroupParser.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// GroupParser.swift
//
//
// Created by Denys Danyliuk on 27.05.2022.
Expand All @@ -8,7 +8,11 @@
import Foundation
import Parsing

public struct GroupParser: Parser {
public struct GroupParser: Parser, Equatable {

typealias Input = String

typealias Output = [Group]

public let groupName: String

Expand Down Expand Up @@ -47,5 +51,9 @@ public struct GroupParser: Parser {
}
return try parser.parse(input)
}

static func == (lhs: GroupParser, rhs: GroupParser) -> Bool {
return lhs.groupName == rhs.groupName
}

}
80 changes: 66 additions & 14 deletions Sources/KPIHubParser/LessonsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public struct LessonsParser: Parser {
// MARK: - Lesson name

let nameParser = Parse {
OpenTagV2("a")
OpenTag("a")
upToNextTag.map { String(Substring($0)) }
CloseTagV2("a")
CloseTag("a")
}
let multipleNamesParser = Parse {
OpenTagV2("span")
OpenTag("span")
Many {
nameParser
} separator: {
Expand All @@ -48,7 +48,7 @@ public struct LessonsParser: Parser {
} terminator: {
Skip { PrefixThrough("</span>".utf8) }
}
OneLineTagV2("br")
OneLineTag("br")
}

// MARK: - Teacher
Expand All @@ -61,11 +61,11 @@ public struct LessonsParser: Parser {
upToNextTag.map { String(Substring($0)) }
}
let teacherParser = Parse(Teacher.init) {
OpenTagV2("a") {
OpenTag("a") {
teacherFullNameParser
}
teacherShortNameParser
CloseTagV2("a")
CloseTag("a")
}
let multipleTeachersParser = Parse {
Optionally {
Expand All @@ -77,15 +77,15 @@ public struct LessonsParser: Parser {
Peek { "<a".utf8 }
}
}
OneLineTagV2("br")
OneLineTag("br")
}

// MARK: - Location

let locationWithLinkParser = Parse {
OpenTagV2("a")
OpenTag("a")
upToNextTag.map { String(Substring($0)) }
CloseTagV2("a")
CloseTag("a")
}
let locationPlainTextParser = Parse {
Whitespace()
Expand All @@ -109,36 +109,36 @@ public struct LessonsParser: Parser {
// MARK: - Lesson

let lessonCellParser = Parse {
OpenTagV2("td")
OpenTag("td")
Optionally {
Parse(RawLesson.init) {
multipleNamesParser
multipleTeachersParser
multipleLocationParser
}
}
CloseTagV2("td")
CloseTag("td")
}

// MARK: - Row

/// Cell with pair number and time
let skipFirstCell = Parse {
OpenTagV2("td")
OpenTag("td")
Skip { PrefixThrough("</td>".utf8) }
}

let rowParser = Parse {
Parse {
Whitespace()
OpenTagV2("tr")
OpenTag("tr")
Whitespace()
}
skipFirstCell
Many(6, element: { lessonCellParser })
Parse {
Whitespace()
CloseTagV2("tr")
CloseTag("tr")
Whitespace()
}
}
Expand Down Expand Up @@ -215,3 +215,55 @@ public struct LessonsParser: Parser {
}

}


public func parse(_ input: inout String) throws -> [Lesson] {

let upToNextTag = PrefixUpTo("<".utf8)
let quotedField = Parse { ... }

// MARK: - Lesson name

let nameParser = Parse { ... }
let multipleNamesParser = Parse { ... }

// MARK: - Teacher

let teacherFullNameParser = Parse { ... }
let teacherShortNameParser = Parse { ... }
let teacherParser = Parse(Teacher.init) { ... }
let multipleTeachersParser = Parse { ... }

// MARK: - Location

let locationWithLinkParser = Parse { ... }
let locationPlainTextParser = Parse { ... }
let multipleLocationParser = Parse { ... }

// MARK: - Lesson

let lessonCellParser = Parse { ... }

// MARK: - Row

/// Cell with pair number and time
let skipFirstCell = Parse { ... }

let rowParser = Parse { ... }

// MARK: - Table

let skipTableHeader = Parse { ... }

let firstTable = Parse { ... }
.replaceError( ... )

let secondTable = Parse { ... }
.replaceError( ... )

let allTables = Parse { ... } with: { ... }

let fullParser = allTables.map { ... }

return try fullParser.parse(input)
}
8 changes: 4 additions & 4 deletions Sources/KPIHubParser/StudySheetActivitiesParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public struct StudySheetActivitiesParser: Parser {

let fieldParser = Parse {
Whitespace()
OpenTagV2("td")
OpenTag("td")
upToNextTag
CloseTagV2("td")
CloseTag("td")
Whitespace()
}
let allFieldsParser = Parse { date, mark, type, teacher, note in
Expand All @@ -49,13 +49,13 @@ public struct StudySheetActivitiesParser: Parser {

let rowsParser = Many {
Whitespace()
OpenTagV2("tr")
OpenTag("tr")
Parse {
Whitespace()
allFieldsParser
Whitespace()
}
CloseTagV2("tr")
CloseTag("tr")
Whitespace()
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/KPIHubParser/StudySheetLessonsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,26 @@ public struct StudySheetLessonsParser: Parser {
let idLinkNameParser = Parse { idLink, name in
IdLinkName(idLink: idLink, name: name)
} with: {
OpenTagV2("td")
OpenTagV2("a") {
OpenTag("td")
OpenTag("a") {
Skip { PrefixThrough("=".utf8) }
idLinkParser
}
upToNextTag
CloseTagV2("a")
CloseTagV2("td")
CloseTag("a")
CloseTag("td")
}

let teacherParser = Parse {
OpenTagV2("td")
OpenTag("td")
Many {
Prefix { $0 != .init(ascii: "<") && $0 != .init(ascii: ",") }
.map { String(Substring($0)) }
} separator: {
",".utf8
Whitespace()
}
CloseTagV2("td")
CloseTag("td")
}

let oneRowParser = Parse { trHeader, idLinkName, teachers in
Expand All @@ -103,7 +103,7 @@ public struct StudySheetLessonsParser: Parser {
} with: {
Whitespace()
Parse {
OpenTagV2("tr") {
OpenTag("tr") {
trHeaderParser
}
Whitespace()
Expand All @@ -116,7 +116,7 @@ public struct StudySheetLessonsParser: Parser {
teacherParser
Whitespace()
}
CloseTagV2("tr")
CloseTag("tr")
}


Expand Down
20 changes: 1 addition & 19 deletions Sources/KPIHubParser/TagParsers/CloseTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,6 @@ import Foundation

struct CloseTag: Parser {

typealias Input = Substring
typealias Output = Void

let tag: String

init(_ tag: String) {
self.tag = tag
}

func parse(_ input: inout Substring) throws -> Void {
try Parse {
"</\(tag)>"
}
.parse(&input)
}
}

struct CloseTagV2: Parser {

typealias Input = Substring.UTF8View
typealias Output = Void

Expand All @@ -44,4 +25,5 @@ struct CloseTagV2: Parser {
}
.parse(&input)
}

}
19 changes: 0 additions & 19 deletions Sources/KPIHubParser/TagParsers/OneLineTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,6 @@ struct OneLineTag: Parser {
self.tag = tag
}

typealias Input = Substring
typealias Output = Void

func parse(_ input: inout Substring) throws -> Void {
try Parse {
"<\(tag)/>"
}
.parse(&input)
}
}

struct OneLineTagV2: Parser {

let tag: String

init(_ tag: String) {
self.tag = tag
}

typealias Input = Substring.UTF8View
typealias Output = Void

Expand Down
Loading

0 comments on commit e275b6e

Please sign in to comment.