Files
swiftpm-mustache/Sources/HummingbirdMustache/Library+FileSystem.swift
Adam Fowler 071b182a00 2.x.x Template struct (#22)
* Start of turning template into a struct

* Everything is Sendable now, just doesnt work

* Add library to context

* Make sure render is initialized with library

* comment about inheritance spec

* Add register back in

* Re-instate register functions

* Re-instate commented out print

* Fix tabbing in Partial tests

* Make HBMustacheLibrary.loadTemplates async

* Update platforms, swift version
2024-01-31 15:43:46 +00:00

45 lines
1.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
extension HBMustacheLibrary {
/// Load templates from a folder
static func loadTemplates(from directory: String, withExtension extension: String = "mustache") async throws -> [String: HBMustacheTemplate] {
var directory = directory
if !directory.hasSuffix("/") {
directory += "/"
}
let extWithDot = ".\(`extension`)"
let fs = FileManager()
guard let enumerator = fs.enumerator(atPath: directory) else { return [:] }
var templates: [String: HBMustacheTemplate] = [:]
for case let path as String in enumerator {
guard path.hasSuffix(extWithDot) else { continue }
guard let data = fs.contents(atPath: directory + path) else { continue }
let string = String(decoding: data, as: Unicode.UTF8.self)
var template: HBMustacheTemplate
do {
template = try HBMustacheTemplate(string: string)
} catch let error as HBMustacheTemplate.ParserError {
throw ParserError(filename: path, context: error.context, error: error.error)
}
// drop ".mustache" from path to get name
let name = String(path.dropLast(extWithDot.count))
templates[name] = template
}
return templates
}
}