Skip to content

Instantly share code, notes, and snippets.

@znck
Last active September 8, 2020 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save znck/d11533f4029eb41bde904a610a15a026 to your computer and use it in GitHub Desktop.
Save znck/d11533f4029eb41bde904a610a15a026 to your computer and use it in GitHub Desktop.
Format VS Code copied code snippet

Format VS Code copied code snippet

Run swift format.swift after copying text from VS Code.

import Cocoa
let pasteboard = NSPasteboard.general
let re1 = try! NSRegularExpression(pattern: "background-color:.*?;")
let re2 = try! NSRegularExpression(pattern: "font-size:.*?;")
let re3 = try! NSRegularExpression(pattern: "font-family:.*?;")
let htmlType = NSPasteboard.PasteboardType(rawValue: "public.html")
for element in pasteboard.pasteboardItems! {
let str = element.string(forType: htmlType)
let mstr = NSMutableString(string: str!)
re1.replaceMatches(
in: mstr,
range: NSRange(location: 0,length: mstr.length),
withTemplate: " ")
re2.replaceMatches(
in: mstr,
range: NSRange(location: 0,length: mstr.length),
withTemplate: "font-size: 32px; ")
re3.replaceMatches(
in: mstr,
range: NSRange(location: 0,length: mstr.length),
withTemplate: "font-family: 'Fira Code'; ")
pasteboard.clearContents()
pasteboard.setString(mstr as String, forType: htmlType)
}
#!/usr/bin/sh
paste_as_html () {
osascript -e 'the clipboard as «class HTML»' | perl -ne 'print chr foreach unpack("C*", pack("H*", substr($_, 11, -3)))'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment