From 625cc0ee07f455bb6c6a92b1cc88fd6e89f10304 Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Sat, 22 May 2021 21:24:57 +0100 Subject: [PATCH 1/3] Add new parameter for date formats, and default to en_US --- Sources/swift-doc/Subcommands/Generate.swift | 24 ++++++++++++------- .../swift-doc/Supporting Types/Layout.swift | 2 +- Sources/swift-doc/Supporting Types/Page.swift | 1 + .../Pages/ExternalTypePage.swift | 6 +++-- .../Supporting Types/Pages/FooterPage.swift | 7 +++++- .../Supporting Types/Pages/GlobalPage.swift | 5 +++- .../Supporting Types/Pages/HomePage.swift | 5 +++- .../Supporting Types/Pages/OperatorPage.swift | 5 +++- .../Supporting Types/Pages/SidebarPage.swift | 5 +++- .../Supporting Types/Pages/TypePage.swift | 5 +++- .../Pages/TypealiasPage.swift | 7 ++++-- 11 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Sources/swift-doc/Subcommands/Generate.swift b/Sources/swift-doc/Subcommands/Generate.swift index a209218c..f846da32 100644 --- a/Sources/swift-doc/Subcommands/Generate.swift +++ b/Sources/swift-doc/Subcommands/Generate.swift @@ -39,6 +39,10 @@ extension SwiftDoc { @Option(name: .long, help: "The minimum access level of the symbols included in generated documentation.") var minimumAccessLevel: AccessLevel = .public + + @Option(name: .long, + help: "The locale used to format the printed dates") + var datesLocale: String = "en_US" } static var configuration = CommandConfiguration(abstract: "Generates Swift documentation") @@ -47,6 +51,8 @@ extension SwiftDoc { var options: Options func run() throws { + let datesLocale = Locale(identifier: self.options.datesLocale) + for directory in options.inputs { var isDirectory: ObjCBool = false if !FileManager.default.fileExists(atPath: directory, isDirectory: &isDirectory) { @@ -72,11 +78,11 @@ extension SwiftDoc { for symbol in module.interface.topLevelSymbols.filter(symbolFilter) { switch symbol.api { case is Class, is Enumeration, is Structure, is Protocol: - pages[route(for: symbol)] = TypePage(module: module, symbol: symbol, baseURL: baseURL, includingChildren: symbolFilter) + pages[route(for: symbol)] = TypePage(module: module, symbol: symbol, baseURL: baseURL, datesLocale: datesLocale, includingChildren: symbolFilter) case let `typealias` as Typealias: - pages[route(for: `typealias`.name)] = TypealiasPage(module: module, symbol: symbol, baseURL: baseURL) + pages[route(for: `typealias`.name)] = TypealiasPage(module: module, symbol: symbol, baseURL: baseURL, datesLocale: datesLocale) case is Operator: - let operatorPage = OperatorPage(module: module, symbol: symbol, baseURL: baseURL, includingImplementations: symbolFilter) + let operatorPage = OperatorPage(module: module, symbol: symbol, baseURL: baseURL, datesLocale: datesLocale, includingImplementations: symbolFilter) if !operatorPage.implementations.isEmpty { pages[route(for: symbol)] = operatorPage } @@ -97,11 +103,11 @@ extension SwiftDoc { symbolsByExternalType[extensionDeclaration.extendedType, default: []] += [symbol] } for (typeName, symbols) in symbolsByExternalType { - pages[route(for: typeName)] = ExternalTypePage(module: module, externalType: typeName, symbols: symbols, baseURL: baseURL) + pages[route(for: typeName)] = ExternalTypePage(module: module, externalType: typeName, symbols: symbols, baseURL: baseURL, datesLocale: datesLocale) } for (name, symbols) in globals { - pages[route(for: name)] = GlobalPage(module: module, name: name, symbols: symbols, baseURL: baseURL) + pages[route(for: name)] = GlobalPage(module: module, name: name, symbols: symbols, baseURL: baseURL, datesLocale: datesLocale) } guard !pages.isEmpty else { @@ -126,11 +132,11 @@ extension SwiftDoc { } else { switch format { case .commonmark: - pages["Home"] = HomePage(module: module, externalTypes: Array(symbolsByExternalType.keys), baseURL: baseURL, symbolFilter: symbolFilter) - pages["_Sidebar"] = SidebarPage(module: module, externalTypes: Set(symbolsByExternalType.keys), baseURL: baseURL, symbolFilter: symbolFilter) - pages["_Footer"] = FooterPage(baseURL: baseURL) + pages["Home"] = HomePage(module: module, externalTypes: Array(symbolsByExternalType.keys), baseURL: baseURL, datesLocale: datesLocale, symbolFilter: symbolFilter) + pages["_Sidebar"] = SidebarPage(module: module, externalTypes: Set(symbolsByExternalType.keys), baseURL: baseURL, datesLocale: datesLocale, symbolFilter: symbolFilter) + pages["_Footer"] = FooterPage(baseURL: baseURL, datesLocale: datesLocale) case .html: - pages["Home"] = HomePage(module: module, externalTypes: Array(symbolsByExternalType.keys), baseURL: baseURL, symbolFilter: symbolFilter) + pages["Home"] = HomePage(module: module, externalTypes: Array(symbolsByExternalType.keys), baseURL: baseURL, datesLocale: datesLocale, symbolFilter: symbolFilter) } try pages.map { $0 }.parallelForEach { diff --git a/Sources/swift-doc/Supporting Types/Layout.swift b/Sources/swift-doc/Supporting Types/Layout.swift index 16429c4f..e003b3f5 100644 --- a/Sources/swift-doc/Supporting Types/Layout.swift +++ b/Sources/swift-doc/Supporting Types/Layout.swift @@ -45,7 +45,7 @@ func layout(_ page: Page) -> HTML { diff --git a/Sources/swift-doc/Supporting Types/Page.swift b/Sources/swift-doc/Supporting Types/Page.swift index 7e4886c2..f22be390 100644 --- a/Sources/swift-doc/Supporting Types/Page.swift +++ b/Sources/swift-doc/Supporting Types/Page.swift @@ -12,6 +12,7 @@ protocol Page: HypertextLiteralConvertible { var title: String { get } var document: CommonMark.Document { get } var html: HypertextLiteral.HTML { get } + var datesLocale: Locale { get } } extension Page { diff --git a/Sources/swift-doc/Supporting Types/Pages/ExternalTypePage.swift b/Sources/swift-doc/Supporting Types/Pages/ExternalTypePage.swift index dd706669..480005ff 100644 --- a/Sources/swift-doc/Supporting Types/Pages/ExternalTypePage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/ExternalTypePage.swift @@ -3,9 +3,9 @@ import SwiftDoc import HypertextLiteral import SwiftMarkup import SwiftSemantics +import Foundation struct ExternalTypePage: Page { - let module: Module let externalType: String let baseURL: String @@ -14,11 +14,13 @@ struct ExternalTypePage: Page { let initializers: [Symbol] let properties: [Symbol] let methods: [Symbol] + let datesLocale: Locale - init(module: Module, externalType: String, symbols: [Symbol], baseURL: String) { + init(module: Module, externalType: String, symbols: [Symbol], baseURL: String, datesLocale: Locale) { self.module = module self.externalType = externalType self.baseURL = baseURL + self.datesLocale = datesLocale self.typealiases = symbols.filter { $0.api is Typealias } self.initializers = symbols.filter { $0.api is Initializer } diff --git a/Sources/swift-doc/Supporting Types/Pages/FooterPage.swift b/Sources/swift-doc/Supporting Types/Pages/FooterPage.swift index 88ff0628..358b2487 100644 --- a/Sources/swift-doc/Supporting Types/Pages/FooterPage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/FooterPage.swift @@ -6,6 +6,7 @@ fileprivate let dateFormatter: DateFormatter = { var dateFormatter = DateFormatter() dateFormatter.dateStyle = .long dateFormatter.timeStyle = .none + return dateFormatter }() @@ -19,9 +20,11 @@ fileprivate let href = "https://github.com/SwiftDocOrg/swift-doc" struct FooterPage: Page { let baseURL: String + let datesLocale: Locale - init(baseURL: String) { + init(baseURL: String, datesLocale: Locale) { self.baseURL = baseURL + self.datesLocale = datesLocale } // MARK: - Page @@ -37,6 +40,8 @@ struct FooterPage: Page { } var html: HypertextLiteral.HTML { + dateFormatter.locale = datesLocale + let timestamp = timestampDateFormatter.string(from: Date()) let dateString = dateFormatter.string(from: Date()) diff --git a/Sources/swift-doc/Supporting Types/Pages/GlobalPage.swift b/Sources/swift-doc/Supporting Types/Pages/GlobalPage.swift index cd8dad0f..848af467 100644 --- a/Sources/swift-doc/Supporting Types/Pages/GlobalPage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/GlobalPage.swift @@ -2,18 +2,21 @@ import SwiftSemantics import SwiftDoc import CommonMarkBuilder import HypertextLiteral +import Foundation struct GlobalPage: Page { let module: Module let name: String let symbols: [Symbol] let baseURL: String + let datesLocale: Locale - init(module: Module, name: String, symbols: [Symbol], baseURL: String) { + init(module: Module, name: String, symbols: [Symbol], baseURL: String, datesLocale: Locale) { self.module = module self.name = name self.symbols = symbols self.baseURL = baseURL + self.datesLocale = datesLocale } // MARK: - Page diff --git a/Sources/swift-doc/Supporting Types/Pages/HomePage.swift b/Sources/swift-doc/Supporting Types/Pages/HomePage.swift index 798544d3..de1a7670 100644 --- a/Sources/swift-doc/Supporting Types/Pages/HomePage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/HomePage.swift @@ -2,10 +2,12 @@ import CommonMarkBuilder import SwiftDoc import SwiftSemantics import HypertextLiteral +import struct Foundation.Locale struct HomePage: Page { var module: Module let baseURL: String + let datesLocale: Locale var classes: [Symbol] = [] var enumerations: [Symbol] = [] @@ -18,9 +20,10 @@ struct HomePage: Page { let externalTypes: [String] - init(module: Module, externalTypes: [String], baseURL: String, symbolFilter: (Symbol) -> Bool) { + init(module: Module, externalTypes: [String], baseURL: String, datesLocale: Locale, symbolFilter: (Symbol) -> Bool) { self.module = module self.baseURL = baseURL + self.datesLocale = datesLocale self.externalTypes = externalTypes diff --git a/Sources/swift-doc/Supporting Types/Pages/OperatorPage.swift b/Sources/swift-doc/Supporting Types/Pages/OperatorPage.swift index 740e7829..9c083599 100644 --- a/Sources/swift-doc/Supporting Types/Pages/OperatorPage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/OperatorPage.swift @@ -2,19 +2,22 @@ import SwiftSemantics import SwiftDoc import CommonMarkBuilder import HypertextLiteral +import Foundation struct OperatorPage: Page { let module: Module let symbol: Symbol let implementations: [Symbol] let baseURL: String + let datesLocale: Locale - init(module: Module, symbol: Symbol, baseURL: String, includingImplementations symbolFilter: (Symbol) -> Bool) { + init(module: Module, symbol: Symbol, baseURL: String, datesLocale: Locale, includingImplementations symbolFilter: (Symbol) -> Bool) { precondition(symbol.api is Operator) self.module = module self.symbol = symbol self.implementations = module.interface.functionsByOperator[symbol]?.filter(symbolFilter).sorted() ?? [] self.baseURL = baseURL + self.datesLocale = datesLocale } // MARK: - Page diff --git a/Sources/swift-doc/Supporting Types/Pages/SidebarPage.swift b/Sources/swift-doc/Supporting Types/Pages/SidebarPage.swift index 4a715dc3..97572db6 100644 --- a/Sources/swift-doc/Supporting Types/Pages/SidebarPage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/SidebarPage.swift @@ -2,6 +2,7 @@ import SwiftSemantics import SwiftDoc import CommonMarkBuilder import HypertextLiteral +import struct Foundation.Locale struct SidebarPage: Page { var module: Module @@ -15,10 +16,12 @@ struct SidebarPage: Page { var globalVariableNames: Set = [] let externalTypes: Set + let datesLocale: Locale - init(module: Module, externalTypes: Set, baseURL: String, symbolFilter: (Symbol) -> Bool) { + init(module: Module, externalTypes: Set, baseURL: String, datesLocale: Locale, symbolFilter: (Symbol) -> Bool) { self.module = module self.baseURL = baseURL + self.datesLocale = datesLocale self.externalTypes = externalTypes diff --git a/Sources/swift-doc/Supporting Types/Pages/TypePage.swift b/Sources/swift-doc/Supporting Types/Pages/TypePage.swift index 345acefb..3af078ff 100644 --- a/Sources/swift-doc/Supporting Types/Pages/TypePage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/TypePage.swift @@ -2,18 +2,21 @@ import SwiftSemantics import SwiftDoc import CommonMarkBuilder import HypertextLiteral +import Foundation struct TypePage: Page { let module: Module let symbol: Symbol let baseURL: String + let datesLocale: Locale let symbolFilter: (Symbol) -> Bool - init(module: Module, symbol: Symbol, baseURL: String, includingChildren symbolFilter: @escaping (Symbol) -> Bool) { + init(module: Module, symbol: Symbol, baseURL: String, datesLocale: Locale, includingChildren symbolFilter: @escaping (Symbol) -> Bool) { precondition(symbol.api is Type) self.module = module self.symbol = symbol self.baseURL = baseURL + self.datesLocale = datesLocale self.symbolFilter = symbolFilter } diff --git a/Sources/swift-doc/Supporting Types/Pages/TypealiasPage.swift b/Sources/swift-doc/Supporting Types/Pages/TypealiasPage.swift index 4c806727..fe47ae25 100644 --- a/Sources/swift-doc/Supporting Types/Pages/TypealiasPage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/TypealiasPage.swift @@ -2,17 +2,20 @@ import SwiftSemantics import SwiftDoc import CommonMarkBuilder import HypertextLiteral +import Foundation struct TypealiasPage: Page { let module: Module let symbol: Symbol let baseURL: String - - init(module: Module, symbol: Symbol, baseURL: String) { + let datesLocale: Locale + + init(module: Module, symbol: Symbol, baseURL: String, datesLocale: Locale) { precondition(symbol.api is Typealias) self.module = module self.symbol = symbol self.baseURL = baseURL + self.datesLocale = datesLocale } // MARK: - Page From 45b22bd9db9975737d0ee02e5f1527cefc821901 Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Sun, 23 May 2021 12:12:05 +0100 Subject: [PATCH 2/3] Remove not needed self Co-authored-by: Max Desiatov --- Sources/swift-doc/Subcommands/Generate.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/swift-doc/Subcommands/Generate.swift b/Sources/swift-doc/Subcommands/Generate.swift index f846da32..7c460cbe 100644 --- a/Sources/swift-doc/Subcommands/Generate.swift +++ b/Sources/swift-doc/Subcommands/Generate.swift @@ -51,7 +51,7 @@ extension SwiftDoc { var options: Options func run() throws { - let datesLocale = Locale(identifier: self.options.datesLocale) + let datesLocale = Locale(identifier: options.datesLocale) for directory in options.inputs { var isDirectory: ObjCBool = false From 35f915070d6c24c4ba607c34b7b72039145e4f3e Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Sun, 23 May 2021 12:15:50 +0100 Subject: [PATCH 3/3] Split long lines --- Sources/swift-doc/Subcommands/Generate.swift | 24 +++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Sources/swift-doc/Subcommands/Generate.swift b/Sources/swift-doc/Subcommands/Generate.swift index 7c460cbe..ecce4416 100644 --- a/Sources/swift-doc/Subcommands/Generate.swift +++ b/Sources/swift-doc/Subcommands/Generate.swift @@ -132,11 +132,29 @@ extension SwiftDoc { } else { switch format { case .commonmark: - pages["Home"] = HomePage(module: module, externalTypes: Array(symbolsByExternalType.keys), baseURL: baseURL, datesLocale: datesLocale, symbolFilter: symbolFilter) - pages["_Sidebar"] = SidebarPage(module: module, externalTypes: Set(symbolsByExternalType.keys), baseURL: baseURL, datesLocale: datesLocale, symbolFilter: symbolFilter) + pages["Home"] = HomePage( + module: module, + externalTypes: Array(symbolsByExternalType.keys), + baseURL: baseURL, + datesLocale: datesLocale, + symbolFilter: symbolFilter + ) + pages["_Sidebar"] = SidebarPage( + module: module, + externalTypes: Set(symbolsByExternalType.keys), + baseURL: baseURL, + datesLocale: datesLocale, + symbolFilter: symbolFilter + ) pages["_Footer"] = FooterPage(baseURL: baseURL, datesLocale: datesLocale) case .html: - pages["Home"] = HomePage(module: module, externalTypes: Array(symbolsByExternalType.keys), baseURL: baseURL, datesLocale: datesLocale, symbolFilter: symbolFilter) + pages["Home"] = HomePage( + module: module, + externalTypes: Array(symbolsByExternalType.keys), + baseURL: baseURL, + datesLocale: datesLocale, + symbolFilter: symbolFilter + ) } try pages.map { $0 }.parallelForEach {