Skip to content

Commit 1c919d2

Browse files
committed
Add some metadata to JSON
To know when it was generated and by what generator. Also convert dates on specs to ISO8601.
1 parent 83245a6 commit 1c919d2

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

Sources/webtags/Command.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct Command: ParsableCommand {
1818
commandName: "webtags",
1919
abstract: "Element definitions from web specifications.",
2020
discussion: "Generates JSON output describing all elements, attributes and predefined attribute values from the given web browser specifications.",
21-
version: "0.1"
21+
version: "0.1-unstable"
2222
)
2323

2424
@Option(name: .customLong("specs"), help: "Short names of the specs to parse and output.")
@@ -38,6 +38,8 @@ struct Command: ParsableCommand {
3838
LoggingSystem.bootstrap(StreamLogHandler.standardError)
3939
let logger = Logger(label: "WebTags")
4040
.withLogLevel(logLevel)
41+
42+
let runStartedDate = Date()
4143
logger.notice("Started run.", metadata: ["logLevel": "\(logLevel)", "webRefIndex": "\(webRefIndex)", "specsToGenerate": "\(specsToGenerate.joined(separator: ","))"], source: "\(Self.self)")
4244

4345
// Parse URL
@@ -62,17 +64,20 @@ struct Command: ParsableCommand {
6264
.filter { specsToGenerate.contains($0.shortname) }
6365
.map { try WebRefCrawlResultTransformer(crawlResult: $0, baseURL: indexURL, parentLogger: logger) }
6466
.map { try $0.transform() }
65-
logger.info("Specs parsed.", metadata: ["specsToGenerate": "\(specsToGenerate.joined(separator: ","))"], source: "\(Self.self)")
67+
logger.info("Specs parsed.", metadata: ["specsToGenerate": "\(specsToGenerate.joined(separator: ","))"], source: "\(Self.self)")
68+
69+
let webTags = WebTags(generatedAt: runStartedDate, generatedSpecs: specsToGenerate, specs: parsedSpecs)
6670

6771
// Encode output as JSON
6872
logger.debug("Generating JSON...", source: "\(Self.self)")
6973
let encoder = JSONEncoder()
74+
encoder.dateEncodingStrategy = .iso8601
7075
if prettyPrint {
7176
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
7277
} else {
7378
encoder.outputFormatting = [.withoutEscapingSlashes]
7479
}
75-
let jsonData = try encoder.encode(parsedSpecs)
80+
let jsonData = try encoder.encode(webTags)
7681
guard let jsonString = String(data: jsonData, encoding: .utf8) else {
7782
throw EncodingFailedError(message: "Failed to convert JSON data to string.")
7883
}

Sources/webtags/Output.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ private let globalElementIdentifiers: Set<String> = [
1212
"core-attributes", // SVG2
1313
]
1414

15+
struct WebTags: Encodable {
16+
let generatedAt: Date
17+
let generatedSpecs: [String]
18+
let generator = Command.configuration.commandName
19+
let generatorVersion = Command.configuration.version
20+
let generatorUrl = "https://github.com/nonstrict-hq/WebTags"
21+
let specs: [Spec]
22+
}
23+
1524
struct Spec: Encodable {
1625
let organization: String
1726
let title: String
1827
let shortname: String
1928
let url: URL
20-
let date: String
29+
let date: Date
2130

2231
let globalAttributes: [Attribute]
2332
let elements: [Element]
@@ -35,12 +44,24 @@ struct Spec: Encodable {
3544
case elements
3645
}
3746

47+
private static let dateFormatter = {
48+
let formatter = DateFormatter()
49+
formatter.locale = Locale(identifier: "en_US_POSIX")
50+
formatter.timeZone = TimeZone(secondsFromGMT: 0)
51+
formatter.dateFormat = "d MMMM yyyy"
52+
return formatter
53+
}()
54+
3855
init(crawlResult: WebRefCrawl.Result, crawledElements: [WebRefCrawl.Element], crawledDfns: [WebRefCrawl.Dfn]) throws {
3956
organization = crawlResult.organization
4057
title = crawlResult.title
4158
shortname = crawlResult.shortname
4259
url = crawlResult.url
43-
date = crawlResult.date
60+
61+
guard let date = Spec.dateFormatter.date(from: crawlResult.date) else {
62+
throw InvalidCrawlResultError(message: "Invalid date: \(crawlResult.date)", result: crawlResult)
63+
}
64+
self.date = date
4465

4566
let attributes = try crawledDfns.compactMap(Attribute.init)
4667
let attributeValues = try crawledDfns.compactMap(AttributeValue.init)

0 commit comments

Comments
 (0)