剪贴板里的样式

高亮剪贴板里的代码片段 我前几天在准备一个培训的slides的时候,想在Keynote中粘贴一段代码,默认的粘贴板中的内容并没有样式,粘进去之后就是纯文本,没有语法高亮不说,默认的,代码的字体会采用Keynote默认的字体,非常难看。 之前在Intellij中有个插件'Copy' on steroids,这个插件可以将Intellij的编辑器中的高亮过的文本拷贝到剪贴板,然后就可以在Keynote中使用了。 我就想能不能有对应的命令行工具,结果还真的找到一个,就叫highlight,主页在这里。(这里略微吐槽一下,这个官方页面的风格以今天的眼光来看,无论是配色还是样式布局等,都非常难看,完全是10年前的风格,不过这个小工具确实很好用)。 基本使用 在Mac下,安装非常简单: $ brew install highlight highlight支持很多格式,HTML,RTF甚至还有LaTeX。不过如果要在Keynote或者PowerPoint中使用,用RTF(Rich Text Format)就可以了。 选择要高亮的文件,比如app.rb require 'sinatra' require 'rack/contrib' require 'active_record' require 'json' require './model/plants' class FrontendApplication < Sinatra::Base get '/' do File.open('index.html').read end end 然后使用命令: $ highlight -O rtf app.rb | pbcopy 即可将高亮过的内容复制到剪切板中,然后只需要CMD+V就可以粘贴了: 如果要生成HTML格式,只需要指定: $ highlight -O html app.rb > app.rb.html 自定义语言(扩展) 如果遇到不支持的语言时,默认的highlight会报错,要查看所有支持的语言,可以使用 $ highlight -p 所有的highlight内置的语言高亮配置都存储在本地的一个目录中,比如在我的机器上,存储位置为/usr/local/Cellar/highlight/3.18_1/share/highlight/langDefs/。 还以Java为例,该目录下会有一个文件,名为java.lang,内容如下: Description="Java" Keywords={ { Id=1, List={"abstract", "default", "if", "private", "this", "do", "implements", "protected", "throw", "break", "import", "public", "throws", "else", "instanceof", "return", "transient", "case", "extends", "try", "catch", "final", "interface", "static", "finally", "strictfp", "volatile", "class", "native", "super", "while", "const", "for", "new", "switch", "continue", "goto", "package", "synchronized", "as", "in", "def", "property"}, }, { Id=2, List={"boolean", "double", "byte", "int", "short", "void", "char", "long", "float"}, }, { Id=3, Regex=[[@\w+]], }, { Id=4, Regex=[[(\w+)\s*\(]], }, } Strings={ Delimiter=[["|']], Escape = [[ \\u\d{4}|\\x?...

January 24, 2015 2 min