Skip to content

Commit f6ce3a2

Browse files
author
Владислав Прусаков
committed
first commit
0 parents  commit f6ce3a2

File tree

10 files changed

+429
-0
lines changed

10 files changed

+429
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
/.swiftpm

Package.resolved

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// swift-tools-version:5.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "CodeSyntaxCSSGenerator",
8+
products: [
9+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
10+
.library(
11+
name: "CodeSyntaxCSSGenerator",
12+
targets: ["CodeSyntaxCSSGenerator"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
.package(url: "https://github.com/johnsundell/publish.git", from: "0.4.0"),
17+
.package(url: "https://github.com/SpectralDragon/PublishColorUtils.git", from: "0.1.0"),
18+
.package(url: "https://github.com/JohnSundell/SplashPublishPlugin.git", from: "0.1.0")
19+
],
20+
targets: [
21+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
22+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
23+
.target(
24+
name: "CodeSyntaxCSSGenerator",
25+
dependencies: ["Publish", "PublishColorUtils", "SplashPublishPlugin"]),
26+
.testTarget(
27+
name: "CodeSyntaxCSSGeneratorTests",
28+
dependencies: ["CodeSyntaxCSSGenerator"]),
29+
]
30+
)

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# CodeSyntaxCSSGenerator
2+
3+
A [Publish](https://github.com/johnsundell/publish) plugin that generates a syntax code css styles for you [Splash Plugin](https://github.com/johnsundell/splashpublishplugin) from Xcode Color Theme.
4+
5+
## Install
6+
7+
To install it into your [Publish](https://github.com/johnsundell/publish) package, add it as a dependency within your `Package.swift` manifest:
8+
```swift
9+
let package = Package(
10+
...
11+
dependencies: [
12+
...
13+
.package(url: "https://github.com/SpectralDragon/CodeSyntaxCSSGenerator.git", from: "0.1.0")
14+
],
15+
targets: [
16+
.target(
17+
...
18+
dependencies: [
19+
...
20+
"CodeSyntaxCSSGenerator"
21+
]
22+
)
23+
]
24+
...
25+
)
26+
```
27+
28+
## Usage
29+
30+
Move `dvtcolortheme` or `xccolortheme` file to Resource dirictory and intall the plugin in any place in build step.
31+
32+
```swift
33+
34+
// My xcode theme `Light.dvtcolortheme`
35+
36+
import CodeSyntaxCSSGenerator
37+
38+
Site().publish(using: [
39+
.installPlugin(
40+
.generateCodeCSS(withClassPrefix: "pre code .s-",
41+
theme: .xcode("Light.dvtcolortheme"))
42+
),
43+
])
44+
```
45+
46+
after that, the plugin will generate `codesyntax.css` file
47+
48+
```css
49+
pre code .s-preprocessing {
50+
color: #63381FFF
51+
}
52+
53+
pre code .s-comment {
54+
color: #007400FF
55+
}
56+
57+
pre code .s-number {
58+
color: #1C00CEFF
59+
}
60+
61+
pre code .s-call {
62+
color: #25464AFF
63+
}
64+
65+
pre code .s-property {
66+
color: #3E6D74FF
67+
}
68+
69+
pre code .s-keyword {
70+
color: #A90D91FF
71+
}
72+
73+
pre code .s-dotAccess {
74+
color: #25464AFF
75+
}
76+
77+
pre code .s-type {
78+
color: #3E6D74FF
79+
}
80+
81+
pre code .s-string {
82+
color: #C41A15FF
83+
}
84+
```
85+
86+
Also plugin can generate css to dark scheme, just use `CodeTheme.dynamic(light: CodeTheme, dark: CodeTheme)` case. The plugin add `@media(prefers-color-scheme: dark)` with passed code theme.
87+
88+
89+
# Features
90+
91+
[ ] intellij theme
92+
[ ] VSCode
93+
94+
Thank you and enjoy 💯
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import Foundation
2+
import Publish
3+
import Splash
4+
import Files
5+
import PublishColorUtils
6+
7+
public extension Plugin {
8+
9+
static func generateCodeCSS(
10+
withClassPrefix prefix: String = "",
11+
resourcePath: Path = "Resources",
12+
codeFileName: String = "codesyntax.css",
13+
theme: CodeTheme
14+
) -> Self {
15+
Plugin(name: "CodeSyntaxCSSGenerator", installer: { context in
16+
17+
if let folder = try? context.folder(at: resourcePath) {
18+
19+
let colors = try theme.colors(resourceFolder: folder)
20+
21+
var content = """
22+
/* THIS FILE WAS AUTO GENERATED. DO NOT CHANGE IT MANUAL */
23+
/* Generated by `CodeSyntaxCSSGeneratorPlugin` https://github.com/SpectralDragon/CodeSyntaxCSSGeneratorPlugin */\n\n
24+
"""
25+
26+
switch colors {
27+
case .single(let value):
28+
let style = Self.cssStyle(prefix, colors: value)
29+
30+
content.append(style)
31+
case .dynamic(let light, let dark):
32+
let style = Self.cssStyle(prefix, colors: light)
33+
34+
content.append(style)
35+
36+
let darkStyle = Self.cssStyle(prefix, colors: dark)
37+
38+
content.append("\n\n@media(prefers-color-scheme: dark) {\n")
39+
40+
content.append("\t\(darkStyle.replacingOccurrences(of: "\n", with: "\n\t"))\n\n")
41+
42+
content.append("}")
43+
}
44+
45+
if folder.containsFile(named: codeFileName) {
46+
try folder.file(at: codeFileName).delete()
47+
}
48+
49+
let codeFile = try folder.createFile(at: codeFileName)
50+
51+
try codeFile.write(content)
52+
}
53+
})
54+
}
55+
56+
/// Generate css style using colors
57+
private static func cssStyle(_ prefix: String, colors: [TokenType: PublishColorUtils.Color]) -> String {
58+
59+
var style = "\n"
60+
61+
for (token, color) in colors {
62+
style.append(
63+
"""
64+
\(prefix)\(token.string) {
65+
\tcolor: \(color.hexWithAlpha)
66+
}\n\n
67+
"""
68+
)
69+
}
70+
71+
return style
72+
}
73+
}
74+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Foundation
2+
import PublishColorUtils
3+
import Splash
4+
5+
extension CodeTheme {
6+
static func colorsFromXcodeScheme(_ scheme: [String: Any]) -> [TokenType: PublishColorUtils.Color] {
7+
let syntaxColors = scheme["DVTSourceTextSyntaxColors"] as! [String: String]
8+
9+
var colors = [TokenType: PublishColorUtils.Color]()
10+
11+
for token in TokenType.allCases {
12+
let strings = syntaxColors[token.xcodeSchemeKey]?.split(separator: " ").map { String($0) } ?? []
13+
guard strings.count == 4 else { continue }
14+
let red = Float(strings[0])!
15+
let green = Float(strings[1])!
16+
let blue = Float(strings[2])!
17+
let alpha = Float(strings[3])!
18+
let color = Color(red: red, green: green, blue: blue, alpha: alpha)
19+
colors[token] = color
20+
}
21+
22+
return colors
23+
}
24+
}
25+
26+
fileprivate extension TokenType {
27+
var xcodeSchemeKey: String {
28+
switch self {
29+
case .call, .dotAccess: return "xcode.syntax.identifier.function"
30+
case .comment: return "xcode.syntax.comment"
31+
case .keyword: return "xcode.syntax.keyword"
32+
case .number: return "xcode.syntax.number"
33+
case .preprocessing: return "xcode.syntax.preprocessor"
34+
case .property: return "xcode.syntax.identifier.variable"
35+
case .string: return "xcode.syntax.string"
36+
case .type: return "xcode.syntax.identifier.type"
37+
case .custom(_): return "xcode.syntax.plain"
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)