Syntax highlight swift code

This commit is contained in:
Christoph Hagen 2022-08-18 08:49:01 +02:00
parent a444c51697
commit 8a264b141b
3 changed files with 31 additions and 2 deletions

View File

@ -54,6 +54,7 @@
E2F8FA2428ACD0A800632026 /* PageImageTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F8FA2328ACD0A800632026 /* PageImageTemplate.swift */; };
E2F8FA2628ACD64500632026 /* PageVideoTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F8FA2528ACD64500632026 /* PageVideoTemplate.swift */; };
E2F8FA2828ACD84400632026 /* VideoType.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F8FA2728ACD84400632026 /* VideoType.swift */; };
E2F8FA2B28AD0BD200632026 /* Splash in Frameworks */ = {isa = PBXBuildFile; productRef = E2F8FA2A28AD0BD200632026 /* Splash */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@ -124,6 +125,7 @@
buildActionMask = 2147483647;
files = (
E22E878C289E4A8900E51191 /* Ink in Frameworks */,
E2F8FA2B28AD0BD200632026 /* Splash in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -273,6 +275,7 @@
name = WebsiteGenerator;
packageProductDependencies = (
E22E878B289E4A8900E51191 /* Ink */,
E2F8FA2A28AD0BD200632026 /* Splash */,
);
productName = WebsiteGenerator;
productReference = E22E875F289D84C300E51191 /* WebsiteGenerator */;
@ -304,6 +307,7 @@
mainGroup = E22E8756289D84C300E51191;
packageReferences = (
E22E878A289E4A8900E51191 /* XCRemoteSwiftPackageReference "ink" */,
E2F8FA2928AD0BD200632026 /* XCRemoteSwiftPackageReference "Splash" */,
);
productRefGroup = E22E8760289D84C300E51191 /* Products */;
projectDirPath = "";
@ -538,6 +542,14 @@
minimumVersion = 0.5.1;
};
};
E2F8FA2928AD0BD200632026 /* XCRemoteSwiftPackageReference "Splash" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/JohnSundell/Splash";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.16.0;
};
};
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
@ -546,6 +558,11 @@
package = E22E878A289E4A8900E51191 /* XCRemoteSwiftPackageReference "ink" */;
productName = Ink;
};
E2F8FA2A28AD0BD200632026 /* Splash */ = {
isa = XCSwiftPackageProductDependency;
package = E2F8FA2928AD0BD200632026 /* XCRemoteSwiftPackageReference "Splash" */;
productName = Splash;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = E22E8757289D84C300E51191 /* Project object */;

View File

@ -8,6 +8,15 @@
"revision" : "77c3d8953374a9cf5418ef0bd7108524999de85a",
"version" : "0.5.1"
}
},
{
"identity" : "splash",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/Splash",
"state" : {
"revision" : "7f4df436eb78fe64fe2c32c58006e9949fa28ad8",
"version" : "0.16.0"
}
}
],
"version" : 2

View File

@ -1,5 +1,6 @@
import Foundation
import Ink
import Splash
struct PageContentGenerator {
@ -7,6 +8,8 @@ struct PageContentGenerator {
private let files: FileProcessor
private let swift = SyntaxHighlighter(format: HTMLOutputFormat())
init(factory: TemplateFactory, files: FileProcessor) {
self.factory = factory
self.files = files
@ -31,8 +34,8 @@ struct PageContentGenerator {
}
let codeModifier = Modifier(target: .codeBlocks) { html, markdown in
if markdown.starts(with: "```swift") {
#warning("Syntax highlight swift code")
return html
let code = markdown.between("```swift", and: "```").trimmed
return "<pre><code>" + swift.highlight(code) + "</pre></code>"
}
hasCodeContent = true
return html