AppDelegate.swift 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // AppDelegate.swift
  3. // Example-Mac
  4. //
  5. // Created by Indragie on 1/15/15.
  6. // Copyright (c) 2015 Indragie Karunaratne. All rights reserved.
  7. //
  8. import Cocoa
  9. import CocoaMarkdown
  10. @NSApplicationMain
  11. class AppDelegate: NSObject, NSApplicationDelegate {
  12. @IBOutlet weak var window: NSWindow!
  13. @IBOutlet var textView: NSTextView!
  14. @IBOutlet weak var openPanelAUxiliaryView: NSView!
  15. @IBOutlet weak var linksBaseUrlTextField: NSTextField!
  16. func applicationDidFinishLaunching(_ notification: Notification) {
  17. if let testFileUrl = Bundle.main.url(forResource:"test", withExtension: "md") {
  18. renderMarkdownFile(atUrl: testFileUrl)
  19. }
  20. }
  21. @IBAction func selectMarkdownFile(_ sender: Any) {
  22. let panel = NSOpenPanel()
  23. panel.allowedFileTypes = ["md", "markdown"]
  24. panel.allowsMultipleSelection = false
  25. panel.prompt = "Render"
  26. panel.accessoryView = openPanelAUxiliaryView
  27. if #available(OSX 10.11, *) {
  28. panel.isAccessoryViewDisclosed = true
  29. }
  30. panel.beginSheetModal(for: window, completionHandler: { (response) in
  31. if response == .OK {
  32. if let selectedFileUrl = panel.url {
  33. var linksBaseUrl: URL? = nil
  34. if self.linksBaseUrlTextField.stringValue.count > 0 {
  35. linksBaseUrl = URL(string: self.linksBaseUrlTextField.stringValue)
  36. }
  37. self.renderMarkdownFile(atUrl: selectedFileUrl, withLinksBaseUrl: linksBaseUrl)
  38. }
  39. }
  40. })
  41. }
  42. func renderMarkdownFile(atUrl url: URL, withLinksBaseUrl linksBaseUrl: URL? = nil) {
  43. let document = CMDocument(contentsOfFile: url.path, options: CMDocumentOptions(rawValue: 0))
  44. if linksBaseUrl != nil {
  45. document?.linksBaseURL = linksBaseUrl
  46. }
  47. let textAttributes = CMTextAttributes()
  48. // Customize the color and font of header elements
  49. textAttributes.addStringAttributes([ .foregroundColor: NSColor(red: 0.0, green: 0.446, blue: 0.657, alpha: 1.0)], forElementWithKinds: .anyHeader)
  50. if #available(OSX 10.11, *) {
  51. textAttributes.addFontAttributes([ .family: "Avenir Next" ,
  52. .traits: [ NSFontDescriptor.TraitKey.symbolic: NSFontDescriptor.SymbolicTraits.italic.rawValue,
  53. NSFontDescriptor.TraitKey.weight: NSFont.Weight.semibold]],
  54. forElementWithKinds: .anyHeader)
  55. // Set specific font traits for header1 and header2
  56. textAttributes.setFontTraits([.weight: NSFont.Weight.heavy], forElementWithKinds: [.header1, .header2])
  57. }
  58. textAttributes.addFontAttributes([ .size: 48 ], forElementWithKinds: .header1)
  59. // Customize the font and paragraph alignment of block-quotes
  60. textAttributes.addFontAttributes([.family: "Snell Roundhand", .size: 19], forElementWithKinds: .blockQuote)
  61. textAttributes.addParagraphStyleAttributes([ .alignment: NSTextAlignment.center.rawValue], forElementWithKinds: .blockQuote)
  62. // Customize the background color of code elements
  63. textAttributes.addStringAttributes([ .backgroundColor: NSColor(white: 0.9, alpha: 0.5)], forElementWithKinds: [.inlineCode, .codeBlock])
  64. let renderer = CMAttributedStringRenderer(document: document, attributes: textAttributes)!
  65. renderer.register(CMHTMLStrikethroughTransformer())
  66. renderer.register(CMHTMLSuperscriptTransformer())
  67. renderer.register(CMHTMLUnderlineTransformer())
  68. if let renderedAttributedString = renderer.render(),
  69. let textStorage = textView.textStorage {
  70. textStorage.replaceCharacters(in: NSRange(location: 0, length: textStorage.length), with: renderedAttributedString)
  71. }
  72. }
  73. }