forked from sbarex/SourceCodeSyntaxHighlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCSHTheme.swift
More file actions
1096 lines (984 loc) · 38.6 KB
/
Copy pathSCSHTheme.swift
File metadata and controls
1096 lines (984 loc) · 38.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SCSHTheme.swift
// SCSHXPCService
//
// Created by sbarex on 18/10/2019.
// Copyright © 2019 sbarex. All rights reserved.
//
//
// This file is part of SyntaxHighlight.
// SyntaxHighlight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SyntaxHighlight is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SyntaxHighlight. If not, see <http://www.gnu.org/licenses/>.
import Cocoa
extension Notification.Name {
static let themeDidSaved = Notification.Name("themeDidSaved")
static let themeDidDeleted = Notification.Name("themeDidDeleted")
}
typealias NotificationThemeSavedData = (theme: SCSHTheme, oldName: String)
typealias NotificationThemeDeletedData = (String)
public protocol SCSHThemePropertyProtocol: class {
var color: String { get set }
func toCSSStyle() -> String
func output() -> String
func toDictionary() -> [String: Any]
init(color: String)
init?(dict: [String: Any]?)
}
public protocol SCSHThemeDelegate: NSObjectProtocol {
/// Notify the delegate that a property is changed.
func themeDidChangeProperty(_ theme: SCSHTheme, property: SCSHThemePropertyProtocol)
/// Notify the delegate that the categories are changed.
func themeDidChangeCategories(_ theme: SCSHTheme)
/// Notify the delegate that the name is changed.
func themeDidChangeName(_ theme: SCSHTheme)
/// Notify the delegate that the description is changed.
func themeDidChangeDescription(_ theme: SCSHTheme)
/// Notify the delegate that the dirty status is changed.
func themeDidChangeDirtyStatus(_ theme: SCSHTheme)
/// Notify the delegate that a new keyword is added.
func themeDidAddKeyword(_ theme: SCSHTheme, keyword: SCSHTheme.Property)
/// Notify the delegate that a keyword is removed.
func themeDidRemoveKeyword(_ theme: SCSHTheme, keyword: SCSHTheme.Property)
}
extension SCSHThemeDelegate {
func themeDidChangeProperty(_ theme: SCSHTheme, property: SCSHThemePropertyProtocol) {}
func themeDidChangeCategories(_ theme: SCSHTheme) {}
func themeDidChangeName(_ theme: SCSHTheme) {}
func themeDidChangeDescription(_ theme: SCSHTheme) {}
func themeDidChangeDirtyStatus(_ theme: SCSHTheme) {}
func themeDidAddKeyword(_ theme: SCSHTheme, keyword: SCSHTheme.Property) {}
func themeDidRemoveKeyword(_ theme: SCSHTheme, keyword: SCSHTheme.Property) {}
}
public class SCSHTheme: NSObject, Sequence {
public static func == (lhs: SCSHTheme, rhs: SCSHTheme) -> Bool {
return lhs.name == rhs.name && lhs.isStandalone == rhs.isStandalone
}
// MARK: - Theme Property
public class CanvasProperty: SCSHThemePropertyProtocol {
internal weak var theme: SCSHTheme?
public var color: String {
didSet {
if oldValue != color {
self.theme?.onPropertyDidChange(self)
}
}
}
required public init(color: String) {
self.color = color
}
public required init?(dict: [String: Any]?) {
self.color = dict?["color"] as? String ?? ""
}
public func toDictionary() -> [String: Any] {
return [
"color": color,
]
}
/// Get CSS inline style attribute for the property.
public func toCSSStyle() -> String {
return " background-color: \(color); \n"
}
public func output() -> String {
return "{ Colour=\"\(color)\" }"
}
}
public class Property: SCSHThemePropertyProtocol {
public enum Name: String {
case plain = "Default"
case canvas = "Canvas"
case number = "Number"
case escape = "Escape"
case string = "String"
case preProcessor = "PreProcessor"
case stringPreProc = "StringPreProc"
case blockComment = "BlockComment"
case lineComment = "LineComment"
case lineNum = "LineNum"
case operatorProp = "Operator"
case interpolation = "Interpolation"
case keyword1 = "Keyword 1"
case keyword2 = "Keyword 2"
case keyword3 = "Keyword 3"
case keyword4 = "Keyword 4"
case keyword5 = "Keyword 5"
case keyword6 = "Keyword 6"
case keyword7 = "Keyword 7"
case keyword8 = "Keyword 8"
case keyword9 = "Keyword 9"
case keyword10 = "Keyword 10"
case keyword11 = "Keyword 11"
case keyword12 = "Keyword 12"
case keyword13 = "Keyword 13"
case keyword14 = "Keyword 14"
case keyword15 = "Keyword 15"
case keyword16 = "Keyword 16"
case keyword17 = "Keyword 17"
case keyword18 = "Keyword 18"
case keyword19 = "Keyword 19"
case keyword20 = "Keyword 20"
case keyword21 = "Keyword 21"
case keyword22 = "Keyword 22"
case keyword23 = "Keyword 23"
case keyword24 = "Keyword 24"
case keyword25 = "Keyword 25"
case keyword26 = "Keyword 26"
static let first: Name = standardProperties.first!
static let last: Name = .keyword26
static let standardProperties: [Name] = [.canvas, .plain, .number, .string, .operatorProp, .blockComment, .lineComment, .lineNum, .escape, .preProcessor, .stringPreProc, .interpolation]
/// Returns a progressive index (zero based) of a keyword property name.
/// Returns nil il the name is not a keyword.
public static func indexOfKeyword(_ name: Name) -> Int? {
switch name {
case .keyword1:
return 0
case .keyword2:
return 1
case .keyword3:
return 2
case .keyword4:
return 3
case .keyword5:
return 4
case .keyword6:
return 5
case .keyword7:
return 6
case .keyword8:
return 7
case .keyword9:
return 8
case .keyword10:
return 9
case .keyword11:
return 10
case .keyword12:
return 11
case .keyword13:
return 12
case .keyword14:
return 13
case .keyword15:
return 14
case .keyword16:
return 15
case .keyword17:
return 16
case .keyword18:
return 17
case .keyword19:
return 18
case .keyword20:
return 19
case .keyword21:
return 20
case .keyword22:
return 21
case .keyword23:
return 22
case .keyword24:
return 23
case .keyword25:
return 24
case .keyword26:
return 25
default:
return nil
}
}
/// Returns the keyword name at index (zero based) specified.
/// Returns nil if the index is out of bounds.
public static func keywordAtIndex(_ index: Int) -> Name? {
switch index {
case 0:
return .keyword1
case 1:
return .keyword2
case 2:
return .keyword3
case 3:
return .keyword4
case 4:
return .keyword5
case 5:
return .keyword6
case 6:
return .keyword7
case 7:
return .keyword8
case 8:
return .keyword9
case 9:
return .keyword10
case 10:
return .keyword11
case 11:
return .keyword12
case 12:
return .keyword13
case 13:
return .keyword14
case 14:
return .keyword15
case 15:
return .keyword16
case 16:
return .keyword17
case 17:
return .keyword18
case 18:
return .keyword19
case 19:
return .keyword20
case 20:
return .keyword21
case 21:
return .keyword22
case 22:
return .keyword23
case 23:
return .keyword24
case 24:
return .keyword25
case 25:
return .keyword26
default:
return nil
}
}
/// Returns the property name at index (zero based).
public static func nameAtIndex(_ index: Int) -> Name? {
if index < standardProperties.count {
return standardProperties[index]
} else {
if let keyword = Name.keywordAtIndex(index - Name.standardProperties.count) {
return keyword
} else {
return nil
}
}
}
/// Returns the property name based of the associated CSS class.
public static func nameForCSSClass(_ className: String) -> Name? {
switch className {
case "hl":
return .plain // .canvas
case "num":
return .number
case "esc":
return .escape
case "str":
return .string
case "ppc":
return .preProcessor
case "pps":
return .stringPreProc
case "com":
return .blockComment
case "slc":
return .lineComment
case "lin":
return .lineNum
case "opt":
return .operatorProp
case "ipl":
return .interpolation
default:
if className.hasPrefix("kw") {
if let n = className.last?.asciiValue {
return Name.keywordAtIndex(Int(n - 97))
}
}
return nil
}
}
/// Returns the next property name.
public var next: Name? {
if self == Name.standardProperties.last {
return .keyword1
} else if let i = Name.standardProperties.firstIndex(of: self) {
return Name.standardProperties[i+1]
} else if let k = Name.indexOfKeyword(self) {
return Name.keywordAtIndex(k+1)
} else {
return nil
}
}
/// Returns the previous property name.
public var prev: Name? {
if let i = Name.standardProperties.firstIndex(of: self) {
if i == 0 {
return nil
} else {
return Name.standardProperties[i-1]
}
} else if let k = Name.indexOfKeyword(self) {
if k == 0 {
return Name.standardProperties.last
} else {
return Name.keywordAtIndex(k-1)
}
} else {
return nil
}
}
/// Returns a description of the property.
public var description: String {
switch self {
case .plain:
return "Plain text"
case .canvas:
return "Background"
case .number:
return "Numbers"
case .escape:
return "Escape sequences"
case .string:
return "Strings"
case .preProcessor:
return "Preprocessor directives"
case .stringPreProc:
return "Strings within directives"
case .blockComment:
return "Block comments"
case .lineComment:
return "Line comments"
case .lineNum:
return "Line numbers"
case .operatorProp:
return "Operators"
case .interpolation:
return "Interpolation sequences"
case .keyword1, .keyword2, .keyword3, .keyword4, .keyword5, .keyword6, .keyword7, .keyword8, .keyword9, .keyword10, .keyword11, .keyword12, .keyword13, .keyword14, .keyword15, .keyword16, .keyword17, .keyword18, .keyword19, .keyword20, .keyword21, .keyword22, .keyword23, .keyword24, .keyword25, .keyword26:
if let index = Name.indexOfKeyword(self) {
return "Keyword \(index + 1)"
}
return "?"
}
}
/// Returns if the property is a keyword.
var isKeyword: Bool {
switch self {
case .keyword1, .keyword2, .keyword3, .keyword4, .keyword5, .keyword6, .keyword7, .keyword8, .keyword9, .keyword10, .keyword11, .keyword12, .keyword13, .keyword14, .keyword15, .keyword16, .keyword17, .keyword18, .keyword19, .keyword20, .keyword21, .keyword22, .keyword23, .keyword24, .keyword25, .keyword26:
return true
default:
return false
}
}
var keywordIndex: Int? {
guard self.isKeyword else {
return nil
}
return Name.indexOfKeyword(self)
}
/// Return the CSS classes associated to the name.
func getCSSClasses() -> [String] {
switch self {
case .plain:
return ["hl"]
case .canvas:
return ["hl"]
case .number:
return ["hl", "num"]
case .escape:
return ["hl", "esc"]
case .string:
return ["hl", "str"]
case .preProcessor:
return ["hl", "ppc"]
case .stringPreProc:
return ["hl", "pps"]
case .blockComment:
return ["hl", "com"]
case .lineComment:
return ["hl", "slc"]
case .lineNum:
return ["hl", "lin"]
case .operatorProp:
return ["hl", "opt"]
case .interpolation:
return ["hl", "ipl"]
case .keyword1, .keyword2, .keyword3, .keyword4, .keyword5, .keyword6, .keyword7, .keyword8, .keyword9, .keyword10, .keyword11, .keyword12, .keyword13, .keyword14, .keyword15, .keyword16, .keyword17, .keyword18, .keyword19, .keyword20, .keyword21, .keyword22, .keyword23, .keyword24, .keyword25, .keyword26:
if let index = Name.indexOfKeyword(self) {
return ["hl", "kw" + String(UnicodeScalar(UInt8(97 + index)))]
}
return []
}
}
}
internal weak var theme: SCSHTheme?
public var color: String {
didSet {
if oldValue != color {
self.theme?.onPropertyDidChange(self)
}
}
}
public var isBold: Bool {
didSet {
if oldValue != isBold {
self.theme?.onPropertyDidChange(self)
}
}
}
public var isItalic: Bool {
didSet {
if oldValue != isItalic {
self.theme?.onPropertyDidChange(self)
}
}
}
public var isUnderline: Bool {
didSet {
if oldValue != isUnderline {
self.theme?.onPropertyDidChange(self)
}
}
}
public convenience init() {
self.init(color: "", isBold: false, isItalic: false, isUnderline: false)
}
required public init(color: String) {
self.color = color
self.isBold = false
self.isItalic = false
self.isUnderline = false
}
public init(color: String, isBold: Bool = false, isItalic: Bool = false, isUnderline: Bool = false) {
self.color = color
self.isBold = isBold
self.isItalic = isItalic
self.isUnderline = isUnderline
}
required public convenience init?(dict: [String: Any]?) {
self.init(color: dict?["color"] as? String ?? "", isBold: dict?["bold"] as? Bool ?? false, isItalic: dict?["italic"] as? Bool ?? false, isUnderline: dict?["underline"] as? Bool ?? false)
}
public func toDictionary() -> [String: Any] {
var dict: [String: Any] = [
"color": color,
]
if isBold {
dict["bold"] = true
}
if isItalic {
dict["italic"] = true
}
if isUnderline {
dict["underline"] = true
}
return dict
}
/// Get CSS inline style attribute for the property.
public func toCSSStyle() -> String {
var css = ""
if color != "" {
css += " color: \(color);\n"
}
if isBold {
css += " font-weight: bold;\n"
}
if isItalic {
css += " font-style: italic;\n"
}
if isUnderline {
css += " text-decoration: underline;\n"
}
return css
}
public func output() -> String {
var s = "{ Colour=\"\(color)\""
if isBold {
s += ", Bold=true"
}
if isItalic {
s += ", Italic=true"
}
if isUnderline {
s += ", Underline=true"
}
s += " }"
return s
}
}
// MARK: - Theme Properties Iterator
public struct PropertiesIterator: IteratorProtocol {
let theme: SCSHTheme
private var index: Property.Name?
init(_ theme: SCSHTheme) {
self.theme = theme
}
mutating public func next() -> SCSHThemePropertyProtocol? {
if index == nil {
index = .canvas
} else {
index = index!.next
if index == nil {
return nil
}
}
return theme[index!]
}
}
// MARK: -
enum ThemeError: Error {
case missingProperty(name: Property.Name)
case missingProperties
case missingUrl
}
// MARK: -
weak var delegate: SCSHThemeDelegate?
public var name: String {
didSet {
if oldValue != name {
delegate?.themeDidChangeName(self)
if !self.isDirty {
self.isDirty = true
}
}
}
}
internal var originalName: String = ""
public var desc: String {
didSet {
if oldValue != desc {
delegate?.themeDidChangeDescription(self)
if !self.isDirty {
self.isDirty = true
}
}
}
}
public var categories: Set<String> {
didSet {
if oldValue != categories {
delegate?.themeDidChangeCategories(self)
if !self.isDirty {
self.isDirty = true
}
}
}
}
/// Indicate if the theme is a standard provided by highlight.
/// Set to false to allow the customization.
public var isStandalone: Bool = true
public var backgroundColor: String {
return self.canvas.color
}
var isLight: Bool {
get {
return categories.contains("light")
}
set {
var c: Set<String> = Set<String>.init(categories)
if newValue {
c.remove("dark")
c.insert("light")
} else {
c.remove("light")
c.insert("dark")
}
categories = c
}
}
var isDark: Bool {
get {
return categories.contains("dark")
}
set {
isLight = !newValue
}
}
/// Full description with the categories.
public var fullDesc: String {
var s = desc
if categories.count > 0 {
s += " [" + categories.joined(separator: " ") + "]"
}
return s
}
var numberOfProperties: Int {
return Property.Name.standardProperties.count + keywords.count
}
subscript(name: Property.Name) -> SCSHThemePropertyProtocol? {
switch name {
case .canvas:
return self.canvas
case .plain:
return self.plain
case .number:
return self.number
case .escape:
return self.escape
case .string:
return self.string
case .preProcessor:
return self.preProcessor
case .stringPreProc:
return self.stringPreProc
case .blockComment:
return self.blockComment
case .lineComment:
return self.lineComment
case .lineNum:
return self.lineNum
case .operatorProp:
return self.operatorProp
case .interpolation:
return self.interpolation
case .keyword1, .keyword2, .keyword3, .keyword4, .keyword5, .keyword6, .keyword7, .keyword8, .keyword9, .keyword10, .keyword11, .keyword12, .keyword13, .keyword14, .keyword15, .keyword16, .keyword17, .keyword18, .keyword19, .keyword20, .keyword21, .keyword22, .keyword23, .keyword24, .keyword25, .keyword26:
if let i = Property.Name.indexOfKeyword(name), i >= 0 && i < keywords.count {
return keywords[i]
}
return nil
}
}
let plain: Property
let canvas: CanvasProperty
let number: Property
let string: Property
let escape: Property
let preProcessor: Property
let stringPreProc: Property
let blockComment: Property
let lineComment: Property
let lineNum: Property
let operatorProp: Property
let interpolation: Property
private(set) var keywords: [Property] = []
@objc dynamic var isDirty: Bool = false {
didSet {
if oldValue != isDirty {
delegate?.themeDidChangeDirtyStatus(self)
}
}
}
public init(name: String, desc: String, categories: Set<String>, plain: Property, canvas: CanvasProperty, number: Property, string: Property, escape: Property, preProcessor: Property, stringPreProc: Property, blockComment: Property, lineComment: Property, lineNum: Property, operatorProp: Property, interpolation: Property, keywords: [Property]) {
self.name = name
self.desc = desc
self.categories = categories
self.canvas = canvas
self.plain = plain
self.number = number
self.string = string
self.escape = escape
self.preProcessor = preProcessor
self.stringPreProc = stringPreProc
self.blockComment = blockComment
self.lineComment = lineComment
self.lineNum = lineNum
self.operatorProp = operatorProp
self.interpolation = interpolation
self.keywords = keywords
super.init()
self.plain.theme = self
self.canvas.theme = self
self.number.theme = self
self.string.theme = self
self.escape.theme = self
self.preProcessor.theme = self
self.stringPreProc.theme = self
self.blockComment.theme = self
self.lineComment.theme = self
self.lineNum.theme = self
self.operatorProp.theme = self
self.interpolation.theme = self
self.isDirty = true
}
convenience public init(name: String) {
self.init(name: name, desc: "", categories: ["light"], plain: Property(color: "#000000"), canvas: CanvasProperty(color: "#ffffff"), number: Property(), string: Property(), escape: Property(), preProcessor: Property(), stringPreProc: Property(), blockComment: Property(), lineComment: Property(), lineNum: Property(), operatorProp: Property(), interpolation: Property(), keywords: [])
}
convenience public init?(dict dictionary: [String: Any]?) {
guard let dict = dictionary else {
return nil
}
let name = dict["name"] as? String ?? ""
let desc = dict["desc"] as? String ?? ""
let categories = dict["categories"] as? Set<String> ?? []
guard let plain = Property(dict: dict[Property.Name.plain.rawValue] as? [String: Any]) else {
return nil
}
guard let canvas = CanvasProperty(dict: dict[Property.Name.canvas.rawValue] as? [String: Any]) else {
return nil
}
guard let number = Property(dict: dict[Property.Name.number.rawValue] as? [String: Any]) else {
return nil
}
guard let string = Property(dict: dict[Property.Name.string.rawValue] as? [String: Any]) else {
return nil
}
guard let escape = Property(dict: dict[Property.Name.escape.rawValue] as? [String: Any]) else {
return nil
}
guard let preProcessor = Property(dict: dict[Property.Name.preProcessor.rawValue] as? [String: Any]) else {
return nil
}
guard let stringPreProc = Property(dict: dict[Property.Name.stringPreProc.rawValue] as? [String: Any]) else {
return nil
}
guard let blockComment = Property(dict: dict[Property.Name.blockComment.rawValue] as? [String: Any]) else {
return nil
}
guard let lineComment = Property(dict: dict[Property.Name.lineComment.rawValue] as? [String: Any]) else {
return nil
}
guard let lineNum = Property(dict: dict[Property.Name.lineNum.rawValue] as? [String: Any]) else {
return nil
}
guard let operatorProp = Property(dict: dict[Property.Name.operatorProp.rawValue] as? [String: Any]) else {
return nil
}
guard let interpolation = Property(dict: dict[Property.Name.interpolation.rawValue] as? [String: Any]) else {
return nil
}
var keywords: [Property] = []
if let kk = dict["keywords"] as? [[String: Any]] {
for k in kk {
if let keyword = Property(dict: k) {
keywords.append(keyword)
}
}
}
self.init(name: name, desc: desc, categories: categories, plain: plain, canvas: canvas, number: number, string: string, escape: escape, preProcessor: preProcessor, stringPreProc: stringPreProc, blockComment: blockComment, lineComment: lineComment, lineNum: lineNum, operatorProp: operatorProp, interpolation: interpolation, keywords: keywords)
self.originalName = dict["originalName"] as? String ?? ""
self.isDirty = false
if let standalone = dict["standalone"] as? Bool {
self.isStandalone = standalone
}
}
public func toDictionary() -> [String: Any] {
let dict: [String: Any] = [
"name": name,
"originalName": originalName,
"desc": desc,
"categories": categories,
Property.Name.plain.rawValue: plain.toDictionary(),
Property.Name.canvas.rawValue: canvas.toDictionary(),
Property.Name.number.rawValue: number.toDictionary(),
Property.Name.string.rawValue: string.toDictionary(),
Property.Name.escape.rawValue: escape.toDictionary(),
Property.Name.preProcessor.rawValue: preProcessor.toDictionary(),
Property.Name.stringPreProc.rawValue: stringPreProc.toDictionary(),
Property.Name.blockComment.rawValue: blockComment.toDictionary(),
Property.Name.lineComment.rawValue: lineComment.toDictionary(),
Property.Name.lineNum.rawValue: lineNum.toDictionary(),
Property.Name.operatorProp.rawValue: operatorProp.toDictionary(),
Property.Name.interpolation.rawValue: interpolation.toDictionary(),
"keywords": keywords.map({ $0.toDictionary() }),
"standalone": isStandalone,
]
return dict
}
func save(to url: URL) throws {
var s = "Description=\"\(desc.escapingForLua())\"\n\n"
s += "Categories = {" + categories.map({ "\"" + $0.escapingForLua() + "\"" }).joined(separator: ", ") + "}\n\n"
s += Property.Name.plain.rawValue + " = " + plain.output() + "\n"
s += Property.Name.canvas.rawValue + " = " + canvas.output() + "\n"
s += Property.Name.number.rawValue + " = " + number.output() + "\n"
s += Property.Name.string.rawValue + " = " + string.output() + "\n"
s += Property.Name.escape.rawValue + " = " + escape.output() + "\n"
s += Property.Name.preProcessor.rawValue + " = " + preProcessor.output() + "\n"
s += Property.Name.stringPreProc.rawValue + " = " + stringPreProc.output() + "\n"
s += Property.Name.blockComment.rawValue + " = " + blockComment.output() + "\n"
s += Property.Name.lineComment.rawValue + " = " + lineComment.output() + "\n"
s += Property.Name.lineNum.rawValue + " = " + lineNum.output() + "\n"
s += Property.Name.operatorProp.rawValue + " = " + operatorProp.output() + "\n"
s += Property.Name.interpolation.rawValue + " = " + interpolation.output() + "\n"
s += "\nKeywords = {\n\t" +
keywords.map({ $0.output() }).joined(separator: ", \n\t") +
"\n}\n\n"
do {
try s.write(to: url, atomically: true, encoding: .utf8)
isDirty = false
originalName = url.deletingPathExtension().lastPathComponent
name = originalName
} catch {
throw error
}
}
/// Get a html code for preview the theme settings.
public func getHtmlExample(font: NSFont, smartCaption: Bool, showColorCodes: Bool = true, extraCSS css: String = "") -> String {
return getHtmlExample(fontName: font.fontName, fontSize: Float(font.pointSize), smartCaption: smartCaption, showColorCodes: showColorCodes, extraCSS: css)
}
/// Get a html code for preview the theme settings.
public func getHtmlExample(fontName: String = "Menlo", fontSize: Float = 12, smartCaption: Bool = false, showColorCodes: Bool = true, extraCSS css: String = "") -> String {
var cssFont = ""
if fontName != "" {
cssFont = " font-family: \(fontName);\n font-size: \(fontSize)pt;\n"
}
let exportProperty = { (name: Property.Name, property: SCSHThemePropertyProtocol)->String in
return "." + name.getCSSClasses().joined(separator: ".") + " {\n" + property.toCSSStyle() + cssFont + " } \n"
}
var style = ""
for name in Property.Name.standardProperties {
guard let prop = self[name] else {
continue
}
style += exportProperty(name, prop)
}
for (i, keyword) in keywords.enumerated() {
if let name = Property.Name.keywordAtIndex(i) {
style += exportProperty(name, keyword)
}
}
let textColor = plain.toCSSStyle()
var s = """
<html>
<head>
<title>\(self.name).theme :: \(self.desc)</title>
<style>
* {
box-sizing: border-box;
}
html, body {
background-color: \(self.canvas.color);
\(cssFont)
user-select: none;
-webkit-user-select: none;
margin: 0;
height: 100%;
}
body {
padding: 1em;
}
.color_code {
\(cssFont)
\(textColor)
display: \(showColorCodes ? "initial" : "none");
text-aling: right;
}
table {
width: 100%;
border-collapse: collapse;
}
td {
padding: 2px;
}
\(style)
\(css)
</style>
</head>
<body class="hl">
<pre class="hl"><table>
"""
var name = Property.Name.standardProperties.first
while name != nil {
if name == .canvas {
name = name!.next
continue
}
guard let prop = self[name!] else {
break
}
s += """
<tr>
<td class="\(name!.getCSSClasses().joined(separator: " "))">\(smartCaption ? name!.rawValue : name!.description)</td>
<td class="color_code">\(prop.color)</td>
</tr>
"""
name = name!.next
}
s += """
</table></pre>
</body>
</html>
"""
return s
}
/// Get a NSAttributedString for preview the theme settings.
public func getAttributedExample(font: NSFont, smartCaption: Bool = false, showColorCodes: Bool = true, extraCSS css: String = "") -> NSAttributedString {
return getAttributedExample(fontName: font.fontName, fontSize: Float(font.pointSize), smartCaption: smartCaption, showColorCodes: showColorCodes, extraCSS: css)
}
/// Get a NSAttributedString for preview the theme settings.
public func getAttributedExample(fontName: String = "Menlo", fontSize: Float = 12, smartCaption: Bool = false, showColorCodes: Bool = true, extraCSS css: String = "") -> NSAttributedString {
return NSAttributedString(html: getHtmlExample(fontName: fontName, fontSize: fontSize, smartCaption: smartCaption, showColorCodes: showColorCodes, extraCSS: css).data(using: .utf8)!, options: [:], documentAttributes: nil)!
}